From decc9a48a9e16d384c18716cec8d52d9f7f6032b Mon Sep 17 00:00:00 2001 From: pissang Date: Thu, 7 Sep 2017 22:23:46 +0800 Subject: [PATCH] Tweaks --- src/Renderer.js | 145 ++++++++++++++++++++++---------------------- src/math/Frustum.js | 5 +- 2 files changed, 77 insertions(+), 73 deletions(-) diff --git a/src/Renderer.js b/src/Renderer.js index 5b7a55198..f3055789f 100644 --- a/src/Renderer.js +++ b/src/Renderer.js @@ -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]; @@ -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 * diff --git a/src/math/Frustum.js b/src/math/Frustum.js index 9cfd1cecb..0c07d5d7d 100644 --- a/src/math/Frustum.js +++ b/src/math/Frustum.js @@ -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;