Jelajahi Sumber

Updating minecraft example to the new geometries.

Mr.doob 11 tahun lalu
induk
melakukan
bd1e4dbb7e
3 mengubah file dengan 85 tambahan dan 53 penghapusan
  1. 30 30
      examples/webgl_geometry_minecraft.html
  2. 28 22
      src/core/BufferGeometry.js
  3. 27 1
      src/core/Geometry2.js

+ 30 - 30
examples/webgl_geometry_minecraft.html

@@ -93,43 +93,46 @@
 				var matrix = new THREE.Matrix4();
 
 				var pxGeometry = new THREE.PlaneGeometry( 100, 100 );
-				pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
-				pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
-				pxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
+				// pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
+				// pxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
+				// pxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
 				pxGeometry.applyMatrix( matrix.makeRotationY( Math.PI / 2 ) );
 				pxGeometry.applyMatrix( matrix.makeTranslation( 50, 0, 0 ) );
 
 				var nxGeometry = new THREE.PlaneGeometry( 100, 100 );
-				nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
-				nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
-				nxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
+				// nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
+				// nxGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
+				// nxGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
 				nxGeometry.applyMatrix( matrix.makeRotationY( - Math.PI / 2 ) );
 				nxGeometry.applyMatrix( matrix.makeTranslation( - 50, 0, 0 ) );
 
 				var pyGeometry = new THREE.PlaneGeometry( 100, 100 );
-				pyGeometry.faceVertexUvs[ 0 ][ 0 ][ 1 ].y = 0.5;
-				pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 0 ].y = 0.5;
-				pyGeometry.faceVertexUvs[ 0 ][ 1 ][ 1 ].y = 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;
 				pyGeometry.applyMatrix( matrix.makeRotationX( - Math.PI / 2 ) );
 				pyGeometry.applyMatrix( matrix.makeTranslation( 0, 50, 0 ) );
 
 				var pzGeometry = new THREE.PlaneGeometry( 100, 100 );
-				pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
-				pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
-				pzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
+				// pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
+				// pzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
+				// pzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
 				pzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, 50 ) );
 
 				var nzGeometry = new THREE.PlaneGeometry( 100, 100 );
-				nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
-				nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
-				nzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
+				// nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].y = 0.5;
+				// nzGeometry.faceVertexUvs[ 0 ][ 0 ][ 2 ].y = 0.5;
+				// nzGeometry.faceVertexUvs[ 0 ][ 1 ][ 2 ].y = 0.5;
 				nzGeometry.applyMatrix( matrix.makeRotationY( Math.PI ) );
 				nzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, -50 ) );
 
 				//
 
-				var geometry = new THREE.Geometry();
-				var dummy = new THREE.Mesh();
+				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 );
 
 				for ( var z = 0; z < worldDepth; z ++ ) {
 
@@ -137,43 +140,40 @@
 
 						var h = getY( x, z );
 
-						dummy.position.x = x * 100 - worldHalfWidth * 100;
-						dummy.position.y = h * 100;
-						dummy.position.z = z * 100 - worldHalfDepth * 100;
+						matrix.makeTranslation(
+							x * 100 - worldHalfWidth * 100,
+							h * 100,
+							z * 100 - worldHalfDepth * 100
+						);
 
 						var px = getY( x + 1, z );
 						var nx = getY( x - 1, z );
 						var pz = getY( x, z + 1 );
 						var nz = getY( x, z - 1 );
 
-						dummy.geometry = pyGeometry;
-						THREE.GeometryUtils.merge( geometry, dummy );
+						geometry.merge( pyGeometry, matrix );
 
 						if ( ( px != h && px != h + 1 ) || x == 0 ) {
 
-							dummy.geometry = pxGeometry;
-							THREE.GeometryUtils.merge( geometry, dummy );
+							geometry.merge( pxGeometry, matrix );
 
 						}
 
 						if ( ( nx != h && nx != h + 1 ) || x == worldWidth - 1 ) {
 
-							dummy.geometry = nxGeometry;
-							THREE.GeometryUtils.merge( geometry, dummy );
+							geometry.merge( nxGeometry, matrix );
 
 						}
 
 						if ( ( pz != h && pz != h + 1 ) || z == worldDepth - 1 ) {
 
-							dummy.geometry = pzGeometry;
-							THREE.GeometryUtils.merge( geometry, dummy );
+							geometry.merge( pzGeometry, matrix );
 
 						}
 
 						if ( ( nz != h && nz != h + 1 ) || z == 0 ) {
 
-							dummy.geometry = nzGeometry;
-							THREE.GeometryUtils.merge( geometry, dummy );
+							geometry.merge( nzGeometry, matrix );
 
 						}
 

+ 28 - 22
src/core/BufferGeometry.js

@@ -353,28 +353,6 @@ THREE.BufferGeometry.prototype = {
 
 	},
 
-	normalizeNormals: function () {
-
-		var normals = this.attributes[ "normal" ].array;
-
-		var x, y, z, n;
-
-		for ( var i = 0, il = normals.length; i < il; i += 3 ) {
-
-			x = normals[ i ];
-			y = normals[ i + 1 ];
-			z = normals[ i + 2 ];
-
-			n = 1.0 / Math.sqrt( x * x + y * y + z * z );
-
-			normals[ i     ] *= n;
-			normals[ i + 1 ] *= n;
-			normals[ i + 2 ] *= n;
-
-		}
-
-	},
-
 	computeTangents: function () {
 
 		// based on http://www.terathon.com/code/tangent.html
@@ -685,6 +663,34 @@ THREE.BufferGeometry.prototype = {
 		return offsets;
 	},
 
+	merge: function () {
+
+		console.log( 'BufferGeometry.merge(): TODO' );
+
+	},
+
+	normalizeNormals: function () {
+
+		var normals = this.attributes[ "normal" ].array;
+
+		var x, y, z, n;
+
+		for ( var i = 0, il = normals.length; i < il; i += 3 ) {
+
+			x = normals[ i ];
+			y = normals[ i + 1 ];
+			z = normals[ i + 2 ];
+
+			n = 1.0 / Math.sqrt( x * x + y * y + z * z );
+
+			normals[ i     ] *= n;
+			normals[ i + 1 ] *= n;
+			normals[ i + 2 ] *= n;
+
+		}
+
+	},
+
 	/*
 		reoderBuffers:
 		Reorder attributes based on a new indexBuffer and indexMap.

+ 27 - 1
src/core/Geometry2.js

@@ -12,4 +12,30 @@ THREE.Geometry2 = function ( vertices, normals, uvs ) {
 
 };
 
-THREE.Geometry2.prototype = Object.create( THREE.BufferGeometry.prototype );
+THREE.Geometry2.prototype = Object.create( THREE.BufferGeometry.prototype );
+
+THREE.Geometry2.prototype.merge = function ( geometry, matrix, offset ) {
+
+	if ( offset === undefined ) offset = 0;
+
+	var vertices = this.attributes[ 'position' ].array;
+	var normals = this.attributes[ 'normal' ].array;
+	var uvs = this.attributes[ 'uv' ].array;
+
+	if ( geometry instanceof THREE.Geometry2 ) {
+
+		var vertices2 = geometry.attributes[ 'position' ].array;
+		var normals2 = geometry.attributes[ 'normal' ].array;
+		var uvs2 = geometry.attributes[ 'uv' ].array;
+
+		for ( var i = 0, l = vertices2.length; i < l; i ++ ) {
+
+			vertices[ i + offset ] = vertices2[ i ];
+			normals[ i + offset ] = normals2[ i ];
+			uvs[ i + offset ] = uvs2[ i ];
+
+		}
+
+	}
+
+};