[12115] | 1 | //--------------------------------------------------------------------------- |
---|
| 2 | //These materials/shaders are part of the NEW InstanceManager implementation |
---|
| 3 | //Written by Matias N. Goldberg ("dark_sylinc") |
---|
| 4 | //--------------------------------------------------------------------------- |
---|
| 5 | |
---|
| 6 | //--------------------------------------------- |
---|
| 7 | //Vertex Shader Input |
---|
| 8 | //--------------------------------------------- |
---|
| 9 | struct VS_INPUT |
---|
| 10 | { |
---|
| 11 | float4 Position : POSITION; |
---|
| 12 | float3 Normal : NORMAL; |
---|
| 13 | float3 Tangent : TANGENT; |
---|
| 14 | float2 uv0 : TEXCOORD0; |
---|
| 15 | |
---|
| 16 | float4 BlendIdx : BLENDINDICES; |
---|
| 17 | float4 BlendWgt : BLENDWEIGHT; |
---|
| 18 | }; |
---|
| 19 | |
---|
| 20 | #include "InstancingVertexInterpolators.cg" |
---|
| 21 | #ifdef ST_DUAL_QUATERNION |
---|
| 22 | #include "DualQuaternionSkinning_Shadow.cg" |
---|
| 23 | #endif |
---|
| 24 | |
---|
| 25 | //--------------------------------------------- |
---|
| 26 | //Main Vertex Shader |
---|
| 27 | //--------------------------------------------- |
---|
| 28 | VS_OUTPUT main_vs( in VS_INPUT input, |
---|
| 29 | uniform float4x4 viewProjMatrix, |
---|
| 30 | #ifdef ST_DUAL_QUATERNION |
---|
| 31 | uniform float2x4 worldDualQuaternion2x4Array[120] |
---|
| 32 | #else |
---|
| 33 | uniform float3x4 worldMatrix3x4Array[80] |
---|
| 34 | #endif |
---|
| 35 | #if defined( DEPTH_SHADOWCASTER ) || defined( DEPTH_SHADOWRECEIVER ) |
---|
| 36 | , uniform float4 depthRange |
---|
| 37 | #endif |
---|
| 38 | #ifdef DEPTH_SHADOWRECEIVER |
---|
| 39 | , uniform float4x4 texViewProjMatrix |
---|
| 40 | #endif |
---|
| 41 | ) |
---|
| 42 | { |
---|
| 43 | VS_OUTPUT output; |
---|
| 44 | |
---|
| 45 | float4 worldPos = 0; |
---|
| 46 | float3 worldNorm = 0; |
---|
| 47 | |
---|
| 48 | int idx = int(input.BlendIdx[0]); |
---|
| 49 | #ifdef ST_DUAL_QUATERNION |
---|
| 50 | //Only dealing with one weight so normalization of the dual quaternion and weighting are unnecessary |
---|
| 51 | float2x4 blendDQ = worldDualQuaternion2x4Array[idx]; |
---|
| 52 | #ifdef BONE_TWO_WEIGHTS |
---|
| 53 | float2x4 blendDQ2 = worldDualQuaternion2x4Array[int(input.BlendIdx[1])]; |
---|
| 54 | |
---|
| 55 | //Accurate antipodality handling. For speed increase, remove the following line |
---|
| 56 | if (dot(blendDQ[0], blendDQ2[0]) < 0.0) blendDQ2 *= -1.0; |
---|
| 57 | |
---|
| 58 | //Blend the dual quaternions based on the weights |
---|
| 59 | blendDQ *= input.BlendWgt.x; |
---|
| 60 | blendDQ += input.BlendWgt.y*blendDQ2; |
---|
| 61 | //Normalize the resultant dual quaternion |
---|
| 62 | blendDQ /= length(blendDQ[0]); |
---|
| 63 | #endif |
---|
| 64 | worldPos = float4(calculateBlendPosition(input.Position, blendDQ), 1.0); |
---|
| 65 | worldNorm = calculateBlendNormal(input.Normal, blendDQ); |
---|
| 66 | #else |
---|
| 67 | worldPos = float4( mul( worldMatrix3x4Array[idx], input.Position ).xyz, 1.0f ); |
---|
| 68 | worldNorm = mul( (float3x3)(worldMatrix3x4Array[idx]), input.Normal ); |
---|
| 69 | #endif |
---|
| 70 | |
---|
| 71 | /*int i; |
---|
| 72 | for( i=0; i<4; i++ ) |
---|
| 73 | { |
---|
| 74 | int idx = int(input.BlendIdx[0]); |
---|
| 75 | worldPos += float4( mul( worldMatrix3x4Array[idx], input.Position ).xyz, 1.0f ) * input.BlendWgt[i]; |
---|
| 76 | worldNorm += mul( (float3x3)(worldMatrix3x4Array[idx]), input.Normal ) * input.BlendWgt[i]; |
---|
| 77 | }*/ |
---|
| 78 | |
---|
| 79 | //Transform the position |
---|
| 80 | output.Position = mul( viewProjMatrix, worldPos ); |
---|
| 81 | |
---|
| 82 | #ifdef DEPTH_SHADOWCASTER |
---|
| 83 | output.ps.unused = float3( 0 ); |
---|
| 84 | output.ps.depth = (output.Position.z - depthRange.x + SHADOW_BIAS) * depthRange.w; |
---|
| 85 | #else |
---|
| 86 | output.ps.uv0 = input.uv0; |
---|
| 87 | |
---|
| 88 | //Pass Normal and position for Blinn Phong lighting |
---|
| 89 | output.ps.Normal = normalize(worldNorm); |
---|
| 90 | output.ps.vPos = worldPos.xyz; |
---|
| 91 | |
---|
| 92 | #ifdef DEPTH_SHADOWRECEIVER |
---|
| 93 | // Calculate the position of vertex in light space to do shadows |
---|
| 94 | output.ps.lightSpacePos = mul( texViewProjMatrix, worldPos ); |
---|
| 95 | // make linear |
---|
| 96 | output.ps.lightSpacePos.z = (output.ps.lightSpacePos.z - depthRange.x) * depthRange.w; |
---|
| 97 | #endif |
---|
| 98 | #endif |
---|
| 99 | |
---|
| 100 | return output; |
---|
| 101 | } |
---|