From 5dec8012eefc3256832ce5917a6b92182ffe5a7b Mon Sep 17 00:00:00 2001 From: Maksims Mihejevs Date: Thu, 30 Jul 2015 16:20:35 +0100 Subject: [PATCH] remove debug shapes --- .../components/camera/camera_system.js | 137 +----- .../components/light/light_component.js | 75 +-- .../components/light/light_system.js | 323 +----------- .../particlesystem_component.js | 13 - .../particlesystem/particlesystem_data.js | 3 - .../particlesystem/particlesystem_system.js | 97 ---- .../physics/collision/collision_system.js | 459 +----------------- .../components/script/script_system.js | 2 +- 8 files changed, 63 insertions(+), 1046 deletions(-) diff --git a/src/framework/components/camera/camera_system.js b/src/framework/components/camera/camera_system.js index a48cc20578b..bf75a2aa8c2 100644 --- a/src/framework/components/camera/camera_system.js +++ b/src/framework/components/camera/camera_system.js @@ -35,12 +35,10 @@ pc.extend(pc, function () { ]; // holds all the active camera components - this.cameras = []; + this.cameras = [ ]; this.on('beforeremove', this.onBeforeRemove, this); this.on('remove', this.onRemove, this); - pc.ComponentSystem.on('toolsUpdate', this.toolsUpdate, this); - }; CameraComponentSystem = pc.inherits(CameraComponentSystem, pc.ComponentSystem); @@ -92,41 +90,6 @@ pc.extend(pc, function () { data.postEffects = new pc.PostEffectQueue(this.app, component); - if (this._inTools) { - var material = new pc.BasicMaterial(); - material.color = new pc.Color(1, 1, 0, 1); - material.update(); - - var indexBuffer = new pc.IndexBuffer(this.app.graphicsDevice, pc.INDEXFORMAT_UINT8, 24); - var indices = new Uint8Array(indexBuffer.lock()); - indices.set([0,1,1,2,2,3,3,0, // Near plane - 4,5,5,6,6,7,7,4, // Far plane - 0,4,1,5,2,6,3,7]); // Near to far edges - indexBuffer.unlock(); - - var format = new pc.VertexFormat(this.app.graphicsDevice, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - var vertexBuffer = new pc.VertexBuffer(this.app.graphicsDevice, format, 8, pc.BUFFER_DYNAMIC); - - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.indexBuffer[0] = indexBuffer; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = indexBuffer.getNumIndices(); - mesh.primitive[0].indexed = true; - - var model = new pc.Model(); - model.graph = component.entity; - model.meshInstances = [ new pc.MeshInstance(model.graph, mesh, material) ]; - - this.app.scene.addModel(model); - - data.model = model; - } - CameraComponentSystem._super.initializeComponentData.call(this, component, data, properties); }, @@ -135,102 +98,12 @@ pc.extend(pc, function () { }, onRemove: function (entity, data) { - if (this._inTools) { - if (this.app.scene.containsModel(data.model)) { - this.app.scene.removeModel(data.model); - } - } - data.camera = null; }, - toolsUpdate: function (fn) { - var components = this.store; - for (var id in components) { - if (components.hasOwnProperty(id)) { - var entity = components[id].entity; - var data = components[id].data; - - if (this._inTools) { - this._updateGfx(entity.camera); - } - } - } - }, - - _updateGfx: function (component) { - if (component.model && component.model.meshInstances.length) { - var vertexBuffer = component.model.meshInstances[0].mesh.vertexBuffer; - - // Retrieve the characteristics of the camera frustum - var nearClip = component.nearClip; - var farClip = component.farClip; - var fov = component.fov * Math.PI / 180.0; - var projection = component.projection; - - // calculate aspect ratio based on the current width / height of the - // graphics device - var device = this.app.graphicsDevice; - var rect = component.rect; - var aspectRatio = (device.width * rect.z) / (device.height * rect.w); - - var x, y; - if (projection === pc.PROJECTION_PERSPECTIVE) { - y = Math.tan(fov / 2.0) * nearClip; - } else { - y = component.camera.getOrthoHeight(); - } - x = y * aspectRatio; - - var positions = new Float32Array(vertexBuffer.lock()); - positions[0] = x; - positions[1] = -y; - positions[2] = -nearClip; - positions[3] = x; - positions[4] = y; - positions[5] = -nearClip; - positions[6] = -x; - positions[7] = y; - positions[8] = -nearClip; - positions[9] = -x; - positions[10] = -y; - positions[11] = -nearClip; - - if (projection === pc.PROJECTION_PERSPECTIVE) { - y = Math.tan(fov / 2.0) * farClip; - x = y * aspectRatio; - } - positions[12] = x; - positions[13] = -y; - positions[14] = -farClip; - positions[15] = x; - positions[16] = y; - positions[17] = -farClip; - positions[18] = -x; - positions[19] = y; - positions[20] = -farClip; - positions[21] = -x; - positions[22] = -y; - positions[23] = -farClip; - vertexBuffer.unlock(); - } - }, - addCamera: function (camera) { this.cameras.push(camera); this.sortCamerasByPriority(); - - // add debug shape to tools - if (this._inTools) { - var model = camera.data.model; - - if (model) { - var scene = this.app.scene; - if (!scene.containsModel(model)) { - scene.addModel(model); - } - } - } }, removeCamera: function (camera) { @@ -238,14 +111,6 @@ pc.extend(pc, function () { if (index >= 0) { this.cameras.splice(index, 1); this.sortCamerasByPriority(); - - // remove debug shape from tools - if (this._inTools) { - var model = camera.data.model; - if (model) { - this.app.scene.removeModel(model); - } - } } }, diff --git a/src/framework/components/light/light_component.js b/src/framework/components/light/light_component.js index f353319671c..78c339e04e8 100644 --- a/src/framework/components/light/light_component.js +++ b/src/framework/components/light/light_component.js @@ -69,13 +69,14 @@ pc.extend(pc, function () { pc.extend(LightComponent.prototype, { onSetType: function (name, oldValue, newValue) { - if (oldValue !== newValue) { - this.system.changeType(this, oldValue, newValue); + if (oldValue === newValue) + return; - // refresh light properties because changing the type does not reset the - // light properties - this.refreshProperties(); - } + this.system.changeType(this, oldValue, newValue); + + // refresh light properties because changing the type does not reset the + // light properties + this.refreshProperties(); }, refreshProperties: function() { @@ -94,9 +95,8 @@ pc.extend(pc, function () { this.onSetShadowUpdateMode("shadowUpdateMode", this.shadowUpdateMode, this.shadowUpdateMode); this.onSetMask("mask", this.mask, this.mask); - if (this.enabled && this.entity.enabled) { + if (this.enabled && this.entity.enabled) this.onEnable(); - } }, updateShadow: function() { @@ -116,9 +116,10 @@ pc.extend(pc, function () { }, onSetShadowDistance: function (name, oldValue, newValue) { - if (this.data.type === 'directional') { - this.light.setShadowDistance(newValue); - } + if (this.data.type !== 'directional') + return; + + this.light.setShadowDistance(newValue); }, onSetShadowResolution: function (name, oldValue, newValue) { @@ -135,27 +136,31 @@ pc.extend(pc, function () { }, onSetRange: function (name, oldValue, newValue) { - if (this.data.type === 'point' || this.data.type === 'spot') { - this.light.setAttenuationEnd(newValue); - } + if (this.data.type !== 'point' && this.data.type !== 'spot') + return; + + this.light.setAttenuationEnd(newValue); }, onSetInnerConeAngle: function (name, oldValue, newValue) { - if (this.data.type === 'spot') { - this.light.setInnerConeAngle(newValue); - } + if (this.data.type !== 'spot') + return; + + this.light.setInnerConeAngle(newValue); }, onSetOuterConeAngle: function (name, oldValue, newValue) { - if (this.data.type === 'spot') { - this.light.setOuterConeAngle(newValue); - } + if (this.data.type !== 'spot') + return; + + this.light.setOuterConeAngle(newValue); }, onSetFalloffMode: function (name, oldValue, newValue) { - if (this.data.type === 'point' || this.data.type === 'spot') { - this.light.setFalloffMode(newValue); - } + if (this.data.type !== 'point' && this.data.type !== 'spot') + return; + + this.light.setFalloffMode(newValue); }, onSetShadowType: function (name, oldValue, newValue) { @@ -167,7 +172,7 @@ pc.extend(pc, function () { }, onSetMask: function (name, oldValue, newValue) { - this.light.mask = newValue; + this.light.mask = newValue; }, onEnable: function () { @@ -176,12 +181,14 @@ pc.extend(pc, function () { this.light.setEnabled(true); var model = this.data.model; - if (model) { - var scene = this.system.app.scene; - if (!scene.containsModel(model)) { - scene.addModel(model); - } - } + if (! model) + return; + + var scene = this.system.app.scene; + if (scene.containsModel(model)) + return; + + scene.addModel(model); }, onDisable: function () { @@ -190,10 +197,10 @@ pc.extend(pc, function () { this.light.setEnabled(false); var model = this.data.model; - if (model) { - var scene = this.system.app.scene; - scene.removeModel(model); - } + if (! model) + return; + + this.system.app.scene.removeModel(model); } }); diff --git a/src/framework/components/light/light_system.js b/src/framework/components/light/light_system.js index 66c708c840a..0acbbd06776 100644 --- a/src/framework/components/light/light_system.js +++ b/src/framework/components/light/light_system.js @@ -1,4 +1,10 @@ pc.extend(pc, function () { + var lightTypes = { + 'directional': pc.LIGHTTYPE_DIRECTIONAL, + 'point': pc.LIGHTTYPE_POINT, + 'spot': pc.LIGHTTYPE_SPOT + }; + /** * @name pc.LightComponentSystem * @constructor Create a new LightComponentSystem. @@ -35,9 +41,7 @@ pc.extend(pc, function () { 'model' ]; - this.implementations = {}; this.on('remove', this.onRemove, this); - pc.ComponentSystem.on('toolsUpdate', this.toolsUpdate, this); }; LightComponentSystem = pc.inherits(LightComponentSystem, pc.ComponentSystem); @@ -52,52 +56,30 @@ pc.extend(pc, function () { data[prop] = _data[prop]; }) - if (!data.type) { + if (! data.type) data.type = component.data.type; - } component.data.type = data.type; - if (data.color && pc.type(data.color) === 'array') { + if (data.color && pc.type(data.color) === 'array') data.color = new pc.Color(data.color[0], data.color[1], data.color[2]); - } if (data.enable) { console.warn("WARNING: enable: Property is deprecated. Set enabled property instead."); data.enabled = data.enable; } - var implementation = this._createImplementation(data.type); - implementation.initialize(component, data); + var light = new pc.Light(); + light.setType(lightTypes[data.type]); + light._node = component.entity; + this.app.scene.addLight(light); + component.data.light = light; LightComponentSystem._super.initializeComponentData.call(this, component, data, properties); }, - _createImplementation: function (type) { - var implementation = this.implementations[type]; - if (!implementation) { - switch (type) { - case 'directional': - implementation = new DirectionalLightImplementation(this); - break; - case 'point': - implementation = new PointLightImplementation(this); - break; - case 'spot': - implementation = new SpotLightImplementation(this); - break; - default: - throw new Error("Invalid light type: " + type); - } - - this.implementations[type] = implementation; - } - - return implementation; - }, - onRemove: function (entity, data) { - this.implementations[data.type].remove(entity, data); + this.app.scene.removeLight(data.light); }, cloneComponent: function (entity, clone) { @@ -124,281 +106,8 @@ pc.extend(pc, function () { this.addComponent(clone, data); }, - toolsUpdate: function (fn) { - var components = this.store; - for (var id in components) { - if (components.hasOwnProperty(id)) { - var entity = components[id].entity; - var componentData = components[id].data; - var implementation = this.implementations[componentData.type]; - if (implementation) { - implementation.toolsUpdate(componentData); - } - } - } - }, - - changeType: function (component, oldType, newType) { - this.implementations[oldType].remove(component.entity, component.data); - this._createImplementation(newType).initialize(component, component.data); - } - }); - - /** - * Light implementations - */ - - LightComponentImplementation = function (system) { - this.system = system; - }; - - LightComponentImplementation.prototype = { - initialize: function (component, data) { - var light = new pc.Light(); - light.setType(this._getLightType()); - light._node = component.entity; - - var app = this.system.app; - app.scene.addLight(light); - - data = data || {}; - data.light = light; - - if (this.system._inTools) { - this._createDebugShape(component, data, light); - } - }, - - _getLightType: function () { - return undefined; - }, - - _createDebugShape: function (component, data, light) { - this.mesh = this._createDebugMesh(); - if (!this.material) { - this.material = this._createDebugMaterial(); - } - - var model = new pc.Model(); - model.graph = component.entity; - model.meshInstances = [ new pc.MeshInstance(component.entity, this.mesh, this.material) ]; - - data.model = model; - }, - - _createDebugMesh: function () { - return undefined; - }, - - _createDebugMaterial: function () { - return undefined; - }, - - remove: function(entity, data) { - var app = this.system.app; - - app.scene.removeModel(data.model); - delete data.model; - - app.scene.removeLight(data.light); - - if (this.system._inTools) { - app.scene.removeModel(data.model); - delete data.model; - } - }, - - toolsUpdate: function (data) { - } - }; - - /** - * Directional Light implementation - */ - - DirectionalLightImplementation = function (system) {}; - DirectionalLightImplementation = pc.inherits(DirectionalLightImplementation, LightComponentImplementation); - DirectionalLightImplementation.prototype = pc.extend(DirectionalLightImplementation.prototype, { - _getLightType: function() { - return pc.LIGHTTYPE_DIRECTIONAL; - }, - - _createDebugMesh: function () { - if (this.mesh) { - return this.mesh; - } - - var app = this.system.app; - var format = new pc.VertexFormat(app.graphicsDevice, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - // Generate the directional light arrow vertex data - vertexData = [ - // Center arrow - 0, 0, 0, 0, -8, 0, // Stalk - -0.5, -8, 0, 0.5, -8, 0, // Arrowhead base - 0.5, -8, 0, 0, -10, 0, // Arrowhead tip - 0, -10, 0, -0.5, -8, 0, // Arrowhead tip - // Lower arrow - 0, 0, -2, 0, -8, -2, // Stalk - -0.25, -8, -2, 0.25, -8, -2, // Arrowhead base - 0.25, -8, -2, 0, -10, -2, // Arrowhead tip - 0, -10, -2, -0.25, -8, -2, // Arrowhead tip - // Lower arrow - 0, 0, 2, 0, -8, 2, // Stalk - -0.25, -8, 2, 0.25, -8, 2, // Arrowhead base - 0.25, -8, 2, 0, -10, 2, // Arrowhead tip - 0, -10, 2, -0.25, -8, 2 // Arrowhead tip - ]; - var rot = new pc.Mat4().setFromAxisAngle(pc.Vec3.UP, 120); - var i; - for (i = 0; i < 24; i++) { - var pos = new pc.Vec3(vertexData[(i+8)*3], vertexData[(i+8)*3+1], vertexData[(i+8)*3+2]); - var posRot = rot.transformPoint(pos, pos); - vertexData[(i+24)*3] = posRot[0]; - vertexData[(i+24)*3+1] = posRot[1]; - vertexData[(i+24)*3+2] = posRot[2]; - } - // Copy vertex data into the vertex buffer - var vertexBuffer = new pc.VertexBuffer(app.graphicsDevice, format, 32); - var positions = new Float32Array(vertexBuffer.lock()); - for (i = 0; i < vertexData.length; i++) { - positions[i] = vertexData[i]; - } - vertexBuffer.unlock(); - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.indexBuffer[0] = null; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = vertexBuffer.getNumVertices(); - mesh.primitive[0].indexed = false; - return mesh; - }, - - _createDebugMaterial: function () { - var material = new pc.BasicMaterial(); - material.color = new pc.Color(1, 1, 0, 1); - material.update(); - return material; - } - - }); - - /** - * Point Light implementation - */ - - PointLightImplementation = function (system) {}; - PointLightImplementation = pc.inherits(PointLightImplementation, LightComponentImplementation); - PointLightImplementation.prototype = pc.extend(PointLightImplementation.prototype, { - _getLightType: function() { - return pc.LIGHTTYPE_POINT; - }, - - _createDebugMesh: function () { - if (this.mesh) { - return this.mesh; - } - - var app = this.system.app; - return pc.createSphere(app.graphicsDevice, { - radius: 0.1 - }); - }, - - _createDebugMaterial: function () { - var material = new pc.BasicMaterial(); - material.color = new pc.Color(1, 1, 0, 1); - material.update(); - return material; - } - - }); - - - /** - * Spot Light implementation - */ - - SpotLightImplementation = function (system) {}; - SpotLightImplementation = pc.inherits(SpotLightImplementation, LightComponentImplementation); - SpotLightImplementation.prototype = pc.extend(SpotLightImplementation.prototype, { - _getLightType: function() { - return pc.LIGHTTYPE_SPOT; - }, - - _createDebugMesh: function () { - var app = this.system.app; - var indexBuffer = this.indexBuffer; - if (!indexBuffer) { - var indexBuffer = new pc.IndexBuffer(app.graphicsDevice, pc.INDEXFORMAT_UINT8, 88); - var inds = new Uint8Array(indexBuffer.lock()); - // Spot cone side lines - inds[0] = 0; - inds[1] = 1; - inds[2] = 0; - inds[3] = 11; - inds[4] = 0; - inds[5] = 21; - inds[6] = 0; - inds[7] = 31; - // Spot cone circle - 40 segments - for (var i = 0; i < 40; i++) { - inds[8 + i * 2 + 0] = i + 1; - inds[8 + i * 2 + 1] = i + 2; - } - indexBuffer.unlock(); - this.indexBuffer = indexBuffer; - } - - var vertexFormat = new pc.VertexFormat(app.graphicsDevice, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - var vertexBuffer = new pc.VertexBuffer(app.graphicsDevice, vertexFormat, 42, pc.BUFFER_DYNAMIC); - - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.indexBuffer[0] = indexBuffer; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = indexBuffer.getNumIndices(); - mesh.primitive[0].indexed = true; - - return mesh; - - }, - - _createDebugMaterial: function () { - return new pc.BasicMaterial(); - }, - - toolsUpdate: function (data) { - var model = data.model; - var meshInstance = model.meshInstances[0]; - var vertexBuffer = meshInstance.mesh.vertexBuffer; - - var oca = Math.PI * data.outerConeAngle / 180; - var ae = data.range; - var y = -ae * Math.cos(oca); - var r = ae * Math.sin(oca); - - var positions = new Float32Array(vertexBuffer.lock()); - positions[0] = 0; - positions[1] = 0; - positions[2] = 0; - var numVerts = vertexBuffer.getNumVertices(); - for (var i = 0; i < numVerts-1; i++) { - var theta = 2 * Math.PI * (i / (numVerts-2)); - var x = r * Math.cos(theta); - var z = r * Math.sin(theta); - positions[(i+1)*3+0] = x; - positions[(i+1)*3+1] = y; - positions[(i+1)*3+2] = z; - } - vertexBuffer.unlock(); + changeType: function (component, oldValue, newValue) { + component.light.setType(lightTypes[newValue]); } }); diff --git a/src/framework/components/particlesystem/particlesystem_component.js b/src/framework/components/particlesystem/particlesystem_component.js index 120c4a02dbe..4fe3add46cd 100644 --- a/src/framework/components/particlesystem/particlesystem_component.js +++ b/src/framework/components/particlesystem/particlesystem_component.js @@ -437,15 +437,7 @@ pc.extend(pc, function() { } } - if (this.data.debugShape) { - if (!this.system.app.scene.containsModel(this.data.debugShape)) { - this.system.app.scene.addModel(this.data.debugShape); - this.system.app.root.addChild(this.data.debugShape.graph); - } - } - ParticleSystemComponent._super.onEnable.call(this); - }, onDisable: function() { @@ -455,11 +447,6 @@ pc.extend(pc, function() { this.system.app.scene.removeModel(this.data.model); } } - - if (this.data.debugShape) { - this.system.app.root.removeChild(this.data.debugShape.graph); - this.system.app.scene.removeModel(this.data.debugShape); - } }, /** diff --git a/src/framework/components/particlesystem/particlesystem_data.js b/src/framework/components/particlesystem/particlesystem_data.js index 1378873939a..182ba7d9d49 100644 --- a/src/framework/components/particlesystem/particlesystem_data.js +++ b/src/framework/components/particlesystem/particlesystem_data.js @@ -57,9 +57,6 @@ pc.extend(pc, function() { this.enabled = true; this.paused = false; - - this.debugShape = null; - }; ParticleSystemComponentData = pc.inherits(ParticleSystemComponentData, pc.ComponentData); diff --git a/src/framework/components/particlesystem/particlesystem_system.js b/src/framework/components/particlesystem/particlesystem_system.js index 1a5cdf8e38b..f2fae862556 100644 --- a/src/framework/components/particlesystem/particlesystem_system.js +++ b/src/framework/components/particlesystem/particlesystem_system.js @@ -79,15 +79,6 @@ pc.extend(pc, function() { this.on('remove', this.onRemove, this); pc.ComponentSystem.on('update', this.onUpdate, this); - pc.ComponentSystem.on('toolsUpdate', this.onToolsUpdate, this); - - var gd = app.graphicsDevice; - this.debugMesh = this._createDebugMesh(); - - this.debugMaterial = new pc.BasicMaterial(); - this.debugMaterial.color = new pc.Color(1, 0.5, 0, 1); - this.debugMaterial.update(); - }; ParticleSystemComponentSystem = pc.inherits(ParticleSystemComponentSystem, pc.ComponentSystem); @@ -127,10 +118,6 @@ pc.extend(pc, function() { } ParticleSystemComponentSystem._super.initializeComponentData.call(this, component, data, properties); - - if (this._inTools) { - this._createDebugShape(component); - } }, cloneComponent: function (entity, clone) { @@ -193,96 +180,12 @@ pc.extend(pc, function() { } }, - onToolsUpdate: function (dt) { - var components = this.store; - - for (var id in components) { - if (components.hasOwnProperty(id)) { - var c = components[id]; - - if (c.data.enabled && c.entity.enabled) { - this._updateDebugShape(c); - } - } - } - }, - onRemove: function(entity, data) { if (data.model) { this.app.scene.removeModel(data.model); entity.removeChild(data.model.getGraph()); data.model = null; } - }, - - _createDebugMesh: function () { - var gd = this.app.graphicsDevice; - - var format = new pc.VertexFormat(gd, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - var vertexBuffer = new pc.VertexBuffer(gd, format, 8); - var positions = new Float32Array(vertexBuffer.lock()); - positions.set([ - -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, - -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5 - ]); - vertexBuffer.unlock(); - - var indexBuffer = new pc.IndexBuffer(gd, pc.INDEXFORMAT_UINT8, 24); - var indices = new Uint8Array(indexBuffer.lock()); - indices.set([ - 0,1,1,2,2,3,3,0, - 4,5,5,6,6,7,7,4, - 0,4,1,5,2,6,3,7 - ]); - indexBuffer.unlock(); - - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.indexBuffer[0] = indexBuffer; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = indexBuffer.getNumIndices(); - mesh.primitive[0].indexed = true; - return mesh; - }, - - _createDebugShape: function (component) { - var node = new pc.GraphNode(); - - var model = new pc.Model(); - model.graph = node; - - model.meshInstances = [ new pc.MeshInstance(node, this.debugMesh, this.debugMaterial) ]; - - component.data.debugShape = model; - - if (component.data.enabled && component.entity.enabled) { - this.app.root.addChild(node); - this.app.scene.addModel(model); - } - - return model; - }, - - _updateDebugShape: function (component) { - var he = component.data.emitterExtents; - var x = he.x; - var y = he.y; - var z = he.z; - - var entity = component.entity; - var root = component.data.debugShape.graph; - root.setPosition(entity.getPosition()); - root.setRotation(entity.getRotation()); - - x = x || 0.0005; - y = y || 0.0005; - z = z || 0.0005; - - root.setLocalScale(x, y, z); } }); diff --git a/src/framework/components/physics/collision/collision_system.js b/src/framework/components/physics/collision/collision_system.js index 7d09f750f79..36bd737f775 100644 --- a/src/framework/components/physics/collision/collision_system.js +++ b/src/framework/components/physics/collision/collision_system.js @@ -27,13 +27,11 @@ pc.extend(pc, function () { 'model' ]; - this.implementations = {}; - this.debugRender = false; + this.implementations = { }; this.on('remove', this.onRemove, this); pc.ComponentSystem.on('update', this.onUpdate, this); - pc.ComponentSystem.on('toolsUpdate', this.onToolsUpdate, this); }; CollisionComponentSystem = pc.inherits(CollisionComponentSystem, pc.ComponentSystem); @@ -136,37 +134,6 @@ pc.extend(pc, function () { entity.trigger.syncEntityToBody(); } } - - if (this.debugRender) { - this.updateDebugShape(entity, data, this._getImplementation(entity)); - } - } - - }, - - updateDebugShape: function (entity, data, impl) { - var app = this.app; - - if (impl !== undefined) { - if (impl.hasDebugShape) { - if (data.model) { - if (!app.scene.containsModel(data.model)) { - if (entity.enabled && data.enabled) { - app.scene.addModel(data.model); - app.root.addChild(data.model.graph); - } - } else { - if (!data.enabled || !entity.enabled) { - app.root.removeChild(data.model.graph); - app.scene.removeModel(data.model); - } - } - } - - if (data.enabled && entity.enabled) { - impl.updateDebugShape(entity, data); - } - } } }, @@ -174,26 +141,6 @@ pc.extend(pc, function () { this.implementations[component.data.type].updateTransform(component, position, rotation, scale); }, - onToolsUpdate: function (dt) { - var id, entity; - var components = this.store; - - for (id in components) { - entity = components[id].entity; - this.updateDebugShape(entity, components[id].data, this._getImplementation(entity)); - } - }, - - /** - * @function - * @name pc.CollisionComponentSystem#setDebugRender - * @description Display collision shape outlines - * @param {Boolean} value Enable or disable - */ - setDebugRender: function (value) { - this.debugRender = value; - }, - /** * @private * Destroys the previous collision type and creates a new one @@ -218,8 +165,6 @@ pc.extend(pc, function () { */ CollisionSystemImpl = function (system) { this.system = system; - // set this to false if you don't want to create a debug shape - this.hasDebugShape = true; }; CollisionSystemImpl.prototype = { @@ -232,7 +177,6 @@ pc.extend(pc, function () { data.model = new pc.Model(); data.model.graph = new pc.GraphNode(); - data.model.meshInstances = [this.createDebugMesh(data)]; }, /** @@ -277,14 +221,6 @@ pc.extend(pc, function () { } }, - /** - * @private - * Optionally creates a debug mesh instance for a collision - */ - createDebugMesh: function (data) { - return undefined; - }, - /** * @private * Creates a physical shape for the collision. This consists @@ -295,13 +231,6 @@ pc.extend(pc, function () { return undefined; }, - /** - * @private - * Updates the transform of the debug shape if one exists - */ - updateDebugShape: function (entity, data) { - }, - updateTransform: function(component, position, rotation, scale) { if (component.entity.trigger) { component.entity.trigger.syncEntityToBody(); @@ -359,52 +288,6 @@ pc.extend(pc, function () { CollisionBoxSystemImpl = pc.inherits(CollisionBoxSystemImpl, CollisionSystemImpl); CollisionBoxSystemImpl.prototype = pc.extend(CollisionBoxSystemImpl.prototype, { - - createDebugMesh: function (data) { - if (!this.mesh) { - var gd = this.system.app.graphicsDevice; - - var format = new pc.VertexFormat(gd, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - var vertexBuffer = new pc.VertexBuffer(gd, format, 8); - var positions = new Float32Array(vertexBuffer.lock()); - positions.set([ - -0.5, -0.5, -0.5, -0.5, -0.5, 0.5, 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, - -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, -0.5 - ]); - vertexBuffer.unlock(); - - var indexBuffer = new pc.IndexBuffer(gd, pc.INDEXFORMAT_UINT8, 24); - var indices = new Uint8Array(indexBuffer.lock()); - indices.set([ - 0,1,1,2,2,3,3,0, - 4,5,5,6,6,7,7,4, - 0,4,1,5,2,6,3,7 - ]); - indexBuffer.unlock(); - - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.indexBuffer[0] = indexBuffer; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = indexBuffer.getNumIndices(); - mesh.primitive[0].indexed = true; - this.mesh = mesh; - } - - if (!this.material) { - var material = new pc.BasicMaterial(); - material.color = new pc.Color(0, 0, 1, 1); - material.update() - this.material = material; - } - - return new pc.MeshInstance(data.model.graph, this.mesh, this.material); - }, - createPhysicalShape: function (entity, data) { if (typeof Ammo !== 'undefined') { var he = data.halfExtents; @@ -413,18 +296,6 @@ pc.extend(pc, function () { } else { return undefined; } - }, - - updateDebugShape : function (entity, data) { - var he = data.halfExtents; - var x = he.x; - var y = he.y; - var z = he.z; - - var root = data.model.graph; - root.setPosition(entity.getPosition()); - root.setRotation(entity.getRotation()); - root.setLocalScale(x * 2, y * 2, z * 2); } }); @@ -437,86 +308,12 @@ pc.extend(pc, function () { CollisionSphereSystemImpl = pc.inherits(CollisionSphereSystemImpl, CollisionSystemImpl); CollisionSphereSystemImpl.prototype = pc.extend(CollisionSphereSystemImpl.prototype, { - createDebugMesh: function (data) { - if (!this.mesh) { - var app = this.system.app; - var gd = app.graphicsDevice; - - // Create the graphical resources required to render a camera frustum - var format = new pc.VertexFormat(gd, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - var vertexBuffer = new pc.VertexBuffer(gd, format, 240); - var positions = new Float32Array(vertexBuffer.lock()); - - var i, x = 0; - var theta; - for (var ring = 0; ring < 3; ring++) { - var xo = 0; - var yo = 1; - var zo = 2; - if (ring === 1) { - xo = 1; - yo = 0; - zo = 2; - } else if (ring === 2) { - xo = 0; - yo = 2; - zo = 1; - } - - for (i = 0; i < 40; i++) { - theta = 2 * Math.PI * (i / 40); - positions[x+xo] = 0.5 * Math.cos(theta); - positions[x+yo] = 0; - positions[x+zo] = 0.5 * Math.sin(theta); - x += 3; - - theta = 2 * Math.PI * ((i + 1) / 40); - positions[x+xo] = 0.5 * Math.cos(theta); - positions[x+yo] = 0; - positions[x+zo] = 0.5 * Math.sin(theta); - x += 3; - } - } - - vertexBuffer.unlock(); - - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = vertexBuffer.getNumVertices(); - mesh.primitive[0].indexed = false; - - this.mesh = mesh; - } - - if (!this.material) { - var material = new pc.BasicMaterial(); - material.color = new pc.Color(0, 0, 1, 1); - material.update(); - this.material = material; - } - - return new pc.MeshInstance(data.model.graph, this.mesh, this.material); - }, - createPhysicalShape: function (entity, data) { if (typeof Ammo !== 'undefined') { return new Ammo.btSphereShape(data.radius); } else { return undefined; } - }, - - updateDebugShape: function (entity, data) { - var root = data.model.graph; - root.setPosition(entity.getPosition()); - root.setRotation(entity.getRotation()); - var s = data.radius * 2; - root.setLocalScale(s, s, s); } }); @@ -529,129 +326,6 @@ pc.extend(pc, function () { CollisionCapsuleSystemImpl = pc.inherits(CollisionCapsuleSystemImpl, CollisionSystemImpl); CollisionCapsuleSystemImpl.prototype = pc.extend(CollisionCapsuleSystemImpl.prototype, { - createDebugMesh: function (data) { - // The capsule collision system creates a separate debug mesh - // for each capsule because of its particular shape. So if a mesh has already - // been created for this component then return it, otherwise create a new one - if (data.model && data.model.meshInstances && data.model.meshInstances.length) { - return data.model.meshInstances[0]; - } else { - var gd = this.system.app.graphicsDevice; - - // Create the graphical resources required to render a capsule shape - var format = new pc.VertexFormat(gd, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - var vertexBuffer = new pc.VertexBuffer(gd, format, 328, pc.BUFFER_DYNAMIC); - this.updateCapsuleShape(data, vertexBuffer); - - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = vertexBuffer.getNumVertices(); - mesh.primitive[0].indexed = false; - - this.mesh = mesh; - } - - // no need to create a new material for each capsule shape - if (!this.material) { - var material = new pc.BasicMaterial(); - material.color = new pc.Color(0, 0, 1, 1); - material.update(); - this.material = material; - } - - return new pc.MeshInstance(data.model.graph, mesh, this.material); - }, - - updateCapsuleShape: function(data, vertexBuffer) { - var axis = (data.axis !== undefined) ? data.axis : 1; - var radius = data.radius || 0.5; - var height = Math.max((data.height || 2) - 2 * radius, 0); - - var positions = new Float32Array(vertexBuffer.lock()); - - var xo = 0; - var yo = 1; - var zo = 2; - if (axis === 0) { - xo = 1; - yo = 0; - zo = 2; - } else if (axis === 2) { - xo = 0; - yo = 2; - zo = 1; - } - - var i, x = 0; - var theta; - // Generate caps - for (cap = -1; cap < 2; cap += 2) { - for (i = 0; i < 40; i++) { - theta = 2 * Math.PI * (i / 40); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = cap * height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - - theta = 2 * Math.PI * ((i + 1) / 40); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = cap * height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - } - - for (i = 0; i < 20; i++) { - theta = Math.PI * (i / 20) + Math.PI * 1.5; - positions[x+xo] = 0; - positions[x+yo] = cap * (height * 0.5 + radius * Math.cos(theta)); - positions[x+zo] = cap * (radius * Math.sin(theta)); - x += 3; - - theta = Math.PI * ((i + 1) / 20) + Math.PI * 1.5; - positions[x+xo] = 0; - positions[x+yo] = cap * (height * 0.5 + radius * Math.cos(theta)); - positions[x+zo] = cap * (radius * Math.sin(theta)); - x += 3; - } - - for (i = 0; i < 20; i++) { - theta = Math.PI * (i / 20) + Math.PI * 1.5; - positions[x+xo] = cap * (radius * Math.sin(theta)); - positions[x+yo] = cap * (height * 0.5 + radius * Math.cos(theta)); - positions[x+zo] = 0; - x += 3; - - theta = Math.PI * ((i + 1) / 20) + Math.PI * 1.5; - positions[x+xo] = cap * (radius * Math.sin(theta)); - positions[x+yo] = cap * (height * 0.5 + radius * Math.cos(theta)); - positions[x+zo] = 0; - x += 3; - } - } - - // Connect caps - for (i = 0; i < 4; i++) { - theta = 2 * Math.PI * (i / 4); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - - theta = 2 * Math.PI * (i / 4); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = -height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - } - - vertexBuffer.unlock(); - }, - createPhysicalShape: function (entity, data) { var shape = null; var axis = (data.axis !== undefined) ? data.axis : 1; @@ -672,25 +346,7 @@ pc.extend(pc, function () { } } return shape; - }, - - updateDebugShape: function (entity, data) { - var root = data.model.graph; - root.setPosition(entity.getPosition()); - root.setRotation(entity.getRotation()); - root.setLocalScale(1, 1, 1); - }, - - recreatePhysicalShapes: function (component) { - var model = component.data.model; - if (model) { - // get the vertex buffer for this collision shape. createDebugMesh - // will return the existing mesh if one exists in this case - var vertexBuffer = this.createDebugMesh(component.data).mesh.vertexBuffer; - this.updateCapsuleShape(component.data, vertexBuffer); - CollisionCapsuleSystemImpl._super.recreatePhysicalShapes.call(this, component); - } - }, + } }); /** @@ -702,94 +358,6 @@ pc.extend(pc, function () { CollisionCylinderSystemImpl = pc.inherits(CollisionCylinderSystemImpl, CollisionSystemImpl); CollisionCylinderSystemImpl.prototype = pc.extend(CollisionCylinderSystemImpl.prototype, { - createDebugMesh: function (data) { - if (data.model && data.model.meshInstances && data.model.meshInstances.length) { - return data.model.meshInstances[0]; - } else { - var gd = this.system.app.graphicsDevice; - - var format = new pc.VertexFormat(gd, [ - { semantic: pc.SEMANTIC_POSITION, components: 3, type: pc.ELEMENTTYPE_FLOAT32 } - ]); - - var vertexBuffer = new pc.VertexBuffer(gd, format, 168, pc.BUFFER_DYNAMIC); - this.updateCylinderShape(data, vertexBuffer); - - var mesh = new pc.Mesh(); - mesh.vertexBuffer = vertexBuffer; - mesh.primitive[0].type = pc.PRIMITIVE_LINES; - mesh.primitive[0].base = 0; - mesh.primitive[0].count = vertexBuffer.getNumVertices(); - mesh.primitive[0].indexed = false; - - if (!this.material) { - var material = new pc.BasicMaterial(); - material.color = new pc.Color(0, 0, 1, 1); - material.update(); - this.material = material; - } - - return new pc.MeshInstance(data.model.graph, mesh, this.material); - } - }, - - updateCylinderShape: function(data, vertexBuffer) { - var axis = (data.axis !== undefined) ? data.axis : 1; - var radius = (data.radius !== undefined) ? data.radius : 0.5; - var height = (data.height !== undefined) ? data.height : 1; - - var positions = new Float32Array(vertexBuffer.lock()); - - var xo = 0; - var yo = 1; - var zo = 2; - if (axis === 0) { - xo = 1; - yo = 0; - zo = 2; - } else if (axis === 2) { - xo = 0; - yo = 2; - zo = 1; - } - - var i, x = 0; - var theta; - // Generate caps - for (cap = -1; cap < 2; cap += 2) { - for (i = 0; i < 40; i++) { - theta = 2 * Math.PI * (i / 40); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = cap * height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - - theta = 2 * Math.PI * ((i + 1) / 40); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = cap * height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - } - } - - // Connect caps - for (i = 0; i < 4; i++) { - theta = 2 * Math.PI * (i / 4); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - - theta = 2 * Math.PI * (i / 4); - positions[x+xo] = radius * Math.cos(theta); - positions[x+yo] = -height * 0.5; - positions[x+zo] = radius * Math.sin(theta); - x += 3; - } - - vertexBuffer.unlock(); - }, - createPhysicalShape: function (entity, data) { var halfExtents = null; var shape = null; @@ -814,32 +382,14 @@ pc.extend(pc, function () { } } return shape; - }, - - updateDebugShape: function (entity, data) { - var root = data.model.graph; - root.setPosition(entity.getPosition()); - root.setRotation(entity.getRotation()); - root.setLocalScale(1, 1, 1); - }, - - recreatePhysicalShapes: function (component) { - var model = component.data.model; - if (model) { - var vertexBuffer = this.createDebugMesh(component.data).mesh.vertexBuffer; - this.updateCylinderShape(component.data, vertexBuffer); - CollisionCylinderSystemImpl._super.recreatePhysicalShapes.call(this, component); - } - }, + } }); /** /* Mesh Collision System */ - CollisionMeshSystemImpl = function (system) { - this.hasDebugShape = false; - }; + CollisionMeshSystemImpl = function (system) { }; CollisionMeshSystemImpl = pc.inherits(CollisionMeshSystemImpl, CollisionSystemImpl); @@ -1001,7 +551,6 @@ pc.extend(pc, function () { CollisionMeshSystemImpl._super.updateTransform.call(this, component, position, rotation, scale); } - }); return { diff --git a/src/framework/components/script/script_system.js b/src/framework/components/script/script_system.js index e53b084c252..79634997ea3 100644 --- a/src/framework/components/script/script_system.js +++ b/src/framework/components/script/script_system.js @@ -236,7 +236,7 @@ pc.extend(pc, function () { var item; for (var i=0, len=updateList.length; i