Skip to content

Commit

Permalink
Merge pull request #372 from playcanvas/remove-debug-shapes
Browse files Browse the repository at this point in the history
Remove components debug shapes
  • Loading branch information
Maksims committed Jul 30, 2015
2 parents 1bbacbf + 5c66bb7 commit 731abaa
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 1,046 deletions.
137 changes: 1 addition & 136 deletions src/framework/components/camera/camera_system.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
},

Expand All @@ -135,117 +98,19 @@ 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) {
var index = this.cameras.indexOf(camera);
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);
}
}
}
},

Expand Down
75 changes: 41 additions & 34 deletions src/framework/components/light/light_component.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -167,7 +172,7 @@ pc.extend(pc, function () {
},

onSetMask: function (name, oldValue, newValue) {
this.light.mask = newValue;
this.light.mask = newValue;
},

onEnable: function () {
Expand All @@ -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 () {
Expand All @@ -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);
}
});

Expand Down
Loading

0 comments on commit 731abaa

Please sign in to comment.