Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
pissang committed Sep 7, 2017
1 parent 2fb7f54 commit decc9a4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 73 deletions.
145 changes: 74 additions & 71 deletions src/Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,85 +510,18 @@ define(function(require) {
var prevMaterial;
var prevShader;

// Status
var depthTest, depthMask;
var culling, cullFace, frontFace;

var culledRenderQueue;
if (preZ) {
var preZPassMaterial = this._prezMaterial || new Material({
shader: new Shader({
vertex: Shader.source('qtek.prez.vertex'),
fragment: Shader.source('qtek.prez.fragment')
})
});
this._prezMaterial = preZPassMaterial;
var preZPassShader = preZPassMaterial.shader;

culledRenderQueue = [];
preZPassShader.bind(_gl);
_gl.colorMask(false, false, false, false);
_gl.depthMask(true);
_gl.enable(_gl.DEPTH_TEST);
for (var i = 0; i < queue.length; i++) {
var renderable = queue[i];
if (!this.ifRenderObject(renderable)) {
continue;
}

var worldM = renderable.worldTransform._array;
var geometry = renderable.geometry;

mat4.multiplyAffine(matrices.WORLDVIEW, matrices.VIEW , worldM);

if (geometry.boundingBox) {
if (this.isFrustumCulled(
renderable, scene, camera, matrices.WORLDVIEW, matrices.PROJECTION
)) {
continue;
}
}
if (renderable.skeleton) { // FIXME skinned mesh
continue;
}

mat4.multiply(matrices.WORLDVIEWPROJECTION, matrices.VIEWPROJECTION , worldM);

if (renderable.cullFace !== cullFace) {
cullFace = renderable.cullFace;
_gl.cullFace(cullFace);
}
if (renderable.frontFace !== frontFace) {
frontFace = renderable.frontFace;
_gl.frontFace(frontFace);
}
if (renderable.culling !== culling) {
culling = renderable.culling;
culling ? _gl.enable(_gl.CULL_FACE) : _gl.disable(_gl.CULL_FACE);
}

var semanticInfo = preZPassShader.matrixSemantics.WORLDVIEWPROJECTION;
preZPassShader.setUniform(_gl, semanticInfo.type, semanticInfo.symbol, matrices.WORLDVIEWPROJECTION);

// PENDING If invoke beforeRender hook
renderable.render(_gl, preZPassMaterial.shader);
culledRenderQueue.push(renderable);
}
_gl.depthFunc(_gl.LEQUAL);
_gl.colorMask(true, true, true, true);
_gl.depthMask(false);

// Reset current shader.
this._currentShader = null;
culledRenderQueue = this._renderPreZ(queue, scene, camera);
}
else {
culledRenderQueue = queue;
_gl.depthFunc(_gl.LESS);
}

culling = null;
cullFace = null;
frontFace = null;
// Status
var depthTest, depthMask;
var culling, cullFace, frontFace;

for (var i = 0; i < culledRenderQueue.length; i++) {
var renderable = culledRenderQueue[i];
Expand Down Expand Up @@ -753,6 +686,76 @@ define(function(require) {
return renderInfo;
},

_renderPreZ: function (queue, scene, camera) {
var _gl = this.gl;
var preZPassMaterial = this._prezMaterial || new Material({
shader: new Shader({
vertex: Shader.source('qtek.prez.vertex'),
fragment: Shader.source('qtek.prez.fragment')
})
});
this._prezMaterial = preZPassMaterial;
var preZPassShader = preZPassMaterial.shader;

var culledRenderQueue = [];
// Status
var culling, cullFace, frontFace;

preZPassShader.bind(_gl);
_gl.colorMask(false, false, false, false);
_gl.depthMask(true);
_gl.enable(_gl.DEPTH_TEST);
for (var i = 0; i < queue.length; i++) {
var renderable = queue[i];
if (!this.ifRenderObject(renderable)) {
continue;
}

var worldM = renderable.worldTransform._array;
var geometry = renderable.geometry;

mat4.multiplyAffine(matrices.WORLDVIEW, matrices.VIEW , worldM);

if (geometry.boundingBox) {
if (this.isFrustumCulled(
renderable, scene, camera, matrices.WORLDVIEW, matrices.PROJECTION
)) {
continue;
}
}
if (renderable.skeleton) { // FIXME skinned mesh
continue;
}

mat4.multiply(matrices.WORLDVIEWPROJECTION, matrices.VIEWPROJECTION , worldM);

if (renderable.cullFace !== cullFace) {
cullFace = renderable.cullFace;
_gl.cullFace(cullFace);
}
if (renderable.frontFace !== frontFace) {
frontFace = renderable.frontFace;
_gl.frontFace(frontFace);
}
if (renderable.culling !== culling) {
culling = renderable.culling;
culling ? _gl.enable(_gl.CULL_FACE) : _gl.disable(_gl.CULL_FACE);
}

var semanticInfo = preZPassShader.matrixSemantics.WORLDVIEWPROJECTION;
preZPassShader.setUniform(_gl, semanticInfo.type, semanticInfo.symbol, matrices.WORLDVIEWPROJECTION);

// PENDING If invoke beforeRender hook
renderable.render(_gl, preZPassMaterial.shader);
culledRenderQueue.push(renderable);
}
_gl.depthFunc(_gl.LEQUAL);
_gl.colorMask(true, true, true, true);
_gl.depthMask(false);

return culledRenderQueue;
},

/**
* If an scene object is culled by camera frustum
*
Expand Down
5 changes: 3 additions & 2 deletions src/math/Frustum.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ define(function(require) {
var near = (-1 - m14) / m10;
var far = (1 - m14) / m10;

boundingBox.min.set(left, bottom, far);
boundingBox.max.set(right, top, near);

boundingBox.min.set(Math.min(left, right), Math.min(bottom, top), Math.min(far, near));
boundingBox.max.set(Math.max(right, left), Math.max(top, bottom), Math.max(near, far));

var min = boundingBox.min._array;
var max = boundingBox.max._array;
Expand Down

0 comments on commit decc9a4

Please sign in to comment.