Kaynağa Gözat

Some Examples + Buffers/Maths

Changed some examples to use SphereBufferGeometry
Added coord getters to Buffers
Changed Matrix apply to work with both Interleaved and Regular Buffers
Ben Adams 10 yıl önce
ebeveyn
işleme
1b6effb204

+ 1 - 1
examples/js/SkyShader.js

@@ -251,7 +251,7 @@ THREE.Sky = function () {
 		side: THREE.BackSide
 		side: THREE.BackSide
 	} );
 	} );
 
 
-	var skyGeo = new THREE.SphereGeometry( 450000, 32, 15 );
+	var skyGeo = new THREE.SphereBufferGeometry( 450000, 32, 15 );
 	var skyMesh = new THREE.Mesh( skyGeo, skyMat );
 	var skyMesh = new THREE.Mesh( skyGeo, skyMat );
 
 
 
 

+ 4 - 4
examples/webgl_camera.html

@@ -1,4 +1,4 @@
-<!DOCTYPE html>
+<!DOCTYPE html>
 <html lang="en">
 <html lang="en">
 	<head>
 	<head>
 		<title>three.js webgl - cameras</title>
 		<title>three.js webgl - cameras</title>
@@ -103,14 +103,14 @@
 				//
 				//
 
 
 				mesh = new THREE.Line(
 				mesh = new THREE.Line(
-					new THREE.WireframeGeometry( new THREE.SphereGeometry( 100, 16, 8 ) ),
+					new THREE.WireframeGeometry( new THREE.SphereBufferGeometry( 100, 16, 8 ) ),
 					new THREE.LineBasicMaterial( { color: 0xffffff } ),
 					new THREE.LineBasicMaterial( { color: 0xffffff } ),
 					THREE.LinePieces
 					THREE.LinePieces
 				);
 				);
 				scene.add( mesh );
 				scene.add( mesh );
 
 
 				var mesh2 = new THREE.Line(
 				var mesh2 = new THREE.Line(
-					new THREE.WireframeGeometry( new THREE.SphereGeometry( 50, 16, 8 ) ),
+					new THREE.WireframeGeometry( new THREE.SphereBufferGeometry( 50, 16, 8 ) ),
 					new THREE.LineBasicMaterial( { color: 0x00ff00 } ),
 					new THREE.LineBasicMaterial( { color: 0x00ff00 } ),
 					THREE.LinePieces
 					THREE.LinePieces
 				);
 				);
@@ -118,7 +118,7 @@
 				mesh.add( mesh2 );
 				mesh.add( mesh2 );
 
 
 				var mesh3 = new THREE.Line(
 				var mesh3 = new THREE.Line(
-					new THREE.WireframeGeometry( new THREE.SphereGeometry( 5, 16, 8 ) ),
+					new THREE.WireframeGeometry( new THREE.SphereBufferGeometry( 5, 16, 8 ) ),
 					new THREE.LineBasicMaterial( { color: 0x0000ff } ),
 					new THREE.LineBasicMaterial( { color: 0x0000ff } ),
 					THREE.LinePieces
 					THREE.LinePieces
 				);
 				);

+ 2 - 2
examples/webgl_shaders_sky.html

@@ -1,4 +1,4 @@
-<!DOCTYPE html>
+<!DOCTYPE html>
 <html lang="en">
 <html lang="en">
 	<head>
 	<head>
 		<title>three.js webgl - shaders - sky sun shader</title>
 		<title>three.js webgl - shaders - sky sun shader</title>
@@ -75,7 +75,7 @@
 
 
 
 
 				// Add Sun Helper
 				// Add Sun Helper
-				sunSphere = new THREE.Mesh( new THREE.SphereGeometry( 20000, 30, 30 ),
+				sunSphere = new THREE.Mesh( new THREE.SphereBufferGeometry( 20000, 30, 30 ),
 					new THREE.MeshBasicMaterial({color: 0xffffff, wireframe: false }));
 					new THREE.MeshBasicMaterial({color: 0xffffff, wireframe: false }));
 				sunSphere.position.y = -700000;
 				sunSphere.position.y = -700000;
 				sunSphere.visible = true;
 				sunSphere.visible = true;

+ 2 - 2
examples/webgl_test_memory.html

@@ -1,4 +1,4 @@
-<!DOCTYPE html>
+<!DOCTYPE html>
 <html lang="en">
 <html lang="en">
 	<head>
 	<head>
 		<title>three.js - webgl</title>
 		<title>three.js - webgl</title>
@@ -72,7 +72,7 @@
 
 
 			function render() {
 			function render() {
 
 
-				var geometry = new THREE.SphereGeometry( 50, Math.random() * 64, Math.random() * 32 );
+			    var geometry = new THREE.SphereBufferGeometry( 50, Math.random() * 64, Math.random() * 32 );
 
 
 				var texture = new THREE.Texture( createImage() );
 				var texture = new THREE.Texture( createImage() );
 				texture.needsUpdate = true;
 				texture.needsUpdate = true;

+ 2 - 2
examples/webgl_test_memory2.html

@@ -1,4 +1,4 @@
-<!DOCTYPE html>
+<!DOCTYPE html>
 <html lang="en">
 <html lang="en">
 	<head>
 	<head>
 		<title>three.js - webgl</title>
 		<title>three.js - webgl</title>
@@ -73,7 +73,7 @@
 
 
 				scene = new THREE.Scene();
 				scene = new THREE.Scene();
 
 
-				geometry = new THREE.SphereGeometry( 15, 64, 32 );
+				geometry = new THREE.SphereBufferGeometry( 15, 64, 32 );
 
 
 				for ( var i = 0; i < N; i ++ ) {
 				for ( var i = 0; i < N; i ++ ) {
 
 

+ 32 - 0
src/core/BufferAttribute.js

@@ -170,6 +170,38 @@ THREE.BufferAttribute.prototype = {
 
 
 	},
 	},
 
 
+	setW: function ( index, w ) {
+
+		this.array[ index * this.itemSize + 3 ] = w;
+
+		return this;
+
+	},
+
+	getX: function ( index ) {
+
+		return this.array[ index * this.itemSize ];
+
+	},
+
+	getY: function ( index ) {
+
+		return this.array[ index * this.itemSize + 1 ];
+
+	},
+
+	getZ: function ( index ) {
+
+		return this.array[ index * this.itemSize + 2 ];
+
+	},
+
+	getW: function ( index ) {
+
+		return this.array[ index * this.itemSize + 3 ];
+
+	},
+
 	setXY: function ( index, x, y ) {
 	setXY: function ( index, x, y ) {
 
 
 		index *= this.itemSize;
 		index *= this.itemSize;

+ 10 - 10
src/core/BufferGeometry.js

@@ -68,7 +68,7 @@ THREE.BufferGeometry.prototype = {
 
 
 		if ( position !== undefined ) {
 		if ( position !== undefined ) {
 
 
-			matrix.applyToVector3Array( position.array );
+			matrix.applyToBuffer( position );
 			position.needsUpdate = true;
 			position.needsUpdate = true;
 
 
 		}
 		}
@@ -79,7 +79,7 @@ THREE.BufferGeometry.prototype = {
 
 
 			var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
 			var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
 
 
-			normalMatrix.applyToVector3Array( normal.array );
+			normalMatrix.applyToBuffer( normal );
 			normal.needsUpdate = true;
 			normal.needsUpdate = true;
 
 
 		}
 		}
@@ -466,16 +466,16 @@ THREE.BufferGeometry.prototype = {
 
 
 			}
 			}
 
 
-			var positions = this.attributes.position.array;
+			var positions = this.attributes.position;
 
 
 			if ( positions ) {
 			if ( positions ) {
 
 
 				var bb = this.boundingBox;
 				var bb = this.boundingBox;
 				bb.makeEmpty();
 				bb.makeEmpty();
 
 
-				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
+				for ( var i = 0, il = positions.length / positions.itemSize; i < il; i ++ ) {
 
 
-					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
+					vector.set( positions.getX( i ), positions.getY( i ), positions.getZ( i ) );
 					bb.expandByPoint( vector );
 					bb.expandByPoint( vector );
 
 
 				}
 				}
@@ -512,7 +512,7 @@ THREE.BufferGeometry.prototype = {
 
 
 			}
 			}
 
 
-			var positions = this.attributes.position.array;
+			var positions = this.attributes.position;
 
 
 			if ( positions ) {
 			if ( positions ) {
 
 
@@ -520,9 +520,9 @@ THREE.BufferGeometry.prototype = {
 
 
 				var center = this.boundingSphere.center;
 				var center = this.boundingSphere.center;
 
 
-				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
+				for ( var i = 0, il = positions.length / positions.itemSize; i < il; i ++ ) {
 
 
-					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
+					vector.set( positions.getX( i ), positions.getY( i ), positions.getZ( i ) );
 					box.expandByPoint( vector );
 					box.expandByPoint( vector );
 
 
 				}
 				}
@@ -534,9 +534,9 @@ THREE.BufferGeometry.prototype = {
 
 
 				var maxRadiusSq = 0;
 				var maxRadiusSq = 0;
 
 
-				for ( var i = 0, il = positions.length; i < il; i += 3 ) {
+				for ( var i = 0, il = positions.length / positions.itemSize; i < il; i ++ ) {
 
 
-					vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
+					vector.set( positions.getX( i ), positions.getY( i ), positions.getZ( i ) );
 					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
 					maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
 
 
 				}
 				}

+ 32 - 0
src/core/InterleavedBufferAttribute.js

@@ -45,6 +45,38 @@ THREE.InterleavedBufferAttribute.prototype = {
 
 
 	},
 	},
 
 
+	setW: function ( index, w ) {
+
+		this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
+
+		return this;
+
+	},
+
+	getX: function ( index ) {
+
+		return this.data.array[ index * this.data.stride + this.offset ];
+
+	},
+
+	getY: function ( index ) {
+
+		return this.data.array[ index * this.data.stride + this.offset + 1 ];
+
+	},
+
+	getZ: function ( index ) {
+
+		return this.data.array[ index * this.data.stride + this.offset + 2 ];
+
+	},
+
+	getW: function ( index ) {
+
+		return this.data.array[ index * this.data.stride + this.offset + 3 ];
+
+	},
+
 	setXY: function ( index, x, y ) {
 	setXY: function ( index, x, y ) {
 
 
 		index = index * this.data.stride + this.offset;
 		index = index * this.data.stride + this.offset;

+ 6 - 5
src/extras/geometries/WireframeGeometry.js

@@ -68,7 +68,7 @@ THREE.WireframeGeometry = function ( geometry ) {
 
 
 		if ( geometry.attributes.index !== undefined ) { // Indexed BufferGeometry
 		if ( geometry.attributes.index !== undefined ) { // Indexed BufferGeometry
 
 
-			var vertices = geometry.attributes.position.array;
+			var vertices = geometry.attributes.position;
 			var indices = geometry.attributes.index.array;
 			var indices = geometry.attributes.index.array;
 			var drawcalls = geometry.drawcalls;
 			var drawcalls = geometry.drawcalls;
 			var numEdges = 0;
 			var numEdges = 0;
@@ -120,10 +120,11 @@ THREE.WireframeGeometry = function ( geometry ) {
 				for ( var j = 0; j < 2; j ++ ) {
 				for ( var j = 0; j < 2; j ++ ) {
 
 
 					var index = 6 * i + 3 * j;
 					var index = 6 * i + 3 * j;
-					var index2 = 3 * edges[ 2 * i + j];
-					coords[ index + 0 ] = vertices[ index2 ];
-					coords[ index + 1 ] = vertices[ index2 + 1 ];
-					coords[ index + 2 ] = vertices[ index2 + 2 ];
+					var index2 = edges[2 * i + j];
+
+					coords[ index + 0 ] = vertices.getX( index2 );
+					coords[ index + 1 ] = vertices.getY( index2 );
+					coords[ index + 2 ] = vertices.getZ( index2 );
 
 
 				}
 				}
 
 

+ 31 - 2
src/math/Matrix3.js

@@ -84,10 +84,11 @@ THREE.Matrix3.prototype = {
 
 
 	applyToVector3Array: function () {
 	applyToVector3Array: function () {
 
 
-		var v1 = new THREE.Vector3();
+		var v1;
 
 
-		return function ( array, offset, length ) {
+		return function applyToVector3Array( array, offset, length ) {
 
 
+			if ( v1 === undefined ) v1 = new THREE.Vector3();
 			if ( offset === undefined ) offset = 0;
 			if ( offset === undefined ) offset = 0;
 			if ( length === undefined ) length = array.length;
 			if ( length === undefined ) length = array.length;
 
 
@@ -111,6 +112,34 @@ THREE.Matrix3.prototype = {
 
 
 	}(),
 	}(),
 
 
+	applyToBuffer: function () {
+
+		var v1;
+
+		return function applyToBuffer( buffer, offset, length ) {
+
+			if ( v1 === undefined ) v1 = new THREE.Vector3();
+			if ( offset === undefined ) offset = 0;
+			if ( length === undefined ) length = buffer.length / buffer.itemSize;
+
+			for ( var i = 0, j = offset; i < length; i ++, j ++ ) {
+
+				v1.x = buffer.getX( j );
+				v1.y = buffer.getY( j );
+				v1.z = buffer.getZ( j );
+
+				v1.applyMatrix3( this );
+
+				buffer.setXYZ( v1.x, v1.y, v1.z );
+
+			}
+
+			return array;
+
+		};
+
+	}(),
+
 	multiplyScalar: function ( s ) {
 	multiplyScalar: function ( s ) {
 
 
 		var te = this.elements;
 		var te = this.elements;

+ 29 - 1
src/math/Matrix4.js

@@ -469,7 +469,7 @@ THREE.Matrix4.prototype = {
 
 
 		var v1;
 		var v1;
 
 
-		return function ( array, offset, length ) {
+		return function applyToVector3Array( array, offset, length ) {
 
 
 			if ( v1 === undefined ) v1 = new THREE.Vector3();
 			if ( v1 === undefined ) v1 = new THREE.Vector3();
 			if ( offset === undefined ) offset = 0;
 			if ( offset === undefined ) offset = 0;
@@ -495,6 +495,34 @@ THREE.Matrix4.prototype = {
 
 
 	}(),
 	}(),
 
 
+	applyToBuffer: function () {
+
+		var v1;
+
+		return function applyToBuffer( buffer, offset, length ) {
+
+			if ( v1 === undefined ) v1 = new THREE.Vector3();
+			if ( offset === undefined ) offset = 0;
+			if ( length === undefined ) length = buffer.length / buffer.itemSize;
+
+			for ( var i = 0, j = offset; i < length; i ++, j ++ ) {
+
+				v1.x = buffer.getX( j );
+				v1.y = buffer.getY( j );
+				v1.z = buffer.getZ( j );
+
+				v1.applyMatrix4( this );
+
+				buffer.setXYZ( v1.x, v1.y, v1.z );
+
+			}
+
+			return array;
+
+		};
+
+	}(),
+
 	rotateAxis: function ( v ) {
 	rotateAxis: function ( v ) {
 
 
 		THREE.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
 		THREE.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );