Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: data/branches/Shader_HS18/programs/Example/GLSLES/ShaderInstancing.vert @ 12091

Last change on this file since 12091 was 12091, checked in by wiesep, 5 years ago

Updated programs and adjusted Material to work with GLSL>150

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