Selaa lähdekoodia

WebGLRenderer: Moved material logic out of renderBufferImmediate().

Mr.doob 7 vuotta sitten
vanhempi
commit
7f8a2e922e
3 muutettua tiedostoa jossa 43 lisäystä ja 53 poistoa
  1. 40 17
      examples/js/MarchingCubes.js
  2. 1 1
      examples/webgl_marchingcubes.html
  3. 2 35
      src/renderers/WebGLRenderer.js

+ 40 - 17
examples/js/MarchingCubes.js

@@ -342,17 +342,40 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 		// normals
 
-		scope.normalArray[ c + 0 ] = norm[ o1 ];
-		scope.normalArray[ c + 1 ] = norm[ o1 + 1 ];
-		scope.normalArray[ c + 2 ] = norm[ o1 + 2 ];
+		if ( scope.material.flatShading === true ) {
 
-		scope.normalArray[ c + 3 ] = norm[ o2 ];
-		scope.normalArray[ c + 4 ] = norm[ o2 + 1 ];
-		scope.normalArray[ c + 5 ] = norm[ o2 + 2 ];
+			var nx = ( norm[ o1 + 0 ] + norm[ o2 + 0 ] + norm[ o3 + 0 ] ) / 3;
+			var ny = ( norm[ o1 + 1 ] + norm[ o2 + 1 ] + norm[ o3 + 1 ] ) / 3;
+			var nz = ( norm[ o1 + 2 ] + norm[ o2 + 2 ] + norm[ o3 + 2 ] ) / 3;
 
-		scope.normalArray[ c + 6 ] = norm[ o3 ];
-		scope.normalArray[ c + 7 ] = norm[ o3 + 1 ];
-		scope.normalArray[ c + 8 ] = norm[ o3 + 2 ];
+			scope.normalArray[ c + 0 ] = nx;
+			scope.normalArray[ c + 1 ] = ny;
+			scope.normalArray[ c + 2 ] = nz;
+
+			scope.normalArray[ c + 3 ] = nx;
+			scope.normalArray[ c + 4 ] = ny;
+			scope.normalArray[ c + 5 ] = nz;
+
+			scope.normalArray[ c + 6 ] = nx;
+			scope.normalArray[ c + 7 ] = ny;
+			scope.normalArray[ c + 8 ] = nz;
+
+
+		} else {
+
+			scope.normalArray[ c + 0 ] = norm[ o1 + 0 ];
+			scope.normalArray[ c + 1 ] = norm[ o1 + 1 ];
+			scope.normalArray[ c + 2 ] = norm[ o1 + 2 ];
+
+			scope.normalArray[ c + 3 ] = norm[ o2 + 0 ];
+			scope.normalArray[ c + 4 ] = norm[ o2 + 1 ];
+			scope.normalArray[ c + 5 ] = norm[ o2 + 2 ];
+
+			scope.normalArray[ c + 6 ] = norm[ o3 + 0 ];
+			scope.normalArray[ c + 7 ] = norm[ o3 + 1 ];
+			scope.normalArray[ c + 8 ] = norm[ o3 + 2 ];
+
+		}
 
 		// uvs
 
@@ -360,13 +383,13 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 			var d = scope.count * 2;
 
-			scope.uvArray[ d + 0 ] = pos[ o1 ];
+			scope.uvArray[ d + 0 ] = pos[ o1 + 0 ];
 			scope.uvArray[ d + 1 ] = pos[ o1 + 2 ];
 
-			scope.uvArray[ d + 2 ] = pos[ o2 ];
+			scope.uvArray[ d + 2 ] = pos[ o2 + 0 ];
 			scope.uvArray[ d + 3 ] = pos[ o2 + 2 ];
 
-			scope.uvArray[ d + 4 ] = pos[ o3 ];
+			scope.uvArray[ d + 4 ] = pos[ o3 + 0 ];
 			scope.uvArray[ d + 5 ] = pos[ o3 + 2 ];
 
 		}
@@ -375,15 +398,15 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 
 		if ( scope.enableColors ) {
 
-			scope.colorArray[ c + 0 ] = pos[ o1 ];
+			scope.colorArray[ c + 0 ] = pos[ o1 + 0 ];
 			scope.colorArray[ c + 1 ] = pos[ o1 + 1 ];
 			scope.colorArray[ c + 2 ] = pos[ o1 + 2 ];
 
-			scope.colorArray[ c + 3 ] = pos[ o2 ];
+			scope.colorArray[ c + 3 ] = pos[ o2 + 0 ];
 			scope.colorArray[ c + 4 ] = pos[ o2 + 1 ];
 			scope.colorArray[ c + 5 ] = pos[ o2 + 2 ];
 
-			scope.colorArray[ c + 6 ] = pos[ o3 ];
+			scope.colorArray[ c + 6 ] = pos[ o3 + 0 ];
 			scope.colorArray[ c + 7 ] = pos[ o3 + 1 ];
 			scope.colorArray[ c + 8 ] = pos[ o3 + 2 ];
 
@@ -438,13 +461,13 @@ THREE.MarchingCubes = function ( resolution, material, enableUvs, enableColors )
 		this.hasPositions = true;
 		this.hasNormals = true;
 
-		if ( this.enableUvs ) {
+		if ( this.enableUvs && this.material.map ) {
 
 			this.hasUvs = true;
 
 		}
 
-		if ( this.enableColors ) {
+		if ( this.enableColors && this.material.vertexColors !== THREE.NoColors ) {
 
 			this.hasColors = true;
 

+ 1 - 1
examples/webgl_marchingcubes.html

@@ -313,7 +313,7 @@
 
 			"flat" :
 			{
-				m: new THREE.MeshPhongMaterial( { color: 0x000000, specular: 0x111111, shininess: 1, flatShading: true } ),
+				m: new THREE.MeshLambertMaterial( { color: 0x000000, flatShading: true } ),
 				h: 0, s: 0, l: 1
 			},
 

+ 2 - 35
src/renderers/WebGLRenderer.js

@@ -602,62 +602,29 @@ function WebGLRenderer( parameters ) {
 		if ( object.hasNormals ) {
 
 			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.normal );
-
-			if ( ! material.isMeshPhongMaterial &&
-				! material.isMeshStandardMaterial &&
-				! material.isMeshNormalMaterial &&
-				material.flatShading === true ) {
-
-				for ( var i = 0, l = object.count * 3; i < l; i += 9 ) {
-
-					var array = object.normalArray;
-
-					var nx = ( array[ i + 0 ] + array[ i + 3 ] + array[ i + 6 ] ) / 3;
-					var ny = ( array[ i + 1 ] + array[ i + 4 ] + array[ i + 7 ] ) / 3;
-					var nz = ( array[ i + 2 ] + array[ i + 5 ] + array[ i + 8 ] ) / 3;
-
-					array[ i + 0 ] = nx;
-					array[ i + 1 ] = ny;
-					array[ i + 2 ] = nz;
-
-					array[ i + 3 ] = nx;
-					array[ i + 4 ] = ny;
-					array[ i + 5 ] = nz;
-
-					array[ i + 6 ] = nx;
-					array[ i + 7 ] = ny;
-					array[ i + 8 ] = nz;
-
-				}
-
-			}
-
 			_gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
 
 			state.enableAttribute( programAttributes.normal );
-
 			_gl.vertexAttribPointer( programAttributes.normal, 3, _gl.FLOAT, false, 0, 0 );
 
 		}
 
-		if ( object.hasUvs && material.map ) {
+		if ( object.hasUvs ) {
 
 			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.uv );
 			_gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
 
 			state.enableAttribute( programAttributes.uv );
-
 			_gl.vertexAttribPointer( programAttributes.uv, 2, _gl.FLOAT, false, 0, 0 );
 
 		}
 
-		if ( object.hasColors && material.vertexColors !== NoColors ) {
+		if ( object.hasColors ) {
 
 			_gl.bindBuffer( _gl.ARRAY_BUFFER, buffers.color );
 			_gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
 
 			state.enableAttribute( programAttributes.color );
-
 			_gl.vertexAttribPointer( programAttributes.color, 3, _gl.FLOAT, false, 0, 0 );
 
 		}