/** * R3.D3.Mesh.Sphere * @param graphics R3.GraphicsRuntime * @param apiMeshSphere * @constructor */ R3.D3.Mesh.Sphere = function ( graphics, apiMeshSphere ) { this.graphics = graphics; this.graphics.isNotThreeThrow(); if (R3.Utils.UndefinedOrNull(apiMeshSphere)) { apiMeshSphere = { meshType: R3.D3.API.Mesh.MESH_TYPE_SPHERE }; } R3.D3.API.Mesh.Sphere.call( this, apiMeshSphere, apiMeshSphere.radius, apiMeshSphere.widthSegments, apiMeshSphere.heightSegments ); R3.D3.Mesh.call( this, this.graphics, this ); }; R3.D3.Mesh.Sphere.prototype = Object.create(R3.D3.Mesh.prototype); R3.D3.Mesh.Sphere.prototype.constructor = R3.D3.Mesh.Sphere; R3.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); } R3.D3.Mesh.prototype.createInstance.call(this); }; R3.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; } R3.D3.Mesh.prototype.updateInstance.call(this, property); }; /** * Converts a R3.D3.Mesh.Sphere to a R3.D3.API.Mesh.Sphere * @returns {R3.D3.API.Mesh.Sphere} */ R3.D3.Mesh.Sphere.prototype.toApiObject = function() { var apiMesh = R3.D3.Mesh.prototype.toApiObject.call(this); var apiMeshSphere = new R3.D3.API.Mesh.Sphere( apiMesh, this.radius, this.widthSegments, this.heightSegments ); return apiMeshSphere; }; R3.D3.Mesh.Sphere.prototype.createPhysicsObjects = function() { R3.Event.Emit( R3.Event.GET_PHYSICS_RUNTIME, null, function(physics){ var apiShapeSphere = new R3.D3.API.Shape( null, 'Sphere Shape (' + this.name + ')' ); apiShapeSphere.parentMesh = this; var shapeSphere = new R3.D3.Shape.Sphere( physics, apiShapeSphere, this.radius ); var apiRigidBody = new R3.D3.API.RigidBody( null, 'Rigid Body (' + this.name + ')', 1, null, new R3.API.Vector3( this.position.x, this.position.y, this.position.z ), new R3.API.Quaternion( this.quaternion.x, this.quaternion.y, this.quaternion.z, this.quaternion.w, 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); var rigidBody = new R3.D3.RigidBody( physics, apiRigidBody ); 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); } ); }; /** * Converts a standard object mesh to a R3.D3.Mesh * @param graphics R3.GraphicsRuntime * @param objectMesh {Object} * @constructor */ R3.D3.Mesh.Sphere.FromObject = function(graphics, objectMesh) { var apiMesh = R3.D3.API.Mesh.FromObject(objectMesh); return new R3.D3.Mesh.Sphere( graphics, apiMesh, objectMesh.radius, objectMesh.widthSegments, objectMesh.heightSegments ); };