| 1 |
attribute vec4 attr_TexCoord0; |
| 2 |
#if defined(USE_LIGHTMAP) || defined(USE_TCGEN) |
| 3 |
attribute vec4 attr_TexCoord1; |
| 4 |
#endif |
| 5 |
attribute vec4 attr_Color; |
| 6 |
|
| 7 |
attribute vec3 attr_Position; |
| 8 |
attribute vec3 attr_Normal; |
| 9 |
attribute vec4 attr_Tangent; |
| 10 |
|
| 11 |
#if defined(USE_VERTEX_ANIMATION) |
| 12 |
attribute vec3 attr_Position2; |
| 13 |
attribute vec3 attr_Normal2; |
| 14 |
attribute vec4 attr_Tangent2; |
| 15 |
#elif defined(USE_BONE_ANIMATION) |
| 16 |
attribute vec4 attr_BoneIndexes; |
| 17 |
attribute vec4 attr_BoneWeights; |
| 18 |
#endif |
| 19 |
|
| 20 |
#if defined(USE_LIGHT) && !defined(USE_LIGHT_VECTOR) |
| 21 |
attribute vec3 attr_LightDirection; |
| 22 |
#endif |
| 23 |
|
| 24 |
#if defined(USE_DELUXEMAP) |
| 25 |
uniform vec4 u_EnableTextures; // x = normal, y = deluxe, z = specular, w = cube |
| 26 |
#endif |
| 27 |
|
| 28 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 29 |
uniform vec3 u_ViewOrigin; |
| 30 |
#endif |
| 31 |
|
| 32 |
#if defined(USE_TCGEN) |
| 33 |
uniform int u_TCGen0; |
| 34 |
uniform vec3 u_TCGen0Vector0; |
| 35 |
uniform vec3 u_TCGen0Vector1; |
| 36 |
uniform vec3 u_LocalViewOrigin; |
| 37 |
#endif |
| 38 |
|
| 39 |
#if defined(USE_TCMOD) |
| 40 |
uniform vec4 u_DiffuseTexMatrix0; |
| 41 |
uniform vec4 u_DiffuseTexMatrix1; |
| 42 |
uniform vec4 u_DiffuseTexMatrix2; |
| 43 |
uniform vec4 u_DiffuseTexMatrix3; |
| 44 |
uniform vec4 u_DiffuseTexMatrix4; |
| 45 |
uniform vec4 u_DiffuseTexMatrix5; |
| 46 |
uniform vec4 u_DiffuseTexMatrix6; |
| 47 |
uniform vec4 u_DiffuseTexMatrix7; |
| 48 |
#endif |
| 49 |
|
| 50 |
uniform mat4 u_ModelViewProjectionMatrix; |
| 51 |
uniform vec4 u_BaseColor; |
| 52 |
uniform vec4 u_VertColor; |
| 53 |
|
| 54 |
#if defined(USE_MODELMATRIX) |
| 55 |
uniform mat4 u_ModelMatrix; |
| 56 |
#endif |
| 57 |
|
| 58 |
#if defined(USE_VERTEX_ANIMATION) |
| 59 |
uniform float u_VertexLerp; |
| 60 |
#elif defined(USE_BONE_ANIMATION) |
| 61 |
uniform mat4 u_BoneMatrix[MAX_GLSL_BONES]; |
| 62 |
#endif |
| 63 |
|
| 64 |
#if defined(USE_LIGHT_VECTOR) |
| 65 |
uniform vec4 u_LightOrigin; |
| 66 |
uniform float u_LightRadius; |
| 67 |
uniform vec3 u_DirectedLight; |
| 68 |
uniform vec3 u_AmbientLight; |
| 69 |
#endif |
| 70 |
|
| 71 |
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) |
| 72 |
uniform vec4 u_PrimaryLightOrigin; |
| 73 |
uniform float u_PrimaryLightRadius; |
| 74 |
#endif |
| 75 |
|
| 76 |
varying vec4 var_TexCoords; |
| 77 |
|
| 78 |
varying vec4 var_Color; |
| 79 |
#if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT) |
| 80 |
varying vec4 var_ColorAmbient; |
| 81 |
#endif |
| 82 |
|
| 83 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 84 |
varying vec4 var_Normal; |
| 85 |
varying vec4 var_Tangent; |
| 86 |
varying vec4 var_Bitangent; |
| 87 |
#endif |
| 88 |
|
| 89 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 90 |
varying vec4 var_LightDir; |
| 91 |
#endif |
| 92 |
|
| 93 |
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) |
| 94 |
varying vec4 var_PrimaryLightDir; |
| 95 |
#endif |
| 96 |
|
| 97 |
#if defined(USE_TCGEN) |
| 98 |
vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 TCGenVector1) |
| 99 |
{ |
| 100 |
vec2 tex = attr_TexCoord0.st; |
| 101 |
|
| 102 |
if (TCGen == TCGEN_LIGHTMAP) |
| 103 |
{ |
| 104 |
tex = attr_TexCoord1.st; |
| 105 |
} |
| 106 |
else if (TCGen == TCGEN_ENVIRONMENT_MAPPED) |
| 107 |
{ |
| 108 |
vec3 viewer = normalize(u_LocalViewOrigin - position); |
| 109 |
vec2 ref = reflect(viewer, normal).yz; |
| 110 |
tex.s = ref.x * -0.5 + 0.5; |
| 111 |
tex.t = ref.y * 0.5 + 0.5; |
| 112 |
} |
| 113 |
else if (TCGen == TCGEN_VECTOR) |
| 114 |
{ |
| 115 |
tex = vec2(dot(position, TCGenVector0), dot(position, TCGenVector1)); |
| 116 |
} |
| 117 |
|
| 118 |
return tex; |
| 119 |
} |
| 120 |
#endif |
| 121 |
|
| 122 |
#if defined(USE_TCMOD) |
| 123 |
vec2 ModTexCoords(vec2 st, vec3 position, vec4 texMatrix[8]) |
| 124 |
{ |
| 125 |
vec2 st2 = st; |
| 126 |
vec2 offsetPos = vec2(position.x + position.z, position.y); |
| 127 |
|
| 128 |
st2 = vec2(st2.x * texMatrix[0].x + st2.y * texMatrix[0].y + texMatrix[0].z, |
| 129 |
st2.x * texMatrix[1].x + st2.y * texMatrix[1].y + texMatrix[1].z); |
| 130 |
st2 += texMatrix[0].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[1].w * 2.0 * M_PI)); |
| 131 |
|
| 132 |
st2 = vec2(st2.x * texMatrix[2].x + st2.y * texMatrix[2].y + texMatrix[2].z, |
| 133 |
st2.x * texMatrix[3].x + st2.y * texMatrix[3].y + texMatrix[3].z); |
| 134 |
st2 += texMatrix[2].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[3].w * 2.0 * M_PI)); |
| 135 |
|
| 136 |
st2 = vec2(st2.x * texMatrix[4].x + st2.y * texMatrix[4].y + texMatrix[4].z, |
| 137 |
st2.x * texMatrix[5].x + st2.y * texMatrix[5].y + texMatrix[5].z); |
| 138 |
st2 += texMatrix[4].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[5].w * 2.0 * M_PI)); |
| 139 |
|
| 140 |
st2 = vec2(st2.x * texMatrix[6].x + st2.y * texMatrix[6].y + texMatrix[6].z, |
| 141 |
st2.x * texMatrix[7].x + st2.y * texMatrix[7].y + texMatrix[7].z); |
| 142 |
st2 += texMatrix[6].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[7].w * 2.0 * M_PI)); |
| 143 |
|
| 144 |
return st2; |
| 145 |
} |
| 146 |
#endif |
| 147 |
|
| 148 |
|
| 149 |
float CalcLightAttenuation(float point, float normDist) |
| 150 |
{ |
| 151 |
// zero light at 1.0, approximating q3 style |
| 152 |
// also don't attenuate directional light |
| 153 |
float attenuation = (0.5 * normDist - 1.5) * point + 1.0; |
| 154 |
|
| 155 |
// clamp attenuation |
| 156 |
#if defined(NO_LIGHT_CLAMP) |
| 157 |
attenuation = max(attenuation, 0.0); |
| 158 |
#else |
| 159 |
attenuation = clamp(attenuation, 0.0, 1.0); |
| 160 |
#endif |
| 161 |
|
| 162 |
return attenuation; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
void main() |
| 167 |
{ |
| 168 |
#if defined(USE_VERTEX_ANIMATION) |
| 169 |
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp); |
| 170 |
vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp); |
| 171 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 172 |
vec3 tangent = mix(attr_Tangent.xyz, attr_Tangent2.xyz, u_VertexLerp); |
| 173 |
#endif |
| 174 |
#elif defined(USE_BONE_ANIMATION) |
| 175 |
mat4 vtxMat = u_BoneMatrix[int(attr_BoneIndexes.x)] * attr_BoneWeights.x; |
| 176 |
vtxMat += u_BoneMatrix[int(attr_BoneIndexes.y)] * attr_BoneWeights.y; |
| 177 |
vtxMat += u_BoneMatrix[int(attr_BoneIndexes.z)] * attr_BoneWeights.z; |
| 178 |
vtxMat += u_BoneMatrix[int(attr_BoneIndexes.w)] * attr_BoneWeights.w; |
| 179 |
mat3 nrmMat = mat3(cross(vtxMat[1].xyz, vtxMat[2].xyz), cross(vtxMat[2].xyz, vtxMat[0].xyz), cross(vtxMat[0].xyz, vtxMat[1].xyz)); |
| 180 |
|
| 181 |
vec3 position = vec3(vtxMat * vec4(attr_Position, 1.0)); |
| 182 |
vec3 normal = normalize(nrmMat * attr_Normal); |
| 183 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 184 |
vec3 tangent = normalize(nrmMat * attr_Tangent.xyz); |
| 185 |
#endif |
| 186 |
#else |
| 187 |
vec3 position = attr_Position; |
| 188 |
vec3 normal = attr_Normal; |
| 189 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 190 |
vec3 tangent = attr_Tangent.xyz; |
| 191 |
#endif |
| 192 |
#endif |
| 193 |
|
| 194 |
#if defined(USE_TCGEN) |
| 195 |
vec2 texCoords = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1); |
| 196 |
#else |
| 197 |
vec2 texCoords = attr_TexCoord0.st; |
| 198 |
#endif |
| 199 |
|
| 200 |
#if defined(USE_TCMOD) |
| 201 |
vec4 diffuseTexMatrix[8]; |
| 202 |
diffuseTexMatrix[0] = u_DiffuseTexMatrix0; |
| 203 |
diffuseTexMatrix[1] = u_DiffuseTexMatrix1; |
| 204 |
diffuseTexMatrix[2] = u_DiffuseTexMatrix2; |
| 205 |
diffuseTexMatrix[3] = u_DiffuseTexMatrix3; |
| 206 |
diffuseTexMatrix[4] = u_DiffuseTexMatrix4; |
| 207 |
diffuseTexMatrix[5] = u_DiffuseTexMatrix5; |
| 208 |
diffuseTexMatrix[6] = u_DiffuseTexMatrix6; |
| 209 |
diffuseTexMatrix[7] = u_DiffuseTexMatrix7; |
| 210 |
var_TexCoords.xy = ModTexCoords(texCoords, position, diffuseTexMatrix); |
| 211 |
#else |
| 212 |
var_TexCoords.xy = texCoords; |
| 213 |
#endif |
| 214 |
|
| 215 |
gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0); |
| 216 |
|
| 217 |
#if defined(USE_MODELMATRIX) |
| 218 |
position = (u_ModelMatrix * vec4(position, 1.0)).xyz; |
| 219 |
normal = (u_ModelMatrix * vec4(normal, 0.0)).xyz; |
| 220 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 221 |
tangent = (u_ModelMatrix * vec4(tangent, 0.0)).xyz; |
| 222 |
#endif |
| 223 |
#endif |
| 224 |
|
| 225 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 226 |
vec3 bitangent = cross(normal, tangent) * attr_Tangent.w; |
| 227 |
#endif |
| 228 |
|
| 229 |
#if defined(USE_LIGHT_VECTOR) |
| 230 |
vec3 L = u_LightOrigin.xyz - (position * u_LightOrigin.w); |
| 231 |
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 232 |
vec3 L = attr_LightDirection; |
| 233 |
#if defined(USE_MODELMATRIX) |
| 234 |
L = (u_ModelMatrix * vec4(L, 0.0)).xyz; |
| 235 |
#endif |
| 236 |
#endif |
| 237 |
|
| 238 |
#if defined(USE_LIGHTMAP) |
| 239 |
var_TexCoords.zw = attr_TexCoord1.st; |
| 240 |
#endif |
| 241 |
|
| 242 |
var_Color = u_VertColor * attr_Color + u_BaseColor; |
| 243 |
|
| 244 |
#if defined(USE_LIGHT_VECTOR) |
| 245 |
#if defined(USE_FAST_LIGHT) |
| 246 |
float sqrLightDist = dot(L, L); |
| 247 |
float NL = clamp(dot(normalize(normal), L) / sqrt(sqrLightDist), 0.0, 1.0); |
| 248 |
float attenuation = CalcLightAttenuation(u_LightOrigin.w, u_LightRadius * u_LightRadius / sqrLightDist); |
| 249 |
|
| 250 |
var_Color.rgb *= u_DirectedLight * (attenuation * NL) + u_AmbientLight; |
| 251 |
#else |
| 252 |
var_ColorAmbient.rgb = u_AmbientLight * var_Color.rgb; |
| 253 |
var_Color.rgb *= u_DirectedLight; |
| 254 |
#if defined(USE_PBR) |
| 255 |
var_ColorAmbient.rgb *= var_ColorAmbient.rgb; |
| 256 |
#endif |
| 257 |
#endif |
| 258 |
#endif |
| 259 |
|
| 260 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) && defined(USE_PBR) |
| 261 |
var_Color.rgb *= var_Color.rgb; |
| 262 |
#endif |
| 263 |
|
| 264 |
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP) |
| 265 |
var_PrimaryLightDir.xyz = u_PrimaryLightOrigin.xyz - (position * u_PrimaryLightOrigin.w); |
| 266 |
var_PrimaryLightDir.w = u_PrimaryLightRadius * u_PrimaryLightRadius; |
| 267 |
#endif |
| 268 |
|
| 269 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 270 |
#if defined(USE_LIGHT_VECTOR) |
| 271 |
var_LightDir = vec4(L, u_LightRadius * u_LightRadius); |
| 272 |
#else |
| 273 |
var_LightDir = vec4(L, 0.0); |
| 274 |
#endif |
| 275 |
#if defined(USE_DELUXEMAP) |
| 276 |
var_LightDir -= u_EnableTextures.y * var_LightDir; |
| 277 |
#endif |
| 278 |
#endif |
| 279 |
|
| 280 |
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) |
| 281 |
vec3 viewDir = u_ViewOrigin - position; |
| 282 |
// store view direction in tangent space to save on varyings |
| 283 |
var_Normal = vec4(normal, viewDir.x); |
| 284 |
var_Tangent = vec4(tangent, viewDir.y); |
| 285 |
var_Bitangent = vec4(bitangent, viewDir.z); |
| 286 |
#endif |
| 287 |
} |