2012-12-04 03:05:34 +00:00
|
|
|
attribute vec4 attr_TexCoord0;
|
2013-03-15 06:11:27 +00:00
|
|
|
#if defined(USE_LIGHTMAP) || defined(USE_TCGEN)
|
2012-12-04 03:05:34 +00:00
|
|
|
attribute vec4 attr_TexCoord1;
|
|
|
|
#endif
|
|
|
|
attribute vec4 attr_Color;
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
attribute vec3 attr_Position;
|
2012-12-04 03:05:34 +00:00
|
|
|
attribute vec3 attr_Normal;
|
2013-11-13 03:21:51 +00:00
|
|
|
attribute vec4 attr_Tangent;
|
2012-12-04 03:05:34 +00:00
|
|
|
|
|
|
|
#if defined(USE_VERTEX_ANIMATION)
|
2013-09-16 07:54:26 +00:00
|
|
|
attribute vec3 attr_Position2;
|
2012-12-04 03:05:34 +00:00
|
|
|
attribute vec3 attr_Normal2;
|
2013-12-13 05:38:01 +00:00
|
|
|
attribute vec4 attr_Tangent2;
|
2018-07-27 22:40:25 +00:00
|
|
|
#elif defined(USE_BONE_ANIMATION)
|
|
|
|
attribute vec4 attr_BoneIndexes;
|
|
|
|
attribute vec4 attr_BoneWeights;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHT) && !defined(USE_LIGHT_VECTOR)
|
|
|
|
attribute vec3 attr_LightDirection;
|
|
|
|
#endif
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_DELUXEMAP)
|
|
|
|
uniform vec4 u_EnableTextures; // x = normal, y = deluxe, z = specular, w = cube
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2012-12-04 03:05:34 +00:00
|
|
|
uniform vec3 u_ViewOrigin;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_TCGEN)
|
|
|
|
uniform int u_TCGen0;
|
2013-03-15 06:11:27 +00:00
|
|
|
uniform vec3 u_TCGen0Vector0;
|
|
|
|
uniform vec3 u_TCGen0Vector1;
|
2013-11-04 03:34:22 +00:00
|
|
|
uniform vec3 u_LocalViewOrigin;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_TCMOD)
|
2024-01-06 14:20:30 +00:00
|
|
|
uniform vec4 u_DiffuseTexMatrix0;
|
|
|
|
uniform vec4 u_DiffuseTexMatrix1;
|
|
|
|
uniform vec4 u_DiffuseTexMatrix2;
|
|
|
|
uniform vec4 u_DiffuseTexMatrix3;
|
|
|
|
uniform vec4 u_DiffuseTexMatrix4;
|
|
|
|
uniform vec4 u_DiffuseTexMatrix5;
|
|
|
|
uniform vec4 u_DiffuseTexMatrix6;
|
|
|
|
uniform vec4 u_DiffuseTexMatrix7;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
uniform mat4 u_ModelViewProjectionMatrix;
|
|
|
|
uniform vec4 u_BaseColor;
|
|
|
|
uniform vec4 u_VertColor;
|
|
|
|
|
|
|
|
#if defined(USE_MODELMATRIX)
|
|
|
|
uniform mat4 u_ModelMatrix;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_VERTEX_ANIMATION)
|
|
|
|
uniform float u_VertexLerp;
|
2018-07-27 22:40:25 +00:00
|
|
|
#elif defined(USE_BONE_ANIMATION)
|
|
|
|
uniform mat4 u_BoneMatrix[MAX_GLSL_BONES];
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHT_VECTOR)
|
|
|
|
uniform vec4 u_LightOrigin;
|
2013-09-18 06:52:40 +00:00
|
|
|
uniform float u_LightRadius;
|
2013-11-04 03:34:22 +00:00
|
|
|
uniform vec3 u_DirectedLight;
|
2012-12-04 03:05:34 +00:00
|
|
|
uniform vec3 u_AmbientLight;
|
|
|
|
#endif
|
|
|
|
|
2013-04-02 07:17:24 +00:00
|
|
|
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
|
|
|
|
uniform vec4 u_PrimaryLightOrigin;
|
2013-09-18 06:52:40 +00:00
|
|
|
uniform float u_PrimaryLightRadius;
|
2013-04-02 07:17:24 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-18 06:52:40 +00:00
|
|
|
varying vec4 var_TexCoords;
|
2012-12-04 03:05:34 +00:00
|
|
|
|
|
|
|
varying vec4 var_Color;
|
2016-02-18 04:06:18 +00:00
|
|
|
#if defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
|
|
|
|
varying vec4 var_ColorAmbient;
|
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-09-18 06:52:40 +00:00
|
|
|
varying vec4 var_Normal;
|
|
|
|
varying vec4 var_Tangent;
|
|
|
|
varying vec4 var_Bitangent;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-09-16 07:54:26 +00:00
|
|
|
varying vec4 var_LightDir;
|
2013-04-02 07:17:24 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
|
2013-09-18 06:52:40 +00:00
|
|
|
varying vec4 var_PrimaryLightDir;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
2013-03-15 06:11:27 +00:00
|
|
|
#if defined(USE_TCGEN)
|
|
|
|
vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3 TCGenVector1)
|
|
|
|
{
|
|
|
|
vec2 tex = attr_TexCoord0.st;
|
|
|
|
|
|
|
|
if (TCGen == TCGEN_LIGHTMAP)
|
|
|
|
{
|
|
|
|
tex = attr_TexCoord1.st;
|
|
|
|
}
|
|
|
|
else if (TCGen == TCGEN_ENVIRONMENT_MAPPED)
|
|
|
|
{
|
2013-10-10 10:41:31 +00:00
|
|
|
vec3 viewer = normalize(u_LocalViewOrigin - position);
|
2013-12-13 05:38:01 +00:00
|
|
|
vec2 ref = reflect(viewer, normal).yz;
|
|
|
|
tex.s = ref.x * -0.5 + 0.5;
|
|
|
|
tex.t = ref.y * 0.5 + 0.5;
|
2013-03-15 06:11:27 +00:00
|
|
|
}
|
|
|
|
else if (TCGen == TCGEN_VECTOR)
|
|
|
|
{
|
|
|
|
tex = vec2(dot(position, TCGenVector0), dot(position, TCGenVector1));
|
|
|
|
}
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2013-03-15 06:11:27 +00:00
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
#if defined(USE_TCMOD)
|
2024-01-06 14:20:30 +00:00
|
|
|
vec2 ModTexCoords(vec2 st, vec3 position, vec4 texMatrix[8])
|
2012-12-04 03:05:34 +00:00
|
|
|
{
|
2024-01-06 14:20:30 +00:00
|
|
|
vec2 st2 = st;
|
2013-12-13 05:38:01 +00:00
|
|
|
vec2 offsetPos = vec2(position.x + position.z, position.y);
|
|
|
|
|
2024-01-06 14:20:30 +00:00
|
|
|
st2 = vec2(st2.x * texMatrix[0].x + st2.y * texMatrix[0].y + texMatrix[0].z,
|
|
|
|
st2.x * texMatrix[1].x + st2.y * texMatrix[1].y + texMatrix[1].z);
|
|
|
|
st2 += texMatrix[0].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[1].w * 2.0 * M_PI));
|
|
|
|
|
|
|
|
st2 = vec2(st2.x * texMatrix[2].x + st2.y * texMatrix[2].y + texMatrix[2].z,
|
|
|
|
st2.x * texMatrix[3].x + st2.y * texMatrix[3].y + texMatrix[3].z);
|
|
|
|
st2 += texMatrix[2].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[3].w * 2.0 * M_PI));
|
|
|
|
|
|
|
|
st2 = vec2(st2.x * texMatrix[4].x + st2.y * texMatrix[4].y + texMatrix[4].z,
|
|
|
|
st2.x * texMatrix[5].x + st2.y * texMatrix[5].y + texMatrix[5].z);
|
|
|
|
st2 += texMatrix[4].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[5].w * 2.0 * M_PI));
|
|
|
|
|
|
|
|
st2 = vec2(st2.x * texMatrix[6].x + st2.y * texMatrix[6].y + texMatrix[6].z,
|
|
|
|
st2.x * texMatrix[7].x + st2.y * texMatrix[7].y + texMatrix[7].z);
|
|
|
|
st2 += texMatrix[6].w * sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(texMatrix[7].w * 2.0 * M_PI));
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2024-01-06 14:20:30 +00:00
|
|
|
return st2;
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-12-13 05:38:01 +00:00
|
|
|
float CalcLightAttenuation(float point, float normDist)
|
2013-09-17 06:41:04 +00:00
|
|
|
{
|
2013-12-13 05:38:01 +00:00
|
|
|
// zero light at 1.0, approximating q3 style
|
2013-09-17 06:41:04 +00:00
|
|
|
// also don't attenuate directional light
|
2013-12-13 05:38:01 +00:00
|
|
|
float attenuation = (0.5 * normDist - 1.5) * point + 1.0;
|
|
|
|
|
2013-09-17 06:41:04 +00:00
|
|
|
// clamp attenuation
|
|
|
|
#if defined(NO_LIGHT_CLAMP)
|
2013-09-18 06:52:40 +00:00
|
|
|
attenuation = max(attenuation, 0.0);
|
2013-09-17 06:41:04 +00:00
|
|
|
#else
|
|
|
|
attenuation = clamp(attenuation, 0.0, 1.0);
|
|
|
|
#endif
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2013-09-17 06:41:04 +00:00
|
|
|
return attenuation;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
#if defined(USE_VERTEX_ANIMATION)
|
2013-12-13 05:38:01 +00:00
|
|
|
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
|
|
|
|
vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp);
|
2016-09-14 10:57:51 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-12-13 05:38:01 +00:00
|
|
|
vec3 tangent = mix(attr_Tangent.xyz, attr_Tangent2.xyz, u_VertexLerp);
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
2018-07-27 22:40:25 +00:00
|
|
|
#elif defined(USE_BONE_ANIMATION)
|
|
|
|
mat4 vtxMat = u_BoneMatrix[int(attr_BoneIndexes.x)] * attr_BoneWeights.x;
|
|
|
|
vtxMat += u_BoneMatrix[int(attr_BoneIndexes.y)] * attr_BoneWeights.y;
|
|
|
|
vtxMat += u_BoneMatrix[int(attr_BoneIndexes.z)] * attr_BoneWeights.z;
|
|
|
|
vtxMat += u_BoneMatrix[int(attr_BoneIndexes.w)] * attr_BoneWeights.w;
|
|
|
|
mat3 nrmMat = mat3(cross(vtxMat[1].xyz, vtxMat[2].xyz), cross(vtxMat[2].xyz, vtxMat[0].xyz), cross(vtxMat[0].xyz, vtxMat[1].xyz));
|
|
|
|
|
|
|
|
vec3 position = vec3(vtxMat * vec4(attr_Position, 1.0));
|
|
|
|
vec3 normal = normalize(nrmMat * attr_Normal);
|
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
|
|
|
vec3 tangent = normalize(nrmMat * attr_Tangent.xyz);
|
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
#else
|
2013-09-16 07:54:26 +00:00
|
|
|
vec3 position = attr_Position;
|
2013-12-13 05:38:01 +00:00
|
|
|
vec3 normal = attr_Normal;
|
2016-09-14 10:57:51 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-12-13 05:38:01 +00:00
|
|
|
vec3 tangent = attr_Tangent.xyz;
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_TCGEN)
|
2013-09-16 07:54:26 +00:00
|
|
|
vec2 texCoords = GenTexCoords(u_TCGen0, position, normal, u_TCGen0Vector0, u_TCGen0Vector1);
|
2013-03-15 06:11:27 +00:00
|
|
|
#else
|
2013-04-02 07:17:24 +00:00
|
|
|
vec2 texCoords = attr_TexCoord0.st;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_TCMOD)
|
2024-01-06 14:20:30 +00:00
|
|
|
vec4 diffuseTexMatrix[8];
|
|
|
|
diffuseTexMatrix[0] = u_DiffuseTexMatrix0;
|
|
|
|
diffuseTexMatrix[1] = u_DiffuseTexMatrix1;
|
|
|
|
diffuseTexMatrix[2] = u_DiffuseTexMatrix2;
|
|
|
|
diffuseTexMatrix[3] = u_DiffuseTexMatrix3;
|
|
|
|
diffuseTexMatrix[4] = u_DiffuseTexMatrix4;
|
|
|
|
diffuseTexMatrix[5] = u_DiffuseTexMatrix5;
|
|
|
|
diffuseTexMatrix[6] = u_DiffuseTexMatrix6;
|
|
|
|
diffuseTexMatrix[7] = u_DiffuseTexMatrix7;
|
|
|
|
var_TexCoords.xy = ModTexCoords(texCoords, position, diffuseTexMatrix);
|
2012-12-04 03:05:34 +00:00
|
|
|
#else
|
2013-09-18 06:52:40 +00:00
|
|
|
var_TexCoords.xy = texCoords;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0);
|
|
|
|
|
|
|
|
#if defined(USE_MODELMATRIX)
|
2013-12-13 05:38:01 +00:00
|
|
|
position = (u_ModelMatrix * vec4(position, 1.0)).xyz;
|
|
|
|
normal = (u_ModelMatrix * vec4(normal, 0.0)).xyz;
|
2016-09-14 10:57:51 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-12-13 05:38:01 +00:00
|
|
|
tangent = (u_ModelMatrix * vec4(tangent, 0.0)).xyz;
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
2016-09-14 10:57:51 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2014-10-29 01:15:13 +00:00
|
|
|
vec3 bitangent = cross(normal, tangent) * attr_Tangent.w;
|
2013-12-13 05:38:01 +00:00
|
|
|
#endif
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
#if defined(USE_LIGHT_VECTOR)
|
|
|
|
vec3 L = u_LightOrigin.xyz - (position * u_LightOrigin.w);
|
2013-12-13 05:38:01 +00:00
|
|
|
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2014-10-29 01:15:13 +00:00
|
|
|
vec3 L = attr_LightDirection;
|
2013-09-16 07:54:26 +00:00
|
|
|
#if defined(USE_MODELMATRIX)
|
2013-09-18 06:52:40 +00:00
|
|
|
L = (u_ModelMatrix * vec4(L, 0.0)).xyz;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
#if defined(USE_LIGHTMAP)
|
2013-09-18 06:52:40 +00:00
|
|
|
var_TexCoords.zw = attr_TexCoord1.st;
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
2013-12-13 05:38:01 +00:00
|
|
|
|
2013-09-24 10:29:49 +00:00
|
|
|
var_Color = u_VertColor * attr_Color + u_BaseColor;
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2016-02-18 04:06:18 +00:00
|
|
|
#if defined(USE_LIGHT_VECTOR)
|
|
|
|
#if defined(USE_FAST_LIGHT)
|
2013-12-13 05:38:01 +00:00
|
|
|
float sqrLightDist = dot(L, L);
|
|
|
|
float NL = clamp(dot(normalize(normal), L) / sqrt(sqrLightDist), 0.0, 1.0);
|
2016-02-18 04:06:18 +00:00
|
|
|
float attenuation = CalcLightAttenuation(u_LightOrigin.w, u_LightRadius * u_LightRadius / sqrLightDist);
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2013-12-13 05:38:01 +00:00
|
|
|
var_Color.rgb *= u_DirectedLight * (attenuation * NL) + u_AmbientLight;
|
2016-02-18 04:06:18 +00:00
|
|
|
#else
|
|
|
|
var_ColorAmbient.rgb = u_AmbientLight * var_Color.rgb;
|
|
|
|
var_Color.rgb *= u_DirectedLight;
|
|
|
|
#if defined(USE_PBR)
|
|
|
|
var_ColorAmbient.rgb *= var_ColorAmbient.rgb;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT) && defined(USE_PBR)
|
|
|
|
var_Color.rgb *= var_Color.rgb;
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-02 07:17:24 +00:00
|
|
|
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
|
2013-09-24 10:29:49 +00:00
|
|
|
var_PrimaryLightDir.xyz = u_PrimaryLightOrigin.xyz - (position * u_PrimaryLightOrigin.w);
|
2013-09-18 06:52:40 +00:00
|
|
|
var_PrimaryLightDir.w = u_PrimaryLightRadius * u_PrimaryLightRadius;
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-09-16 07:54:26 +00:00
|
|
|
#if defined(USE_LIGHT_VECTOR)
|
2013-09-18 06:52:40 +00:00
|
|
|
var_LightDir = vec4(L, u_LightRadius * u_LightRadius);
|
2013-09-16 07:54:26 +00:00
|
|
|
#else
|
|
|
|
var_LightDir = vec4(L, 0.0);
|
|
|
|
#endif
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_DELUXEMAP)
|
2013-12-13 05:38:01 +00:00
|
|
|
var_LightDir -= u_EnableTextures.y * var_LightDir;
|
2013-11-04 03:34:22 +00:00
|
|
|
#endif
|
2013-09-16 07:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-09-24 10:29:49 +00:00
|
|
|
vec3 viewDir = u_ViewOrigin - position;
|
2013-09-18 06:52:40 +00:00
|
|
|
// store view direction in tangent space to save on varyings
|
2013-09-24 10:29:49 +00:00
|
|
|
var_Normal = vec4(normal, viewDir.x);
|
|
|
|
var_Tangent = vec4(tangent, viewDir.y);
|
|
|
|
var_Bitangent = vec4(bitangent, viewDir.z);
|
2013-09-18 06:52:40 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|