Skip to content

Commit

Permalink
Merge branch 'patchVisible' into prtdrawcalls
Browse files Browse the repository at this point in the history
  • Loading branch information
guycalledfrank committed Jun 8, 2016
2 parents f8ecdc5 + 6945d70 commit 4b5c0f6
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
52 changes: 52 additions & 0 deletions src/framework/components/model/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,58 @@ pc.extend(pc, function () {
this.system.app.scene.removeModel(model);
}
}
},

/**
* @function
* @name pc.ModelComponent#hide
* @description Stop rendering model without removing it from the scene hierarchy.
* This method sets the {@link pc.MeshInstance#visible} property of every MeshInstance in the model to false
* Note, this does not remove the model or mesh instances from the scene hierarchy or draw call list.
* So the model component still incurs some CPU overhead.
* @example
* this.timer = 0;
* this.visible = true;
* // ...
* // blink model every 0.1 seconds
* this.timer += dt;
* if (this.timer > 0.1) {
* if (!this.visible) {
* this.entity.model.show();
* this.visible = true;
* } else {
* this.entity.model.hide();
* this.visible = false;
* }
* this.timer = 0;
* }
*/
hide: function () {
var model = this.data.model;
if (model) {
var i, l;
var instances = model.meshInstances;
for (i = 0, l = instances.length; i < l; i++) {
instances[i].visible = false;
}
}
},

/**
* @function
* @name pc.ModelComponent#show
* @description Enable rendering of the model if hidden using {@link pc.ModelComponent#hide}.
* This method sets all the {@link pc.MeshInstance#visible} property on all mesh instances to true.
*/
show: function () {
var model = this.data.model;
if (model) {
var i, l;
var instances = model.meshInstances;
for (i = 0, l = instances.length; i < l; i++) {
instances[i].visible = true;
}
}
}
});

Expand Down
4 changes: 3 additions & 1 deletion src/scene/forward-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,8 @@ pc.extend(pc, function () {
pc.extend(ForwardRenderer.prototype, {

_isVisible: function(camera, meshInstance) {
if (!meshInstance.visible) return false;

meshPos = meshInstance.aabb.center;
if (meshInstance.node._dirtyScale) {
meshInstance._aabb._radius = meshInstance._aabb.halfExtents.length();
Expand Down Expand Up @@ -964,7 +966,7 @@ pc.extend(pc, function () {
visible = true;
meshPos = null;
if (!drawCall.command) {
if (drawCall._hidden) continue; // use _hidden property to quickly hide/show meshInstances
if (!drawCall.visible) continue; // use hidden property to quickly hide/show meshInstances
meshInstance = drawCall;

// Only alpha sort and cull mesh instances in the main world
Expand Down
3 changes: 3 additions & 0 deletions src/scene/mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ pc.extend(pc, function () {
* mesh instance.
* @property {Boolean} castShadow Controls whether the mesh instances casts shadows.
* Defaults to false.
* @property {Boolean} visible Enable rendering for this mesh instance. Use visible property to enable/disable rendering without overhead of removing from scene.
* But note that the mesh instance is still in the hierarchy and still in the draw call list.
* @property {Number} layer The layer used by this mesh instance. Can be:
* <ul>
* <li>pc.LAYER_WORLD</li>
Expand Down Expand Up @@ -109,6 +111,7 @@ pc.extend(pc, function () {
this._shaderDefs |= mesh.vertexBuffer.format.hasColor? pc.SHADERDEF_VCOLOR : 0;

// Render options
this.visible = true;
this.layer = pc.LAYER_WORLD;
this.renderStyle = pc.RENDERSTYLE_SOLID;
this.castShadow = false;
Expand Down

0 comments on commit 4b5c0f6

Please sign in to comment.