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-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
2012-12-04 03:05:34 +00:00
|
|
|
attribute vec3 attr_Tangent;
|
|
|
|
attribute vec3 attr_Bitangent;
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
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-11-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
2012-12-04 03:05:34 +00:00
|
|
|
attribute vec3 attr_Tangent2;
|
|
|
|
attribute vec3 attr_Bitangent2;
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
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)
|
|
|
|
uniform vec4 u_DiffuseTexMatrix;
|
|
|
|
uniform vec4 u_DiffuseTexOffTurb;
|
|
|
|
#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;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHT_VECTOR)
|
|
|
|
uniform vec4 u_LightOrigin;
|
2013-09-18 06:52:40 +00:00
|
|
|
uniform float u_LightRadius;
|
|
|
|
#if defined(USE_FAST_LIGHT)
|
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
|
|
|
|
#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;
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-11-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
2013-09-18 06:52:40 +00:00
|
|
|
varying vec4 var_Normal;
|
|
|
|
varying vec4 var_Tangent;
|
|
|
|
varying vec4 var_Bitangent;
|
2013-11-05 05:53:05 +00:00
|
|
|
#else
|
|
|
|
varying vec3 var_Normal;
|
|
|
|
varying vec3 var_ViewDir;
|
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
|
2013-09-18 06:52:40 +00:00
|
|
|
varying vec3 var_LightColor;
|
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-03-15 06:11:27 +00:00
|
|
|
tex = -reflect(viewer, normal).yz * vec2(0.5, -0.5) + 0.5;
|
|
|
|
}
|
|
|
|
else if (TCGen == TCGEN_VECTOR)
|
|
|
|
{
|
|
|
|
tex = vec2(dot(position, TCGenVector0), dot(position, TCGenVector1));
|
|
|
|
}
|
|
|
|
|
|
|
|
return tex;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
#if defined(USE_TCMOD)
|
|
|
|
vec2 ModTexCoords(vec2 st, vec3 position, vec4 texMatrix, vec4 offTurb)
|
|
|
|
{
|
|
|
|
float amplitude = offTurb.z;
|
|
|
|
float phase = offTurb.w;
|
|
|
|
vec2 st2 = vec2(dot(st, texMatrix.xz), dot(st, texMatrix.yw)) + offTurb.xy;
|
|
|
|
|
2013-09-24 10:29:49 +00:00
|
|
|
vec3 offsetPos = position / 1024.0;
|
2012-12-04 03:05:34 +00:00
|
|
|
offsetPos.x += offsetPos.z;
|
|
|
|
|
|
|
|
vec2 texOffset = sin((offsetPos.xy + vec2(phase)) * 2.0 * M_PI);
|
|
|
|
|
|
|
|
return st2 + texOffset * amplitude;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2013-09-17 06:41:04 +00:00
|
|
|
float CalcLightAttenuation(vec3 dir, float sqrRadius)
|
|
|
|
{
|
|
|
|
// point light at >0 radius, directional otherwise
|
|
|
|
float point = float(sqrRadius > 0.0);
|
|
|
|
|
|
|
|
// inverse square light
|
|
|
|
float attenuation = sqrRadius / dot(dir, dir);
|
|
|
|
|
|
|
|
// zero light at radius, approximating q3 style
|
|
|
|
// also don't attenuate directional light
|
|
|
|
attenuation = (0.5 * attenuation - 1.5) * point + 1.0;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
|
return attenuation;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-12-04 03:05:34 +00:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
#if defined(USE_VERTEX_ANIMATION)
|
2013-11-04 03:34:22 +00:00
|
|
|
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
|
2012-12-04 03:05:34 +00:00
|
|
|
vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp));
|
2013-11-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
2012-12-04 03:05:34 +00:00
|
|
|
vec3 tangent = normalize(mix(attr_Tangent, attr_Tangent2, u_VertexLerp));
|
|
|
|
vec3 bitangent = normalize(mix(attr_Bitangent, attr_Bitangent2, u_VertexLerp));
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
#else
|
2013-09-16 07:54:26 +00:00
|
|
|
vec3 position = attr_Position;
|
2012-12-04 03:05:34 +00:00
|
|
|
vec3 normal = attr_Normal;
|
2013-11-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
2012-12-04 03:05:34 +00:00
|
|
|
vec3 tangent = attr_Tangent;
|
|
|
|
vec3 bitangent = attr_Bitangent;
|
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)
|
2013-09-18 06:52:40 +00:00
|
|
|
var_TexCoords.xy = ModTexCoords(texCoords, position, u_DiffuseTexMatrix, u_DiffuseTexOffTurb);
|
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)
|
|
|
|
position = (u_ModelMatrix * vec4(position, 1.0)).xyz;
|
|
|
|
normal = (u_ModelMatrix * vec4(normal, 0.0)).xyz;
|
2013-11-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
2013-09-16 07:54:26 +00:00
|
|
|
tangent = (u_ModelMatrix * vec4(tangent, 0.0)).xyz;
|
|
|
|
bitangent = (u_ModelMatrix * vec4(bitangent, 0.0)).xyz;
|
2013-11-05 05:53:05 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +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-11-04 03:34:22 +00:00
|
|
|
#elif defined(USE_LIGHT)
|
2013-09-16 07:54:26 +00:00
|
|
|
vec3 L = attr_LightDirection;
|
|
|
|
#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-09-24 10:29:49 +00:00
|
|
|
var_Color = u_VertColor * attr_Color + u_BaseColor;
|
2012-12-04 03:05:34 +00:00
|
|
|
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
|
2013-09-24 10:29:49 +00:00
|
|
|
var_LightColor = var_Color.rgb;
|
2012-12-04 03:05:34 +00:00
|
|
|
var_Color.rgb = vec3(1.0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT)
|
2013-09-17 06:41:04 +00:00
|
|
|
float attenuation = CalcLightAttenuation(L, u_LightRadius * u_LightRadius);
|
2013-04-02 07:17:24 +00:00
|
|
|
float NL = clamp(dot(normal, normalize(L)), 0.0, 1.0);
|
2012-12-04 03:05:34 +00:00
|
|
|
|
2013-09-16 07:54:26 +00:00
|
|
|
var_Color.rgb *= u_DirectedLight * attenuation * NL + u_AmbientLight;
|
|
|
|
#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)
|
|
|
|
var_LightDir *= 1.0 - u_EnableTextures.y;
|
|
|
|
#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-16 07:54:26 +00:00
|
|
|
#endif
|
|
|
|
|
2013-11-04 03:34:22 +00:00
|
|
|
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
|
2013-11-05 05:53:05 +00:00
|
|
|
#if defined(USE_VERT_TANGENT_SPACE)
|
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-11-05 05:53:05 +00:00
|
|
|
#else
|
|
|
|
var_Normal = normal;
|
|
|
|
var_ViewDir = viewDir;
|
|
|
|
#endif
|
2013-09-18 06:52:40 +00:00
|
|
|
#endif
|
2012-12-04 03:05:34 +00:00
|
|
|
}
|