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

185 lines
4.4 KiB
JavaScript
Raw Permalink Normal View History

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