Browse Source

First rough version of skinning via float vertex textures.

alteredq 13 years ago
parent
commit
e9dad06e4c
5 changed files with 248 additions and 166 deletions
  1. 104 103
      build/Three.js
  2. 59 58
      build/custom/ThreeWebGL.js
  3. 10 1
      src/objects/SkinnedMesh.js
  4. 23 1
      src/renderers/WebGLRenderer.js
  5. 52 3
      src/renderers/WebGLShaders.js

File diff suppressed because it is too large
+ 104 - 103
build/Three.js


File diff suppressed because it is too large
+ 59 - 58
build/custom/ThreeWebGL.js


+ 10 - 1
src/objects/SkinnedMesh.js

@@ -62,7 +62,14 @@ THREE.SkinnedMesh = function ( geometry, material ) {
 
 
 		}
 		}
 
 
-		this.boneMatrices = new Float32Array( 16 * this.bones.length );
+		//this.boneMatrices = new Float32Array( 16 * this.bones.length );
+
+		this.boneMatrices = new Float32Array( 64 * 64 * 4 ); // max 1024 bones
+		this.boneTexture = new THREE.DataTexture( this.boneMatrices, 64, 64, THREE.RGBAFormat, THREE.FloatType );
+		this.boneTexture.minFilter = THREE.NearestFilter;
+		this.boneTexture.magFilter = THREE.NearestFilter;
+		this.boneTexture.generateMipmaps = false;
+		this.boneTexture.flipY = false;
 
 
 		this.pose();
 		this.pose();
 
 
@@ -140,6 +147,8 @@ THREE.SkinnedMesh.prototype.updateMatrixWorld = function ( force ) {
 
 
 	}
 	}
 
 
+	this.boneTexture.needsUpdate = true;
+
 };
 };
 
 
 /*
 /*

+ 23 - 1
src/renderers/WebGLRenderer.js

@@ -4794,11 +4794,21 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 		if ( material.skinning ) {
 		if ( material.skinning ) {
 
 
+			/*
 			if ( p_uniforms.boneGlobalMatrices !== null ) {
 			if ( p_uniforms.boneGlobalMatrices !== null ) {
 
 
 				_gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices );
 				_gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices );
 
 
 			}
 			}
+			*/
+
+			if ( p_uniforms.boneTexture !== null ) {
+
+				var textureUnit = 12; // shadowMap texture array starts from 6, 12 should leave space for 6 shadowmaps
+				_gl.uniform1i( p_uniforms.boneTexture, textureUnit );
+				_this.setTexture( object.boneTexture, textureUnit );
+
+			}
 
 
 		}
 		}
 
 
@@ -5815,7 +5825,9 @@ THREE.WebGLRenderer = function ( parameters ) {
 		identifiers = [
 		identifiers = [
 
 
 			'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition',
 			'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition',
-			'boneGlobalMatrices', 'morphTargetInfluences'
+			//'boneGlobalMatrices',
+			'boneTexture',
+			'morphTargetInfluences'
 
 
 		];
 		];
 
 
@@ -6360,6 +6372,7 @@ THREE.WebGLRenderer = function ( parameters ) {
 		//  - limit here is ANGLE's 254 max uniform vectors
 		//  - limit here is ANGLE's 254 max uniform vectors
 		//    (up to 54 should be safe)
 		//    (up to 54 should be safe)
 
 
+		/*
 		var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
 		var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
 		var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
 		var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
 
 
@@ -6375,6 +6388,15 @@ THREE.WebGLRenderer = function ( parameters ) {
 
 
 			}
 			}
 
 
+		}
+		*/
+
+		var maxBones = 0;
+
+		if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
+
+			maxBones = object.bones.length;
+
 		}
 		}
 
 
 		return maxBones;
 		return maxBones;

+ 52 - 3
src/renderers/WebGLShaders.js

@@ -957,7 +957,44 @@ THREE.ShaderChunk = {
 
 
 		"#ifdef USE_SKINNING",
 		"#ifdef USE_SKINNING",
 
 
-			"uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
+			//"uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
+			"uniform sampler2D boneTexture;",
+
+			"#define N_SKIN_PIXEL_X 64.0",
+			"#define N_SKIN_PIXEL_Y 64.0",
+
+			"mat4 getBoneMatrix( const in float i ) {",
+
+				"float j = i * 4.0;",
+				"float x = mod( j, N_SKIN_PIXEL_X );",
+				"float y = floor( j / N_SKIN_PIXEL_X );",
+
+				"const float dx = 1.0 / N_SKIN_PIXEL_X;",
+				"const float dy = 1.0 / N_SKIN_PIXEL_Y;",
+
+				"y = dy * ( y + 0.5 );",
+
+				"vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );",
+				"vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );",
+				"vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );",
+				"vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );",
+
+				"mat4 bone = mat4( v1, v2, v3, v4 );",
+
+				"return bone;",
+
+			"}",
+
+		"#endif"
+
+	].join("\n"),
+
+	skinbase_vertex: [
+
+		"#ifdef USE_SKINNING",
+
+			"mat4 boneMatX = getBoneMatrix( skinIndex.x );",
+			"mat4 boneMatY = getBoneMatrix( skinIndex.y );",
 
 
 		"#endif"
 		"#endif"
 
 
@@ -967,8 +1004,11 @@ THREE.ShaderChunk = {
 
 
 		"#ifdef USE_SKINNING",
 		"#ifdef USE_SKINNING",
 
 
-			"vec4 skinned  = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
-			"skinned 	  += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
+			"vec4 skinned  = boneMatX * skinVertexA * skinWeight.x;",
+			"skinned 	  += boneMatY * skinVertexB * skinWeight.y;",
+
+			//"vec4 skinned  = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
+			//"skinned 	  += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
 
 
 			"gl_Position  = projectionMatrix * modelViewMatrix * skinned;",
 			"gl_Position  = projectionMatrix * modelViewMatrix * skinned;",
 
 
@@ -1062,8 +1102,13 @@ THREE.ShaderChunk = {
 
 
 		"#ifdef USE_SKINNING",
 		"#ifdef USE_SKINNING",
 
 
+			/*
 			"mat4 skinMatrix = skinWeight.x * boneGlobalMatrices[ int( skinIndex.x ) ];",
 			"mat4 skinMatrix = skinWeight.x * boneGlobalMatrices[ int( skinIndex.x ) ];",
 			"skinMatrix 	+= skinWeight.y * boneGlobalMatrices[ int( skinIndex.y ) ];",
 			"skinMatrix 	+= skinWeight.y * boneGlobalMatrices[ int( skinIndex.y ) ];",
+			*/
+
+			"mat4 skinMatrix = skinWeight.x * boneMatX;",
+			"skinMatrix 	+= skinWeight.y * boneMatY;",
 
 
 			"vec4 skinnedNormal = skinMatrix * vec4( transformedNormal, 0.0 );",
 			"vec4 skinnedNormal = skinMatrix * vec4( transformedNormal, 0.0 );",
 			"transformedNormal = skinnedNormal.xyz;",
 			"transformedNormal = skinnedNormal.xyz;",
@@ -1591,6 +1636,7 @@ THREE.ShaderLib = {
 				THREE.ShaderChunk[ "lightmap_vertex" ],
 				THREE.ShaderChunk[ "lightmap_vertex" ],
 				THREE.ShaderChunk[ "envmap_vertex" ],
 				THREE.ShaderChunk[ "envmap_vertex" ],
 				THREE.ShaderChunk[ "color_vertex" ],
 				THREE.ShaderChunk[ "color_vertex" ],
+				THREE.ShaderChunk[ "skinbase_vertex" ],
 				THREE.ShaderChunk[ "skinning_vertex" ],
 				THREE.ShaderChunk[ "skinning_vertex" ],
 				THREE.ShaderChunk[ "morphtarget_vertex" ],
 				THREE.ShaderChunk[ "morphtarget_vertex" ],
 				THREE.ShaderChunk[ "default_vertex" ],
 				THREE.ShaderChunk[ "default_vertex" ],
@@ -1679,6 +1725,7 @@ THREE.ShaderLib = {
 				THREE.ShaderChunk[ "color_vertex" ],
 				THREE.ShaderChunk[ "color_vertex" ],
 
 
 				THREE.ShaderChunk[ "morphnormal_vertex" ],
 				THREE.ShaderChunk[ "morphnormal_vertex" ],
+				THREE.ShaderChunk[ "skinbase_vertex" ],
 				THREE.ShaderChunk[ "skinnormal_vertex" ],
 				THREE.ShaderChunk[ "skinnormal_vertex" ],
 
 
 				"#ifndef USE_ENVMAP",
 				"#ifndef USE_ENVMAP",
@@ -1805,6 +1852,7 @@ THREE.ShaderLib = {
 				"vViewPosition = -mvPosition.xyz;",
 				"vViewPosition = -mvPosition.xyz;",
 
 
 				THREE.ShaderChunk[ "morphnormal_vertex" ],
 				THREE.ShaderChunk[ "morphnormal_vertex" ],
+				THREE.ShaderChunk[ "skinbase_vertex" ],
 				THREE.ShaderChunk[ "skinnormal_vertex" ],
 				THREE.ShaderChunk[ "skinnormal_vertex" ],
 
 
 				"vNormal = transformedNormal;",
 				"vNormal = transformedNormal;",
@@ -1945,6 +1993,7 @@ THREE.ShaderLib = {
 
 
 				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
 				"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
 
 
+				THREE.ShaderChunk[ "skinbase_vertex" ],
 				THREE.ShaderChunk[ "skinning_vertex" ],
 				THREE.ShaderChunk[ "skinning_vertex" ],
 				THREE.ShaderChunk[ "morphtarget_vertex" ],
 				THREE.ShaderChunk[ "morphtarget_vertex" ],
 				THREE.ShaderChunk[ "default_vertex" ],
 				THREE.ShaderChunk[ "default_vertex" ],

Some files were not shown because too many files changed in this diff