OpenGL2: Small glsl shader optimizations, fixes, and cleanup.

This commit is contained in:
SmileTheory 2013-12-12 21:38:01 -08:00
parent efe8437cde
commit 623d107f42
15 changed files with 255 additions and 244 deletions

View file

@ -1,4 +1,4 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec4 attr_TexCoord0;
uniform mat4 u_ModelViewProjectionMatrix;
@ -8,6 +8,6 @@ varying vec2 var_TexCoords;
void main()
{
gl_Position = u_ModelViewProjectionMatrix * attr_Position;
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
var_TexCoords = attr_TexCoord0.st;
}

View file

@ -1,4 +1,4 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec4 attr_TexCoord0;
uniform mat4 u_ModelViewProjectionMatrix;
@ -8,6 +8,6 @@ varying vec2 var_TexCoords;
void main()
{
gl_Position = u_ModelViewProjectionMatrix * attr_Position;
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
var_TexCoords = attr_TexCoord0.st;
}

View file

@ -1,4 +1,4 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec4 attr_TexCoord0;
attribute vec3 attr_Normal;
@ -48,7 +48,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
}
else if (u_DeformGen == DGEN_WAVE_SQUARE)
{
func = sign(sin(value * 2.0 * M_PI));
func = sign(0.5 - fract(value));
}
else if (u_DeformGen == DGEN_WAVE_TRIANGLE)
{
@ -62,7 +62,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
{
func = (1.0 - fract(value));
}
else if (u_DeformGen == DGEN_BULGE)
else // if (u_DeformGen == DGEN_BULGE)
{
func = sin(value);
}
@ -73,16 +73,16 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
void main()
{
vec4 position = attr_Position;
vec3 normal = attr_Normal;
vec3 position = attr_Position;
vec3 normal = attr_Normal * 2.0 - vec3(1.0);
#if defined(USE_DEFORM_VERTEXES)
position.xyz = DeformPosition(position.xyz, normal, attr_TexCoord0.st);
position = DeformPosition(position, normal, attr_TexCoord0.st);
#endif
gl_Position = u_ModelViewProjectionMatrix * position;
gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0);
vec3 dist = u_DlightInfo.xyz - position.xyz;
vec3 dist = u_DlightInfo.xyz - position;
var_Tex1 = dist.xy * u_DlightInfo.a + vec2(0.5);
float dlightmod = step(0.0, dot(dist, normal));

View file

@ -1,4 +1,4 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec4 attr_TexCoord0;
uniform mat4 u_ModelViewProjectionMatrix;
@ -8,6 +8,6 @@ varying vec2 var_TexCoords;
void main()
{
gl_Position = u_ModelViewProjectionMatrix * attr_Position;
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
var_TexCoords = attr_TexCoord0.st;
}

View file

@ -5,5 +5,5 @@ varying float var_Scale;
void main()
{
gl_FragColor = u_Color;
gl_FragColor.a *= sqrt(clamp(var_Scale, 0.0, 1.0));
gl_FragColor.a = sqrt(clamp(var_Scale, 0.0, 1.0));
}

View file

@ -1,27 +1,30 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec3 attr_Normal;
attribute vec4 attr_TexCoord0;
//#if defined(USE_VERTEX_ANIMATION)
attribute vec4 attr_Position2;
#if defined(USE_VERTEX_ANIMATION)
attribute vec3 attr_Position2;
attribute vec3 attr_Normal2;
//#endif
#endif
uniform vec4 u_FogDistance;
uniform vec4 u_FogDepth;
uniform float u_FogEyeT;
//#if defined(USE_DEFORM_VERTEXES)
#if defined(USE_DEFORM_VERTEXES)
uniform int u_DeformGen;
uniform float u_DeformParams[5];
//#endif
#endif
uniform float u_Time;
uniform mat4 u_ModelViewProjectionMatrix;
//#if defined(USE_VERTEX_ANIMATION)
#if defined(USE_VERTEX_ANIMATION)
uniform float u_VertexLerp;
//#endif
#endif
uniform vec4 u_Color;
varying float var_Scale;
@ -57,7 +60,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
}
else if (u_DeformGen == DGEN_WAVE_SQUARE)
{
func = sign(sin(value * 2.0 * M_PI));
func = sign(0.5 - fract(value));
}
else if (u_DeformGen == DGEN_WAVE_TRIANGLE)
{
@ -71,7 +74,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
{
func = (1.0 - fract(value));
}
else if (u_DeformGen == DGEN_BULGE)
else // if (u_DeformGen == DGEN_BULGE)
{
func = sin(value);
}
@ -80,15 +83,15 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
}
#endif
float CalcFog(vec4 position)
float CalcFog(vec3 position)
{
float s = dot(position, u_FogDistance) * 8.0;
float t = dot(position, u_FogDepth);
float s = dot(vec4(position, 1.0), u_FogDistance) * 8.0;
float t = dot(vec4(position, 1.0), u_FogDepth);
float eyeOutside = step(0.0, -u_FogEyeT);
float fogged = step(eyeOutside, t);
t = max(t, 1e-6);
float eyeOutside = float(u_FogEyeT < 0.0);
float fogged = float(t >= eyeOutside);
t += 1e-6;
t *= fogged / (t - u_FogEyeT * eyeOutside);
return s * t;
@ -97,18 +100,19 @@ float CalcFog(vec4 position)
void main()
{
#if defined(USE_VERTEX_ANIMATION)
vec4 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp));
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp);
normal = normalize(normal - vec3(0.5));
#else
vec4 position = attr_Position;
vec3 normal = attr_Normal;
vec3 position = attr_Position;
vec3 normal = attr_Normal * 2.0 - vec3(1.0);
#endif
#if defined(USE_DEFORM_VERTEXES)
position.xyz = DeformPosition(position.xyz, normal, attr_TexCoord0.st);
#endif
gl_Position = u_ModelViewProjectionMatrix * position;
gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0);
var_Scale = CalcFog(position);
var_Scale = CalcFog(position) * u_Color.a * u_Color.a;
}

View file

@ -37,6 +37,8 @@ void main()
{
color = color2;
}
//color = color * (u_Texture1Env.xxxx + color2 * u_Texture1Env.z) + color2 * u_Texture1Env.y;
#endif
gl_FragColor = color * var_Color;

View file

@ -89,11 +89,11 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
}
else if (u_DeformGen == DGEN_WAVE_SQUARE)
{
func = sign(sin(value * 2.0 * M_PI));
func = sign(fract(0.5 - value));
}
else if (u_DeformGen == DGEN_WAVE_TRIANGLE)
{
func = 1.0 - abs(4.0 * fract(value + 0.25) - 2.0);
func = abs(fract(value + 0.75) - 0.5) * 4.0 - 1.0;
}
else if (u_DeformGen == DGEN_WAVE_SAWTOOTH)
{
@ -103,7 +103,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
{
func = (1.0 - fract(value));
}
else if (u_DeformGen == DGEN_BULGE)
else // if (u_DeformGen == DGEN_BULGE)
{
func = sin(value);
}
@ -124,7 +124,9 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3
else if (TCGen == TCGEN_ENVIRONMENT_MAPPED)
{
vec3 viewer = normalize(u_LocalViewOrigin - position);
tex = -reflect(viewer, normal).yz * vec2(0.5, -0.5) + 0.5;
vec2 ref = reflect(viewer, normal).yz;
tex.s = ref.x * -0.5 + 0.5;
tex.t = ref.y * 0.5 + 0.5;
}
else if (TCGen == TCGEN_VECTOR)
{
@ -139,13 +141,14 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3
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;
float phase = offTurb.w * 2.0 * M_PI;
vec2 st2;
st2.x = st.x * texMatrix.x + (st.y * texMatrix.z + offTurb.x);
st2.y = st.x * texMatrix.y + (st.y * texMatrix.w + offTurb.y);
vec3 offsetPos = position / 1024.0;
offsetPos.x += offsetPos.z;
vec2 offsetPos = vec2(position.x + position.z, position.y);
vec2 texOffset = sin((offsetPos.xy + vec2(phase)) * 2.0 * M_PI);
vec2 texOffset = sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(phase));
return st2 + texOffset * amplitude;
}
@ -186,13 +189,13 @@ vec4 CalcColor(vec3 position, vec3 normal)
#if defined(USE_FOG)
float CalcFog(vec3 position)
{
float s = (dot(position, u_FogDistance.xyz) + u_FogDistance.w) * 8.0;
float t = dot(position, u_FogDepth.xyz) + u_FogDepth.w;
float s = dot(vec4(position, 1.0), u_FogDistance) * 8.0;
float t = dot(vec4(position, 1.0), u_FogDepth);
float eyeOutside = step(0.0, -u_FogEyeT);
float fogged = step(eyeOutside, t);
t = max(t, 1e-6);
float eyeOutside = float(u_FogEyeT < 0.0);
float fogged = float(t < eyeOutside);
t += 1e-6;
t *= fogged / (t - u_FogEyeT * eyeOutside);
return s * t;
@ -202,8 +205,9 @@ float CalcFog(vec3 position)
void main()
{
#if defined(USE_VERTEX_ANIMATION)
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp) * 2.0 - vec3(1.0));
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp);
normal = normalize(normal - vec3(0.5));
#else
vec3 position = attr_Position;
vec3 normal = attr_Normal * 2.0 - vec3(1.0);

View file

@ -166,22 +166,41 @@ vec3 EnvironmentBRDF(float gloss, float NE, vec3 specular)
float a1 = t.w;
return clamp( a0 + specular * ( a1 - a0 ), 0.0, 1.0 );
#elif 0
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
// from http://seblagarde.wordpress.com/2011/08/17/hello-world/
return mix(specular.rgb, max(specular.rgb, vec3(gloss)), CalcFresnel(NE));
#else
// from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx
// from http://advances.realtimerendering.com/s2011/Lazarov-Physically-Based-Lighting-in-Black-Ops%20%28Siggraph%202011%20Advances%20in%20Real-Time%20Rendering%20Course%29.pptx
return mix(specular.rgb, vec3(1.0), CalcFresnel(NE) / (4.0 - 3.0 * gloss));
#endif
}
float CalcBlinn(float NH, float shininess)
{
#if 0
// from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
float a = shininess + 0.775;
return exp(a * NH - a);
#if defined(USE_BLINN) || defined(USE_BLINN_FRESNEL)
// Normalized Blinn-Phong
float norm = shininess * 0.125 + 1.0;
#elif defined(USE_MCAULEY)
// Cook-Torrance as done by Stephen McAuley
// http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes_v2.pdf
float norm = shininess * 0.25 + 0.125;
#elif defined(USE_GOTANDA)
// Neumann-Neumann as done by Yoshiharu Gotanda
// http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
float norm = shininess * 0.124858 + 0.269182;
#elif defined(USE_LAZAROV)
// Cook-Torrance as done by Dimitar Lazarov
// http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
float norm = shininess * 0.125 + 0.25;
#else
return pow(NH, shininess);
float norm = 1.0;
#endif
#if 0
// from http://seblagarde.wordpress.com/2012/06/03/spherical-gaussien-approximation-for-blinn-phong-phong-and-fresnel/
float a = shininess + 0.775;
return norm * exp(a * NH - a);
#else
return norm * pow(NH, shininess);
#endif
}
@ -215,145 +234,122 @@ float CalcFresnel(float EH)
float CalcVisibility(float NH, float NL, float NE, float EH, float gloss)
{
#if 1
// From http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
#if defined(USE_GOTANDA)
// Neumann-Neumann as done by Yoshiharu Gotanda
// http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
return 1.0 / max(max(NL, NE), EPSILON);
#elif defined(USE_LAZAROV)
// Cook-Torrance as done by Dimitar Lazarov
// http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
float k = min(1.0, gloss + 0.545);
return 1.0 / (k * EH * EH + (1.0 - k));
#elif 0
return 1.0 / (k * (EH * EH - 1.0) + 1.0);
#elif defined(USE_GGX)
float roughness = exp2(gloss * -6.5);
#if defined(USE_GGX)
// From http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
float k = roughness + 1.0;
k *= k * 0.125;
#else
float k = roughness;
#endif
// Modified from http://blog.selfshadow.com/publications/s2013-shading-course/karis/s2013_pbs_epic_notes_v2.pdf
// NL, NE in numerator factored out from cook-torrance
float k = roughness + 1.0;
k *= k * 0.125;
float k2 = 1.0 - k;
float invGeo1 = NL * k2 + k;
float invGeo2 = NE * k2 + k;
return 1.0 / (invGeo1 * invGeo2);
#else
float geo = 2.0 * NH * min(NE, NL);
geo /= max(EH, geo);
return geo;
return 1.0;
#endif
}
vec3 CalcSpecular(vec3 specular, float NH, float NL, float NE, float EH, float gloss, float shininess)
{
float blinn = CalcBlinn(NH, shininess);
#if defined(USE_GGX)
float distrib = CalcGGX(NH, gloss);
#else
float distrib = CalcBlinn(NH, shininess);
#endif
#if defined(USE_BLINN)
vec3 fSpecular = specular;
#else
vec3 fSpecular = mix(specular, vec3(1.0), CalcFresnel(EH));
#endif
float vis = CalcVisibility(NH, NL, NE, EH, gloss);
#if defined(USE_BLINN)
// Normalized Blinn-Phong
return specular * blinn * (shininess * 0.125 + 1.0);
#elif defined(USE_BLINN_FRESNEL)
// Normalized Blinn-Phong with Fresnel
return fSpecular * blinn * (shininess * 0.125 + 1.0);
#elif defined(USE_MCAULEY)
// Cook-Torrance as done by Stephen McAuley
// http://blog.selfshadow.com/publications/s2012-shading-course/mcauley/s2012_pbs_farcry3_notes_v2.pdf
return fSpecular * blinn * (shininess * 0.25 + 0.125);
#elif defined(USE_GOTANDA)
// Neumann-Neumann as done by Yoshiharu Gotanda
// http://research.tri-ace.com/Data/s2012_beyond_CourseNotes.pdf
return fSpecular * blinn * (shininess * 0.124858 + 0.269182) / max(max(NL, NE), EPSILON);
#elif defined(USE_LAZAROV)
// Cook-Torrance as done by Dimitar Lazarov
// http://blog.selfshadow.com/publications/s2013-shading-course/lazarov/s2013_pbs_black_ops_2_notes.pdf
return fSpecular * blinn * (shininess * 0.125 + 0.25) * vis;
#endif
return vec3(0.0);
return fSpecular * (distrib * vis);
}
float CalcLightAttenuation(vec3 dir, float sqrRadius)
float CalcLightAttenuation(float point, float normDist)
{
// 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
// zero light at 1.0, approximating q3 style
// also don't attenuate directional light
attenuation = (0.5 * attenuation - 1.5) * point + 1.0;
float attenuation = (0.5 * normDist - 1.5) * point + 1.0;
// clamp attenuation
#if defined(NO_LIGHT_CLAMP)
attenuation = max(attenuation, 0.0);
#else
attenuation = clamp(attenuation, 0.0, 1.0);
#endif
return attenuation;
}
// from http://www.thetenthplanet.de/archives/1180
mat3 cotangent_frame( vec3 N, vec3 p, vec2 uv )
{
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx( p );
vec3 dp2 = dFdy( p );
vec2 duv1 = dFdx( uv );
vec2 duv2 = dFdy( uv );
// solve the linear system
vec3 dp2perp = cross( dp2, N );
vec3 dp1perp = cross( N, dp1 );
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
// construct a scale-invariant frame
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
return mat3( T * invmax, B * invmax, N );
// get edge vectors of the pixel triangle
vec3 dp1 = dFdx( p );
vec3 dp2 = dFdy( p );
vec2 duv1 = dFdx( uv );
vec2 duv2 = dFdy( uv );
// solve the linear system
vec3 dp2perp = cross( dp2, N );
vec3 dp1perp = cross( N, dp1 );
vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
// construct a scale-invariant frame
float invmax = inversesqrt( max( dot(T,T), dot(B,B) ) );
return mat3( T * invmax, B * invmax, N );
}
void main()
{
vec3 L, N, E, H;
float NL, NH, NE, EH;
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
#if defined(USE_VERT_TANGENT_SPACE)
mat3 tangentToWorld = mat3(var_Tangent.xyz, var_Bitangent.xyz, var_Normal.xyz);
E = vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w);
#else
mat3 tangentToWorld = cotangent_frame(var_Normal, -var_ViewDir, var_TexCoords.xy);
E = var_ViewDir;
#endif
#endif
#if defined(USE_DELUXEMAP)
L = texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5);
L = L * u_EnableTextures.y + var_LightDir.xyz;
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
E = normalize(E);
L = var_LightDir.xyz;
#endif
#if (defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)) || defined(USE_PARALLAXMAP)
#if defined(USE_VERT_TANGENT_SPACE)
E = normalize(vec3(var_Normal.w, var_Tangent.w, var_Bitangent.w));
#else
E = normalize(var_ViewDir);
#if defined(USE_DELUXEMAP)
L += (texture2D(u_DeluxeMap, var_TexCoords.zw).xyz - vec3(0.5)) * u_EnableTextures.y;
#endif
float sqrLightDist = dot(L, L);
#endif
#if defined(USE_LIGHTMAP)
vec4 lightSample = texture2D(u_LightMap, var_TexCoords.zw).rgba;
#if defined(RGBM_LIGHTMAP)
lightSample.rgb *= 32.0 * lightSample.a;
#endif
vec4 lightSample = texture2D(u_LightMap, var_TexCoords.zw);
vec3 lightColor = lightSample.rgb;
#if defined(RGBM_LIGHTMAP)
lightColor *= 32.0 * lightSample.a;
#endif
#elif defined(USE_LIGHT_VECTOR) && !defined(USE_FAST_LIGHT)
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(L, var_LightDir.w);
vec3 lightColor = u_DirectedLight * CalcLightAttenuation(float(var_LightDir.w > 0.0), var_LightDir.w / sqrLightDist);
vec3 ambientColor = u_AmbientLight;
#elif defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
vec3 lightColor = var_LightColor;
@ -383,21 +379,22 @@ void main()
N.xy = texture2D(u_NormalMap, texCoords).rg - vec2(0.5);
#endif
N.xy *= u_EnableTextures.x;
N.z = sqrt(0.25 - dot(N.xy, N.xy));
N = normalize(tangentToWorld * N);
N.z = sqrt((0.25 - N.x * N.x) - N.y * N.y);
N = tangentToWorld * N;
#else
N = normalize(var_Normal.xyz);
N = var_Normal.xyz;
#endif
L = normalize(L);
N = normalize(N);
L /= sqrt(sqrLightDist);
#if defined(USE_SHADOWMAP)
vec2 shadowTex = gl_FragCoord.xy * r_FBufScale;
float shadowValue = texture2D(u_ShadowMap, shadowTex).r;
// surfaces not facing the light are always shadowed
shadowValue *= step(0.0, dot(var_Normal.xyz, var_PrimaryLightDir.xyz));
shadowValue *= float(dot(var_Normal.xyz, var_PrimaryLightDir.xyz) > 0.0);
#if defined(SHADOWMAP_MODULATE)
//vec3 shadowColor = min(u_PrimaryLightAmbient, lightColor);
vec3 shadowColor = u_PrimaryLightAmbient * lightColor;
@ -428,44 +425,41 @@ void main()
NL = clamp(dot(N, L), 0.0, 1.0);
NE = clamp(dot(N, E), 0.0, 1.0);
vec4 specular = vec4(1.0);
#if defined(USE_SPECULARMAP)
vec4 specular = texture2D(u_SpecularMap, texCoords);
specular = (specular - vec4(1.0)) * u_EnableTextures.z + vec4(1.0);
specular += texture2D(u_SpecularMap, texCoords) * u_EnableTextures.z - u_EnableTextures.zzzz;
#if defined(USE_GAMMA2_TEXTURES)
specular.rgb *= specular.rgb;
#endif
#else
vec4 specular = vec4(1.0);
#endif
specular *= u_MaterialInfo.xxxy;
float gloss = specular.a;
float shininess = exp2(gloss * 13.0);
#if defined(SPECULAR_IS_METALLIC)
// diffuse is actually base color, and red of specular is metallicness
// diffuse is actually base color, and red of specular is metallicness
float metallic = specular.r;
specular.rgb = vec3(0.04) + 0.96 * diffuse.rgb * metallic;
specular.rgb = (0.96 * metallic) * diffuse.rgb + vec3(0.04);
diffuse.rgb *= 1.0 - metallic;
#else
// adjust diffuse by specular reflectance, to maintain energy conservation
diffuse.rgb *= vec3(1.0) - specular.rgb;
#endif
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
#if defined(r_deluxeSpecular) || defined(USE_LIGHT_VECTOR)
float adjGloss = gloss;
float adjShininess = shininess;
#if !defined(USE_LIGHT_VECTOR)
#if !defined(USE_LIGHT_VECTOR)
adjGloss *= r_deluxeSpecular;
adjShininess = exp2(adjGloss * 13.0);
#endif
#endif
H = normalize(L + E);
EH = clamp(dot(E, H), 0.0, 1.0);
@ -477,16 +471,16 @@ void main()
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, adjGloss, adjShininess);
#endif
#endif
gl_FragColor.rgb = lightColor * reflectance * NL;
gl_FragColor.rgb = lightColor * reflectance * NL;
gl_FragColor.rgb += ambientColor * (diffuse.rgb + specular.rgb);
#if defined(USE_CUBEMAP)
reflectance = EnvironmentBRDF(gloss, NE, specular.rgb);
vec3 R = reflect(E, N);
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
vec3 cubeLightColor = textureCubeLod(u_CubeMap, R, 7.0 - gloss * 7.0).rgb * u_EnableTextures.w;
#if defined(USE_LIGHTMAP)
cubeLightColor *= lightSample.rgb;
@ -495,29 +489,40 @@ void main()
#else
cubeLightColor *= lightColor * NL + ambientColor;
#endif
//gl_FragColor.rgb += diffuse.rgb * textureCubeLod(u_CubeMap, N, 7.0).rgb * u_EnableTextures.w;
gl_FragColor.rgb += cubeLightColor * reflectance;
#endif
#if defined(USE_PRIMARY_LIGHT)
L = var_PrimaryLightDir.xyz; //normalize(var_PrimaryLightDir.xyz);
NL = clamp(dot(N, L), 0.0, 1.0);
vec3 L2, H2;
float NL2, EH2, NH2;
H = normalize(L + E);
EH = clamp(dot(E, H), 0.0, 1.0);
NH = clamp(dot(N, H), 0.0, 1.0);
L2 = var_PrimaryLightDir.xyz;
reflectance = CalcDiffuse(diffuse.rgb, N, L, E, NE, NL, shininess);
reflectance += CalcSpecular(specular.rgb, NH, NL, NE, EH, gloss, shininess);
// enable when point lights are supported as primary lights
//sqrLightDist = dot(L2, L2);
//L2 /= sqrt(sqrLightDist);
NL2 = clamp(dot(N, L2), 0.0, 1.0);
H2 = normalize(L2 + E);
EH2 = clamp(dot(E, H2), 0.0, 1.0);
NH2 = clamp(dot(N, H2), 0.0, 1.0);
reflectance = CalcDiffuse(diffuse.rgb, N, L2, E, NE, NL2, shininess);
reflectance += CalcSpecular(specular.rgb, NH2, NL2, NE, EH2, gloss, shininess);
lightColor = u_PrimaryLightColor;
// enable when point lights are supported as primary lights
//lightColor *= CalcLightAttenuation(float(u_PrimaryLightDir.w > 0.0), u_PrimaryLightDir.w / sqrLightDist);
lightColor = u_PrimaryLightColor; // * CalcLightAttenuation(L, u_PrimaryLightDir.w);
#if defined(USE_SHADOWMAP)
lightColor *= shadowValue;
#endif
gl_FragColor.rgb += lightColor * reflectance * NL;
gl_FragColor.rgb += lightColor * reflectance * NL2;
#endif
gl_FragColor.a = diffuse.a;

View file

@ -14,8 +14,7 @@ attribute vec4 attr_Tangent;
attribute vec3 attr_Position2;
attribute vec3 attr_Normal2;
#if defined(USE_VERT_TANGENT_SPACE)
attribute vec3 attr_Tangent2;
attribute vec3 attr_Bitangent2;
attribute vec4 attr_Tangent2;
#endif
#endif
@ -108,13 +107,15 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3
else if (TCGen == TCGEN_ENVIRONMENT_MAPPED)
{
vec3 viewer = normalize(u_LocalViewOrigin - position);
tex = -reflect(viewer, normal).yz * vec2(0.5, -0.5) + 0.5;
vec2 ref = reflect(viewer, normal).yz;
tex.s = ref.x * -0.5 + 0.5;
tex.t = ref.y * 0.5 + 0.5;
}
else if (TCGen == TCGEN_VECTOR)
{
tex = vec2(dot(position, TCGenVector0), dot(position, TCGenVector1));
}
return tex;
}
#endif
@ -123,38 +124,33 @@ vec2 GenTexCoords(int TCGen, vec3 position, vec3 normal, vec3 TCGenVector0, vec3
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;
float phase = offTurb.w * 2.0 * M_PI;
vec2 st2;
st2.x = st.x * texMatrix.x + (st.y * texMatrix.z + offTurb.x);
st2.y = st.x * texMatrix.y + (st.y * texMatrix.w + offTurb.y);
vec2 offsetPos = vec2(position.x + position.z, position.y);
vec2 texOffset = sin(offsetPos * (2.0 * M_PI / 1024.0) + vec2(phase));
vec3 offsetPos = position / 1024.0;
offsetPos.x += offsetPos.z;
vec2 texOffset = sin((offsetPos.xy + vec2(phase)) * 2.0 * M_PI);
return st2 + texOffset * amplitude;
}
#endif
float CalcLightAttenuation(vec3 dir, float sqrRadius)
float CalcLightAttenuation(float point, float normDist)
{
// 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
// zero light at 1.0, approximating q3 style
// also don't attenuate directional light
attenuation = (0.5 * attenuation - 1.5) * point + 1.0;
float attenuation = (0.5 * normDist - 1.5) * point + 1.0;
// clamp attenuation
#if defined(NO_LIGHT_CLAMP)
attenuation = max(attenuation, 0.0);
#else
attenuation = clamp(attenuation, 0.0, 1.0);
#endif
return attenuation;
}
@ -162,22 +158,22 @@ float CalcLightAttenuation(vec3 dir, float sqrRadius)
void main()
{
#if defined(USE_VERTEX_ANIMATION)
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp) * 2.0 - vec3(1.0));
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp);
#if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
vec3 tangent = normalize(mix(attr_Tangent.xyz, attr_Tangent2.xyz, u_VertexLerp) * 2.0 - vec3(1.0));
vec3 tangent = mix(attr_Tangent.xyz, attr_Tangent2.xyz, u_VertexLerp);
#endif
#else
vec3 position = attr_Position;
vec3 normal = attr_Normal * 2.0 - vec3(1.0);
vec3 normal = attr_Normal;
#if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
vec3 tangent = attr_Tangent.xyz * 2.0 - vec3(1.0);
vec3 tangent = attr_Tangent.xyz;
#endif
#endif
normal = normal * 2.0 - vec3(1.0);
#if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
vec3 bitangent = cross(normal, tangent);
bitangent *= attr_Tangent.w * 2.0 - 1.0;
tangent = tangent * 2.0 - vec3(1.0);
#endif
#if defined(USE_TCGEN)
@ -195,17 +191,20 @@ void main()
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;
#if defined(USE_VERT_TANGENT_SPACE)
tangent = (u_ModelMatrix * vec4(tangent, 0.0)).xyz;
bitangent = (u_ModelMatrix * vec4(bitangent, 0.0)).xyz;
position = (u_ModelMatrix * vec4(position, 1.0)).xyz;
normal = (u_ModelMatrix * vec4(normal, 0.0)).xyz;
#if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
tangent = (u_ModelMatrix * vec4(tangent, 0.0)).xyz;
#endif
#endif
#if defined(USE_VERT_TANGENT_SPACE) && defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
vec3 bitangent = cross(normal, tangent) * (attr_Tangent.w * 2.0 - 1.0);
#endif
#if defined(USE_LIGHT_VECTOR)
vec3 L = u_LightOrigin.xyz - (position * u_LightOrigin.w);
#elif defined(USE_LIGHT)
#elif defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
vec3 L = attr_LightDirection * 2.0 - vec3(1.0);
#if defined(USE_MODELMATRIX)
L = (u_ModelMatrix * vec4(L, 0.0)).xyz;
@ -215,7 +214,7 @@ void main()
#if defined(USE_LIGHTMAP)
var_TexCoords.zw = attr_TexCoord1.st;
#endif
var_Color = u_VertColor * attr_Color + u_BaseColor;
#if defined(USE_LIGHT_VERTEX) && !defined(USE_FAST_LIGHT)
var_LightColor = var_Color.rgb;
@ -223,10 +222,11 @@ void main()
#endif
#if defined(USE_LIGHT_VECTOR) && defined(USE_FAST_LIGHT)
float attenuation = CalcLightAttenuation(L, u_LightRadius * u_LightRadius);
float NL = clamp(dot(normal, normalize(L)), 0.0, 1.0);
float sqrLightDist = dot(L, L);
float attenuation = CalcLightAttenuation(u_LightOrigin.w, u_LightRadius * u_LightRadius / sqrLightDist);
float NL = clamp(dot(normalize(normal), L) / sqrt(sqrLightDist), 0.0, 1.0);
var_Color.rgb *= u_DirectedLight * attenuation * NL + u_AmbientLight;
var_Color.rgb *= u_DirectedLight * (attenuation * NL) + u_AmbientLight;
#endif
#if defined(USE_PRIMARY_LIGHT) || defined(USE_SHADOWMAP)
@ -241,15 +241,12 @@ void main()
var_LightDir = vec4(L, 0.0);
#endif
#if defined(USE_DELUXEMAP)
var_LightDir *= 1.0 - u_EnableTextures.y;
var_LightDir -= u_EnableTextures.y * var_LightDir;
#endif
#endif
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
vec3 viewDir = u_ViewOrigin - position;
#endif
#if defined(USE_LIGHT) && !defined(USE_FAST_LIGHT)
#if defined(USE_VERT_TANGENT_SPACE)
// store view direction in tangent space to save on varyings
var_Normal = vec4(normal, viewDir.x);

View file

@ -1,4 +1,4 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec3 attr_Normal;
uniform mat4 u_ModelViewProjectionMatrix;
@ -8,10 +8,8 @@ varying vec3 var_Normal;
void main()
{
vec4 position = attr_Position;
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
gl_Position = u_ModelViewProjectionMatrix * position;
var_Position = position.xyz;
var_Normal = attr_Normal;
var_Position = attr_Position;
var_Normal = attr_Normal * 2.0 - vec3(1.0);
}

View file

@ -1,9 +1,9 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec3 attr_Normal;
attribute vec4 attr_TexCoord0;
//#if defined(USE_VERTEX_ANIMATION)
attribute vec4 attr_Position2;
attribute vec3 attr_Position2;
attribute vec3 attr_Normal2;
//#endif
@ -54,7 +54,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
}
else if (u_DeformGen == DGEN_WAVE_SQUARE)
{
func = sign(sin(value * 2.0 * M_PI));
func = sign(0.5 - fract(value));
}
else if (u_DeformGen == DGEN_WAVE_TRIANGLE)
{
@ -68,7 +68,7 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
{
func = (1.0 - fract(value));
}
else if (u_DeformGen == DGEN_BULGE)
else // if (u_DeformGen == DGEN_BULGE)
{
func = sin(value);
}
@ -78,12 +78,13 @@ vec3 DeformPosition(const vec3 pos, const vec3 normal, const vec2 st)
void main()
{
vec4 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = normalize(mix(attr_Normal, attr_Normal2, u_VertexLerp));
vec3 position = mix(attr_Position, attr_Position2, u_VertexLerp);
vec3 normal = mix(attr_Normal, attr_Normal2, u_VertexLerp);
normal = normalize(normal - vec3(0.5));
position.xyz = DeformPosition(position.xyz, normal, attr_TexCoord0.st);
position = DeformPosition(position, normal, attr_TexCoord0.st);
gl_Position = u_ModelViewProjectionMatrix * position;
gl_Position = u_ModelViewProjectionMatrix * vec4(position, 1.0);
var_Position = (u_ModelMatrix * position).xyz;
var_Position = (u_ModelMatrix * vec4(position, 1.0)).xyz;
}

View file

@ -3,7 +3,7 @@
uniform sampler2D u_DiffuseMap;
uniform vec4 u_Color;
varying vec2 var_Tex1;
varying vec2 var_Tex1;
void main()

View file

@ -1,6 +1,6 @@
#version 120
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec4 attr_TexCoord0;
uniform mat4 u_ModelViewProjectionMatrix;
@ -10,6 +10,6 @@ varying vec2 var_Tex1;
void main()
{
gl_Position = u_ModelViewProjectionMatrix * attr_Position;
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
var_Tex1 = attr_TexCoord0.st;
}

View file

@ -1,4 +1,4 @@
attribute vec4 attr_Position;
attribute vec3 attr_Position;
attribute vec4 attr_TexCoord0;
uniform mat4 u_ModelViewProjectionMatrix;
@ -8,6 +8,6 @@ varying vec2 var_TexCoords;
void main()
{
gl_Position = u_ModelViewProjectionMatrix * attr_Position;
gl_Position = u_ModelViewProjectionMatrix * vec4(attr_Position, 1.0);
var_TexCoords = attr_TexCoord0.st;
}