r3-legacy/bak/game-lib-d3-mesh-sphere.js

185 lines
4.6 KiB
JavaScript

/**
* GameLib.D3.Mesh.Sphere
* @param graphics GameLib.GraphicsRuntime
* @param apiMeshSphere
* @constructor
*/
GameLib.D3.Mesh.Sphere = function (
graphics,
apiMeshSphere
) {
this.graphics = graphics;
this.graphics.isNotThreeThrow();
if (GameLib.Utils.UndefinedOrNull(apiMeshSphere)) {
apiMeshSphere = {
meshType: GameLib.D3.API.Mesh.MESH_TYPE_SPHERE
};
}
GameLib.D3.API.Mesh.Sphere.call(
this,
apiMeshSphere,
apiMeshSphere.radius,
apiMeshSphere.widthSegments,
apiMeshSphere.heightSegments
);
GameLib.D3.Mesh.call(
this,
this.graphics,
this
);
};
GameLib.D3.Mesh.Sphere.prototype = Object.create(GameLib.D3.Mesh.prototype);
GameLib.D3.Mesh.Sphere.prototype.constructor = GameLib.D3.Mesh.Sphere;
GameLib.D3.Mesh.Sphere.prototype.createInstance = function() {
var geometry = null;
if (this.vertices.length === 0) {
geometry = new THREE.SphereGeometry(
this.radius,
this.widthSegments,
this.heightSegments
);
this.updateVerticesFromGeometryInstance(geometry);
}
GameLib.D3.Mesh.prototype.createInstance.call(this);
};
GameLib.D3.Mesh.Sphere.prototype.updateInstance = function(property) {
if (
property === 'radius' ||
property === 'widthSegments' ||
property === 'heightSegments'
) {
var geometry = new THREE.SphereGeometry(
this.radius,
this.widthSegments,
this.heightSegments
);
this.updateVerticesFromGeometryInstance(geometry);
geometry = this.createInstanceGeometry();
this.instance.geometry = geometry;
return;
}
GameLib.D3.Mesh.prototype.updateInstance.call(this, property);
};
/**
* Converts a GameLib.D3.Mesh.Sphere to a GameLib.D3.API.Mesh.Sphere
* @returns {GameLib.D3.API.Mesh.Sphere}
*/
GameLib.D3.Mesh.Sphere.prototype.toApiObject = function() {
var apiMesh = GameLib.D3.Mesh.prototype.toApiObject.call(this);
var apiMeshSphere = new GameLib.D3.API.Mesh.Sphere(
apiMesh,
this.radius,
this.widthSegments,
this.heightSegments
);
return apiMeshSphere;
};
GameLib.D3.Mesh.Sphere.prototype.createPhysicsObjects = function() {
GameLib.Event.Emit(
GameLib.Event.GET_PHYSICS_RUNTIME,
null,
function(physics){
var apiShapeSphere = new GameLib.D3.API.Shape(
null,
'Sphere Shape (' + this.name + ')'
);
apiShapeSphere.parentMesh = this;
var shapeSphere = new GameLib.D3.Shape.Sphere(
physics,
apiShapeSphere,
this.radius
);
var apiRigidBody = new GameLib.D3.API.RigidBody(
null,
'Rigid Body (' + this.name + ')',
1,
null,
new GameLib.API.Vector3(
this.position.x,
this.position.y,
this.position.z
),
new GameLib.API.Quaternion(
this.quaternion.x,
this.quaternion.y,
this.quaternion.z,
this.quaternion.w,
new GameLib.API.Vector3(
this.quaternion.axis.x,
this.quaternion.axis.y,
this.quaternion.axis.z
),
this.quaternion.angle
)
);
apiRigidBody.parentMesh = this;
apiRigidBody.shapes.push(shapeSphere);
var rigidBody = new GameLib.D3.RigidBody(
physics,
apiRigidBody
);
if (this.parentEntity instanceof GameLib.Entity) {
this.parentEntity.addComponent(shapeSphere);
this.parentEntity.addComponent(rigidBody);
}
}.bind(this),
function(error){
console.log(error.message);
throw new Error(error.message);
}
);
};
/**
* Converts a standard object mesh to a GameLib.D3.Mesh
* @param graphics GameLib.GraphicsRuntime
* @param objectMesh {Object}
* @constructor
*/
GameLib.D3.Mesh.Sphere.FromObject = function(graphics, objectMesh) {
var apiMesh = GameLib.D3.API.Mesh.FromObject(objectMesh);
return new GameLib.D3.Mesh.Sphere(
graphics,
apiMesh,
objectMesh.radius,
objectMesh.widthSegments,
objectMesh.heightSegments
);
};