| 1 | //--------------------------------------------------------------------------- |
|---|
| 2 | //These materials/shaders are part of the NEW InstanceManager implementation |
|---|
| 3 | //Written by Matias N. Goldberg ("dark_sylinc") |
|---|
| 4 | //--------------------------------------------------------------------------- |
|---|
| 5 | #version 150 |
|---|
| 6 | |
|---|
| 7 | //Vertex input |
|---|
| 8 | in vec4 vertex; |
|---|
| 9 | in vec3 normal; |
|---|
| 10 | in vec3 tangent; |
|---|
| 11 | in vec4 uv0; |
|---|
| 12 | in vec4 blendIndices; |
|---|
| 13 | in vec4 blendWeights; |
|---|
| 14 | |
|---|
| 15 | //Parameters |
|---|
| 16 | uniform mat4 viewProjMatrix; |
|---|
| 17 | //uniform mat4x3 worldMatrix3x4Array[80]; |
|---|
| 18 | #ifdef ST_DUAL_QUATERNION |
|---|
| 19 | uniform vec4 worldDualQuaternion2x4Array[240]; |
|---|
| 20 | #else |
|---|
| 21 | uniform vec4 worldMatrix3x4Array[240]; //240 = 80*3 |
|---|
| 22 | #endif |
|---|
| 23 | |
|---|
| 24 | |
|---|
| 25 | #if (DEPTH_SHADOWCASTER || DEPTH_SHADOWRECEIVER) |
|---|
| 26 | uniform vec4 depthRange; |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | #if DEPTH_SHADOWRECEIVER |
|---|
| 30 | uniform mat4 texViewProjMatrix; |
|---|
| 31 | #endif |
|---|
| 32 | |
|---|
| 33 | //Output |
|---|
| 34 | #if DEPTH_SHADOWCASTER |
|---|
| 35 | out vec2 depth; |
|---|
| 36 | #else |
|---|
| 37 | out vec2 _uv0; |
|---|
| 38 | out vec3 oNormal; |
|---|
| 39 | out vec3 oVPos; |
|---|
| 40 | #if DEPTH_SHADOWRECEIVER |
|---|
| 41 | out vec4 oLightSpacePos; |
|---|
| 42 | #endif |
|---|
| 43 | #endif |
|---|
| 44 | |
|---|
| 45 | vec3 calculateBlendPosition(vec3 position, mat2x4 blendDQ) |
|---|
| 46 | { |
|---|
| 47 | vec3 blendPosition = position + 2.0*cross(blendDQ[0].yzw, cross(blendDQ[0].yzw, position) + blendDQ[0].x*position); |
|---|
| 48 | vec3 trans = 2.0*(blendDQ[0].x*blendDQ[1].yzw - blendDQ[1].x*blendDQ[0].yzw + cross(blendDQ[0].yzw, blendDQ[1].yzw)); |
|---|
| 49 | blendPosition += trans; |
|---|
| 50 | |
|---|
| 51 | return blendPosition; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | vec3 calculateBlendNormal(vec3 normal, mat2x4 blendDQ) |
|---|
| 55 | { |
|---|
| 56 | return normal + 2.0*cross(blendDQ[0].yzw, cross(blendDQ[0].yzw, normal) + blendDQ[0].x*normal); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | //--------------------------------------------- |
|---|
| 60 | //Main Vertex Shader |
|---|
| 61 | //--------------------------------------------- |
|---|
| 62 | void main(void) |
|---|
| 63 | { |
|---|
| 64 | vec4 worldPos; |
|---|
| 65 | vec3 worldNorm; |
|---|
| 66 | |
|---|
| 67 | #ifdef ST_DUAL_QUATERNION |
|---|
| 68 | int idx = int(blendIndices[0]) * 2; |
|---|
| 69 | mat2x4 blendDQ; |
|---|
| 70 | blendDQ[0] = worldDualQuaternion2x4Array[idx]; |
|---|
| 71 | blendDQ[1] = worldDualQuaternion2x4Array[idx + 1]; |
|---|
| 72 | #ifdef BONE_TWO_WEIGHTS |
|---|
| 73 | int idx2 = int(blendIndices[1]) * 2; |
|---|
| 74 | mat2x4 blendDQ2; |
|---|
| 75 | blendDQ2[0] = worldDualQuaternion2x4Array[idx2]; |
|---|
| 76 | blendDQ2[1] = worldDualQuaternion2x4Array[idx2 + 1]; |
|---|
| 77 | |
|---|
| 78 | //Accurate antipodality handling. For speed increase, remove the following line |
|---|
| 79 | if (dot(blendDQ[0], blendDQ2[0]) < 0.0) blendDQ2 *= -1.0; |
|---|
| 80 | |
|---|
| 81 | //Blend the dual quaternions based on the weights |
|---|
| 82 | blendDQ *= blendWeights.x; |
|---|
| 83 | blendDQ += blendWeights.y*blendDQ2; |
|---|
| 84 | //Normalize the resultant dual quaternion |
|---|
| 85 | blendDQ /= length(blendDQ[0]); |
|---|
| 86 | #endif |
|---|
| 87 | worldPos = vec4(calculateBlendPosition(vertex.xyz, blendDQ), 1.0); |
|---|
| 88 | worldNorm = calculateBlendNormal(normal, blendDQ); |
|---|
| 89 | #else |
|---|
| 90 | mat4 worldMatrix; |
|---|
| 91 | int idx = int(blendIndices[0]) * 3; |
|---|
| 92 | worldMatrix[0] = worldMatrix3x4Array[idx]; |
|---|
| 93 | worldMatrix[1] = worldMatrix3x4Array[idx + 1]; |
|---|
| 94 | worldMatrix[2] = worldMatrix3x4Array[idx + 2]; |
|---|
| 95 | worldMatrix[3] = vec4( 0, 0, 0, 1 ); |
|---|
| 96 | |
|---|
| 97 | worldPos = vertex * worldMatrix; |
|---|
| 98 | worldNorm = normal * mat3(worldMatrix); |
|---|
| 99 | #endif |
|---|
| 100 | |
|---|
| 101 | //Transform the position |
|---|
| 102 | gl_Position = viewProjMatrix * worldPos; |
|---|
| 103 | |
|---|
| 104 | #if DEPTH_SHADOWCASTER |
|---|
| 105 | depth.x = (gl_Position.z - depthRange.x) * depthRange.w; |
|---|
| 106 | depth.y = depthRange.w; |
|---|
| 107 | #else |
|---|
| 108 | _uv0 = uv0.xy; |
|---|
| 109 | oNormal = worldNorm; |
|---|
| 110 | oVPos = worldPos.xyz; |
|---|
| 111 | |
|---|
| 112 | #if DEPTH_SHADOWRECEIVER |
|---|
| 113 | oLightSpacePos = texViewProjMatrix * worldPos; |
|---|
| 114 | oLightSpacePos.z = (oLightSpacePos.z - depthRange.x) * depthRange.w; |
|---|
| 115 | #endif |
|---|
| 116 | #endif |
|---|
| 117 | } |
|---|