Browse Source

WebGLRenderer: Rescuing MorphTargets. Work in Progress...

Mr.doob 10 years ago
parent
commit
c7d0583df1

+ 1 - 0
src/core/BufferAttribute.js

@@ -7,6 +7,7 @@ THREE.BufferAttribute = function ( array, itemSize ) {
 	this.array = array;
 	this.itemSize = itemSize;
 
+	this.enabled = true;
 	this.needsUpdate = false;
 
 };

+ 36 - 0
src/core/BufferGeometry.js

@@ -159,6 +159,8 @@ THREE.BufferGeometry.prototype = {
 
 		} else if ( object instanceof THREE.Mesh ) {
 
+			// skinning
+
 			if ( object instanceof THREE.SkinnedMesh ) {
 
 				if ( geometry instanceof THREE.Geometry ) {
@@ -176,6 +178,40 @@ THREE.BufferGeometry.prototype = {
 
 			}
 
+			// morphs
+
+			if ( object.morphTargetInfluences !== undefined ) {
+
+				if ( geometry instanceof THREE.Geometry ) {
+
+					console.log( 'THREE.BufferGeometry.setFromObject(): Converted THREE.Geometry to THREE.DynamicGeometry as required for MorphTargets.', geometry );
+					geometry = new THREE.DynamicGeometry().fromGeometry( geometry );
+
+				}
+
+				this.morphsInfluences = new THREE.Float32Attribute( object.morphTargetInfluences, 1 ).copyArray( object.morphTargetInfluences );
+
+				// positions
+
+				var morphTargets = geometry.morphTargets;
+
+				if ( morphTargets.length > 0 ) {
+
+					for ( var i = 0, l = morphTargets.length; i < l; i ++ ) {
+
+						var morphTarget = morphTargets[ i ];
+
+						var attribute = new THREE.Float32Attribute( morphTarget.vertices.length * 3, 3 );
+						attribute.enabled = false;
+
+						this.addAttribute( 'position_' + i, attribute.copyVector3sArray( morphTarget.vertices ) );
+
+					}
+
+				}
+
+			}
+
 			if ( geometry instanceof THREE.DynamicGeometry ) {
 
 				this.fromDynamicGeometry( geometry );

+ 7 - 0
src/core/DynamicGeometry.js

@@ -82,6 +82,13 @@ THREE.DynamicGeometry.prototype = {
 
 			}
 
+			if ( vertexUvs === undefined ) {
+
+				console.warn( 'THREE.DynamicGeometry.fromGeometry(): Missing vertexUVs', i );
+				vertexUvs = [ new THREE.Vector2(), new THREE.Vector2(), new THREE.Vector2() ];
+
+			}
+
 			for ( var j = 0, jl = vertexUvs.length; j < jl; j ++ ) {
 
 				this.uvs[ indices[ j ] ] = vertexUvs[ j ];

+ 2 - 0
src/renderers/WebGLRenderer.js

@@ -2233,6 +2233,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 		}
 
+		/*
 		if ( material.morphTargets ) {
 
 			if ( ! object.__webglMorphTargetInfluences ) {
@@ -2242,6 +2243,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 			}
 
 		}
+		*/
 
 		var refreshProgram = false;
 		var refreshMaterial = false;

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

@@ -128,6 +128,9 @@ THREE.WebGLObjects = function ( gl, info ) {
 
 				var key = attributesKeys[ i ];
 				var attribute = attributes[ key ];
+
+				if ( attribute.enabled === false ) continue;
+
 				var bufferType = ( key === 'index' ) ? gl.ELEMENT_ARRAY_BUFFER : gl.ARRAY_BUFFER;
 
 				var data = ( attribute instanceof THREE.InterleavedBufferAttribute ) ? attribute.data : attribute;

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

@@ -75,6 +75,7 @@ THREE.WebGLProgram = ( function () {
 
 		var index0AttributeName = material.index0AttributeName;
 
+		/*
 		if ( index0AttributeName === undefined && parameters.morphTargets === true ) {
 
 			// programs with morphTargets displace position out of attribute 0
@@ -82,6 +83,7 @@ THREE.WebGLProgram = ( function () {
 			index0AttributeName = 'position';
 
 		}
+		*/
 
 		var shadowMapTypeDefine = 'SHADOWMAP_TYPE_BASIC';