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