From 20b77b2785afaa4d00a1ecd222e6de1a3ec76006 Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Tue, 28 Jul 2015 18:13:33 -0400 Subject: [PATCH] Reverting MeshFaceMaterial removal. --- examples/canvas_materials.html | 9 +++++++++ examples/js/loaders/BinaryLoader.js | 14 +++++++------- examples/js/renderers/Projector.js | 2 +- examples/webgl_materials.html | 19 ++++++++++++++++--- src/core/Face3.js | 6 +++++- src/core/Geometry.js | 6 +++++- src/extras/geometries/BoxGeometry.js | 16 +++++++++------- src/loaders/JSONLoader.js | 9 ++++++--- 8 files changed, 58 insertions(+), 23 deletions(-) diff --git a/examples/canvas_materials.html b/examples/canvas_materials.html index 112e7a6472..e7ed1782ad 100644 --- a/examples/canvas_materials.html +++ b/examples/canvas_materials.html @@ -80,6 +80,15 @@ ]; + for ( var i = 0, l = geometry.faces.length; i < l; i ++ ) { + + var face = geometry.faces[ i ]; + if ( Math.random() > 0.5 ) face.materialIndex = Math.floor( Math.random() * materials.length ); + + } + + materials.push( new THREE.MeshFaceMaterial( materials ) ); + objects = []; for ( var i = 0, l = materials.length; i < l; i ++ ) { diff --git a/examples/js/loaders/BinaryLoader.js b/examples/js/loaders/BinaryLoader.js index c104dae695..bf2736a39a 100644 --- a/examples/js/loaders/BinaryLoader.js +++ b/examples/js/loaders/BinaryLoader.js @@ -232,7 +232,7 @@ THREE.BinaryLoader.prototype = { + metaData.nquad_smooth_uv * ( metaData.vertex_index_bytes*4 + metaData.material_index_bytes + metaData.normal_index_bytes*4 + metaData.uv_index_bytes*4 ); console.log( "total bytes: " + total ); */ - + return metaData; } @@ -429,7 +429,7 @@ THREE.BinaryLoader.prototype = { m = materialIndexBuffer[ i ]; - scope.faces.push( new THREE.Face3( a, b, c ) ); + scope.faces.push( new THREE.Face3( a, b, c, null, null, m ) ); } @@ -451,8 +451,8 @@ THREE.BinaryLoader.prototype = { m = materialIndexBuffer[ i ]; - scope.faces.push( new THREE.Face3( a, b, d ) ); - scope.faces.push( new THREE.Face3( b, c, d ) ); + scope.faces.push( new THREE.Face3( a, b, d, null, null, m ) ); + scope.faces.push( new THREE.Face3( b, c, d, null, null, m ) ); } @@ -495,7 +495,7 @@ THREE.BinaryLoader.prototype = { new THREE.Vector3( nax, nay, naz ), new THREE.Vector3( nbx, nby, nbz ), new THREE.Vector3( ncx, ncy, ncz ) - ] ) ); + ], null, m ) ); } @@ -544,13 +544,13 @@ THREE.BinaryLoader.prototype = { new THREE.Vector3( nax, nay, naz ), new THREE.Vector3( nbx, nby, nbz ), new THREE.Vector3( ndx, ndy, ndz ) - ] ) ); + ], null, m ) ); scope.faces.push( new THREE.Face3( b, c, d, [ new THREE.Vector3( nbx, nby, nbz ), new THREE.Vector3( ncx, ncy, ncz ), new THREE.Vector3( ndx, ndy, ndz ) - ] ) ); + ], null, m ) ); } diff --git a/examples/js/renderers/Projector.js b/examples/js/renderers/Projector.js index 331323601b..faef098c5c 100644 --- a/examples/js/renderers/Projector.js +++ b/examples/js/renderers/Projector.js @@ -517,7 +517,7 @@ THREE.Projector = function () { var face = faces[ f ]; material = isFaceMaterial === true - ? objectMaterials.materials[ 0 ] // objectMaterials.materials[ face.materialIndex ] + ? objectMaterials.materials[ face.materialIndex ] : object.material; if ( material === undefined ) continue; diff --git a/examples/webgl_materials.html b/examples/webgl_materials.html index 6d670f350d..dc9d9e19d5 100644 --- a/examples/webgl_materials.html +++ b/examples/webgl_materials.html @@ -91,6 +91,18 @@ var geometry_smooth = new THREE.SphereGeometry( 70, 32, 16 ); var geometry_flat = new THREE.SphereGeometry( 70, 32, 16 ); + var geometry_pieces = new THREE.SphereGeometry( 70, 32, 16 ); // Extra geometry to be broken down for MeshFaceMaterial + + for ( var i = 0, l = geometry_pieces.faces.length; i < l; i ++ ) { + + var face = geometry_pieces.faces[ i ]; + face.materialIndex = Math.floor( Math.random() * materials.length ); + + } + + geometry_pieces.materials = materials; + + materials.push( new THREE.MeshFaceMaterial( materials ) ); objects = []; @@ -100,7 +112,8 @@ material = materials[ i ]; - geometry = material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth; + geometry = material instanceof THREE.MeshFaceMaterial ? geometry_pieces : + ( material.shading == THREE.FlatShading ? geometry_flat : geometry_smooth ); sphere = new THREE.Mesh( geometry, material ); @@ -225,8 +238,8 @@ } - materials[ materials.length - 2 ].emissive.setHSL( 0.54, 1, 0.35 * ( 0.5 + 0.5 * Math.sin( 35 * timer ) ) ); - materials[ materials.length - 3 ].emissive.setHSL( 0.04, 1, 0.35 * ( 0.5 + 0.5 * Math.cos( 35 * timer ) ) ); + materials[ materials.length - 3 ].emissive.setHSL( 0.54, 1, 0.35 * ( 0.5 + 0.5 * Math.sin( 35 * timer ) ) ); + materials[ materials.length - 4 ].emissive.setHSL( 0.04, 1, 0.35 * ( 0.5 + 0.5 * Math.cos( 35 * timer ) ) ); particleLight.position.x = Math.sin( timer * 7 ) * 300; particleLight.position.y = Math.cos( timer * 5 ) * 400; diff --git a/src/core/Face3.js b/src/core/Face3.js index 5db0b1a28c..e97c01652b 100644 --- a/src/core/Face3.js +++ b/src/core/Face3.js @@ -3,7 +3,7 @@ * @author alteredq / http://alteredqualia.com/ */ -THREE.Face3 = function ( a, b, c, normal, color ) { +THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) { this.a = a; this.b = b; @@ -17,6 +17,8 @@ THREE.Face3 = function ( a, b, c, normal, color ) { this.vertexTangents = []; + this.materialIndex = materialIndex !== undefined ? materialIndex : 0; + }; THREE.Face3.prototype = { @@ -32,6 +34,8 @@ THREE.Face3.prototype = { this.normal.copy( source.normal ); this.color.copy( source.color ); + this.materialIndex = source.materialIndex; + for ( var i = 0, il = source.vertexNormals.length; i < il; i ++ ) { this.vertexNormals[ i ] = source.vertexNormals[ i ].clone(); diff --git a/src/core/Geometry.js b/src/core/Geometry.js index 92170ef540..2d7e7f181b 100644 --- a/src/core/Geometry.js +++ b/src/core/Geometry.js @@ -645,7 +645,7 @@ THREE.Geometry.prototype = { }, - merge: function ( geometry, matrix ) { + merge: function ( geometry, matrix, materialIndexOffset ) { if ( geometry instanceof THREE.Geometry === false ) { @@ -663,6 +663,8 @@ THREE.Geometry.prototype = { uvs1 = this.faceVertexUvs[ 0 ], uvs2 = geometry.faceVertexUvs[ 0 ]; + if ( materialIndexOffset === undefined ) materialIndexOffset = 0; + if ( matrix !== undefined ) { normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix ); @@ -723,6 +725,8 @@ THREE.Geometry.prototype = { } + faceCopy.materialIndex = face.materialIndex + materialIndexOffset; + faces1.push( faceCopy ); } diff --git a/src/extras/geometries/BoxGeometry.js b/src/extras/geometries/BoxGeometry.js index e9bb954e5b..47f6898056 100644 --- a/src/extras/geometries/BoxGeometry.js +++ b/src/extras/geometries/BoxGeometry.js @@ -28,14 +28,14 @@ THREE.BoxGeometry = function ( width, height, depth, widthSegments, heightSegmen var height_half = height / 2; var depth_half = depth / 2; - buildPlane( 'z', 'y', - 1, - 1, depth, height, width_half ); // px - buildPlane( 'z', 'y', 1, - 1, depth, height, - width_half ); // nx - buildPlane( 'x', 'z', 1, 1, width, depth, height_half ); // py - buildPlane( 'x', 'z', 1, - 1, width, depth, - height_half ); // ny - buildPlane( 'x', 'y', 1, - 1, width, height, depth_half ); // pz - buildPlane( 'x', 'y', - 1, - 1, width, height, - depth_half ); // nz + buildPlane( 'z', 'y', - 1, - 1, depth, height, width_half, 0 ); // px + buildPlane( 'z', 'y', 1, - 1, depth, height, - width_half, 1 ); // nx + buildPlane( 'x', 'z', 1, 1, width, depth, height_half, 2 ); // py + buildPlane( 'x', 'z', 1, - 1, width, depth, - height_half, 3 ); // ny + buildPlane( 'x', 'y', 1, - 1, width, height, depth_half, 4 ); // pz + buildPlane( 'x', 'y', - 1, - 1, width, height, - depth_half, 5 ); // nz - function buildPlane( u, v, udir, vdir, width, height, depth ) { + function buildPlane( u, v, udir, vdir, width, height, depth, materialIndex ) { var w, ix, iy, gridX = scope.widthSegments, @@ -100,6 +100,7 @@ THREE.BoxGeometry = function ( width, height, depth, widthSegments, heightSegmen var face = new THREE.Face3( a + offset, b + offset, d + offset ); face.normal.copy( normal ); face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() ); + face.materialIndex = materialIndex; scope.faces.push( face ); scope.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] ); @@ -107,6 +108,7 @@ THREE.BoxGeometry = function ( width, height, depth, widthSegments, heightSegmen face = new THREE.Face3( b + offset, c + offset, d + offset ); face.normal.copy( normal ); face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() ); + face.materialIndex = materialIndex; scope.faces.push( face ); scope.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] ); diff --git a/src/loaders/JSONLoader.js b/src/loaders/JSONLoader.js index 353abe74d4..f53edbcd65 100644 --- a/src/loaders/JSONLoader.js +++ b/src/loaders/JSONLoader.js @@ -91,7 +91,7 @@ THREE.JSONLoader.prototype = { offset, zLength, - colorIndex, normalIndex, uvIndex, + colorIndex, normalIndex, uvIndex, materialIndex, type, isQuad, @@ -178,7 +178,9 @@ THREE.JSONLoader.prototype = { if ( hasMaterial ) { - offset ++; + materialIndex = faces[ offset ++ ]; + faceA.materialIndex = materialIndex; + faceB.materialIndex = materialIndex; } @@ -285,7 +287,8 @@ THREE.JSONLoader.prototype = { if ( hasMaterial ) { - offset ++; + materialIndex = faces[ offset ++ ]; + face.materialIndex = materialIndex; } -- GitLab