Ver Fonte

Skin.hx now have utils shader to make possible to extend skinning to other components.

borisrp há 7 meses atrás
pai
commit
72d3f58a52
1 ficheiros alterados com 42 adições e 15 exclusões
  1. 42 15
      h3d/shader/Skin.hx

+ 42 - 15
h3d/shader/Skin.hx

@@ -1,9 +1,41 @@
 package h3d.shader;
 package h3d.shader;
 
 
+class Utils  extends hxsl.Shader {
+	static var SRC = {
+		var boneMatrixX : Mat3x4;
+		var boneMatrixY : Mat3x4;
+		var boneMatrixZ : Mat3x4;
+		var boneMatrixW : Mat3x4;
+
+		var skinWeights : Vec4;
+
+		function applySkinPoint(p : Vec3) : Vec3 {
+			var transformed = (p * boneMatrixX) * skinWeights.x +
+					          (p * boneMatrixY) * skinWeights.y +
+				              (p * boneMatrixZ) * skinWeights.z;
+			if(skinWeights.w > 0.0) {
+				transformed += (p * boneMatrixW) * skinWeights.w;
+			}
+			return transformed;
+		}
+
+		function applySkinVec(v : Vec3) : Vec3 {
+			var transformed = (v * mat3(boneMatrixX)) * skinWeights.x +
+			                  (v * mat3(boneMatrixY)) * skinWeights.y +
+				   			  (v * mat3(boneMatrixZ)) * skinWeights.z;
+			if(skinWeights.w > 0.0) {
+				transformed += (v * mat3(boneMatrixW)) * skinWeights.w;
+			}
+			return transformed;
+		}
+	}
+}
 class Skin extends SkinBase {
 class Skin extends SkinBase {
 
 
 	static var SRC = {
 	static var SRC = {
 
 
+		@:import h3d.shader.Skin.Utils;
+
 		@input var input : {
 		@input var input : {
 			var position : Vec3;
 			var position : Vec3;
 			var normal : Vec3;
 			var normal : Vec3;
@@ -15,25 +47,20 @@ class Skin extends SkinBase {
 		var previousTransformedPosition : Vec3;
 		var previousTransformedPosition : Vec3;
 
 
 		function vertex() {
 		function vertex() {
-			transformedPosition =
-				(relativePosition * bonesMatrixes[int(input.indexes.x)]) * input.weights.x +
-				(relativePosition * bonesMatrixes[int(input.indexes.y)]) * input.weights.y +
-				(relativePosition * bonesMatrixes[int(input.indexes.z)]) * input.weights.z;
-			transformedNormal = 
-				(input.normal * mat3(bonesMatrixes[int(input.indexes.x)])) * input.weights.x +
-				(input.normal * mat3(bonesMatrixes[int(input.indexes.y)])) * input.weights.y +
-				(input.normal * mat3(bonesMatrixes[int(input.indexes.z)])) * input.weights.z;
-
+			boneMatrixX = bonesMatrixes[int(input.indexes.x)];
+			boneMatrixY = bonesMatrixes[int(input.indexes.y)];
+			boneMatrixZ = bonesMatrixes[int(input.indexes.z)];
+			boneMatrixW = bonesMatrixes[int(input.indexes.w)];
+			skinWeights = vec4(input.weights, 0.0);
 			if(fourBonesByVertex) {
 			if(fourBonesByVertex) {
-				var w4 = 1 - (input.weights.x + input.weights.y + input.weights.z);
-				transformedPosition += (relativePosition * bonesMatrixes[int(input.indexes.w)]) * w4;
-				transformedNormal += (input.normal * mat3(bonesMatrixes[int(input.indexes.z)])) * w4;
+				skinWeights.z = 1 - (input.weights.x + input.weights.y + input.weights.z);
 			}
 			}
-			
+
+			transformedPosition = applySkinPoint(relativePosition);
+			transformedNormal = applySkinVec(input.normal);
+
 			previousTransformedPosition = transformedPosition;
 			previousTransformedPosition = transformedPosition;
 			transformedNormal = normalize(transformedNormal);
 			transformedNormal = normalize(transformedNormal);
 		}
 		}
-
 	};
 	};
-
 }
 }