Bladeren bron

Trying to get the minecraft example to work with the new geometry classes.
Implemented Geometry2.merge().
Added offset and length parameters to Matrix3/Matrix4.applyToVector3Array().

Mr.doob 11 jaren geleden
bovenliggende
commit
6243b69ac5

+ 5 - 5
examples/webgl_geometry_minecraft.html

@@ -107,6 +107,8 @@
 				nxGeometry.applyMatrix( matrix.makeTranslation( - 50, 0, 0 ) );
 
 				var pyGeometry = new THREE.PlaneGeometry( 100, 100 );
+				pyGeometry.attributes.uv.array[ 5 ] = 0.5;
+				pyGeometry.attributes.uv.array[ 7 ] = 0.5;
 				// pyGeometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5;
 				// pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 0 ].y = 0.5;
 				// pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 1 ].y = 0.5;
@@ -128,11 +130,7 @@
 
 				//
 
-				var vertices = new Float32Array( worldWidth * worldDepth * 3 * 3 * 2 );
-				var normals = new Float32Array( worldWidth * worldDepth * 3 * 3 * 2 );
-				var uvs = new Float32Array( worldWidth * worldDepth * 3 * 2 * 2 );
-
-				var geometry = new THREE.Geometry2( vertices, normals, uvs );
+				var geometry = new THREE.Geometry2( worldWidth * worldDepth * 2 ); // 2 triangles
 
 				for ( var z = 0; z < worldDepth; z ++ ) {
 
@@ -181,6 +179,8 @@
 
 				}
 
+				geometry.computeBoundingSphere();
+
 				var texture = THREE.ImageUtils.loadTexture( 'textures/minecraft/atlas.png' );
 				texture.magFilter = THREE.NearestFilter;
 				texture.minFilter = THREE.LinearMipMapLinearFilter;

+ 94 - 18
src/core/Geometry2.js

@@ -2,40 +2,116 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.Geometry2 = function ( vertices, normals, uvs ) {
+THREE.Geometry2 = function ( size ) {
 
 	THREE.BufferGeometry.call( this );
 
+	if ( size !== undefined ) {
+
+		var vertices = new Float32Array( size * 3 * 3 );
+		var normals = new Float32Array( size * 3 * 3 );
+		var uvs = new Float32Array( size * 3 * 2 );
+
+		this.attributes[ 'position' ] = { array: vertices, itemSize: 3 };
+		this.attributes[ 'normal' ] = { array: normals, itemSize: 3 };
+		this.attributes[ 'uv' ] = { array: uvs, itemSize: 2 };
+
+	}
+
+};
+
+THREE.Geometry2.prototype = Object.create( THREE.BufferGeometry.prototype );
+
+THREE.Geometry2.prototype.setArrays = function ( vertices, normals, uvs ) {
+
 	this.attributes[ 'position' ] = { array: vertices, itemSize: 3 };
 	this.attributes[ 'normal' ] = { array: normals, itemSize: 3 };
-	this.attributes[ 'uv' ] = { array: uvs, itemSize: 2 };	
+	this.attributes[ 'uv' ] = { array: uvs, itemSize: 2 };
+
+	return this;
 
 };
 
-THREE.Geometry2.prototype = Object.create( THREE.BufferGeometry.prototype );
+THREE.Geometry2.prototype.merge = ( function () {
+
+	var offset = 0;
+	var normalMatrix = new THREE.Matrix3();
+
+	return function ( geometry, matrix, startOffset ) {
+
+		if ( startOffset !== undefined ) offset = startOffset;
+
+		var offset2 = offset * 2;
+		var offset3 = offset * 3;
+
+		var vertices = this.attributes[ 'position' ].array;
+		var normals = this.attributes[ 'normal' ].array;
+		var uvs = this.attributes[ 'uv' ].array;
 
-THREE.Geometry2.prototype.merge = function ( geometry, matrix, offset ) {
+		if ( geometry instanceof THREE.Geometry2 ) {
 
-	if ( offset === undefined ) offset = 0;
+			var vertices2 = geometry.attributes[ 'position' ].array;
+			var normals2 = geometry.attributes[ 'normal' ].array;
+			var uvs2 = geometry.attributes[ 'uv' ].array;
 
-	var vertices = this.attributes[ 'position' ].array;
-	var normals = this.attributes[ 'normal' ].array;
-	var uvs = this.attributes[ 'uv' ].array;
+			for ( var i = 0, l = vertices2.length; i < l; i += 3 ) {
 
-	if ( geometry instanceof THREE.Geometry2 ) {
+				vertices[ i + offset3     ] = vertices2[ i     ];
+				vertices[ i + offset3 + 1 ] = vertices2[ i + 1 ];
+				vertices[ i + offset3 + 2 ] = vertices2[ i + 2 ];
 
-		var vertices2 = geometry.attributes[ 'position' ].array;
-		var normals2 = geometry.attributes[ 'normal' ].array;
-		var uvs2 = geometry.attributes[ 'uv' ].array;
+				normals[ i + offset3     ] = normals2[ i     ];
+				normals[ i + offset3 + 1 ] = normals2[ i + 1 ];
+				normals[ i + offset3 + 2 ] = normals2[ i + 2 ];
 
-		for ( var i = 0, l = vertices2.length; i < l; i ++ ) {
+				uvs[ i + offset2     ] = uvs2[ i     ];
+				uvs[ i + offset2 + 1 ] = uvs2[ i + 1 ];
 
-			vertices[ i + offset ] = vertices2[ i ];
-			normals[ i + offset ] = normals2[ i ];
-			uvs[ i + offset ] = uvs2[ i ];
+			}
+
+		} else if ( geometry instanceof THREE.IndexedGeometry2 ) {
+
+			var indices2 = geometry.attributes[ 'index' ].array;
+			var vertices2 = geometry.attributes[ 'position' ].array;
+			var normals2 = geometry.attributes[ 'normal' ].array;
+			var uvs2 = geometry.attributes[ 'uv' ].array;
+
+			for ( var i = 0, l = indices2.length; i < l; i ++ ) {
+
+				var index = indices2[ i ];
+
+				var index3 = index * 3;
+				var i3 = i * 3;
+
+				vertices[ i3 + offset3 ] = vertices2[ index3 ];
+				vertices[ i3 + offset3 + 1 ] = vertices2[ index3 + 1 ];
+				vertices[ i3 + offset3 + 2 ] = vertices2[ index3 + 2 ];
+
+				normals[ i3 + offset3 ] = normals2[ index3 ];
+				normals[ i3 + offset3 + 1 ] = normals2[ index3 + 1 ];
+				normals[ i3 + offset3 + 2 ] = normals2[ index3 + 2 ];
+
+				var index2 = index * 2;
+				var i2 = i * 2;
+
+				uvs[ i2 + offset2 ] = uvs2[ index2 ];
+				uvs[ i2 + offset2 + 1 ] = uvs2[ index2 + 1 ];
+
+			}
+
+			if ( matrix !== undefined ) {
+
+				matrix.applyToVector3Array( vertices, offset3, indices2.length * 3 );
+
+				normalMatrix.getNormalMatrix( matrix );
+				normalMatrix.applyToVector3Array( normals, offset3, indices2.length * 3 );
+
+			}
+
+			offset += indices2.length;
 
 		}
 
-	}
+	};
 
-};
+} )();

+ 9 - 3
src/core/IndexedGeometry2.js

@@ -2,15 +2,21 @@
  * @author mrdoob / http://mrdoob.com/
  */
 
-THREE.IndexedGeometry2 = function ( indices, vertices, normals, uvs ) {
+THREE.IndexedGeometry2 = function () {
 
 	THREE.BufferGeometry.call( this );
 
+};
+
+THREE.IndexedGeometry2.prototype = Object.create( THREE.BufferGeometry.prototype );
+
+THREE.IndexedGeometry2.prototype.setArrays = function ( indices, vertices, normals, uvs ) {
+
 	this.attributes[ 'index' ] = { array: indices, itemSize: 1 };
 	this.attributes[ 'position' ] = { array: vertices, itemSize: 3 };
 	this.attributes[ 'normal' ] = { array: normals, itemSize: 3 };
 	this.attributes[ 'uv' ] = { array: uvs, itemSize: 2 };	
 
-};
+	return this;
 
-THREE.IndexedGeometry2.prototype = Object.create( THREE.BufferGeometry.prototype );
+};

+ 4 - 1
src/extras/geometries/PlaneGeometry.js

@@ -81,7 +81,10 @@ THREE.PlaneGeometry = function ( width, height, widthSegments, heightSegments )
 
 	}
 
-	THREE.IndexedGeometry2.call( this, indices, vertices, normals, uvs );
+	THREE.IndexedGeometry2.call( this );
+
+	this.setArrays( indices, vertices, normals, uvs );
+	this.computeBoundingSphere();
 
 };
 

+ 12 - 9
src/math/Matrix3.js

@@ -80,23 +80,26 @@ THREE.Matrix3.prototype = {
 
 		var v1 = new THREE.Vector3();
 
-		return function ( a ) {
+		return function ( array, offset, length ) {
 
-			for ( var i = 0, il = a.length; i < il; i += 3 ) {
+			if ( offset === undefined ) offset = 0;
+			if ( length === undefined ) length = array.length;
 
-				v1.x = a[ i ];
-				v1.y = a[ i + 1 ];
-				v1.z = a[ i + 2 ];
+			for ( var i = 0, j = offset, il; i < length; i += 3, j += 3 ) {
+
+				v1.x = array[ j ];
+				v1.y = array[ j + 1 ];
+				v1.z = array[ j + 2 ];
 
 				v1.applyMatrix3( this );
 
-				a[ i ]     = v1.x;
-				a[ i + 1 ] = v1.y;
-				a[ i + 2 ] = v1.z;
+				array[ j ]     = v1.x;
+				array[ j + 1 ] = v1.y;
+				array[ j + 2 ] = v1.z;
 
 			}
 
-			return a;
+			return array;
 
 		};
 

+ 12 - 9
src/math/Matrix4.js

@@ -438,23 +438,26 @@ THREE.Matrix4.prototype = {
 
 		var v1 = new THREE.Vector3();
 
-		return function ( a ) {
+		return function ( array, offset, length ) {
 
-			for ( var i = 0, il = a.length; i < il; i += 3 ) {
+			if ( offset === undefined ) offset = 0;
+			if ( length === undefined ) length = array.length;
 
-				v1.x = a[ i ];
-				v1.y = a[ i + 1 ];
-				v1.z = a[ i + 2 ];
+			for ( var i = 0, j = offset, il; i < length; i += 3, j += 3 ) {
+
+				v1.x = array[ j ];
+				v1.y = array[ j + 1 ];
+				v1.z = array[ j + 2 ];
 
 				v1.applyMatrix4( this );
 
-				a[ i ]     = v1.x;
-				a[ i + 1 ] = v1.y;
-				a[ i + 2 ] = v1.z;
+				array[ j ]     = v1.x;
+				array[ j + 1 ] = v1.y;
+				array[ j + 2 ] = v1.z;
 
 			}
 
-			return a;
+			return array;
 
 		};