Browse Source

WebGLRenderer: Working custom attributes.

Mr.doob 10 years ago
parent
commit
479432b9f3
3 changed files with 81 additions and 1 deletions
  1. 8 0
      src/core/BufferAttribute.js
  2. 71 1
      src/core/BufferGeometry.js
  3. 2 0
      src/renderers/webgl/WebGLObjects.js

+ 8 - 0
src/core/BufferAttribute.js

@@ -36,6 +36,14 @@ THREE.BufferAttribute.prototype = {
 
 	},
 
+	copyArray: function ( array ) {
+
+		this.array.set( array );
+
+		return this;
+
+	},
+
 	copyColorsArray: function ( colors ) {
 
 		var array = this.array, offset = 0;

+ 71 - 1
src/core/BufferGeometry.js

@@ -142,7 +142,39 @@ THREE.BufferGeometry.prototype = {
 
 		if ( material.attributes !== undefined ) {
 
-			console.warn( 'THREE.BufferGeometry.setFromObject(). TODO: material.attributes', material );
+			var attributes = material.attributes;
+
+			for ( var name in attributes ) {
+
+				var attribute = attributes[ name ];
+
+				var type = attribute.type;
+				var array = attribute.value;
+
+				switch ( type ) {
+
+					case "f":
+						var floats = new Float32Array( array.length );
+						this.addAttribute( name, new THREE.BufferAttribute( floats, 1 ).copyArray( array ) );
+						break;
+
+					case "c":
+						var colors = new Float32Array( array.length * 3 );
+						this.addAttribute( name, new THREE.BufferAttribute( colors, 3 ).copyColorsArray( array ) );
+						break;
+
+					case "v3":
+						var colors = new Float32Array( array.length * 3 );
+						this.addAttribute( name, new THREE.BufferAttribute( colors, 3 ).copyVector3sArray( array ) );
+						break;
+
+					default:
+						console.warn( 'THREE.BufferGeometry.setFromObject(). TODO: attribute unsupported', type );
+						break;
+
+				}
+
+			}
 
 		}
 
@@ -186,6 +218,44 @@ THREE.BufferGeometry.prototype = {
 
 	},
 
+	updateFromMaterial: function ( material ) {
+
+		if ( material.attributes !== undefined ) {
+
+			var attributes = material.attributes;
+
+			for ( var name in attributes ) {
+
+				var attribute = attributes[ name ];
+
+				var type = attribute.type;
+				var array = attribute.value;
+
+				switch ( type ) {
+
+					case "f":
+						this.attributes[ name ].copyArray( array );
+						this.attributes[ name ].needsUpdate = true;
+						break;
+
+					case "c":
+						this.attributes[ name ].copyColorsArray( array );
+						this.attributes[ name ].needsUpdate = true;
+						break;
+
+					case "v3":
+						this.attributes[ name ].copyVector3sArray( array );
+						this.attributes[ name ].needsUpdate = true;
+						break;
+
+				}
+
+			}
+
+		}
+
+	},
+
 	fromGeometry: function ( geometry, material ) {
 
 		material = material || { 'vertexColors': THREE.NoColors };

+ 2 - 0
src/renderers/webgl/WebGLObjects.js

@@ -121,6 +121,8 @@ THREE.WebGLObjects = function ( gl, info ) {
 
 		}
 
+		geometry.updateFromMaterial( object.material );
+
 		//
 
 		if ( geometry instanceof THREE.BufferGeometry ) {