Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Example/Cg/DualQuaternion.cg @ 12083

Last change on this file since 12083 was 12083, checked in by wiesep, 6 years ago

Reorganised shader programs

File size: 5.5 KB
Line 
1#include "DualQuaternion_Common.cg"
2
3//Dual quaternion skinning with per-vertex antipodality handling (this is the most robust, but not the most efficient way):
4void dualQuaternionHardwareSkinningTwoWeights_vp(       
5        float4 position : POSITION,
6        float3 normal   : NORMAL,
7        float2 uv       : TEXCOORD0,
8        float4 blendIdx : BLENDINDICES,
9        float4 blendWgt : BLENDWEIGHT,
10       
11        out float4 oPosition : POSITION,
12        out float2 oUv       : TEXCOORD0,
13        out float4 colour    : COLOR,
14        // Support up to 24 bones of float3x4
15        // vs_1_1 only supports 96 params so more than this is not feasible
16        uniform float2x4 worldDualQuaternion2x4Array[24],
17        uniform float4x4 viewProjectionMatrix,
18        uniform float4   lightPos[2],
19        uniform float4   lightDiffuseColour[2],
20        uniform float4   ambient,
21        uniform float4   diffuse)
22{               
23        float2x4 blendDQ = blendTwoWeightsAntipod(blendWgt, blendIdx, worldDualQuaternion2x4Array);
24
25        float len = length(blendDQ[0]);
26        blendDQ /= len;
27
28        float3 blendPosition = calculateBlendPosition(position.xyz, blendDQ);
29
30        //No need to normalize, the magnitude of the normal is preserved because only rotation is performed
31        float3 blendNormal = calculateBlendNormal(normal, blendDQ);
32       
33        oPosition = mul(viewProjectionMatrix, float4(blendPosition, 1.0));
34       
35        // Lighting - support point and directional
36        float3 lightDir0 = normalize(lightPos[0].xyz - (blendPosition.xyz * lightPos[0].w));
37        float3 lightDir1 = normalize(lightPos[1].xyz - (blendPosition.xyz * lightPos[1].w));
38
39        oUv = uv;
40
41        colour = diffuse * (ambient + (saturate(dot(lightDir0, blendNormal)) * lightDiffuseColour[0]) +
42                (saturate(dot(lightDir1, blendNormal)) * lightDiffuseColour[1]));               
43}
44
45
46//Shadow caster pass
47void dualQuaternionHardwareSkinningTwoWeightsCaster_vp(
48        float4 position : POSITION,
49        float4 blendIdx : BLENDINDICES,
50        float4 blendWgt : BLENDWEIGHT,
51       
52        out float4 oPosition : POSITION,
53        out float4 colour    : COLOR,
54        // Support up to 24 bones of float3x4
55        // vs_1_1 only supports 96 params so more than this is not feasible
56        uniform float2x4 worldDualQuaternion2x4Array[24],
57        uniform float4x4 viewProjectionMatrix,
58        uniform float4   ambient)
59{
60        float2x4 blendDQ = blendTwoWeightsAntipod(blendWgt, blendIdx, worldDualQuaternion2x4Array);
61
62        float len = length(blendDQ[0]);
63        blendDQ /= len;
64
65        float3 blendPosition = calculateBlendPosition(position.xyz, blendDQ);
66
67        // view / projection
68        oPosition = mul(viewProjectionMatrix, float4(blendPosition, 1.0));
69       
70        colour = ambient;
71}
72
73//Two-phase skinning: dual quaternion skinning with scale and shear transformations
74void dualQuaternionHardwareSkinningTwoWeightsTwoPhase_vp(
75        float4 position : POSITION,
76        float3 normal   : NORMAL,
77        float2 uv       : TEXCOORD0,
78        float4 blendIdx : BLENDINDICES,
79        float4 blendWgt : BLENDWEIGHT,
80       
81
82        out float4 oPosition : POSITION,
83        out float2 oUv       : TEXCOORD0,
84        out float4 colour    : COLOR,
85        // Support up to 24 bones of float3x4
86        // vs_1_1 only supports 96 params so more than this is not feasible
87        uniform float2x4 worldDualQuaternion2x4Array[24],
88        uniform float3x4 scaleM[24],
89        uniform float4x4 viewProjectionMatrix,
90        uniform float4   lightPos[2],
91        uniform float4   lightDiffuseColour[2],
92        uniform float4   ambient,
93        uniform float4   diffuse)
94{
95        //First phase - applies scaling and shearing:
96        float3x4 blendS = blendWgt.x*scaleM[blendIdx.x];
97        blendS += blendWgt.y*scaleM[blendIdx.y];       
98               
99        float3 pass1_position = mul(blendS, position);
100        float3x3 blendSrotAT = adjointTransposeMatrix(float3x3(blendS));
101        float3 pass1_normal = normalize(mul(blendSrotAT, normal.xyz));
102
103        //Second phase
104        float2x4 blendDQ = blendTwoWeightsAntipod(blendWgt, blendIdx, worldDualQuaternion2x4Array);
105       
106        float len = length(blendDQ[0]);
107        blendDQ /= len;
108
109        float3 blendPosition = calculateBlendPosition(pass1_position, blendDQ);
110
111        //No need to normalize, the magnitude of the normal is preserved because only rotation is performed
112        float3 blendNormal = calculateBlendNormal(pass1_normal, blendDQ);
113
114        oPosition = mul(viewProjectionMatrix, float4(blendPosition, 1.0));
115
116        // Lighting - support point and directional
117        float3 lightDir0 = normalize(lightPos[0].xyz - (blendPosition.xyz * lightPos[0].w));
118        float3 lightDir1 = normalize(lightPos[1].xyz - (blendPosition.xyz * lightPos[1].w));
119
120        oUv = uv;
121        colour = diffuse * (ambient + (saturate(dot(lightDir0, blendNormal)) * lightDiffuseColour[0]) +
122                (saturate(dot(lightDir1, blendNormal)) * lightDiffuseColour[1]));
123}
124
125//Two-phase skinning shadow caster pass
126void dualQuaternionHardwareSkinningTwoWeightsTwoPhaseCaster_vp(
127        float4 position : POSITION,
128        float4 blendIdx : BLENDINDICES,
129        float4 blendWgt : BLENDWEIGHT,
130       
131        out float4 oPosition : POSITION,
132        out float4 colour    : COLOR,
133        // Support up to 24 bones of float3x4
134        // vs_1_1 only supports 96 params so more than this is not feasible
135        uniform float2x4 worldDualQuaternion2x4Array[24],
136        uniform float3x4 scaleM[24],
137        uniform float4x4 viewProjectionMatrix,
138        uniform float4   ambient)
139{
140        //First phase - applies scaling and shearing:
141        float3x4 blendS = blendWgt.x*scaleM[blendIdx.x];
142        blendS += blendWgt.y*scaleM[blendIdx.y];       
143               
144        float3 pass1_position = mul(blendS, position);
145
146        //Second phase
147        float2x4 blendDQ = blendTwoWeightsAntipod(blendWgt, blendIdx, worldDualQuaternion2x4Array);
148       
149        float len = length(blendDQ[0]);
150        blendDQ /= len;
151
152        float3 blendPosition = calculateBlendPosition(pass1_position, blendDQ);
153
154        // view / projection
155        oPosition = mul(viewProjectionMatrix, float4(blendPosition, 1.0));
156       
157        colour = ambient;
158}
159
Note: See TracBrowser for help on using the repository browser.