Here you go:
attribute highp vec3 position;
attribute mediump vec3 normal;
attribute mediump vec2 uv;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec3 lightVarying;
uniform highp mat3 agk_WorldNormal;
uniform highp mat4 agk_World;
uniform highp mat4 agk_ViewProj;
vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );
void main()
{
vec4 pos = agk_World * vec4(position,1.0);
gl_Position = agk_ViewProj * pos;
vec3 norm = normalize(agk_WorldNormal * normal);
posVarying = pos.xyz;
normalVarying = norm;
lightVarying = GetVSLighting( norm, posVarying );
}
uniform sampler2D texture0;
uniform sampler2D texture1;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec3 lightVarying;
mediump vec3 grassColor = vec3(0.89, 1.08, 0.77);
mediump vec3 rockColor = vec3(0.75, 0.75, 0.75);
mediump vec3 GetPSLighting( mediump vec3 normal, highp vec3 pos );
mediump vec3 ApplyFog( mediump vec3 color, highp vec3 pointPos );
void main()
{
vec3 norm = normalize(normalVarying);
vec3 texCoord = posVarying;
if (normalVarying.x < 0.0)
texCoord.x = -texCoord.x;
if (normalVarying.y < 0.0)
texCoord.y = -texCoord.y;
if (normalVarying.z < 0.0)
texCoord.z = -texCoord.z;
vec3 diffuseX = texture2D(texture0, texCoord.zy * 0.5).rgb;
vec3 diffuseY = texture2D(texture0, texCoord.xz * 0.5).rgb;
vec3 diffuseZ = texture2D(texture0, texCoord.xy * 0.5).rgb;
vec3 weight = abs(normalVarying);
weight = weight / (weight.x + weight.y + weight.z);
vec3 diffuse = diffuseX * weight.x + diffuseY * weight.y + diffuseZ * weight.z;
vec3 normX = texture2D(texture1, texCoord.zy * 0.5).xyz * 2.0 - 1.0;
vec3 normY = texture2D(texture1, texCoord.xz * 0.5).xyz * 2.0 - 1.0;
vec3 normZ = texture2D(texture1, texCoord.xy * 0.5).xyz * 2.0 - 1.0;
//Swizzled Normals
vec3 axisSign = step(0.0, normalVarying) * 2.0 - 1.0;
normX.z *= axisSign.x;
normY.z *= axisSign.y;
normZ.z *= axisSign.z;
//Whiteout Blend
//~ normX = vec3(normX.xy + normalVarying.zy, abs(normX.z) * normalVarying.x);
//~ normY = vec3(normY.xy + normalVarying.xz, abs(normY.z) * normalVarying.y);
//~ normZ = vec3(normZ.xy + normalVarying.xy, abs(normZ.z) * normalVarying.z);
//UDN Blend
//~ normX = vec3(normX.xy + normalVarying.zy, normalVarying.x);
//~ normY = vec3(normY.xy + normalVarying.xz, normalVarying.y);
//~ normZ = vec3(normZ.xy + normalVarying.xy, normalVarying.z);
vec3 normal = normalize(normX.zyx * weight.x + normY.xzy * weight.y + normZ.xyz * weight.z);
vec3 light = lightVarying + GetPSLighting( normal, posVarying );
float t = dot(vec3(0.0,1.0,0.0), norm);
vec3 colorNormal = mix( rockColor, grassColor, pow(max(0.0,t),0.7));
vec4 finalColor = vec4(diffuse * colorNormal * light, 1.0);
finalColor.rgb = ApplyFog( finalColor.rgb, posVarying );
gl_FragColor = finalColor;
}
This is the un-revised Triplanar shader i used for my Marching cube world some time ago, as real UV mapping in such a generated world is quite hard(but possible).