Browse Source

Merge pull request #6344 from benaadams/depth

Add depth function to materials
Ricardo Cabello 10 years ago
parent
commit
7cf11ed945
4 changed files with 80 additions and 0 deletions
  1. 11 0
      src/Three.js
  2. 2 0
      src/materials/Material.js
  3. 1 0
      src/renderers/WebGLRenderer.js
  4. 66 0
      src/renderers/webgl/WebGLState.js

+ 11 - 0
src/Three.js

@@ -115,6 +115,17 @@ THREE.DstColorFactor = 208;
 THREE.OneMinusDstColorFactor = 209;
 THREE.SrcAlphaSaturateFactor = 210;
 
+// depth modes
+
+THREE.NeverDepth = 0;
+THREE.AlwaysDepth = 1;
+THREE.LessDepth = 2;
+THREE.LessEqualDepth = 3;
+THREE.EqualDepth = 4;
+THREE.GreaterEqualDepth = 5;
+THREE.GreaterDepth = 6;
+THREE.NotEqualDepth = 7;
+
 
 // TEXTURE CONSTANTS
 

+ 2 - 0
src/materials/Material.js

@@ -26,6 +26,7 @@ THREE.Material = function () {
 	this.blendDstAlpha = null;
 	this.blendEquationAlpha = null;
 
+	this.depthFunc = THREE.LessEqualDepth;
 	this.depthTest = true;
 	this.depthWrite = true;
 
@@ -152,6 +153,7 @@ THREE.Material.prototype = {
 		material.blendDstAlpha = this.blendDstAlpha;
 		material.blendEquationAlpha = this.blendEquationAlpha;
 
+		material.depthFunc = this.depthFunc;
 		material.depthTest = this.depthTest;
 		material.depthWrite = this.depthWrite;
 

+ 1 - 0
src/renderers/WebGLRenderer.js

@@ -2479,6 +2479,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 		}
 
+		state.setDepthFunc( material.depthFunc );
 		state.setDepthTest( material.depthTest );
 		state.setDepthWrite( material.depthWrite );
 		state.setColorWrite( material.colorWrite );

+ 66 - 0
src/renderers/webgl/WebGLState.js

@@ -15,6 +15,7 @@ THREE.WebGLState = function ( gl, paramThreeToGL ) {
 	var currentBlendSrcAlpha = null;
 	var currentBlendDstAlpha = null;
 
+	var currentDepthFunc = null;
 	var currentDepthTest = null;
 	var currentDepthWrite = null;
 
@@ -150,6 +151,71 @@ THREE.WebGLState = function ( gl, paramThreeToGL ) {
 
 	};
 
+	this.setDepthFunc = function ( depthFunc ) {
+
+	    if ( currentDepthFunc !== depthFunc ) {
+
+	        if ( depthFunc ) {
+
+	            switch ( depthFunc ) {
+
+	                case THREE.NeverDepth:
+
+	                    gl.depthFunc( gl.NEVER );
+	                    break;
+
+	                case THREE.AlwaysDepth:
+
+	                    gl.depthFunc( gl.ALWAYS );
+	                    break;
+
+	                case THREE.LessDepth:
+
+	                    gl.depthFunc( gl.LESS );
+	                    break;
+
+	                case THREE.LessEqualDepth:
+
+	                    gl.depthFunc( gl.LEQUAL );
+	                    break;
+
+	                case THREE.EqualDepth:
+
+	                    gl.depthFunc( gl.EQUAL );
+	                    break;
+
+	                case THREE.GreaterEqualDepth:
+
+	                    gl.depthFunc( gl.GEQUAL );
+	                    break;
+
+	                case THREE.GreaterDepth:
+
+	                    gl.depthFunc( gl.GREATER );
+	                    break;
+
+	                case THREE.NotEqualDepth:
+
+	                    gl.depthFunc( gl.NOTEQUAL );
+	                    break;
+
+	                default:
+
+                        gl.depthFunc( gl.LEQUAL );
+	            }
+
+	        } else {
+
+	            gl.depthFunc( gl.LEQUAL );
+
+	        }
+
+	        currentDepthFunc = depthFunc;
+
+	    }
+
+	};
+
 	this.setDepthTest = function ( depthTest ) {
 
 		if ( currentDepthTest !== depthTest ) {