Debian Codegraph @

code/renderergl2/glsl/generic_vp.glsl

raw
1 attribute vec3 attr_Position;
2 attribute vec3 attr_Normal;
3
4 #if defined(USE_VERTEX_ANIMATION)
5 attribute vec3 attr_Position2;
6 attribute vec3 attr_Normal2;
7 #elif defined(USE_BONE_ANIMATION)
8 attribute vec4 attr_BoneIndexes;
9 attribute vec4 attr_BoneWeights;
10 #endif
11
12 attribute vec4 attr_Color;
13 attribute vec4 attr_TexCoord0;
14
15 #if defined(USE_TCGEN)
16 attribute vec4 attr_TexCoord1;
17 #endif
18
19 #if defined(USE_TCMOD)
20 uniform vec4 u_DiffuseTexMatrix0;
21 uniform vec4 u_DiffuseTexMatrix1;
22 uniform vec4 u_DiffuseTexMatrix2;
23 uniform vec4 u_DiffuseTexMatrix3;
24 uniform vec4 u_DiffuseTexMatrix4;
25 uniform vec4 u_DiffuseTexMatrix5;
26 uniform vec4 u_DiffuseTexMatrix6;
27 uniform vec4 u_DiffuseTexMatrix7;
28 #endif
29
30 #if defined(USE_TCGEN) || defined(USE_RGBAGEN)
31 uniform vec3 u_LocalViewOrigin;
32 #endif
33
34 #if defined(USE_TCGEN)
35 uniform int u_TCGen0;
36 uniform vec3 u_TCGen0Vector0;
37 uniform vec3 u_TCGen0Vector1;
38 #endif
39
40 #if defined(USE_FOG)
41 uniform vec4 u_FogDistance;
42 uniform vec4 u_FogDepth;
43 uniform float u_FogEyeT;
44 uniform vec4 u_FogColorMask;
45 #endif
46
47 #if defined(USE_DEFORM_VERTEXES)
48 uniform int u_DeformGen;
49 uniform float u_DeformParams[5];
50 uniform float u_Time;
51 #endif
52
53 uniform mat4 u_ModelViewProjectionMatrix;
54 uniform vec4 u_BaseColor;
55 uniform vec4 u_VertColor;
56
57 #if defined(USE_RGBAGEN)
58 uniform int u_ColorGen;
59 uniform int u_AlphaGen;
60 uniform vec3 u_AmbientLight;
61 uniform vec3 u_DirectedLight;
62 uniform vec3 u_ModelLightDir;
63 uniform float u_PortalRange;
64 #endif
65
66 #if defined(USE_VERTEX_ANIMATION)
67 uniform float u_VertexLerp;
68 #elif defined(USE_BONE_ANIMATION)
69 uniform mat4 u_BoneMatrix[MAX_GLSL_BONES];
70 #endif
71
72 varying vec2 var_DiffuseTex;
73 varying vec4 var_Color;
74
75 #if defined(USE_DEFORM_VERTEXES)
76 vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
77 {
78 float base = u_DeformParams[0];
79 float amplitude = u_DeformParams[1];
80 float phase = u_DeformParams[2];
81 float frequency = u_DeformParams[3];
82 float spread = u_DeformParams[4];
83
84 if (u_DeformGen == DGEN_BULGE)
85 {
86 phase *= st.x;
87 }
88 else // if (u_DeformGen <= DGEN_WAVE_INVERSE_SAWTOOTH)
89 {
90 phase += dot(pos.xyz, vec3(spread));
91 }
92
93 float value = phase + (u_Time * frequency);
94 float func;
95
96 if (u_DeformGen == DGEN_WAVE_SIN)
97 {
98 func = sin(value * 2.0 * M_PI);
99 }
100 else if (u_DeformGen == DGEN_WAVE_SQUARE)
101 {
102 func = sign(fract(0.5 - value));
103 }
104 else if (u_DeformGen == DGEN_WAVE_TRIANGLE)
105 {
106 func = abs(fract(value + 0.75) - 0.5) * 4.0 - 1.0;
107 }
108 else if (u_DeformGen == DGEN_WAVE_SAWTOOTH)
109 {
110 func = fract(value);
111 }
112 else if (u_DeformGen == DGEN_WAVE_INVERSE_SAWTOOTH)
113 {
114 func = (1.0 - fract(value));
115 }
116 else // if (u_DeformGen == DGEN_BULGE)
117 {
118 func = sin(value);
119 }
120
121 return pos + normal * (base + func * amplitude);
122 }
123 #endif
124
125 #if defined(USE_TCGEN)
126 vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 TCGenVector1)
127 {
128 vec2 tex = attr_TexCoord0.st;
129
130 if (TCGen == TCGEN_LIGHTMAP)
131 {
132 tex = attr_TexCoord1.st;
133 }
134 else if (TCGen == TCGEN_ENVIRONMENT_MAPPED)
135 {
136 vec3 viewer = normalize(u_LocalViewOrigin - position);
137 vec2 ref = reflect(viewer, normal).yz;
138 tex.s = ref.x * -0.5 + 0.5;
139 tex.t = ref.y * 0.5 + 0.5;
140 }
141 else if (TCGen == TCGEN_VECTOR)
142 {
143 tex = vec2(dot(position, TCGenVector0), dot(position, TCGenVector1));
144 }
145
146 return tex;
147 }
148 #endif
149
150 #if defined(USE_TCMOD)
151 vec2 ModTexCoords(vec2 st, vec3 position, vec4 texMatrix[8])
152 {
153 vec2 st2 = st;
154 vec2 offsetPos = vec2(position.x + position.z, position.y);
155
156 st2 = vec2(st2.x * texMatrix[0].x + st2.y * texMatrix[0].y + texMatrix[0].z,
157 st2.x * texMatrix[1].x + st2.y * texMatrix[1].y + texMatrix[1].z);
158 st2 += texMatrix[0].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[1].w * 2.0 * M_PI));
159
160 st2 = vec2(st2.x * texMatrix[2].x + st2.y * texMatrix[2].y + texMatrix[2].z,
161 st2.x * texMatrix[3].x + st2.y * texMatrix[3].y + texMatrix[3].z);
162 st2 += texMatrix[2].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[3].w * 2.0 * M_PI));
163
164 st2 = vec2(st2.x * texMatrix[4].x + st2.y * texMatrix[4].y + texMatrix[4].z,
165 st2.x * texMatrix[5].x + st2.y * texMatrix[5].y + texMatrix[5].z);
166 st2 += texMatrix[4].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[5].w * 2.0 * M_PI));
167
168 st2 = vec2(st2.x * texMatrix[6].x + st2.y * texMatrix[6].y + texMatrix[6].z,
169 st2.x * texMatrix[7].x + st2.y * texMatrix[7].y + texMatrix[7].z);
170 st2 += texMatrix[6].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[7].w * 2.0 * M_PI));
171
172 return st2;
173 }
174 #endif
175
176 #if defined(USE_RGBAGEN)
177 vec4 CalcColor(vec3 position, vec3 normal)
178 {
179 vec4 color = u_VertColor * attr_Color + u_BaseColor;
180
181 if (u_ColorGen == CGEN_LIGHTING_DIFFUSE)
182 {
183 float incoming = clamp(dot(normal, u_ModelLightDir), 0.0, 1.0);
184
185 color.rgb = clamp(u_DirectedLight * incoming + u_AmbientLight, 0.0, 1.0);
186 }
187
188 vec3 viewer = u_LocalViewOrigin - position;
189
190 if (u_AlphaGen == AGEN_LIGHTING_SPECULAR)
191 {
192 vec3 lightDir = normalize(vec3(-960.0, 1980.0, 96.0) - position);
193 vec3 reflected = -reflect(lightDir, normal);
194
195 color.a = clamp(dot(reflected, normalize(viewer)), 0.0, 1.0);
196 color.a *= color.a;
197 color.a *= color.a;
198 }
199 else if (u_AlphaGen == AGEN_PORTAL)
200 {
201 color.a = clamp(length(viewer) / u_PortalRange, 0.0, 1.0);
202 }
203
204 return color;
205 }
206 #endif
207
208 #if defined(USE_FOG)
209 float CalcFog(vec3 position)
210 {
211 float s = dot(vec4(position, 1.0), u_FogDistance) * 8.0;
212 float t = dot(vec4(position, 1.0), u_FogDepth);
213
214 float eyeOutside = float(u_FogEyeT < 0.0);
215 float fogged = float(t >= eyeOutside);
216
217 t += 1e-6;
218 t *= fogged / (t - u_FogEyeT * eyeOutside);
219
220 return s * t;
221 }
222 #endif
223
224 void main()
225 {
226 #if defined(USE_VERTEX_ANIMATION)
227 vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
228 vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp);
229 #elif defined(USE_BONE_ANIMATION)
230 mat4 vtxMat = u_BoneMatrix[int(attr_BoneIndexes.x)] * attr_BoneWeights.x;
231 vtxMat += u_BoneMatrix[int(attr_BoneIndexes.y)] * attr_BoneWeights.y;
232 vtxMat += u_BoneMatrix[int(attr_BoneIndexes.z)] * attr_BoneWeights.z;
233 vtxMat += u_BoneMatrix[int(attr_BoneIndexes.w)] * attr_BoneWeights.w;
234 mat3 nrmMat = mat3(cross(vtxMat[1].xyz, vtxMat[2].xyz), cross(vtxMat[2].xyz, vtxMat[0].xyz), cross(vtxMat[0].xyz, vtxMat[1].xyz));
235
236 vec3 position = vec3(vtxMat * vec4(attr_Position, 1.0));
237 vec3 normal = normalize(nrmMat * attr_Normal);
238 #else
239 vec3 position = attr_Position;
240 vec3 normal = attr_Normal;
241 #endif
242
243 #if defined(USE_DEFORM_VERTEXES)
244 position = DeformPosition(position, normal, attr_TexCoord0.st);
245 #endif
246
247 gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0);
248
249 #if defined(USE_TCGEN)
250 vec2 tex = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1);
251 #else
252 vec2 tex = attr_TexCoord0.st;
253 #endif
254
255 #if defined(USE_TCMOD)
256 vec4 diffuseTexMatrix[8];
257 diffuseTexMatrix[0] = u_DiffuseTexMatrix0;
258 diffuseTexMatrix[1] = u_DiffuseTexMatrix1;
259 diffuseTexMatrix[2] = u_DiffuseTexMatrix2;
260 diffuseTexMatrix[3] = u_DiffuseTexMatrix3;
261 diffuseTexMatrix[4] = u_DiffuseTexMatrix4;
262 diffuseTexMatrix[5] = u_DiffuseTexMatrix5;
263 diffuseTexMatrix[6] = u_DiffuseTexMatrix6;
264 diffuseTexMatrix[7] = u_DiffuseTexMatrix7;
265 var_DiffuseTex = ModTexCoords(tex, position, diffuseTexMatrix);
266 #else
267 var_DiffuseTex = tex;
268 #endif
269
270 #if defined(USE_RGBAGEN)
271 var_Color = CalcColor(position, normal);
272 #else
273 var_Color = u_VertColor * attr_Color + u_BaseColor;
274 #endif
275
276 #if defined(USE_FOG)
277 var_Color *= vec4(1.0) - u_FogColorMask * sqrt(clamp(CalcFog(position), 0.0, 1.0));
278 #endif
279 }