Advanced Shader Material
This examples facilitates several more advanced X3D rendering techniques like the ComposedShader node for specifying user defined shader programs, multi texturing, which allows having more than one image texture, the ComposedCubeMapTexture for enabling cube mapping, and the additional FloatVertexAttribute nodes for defining shader specific vertex attributes such as tangents and binormals that are necessary e.g. for bumb mapping. The geometry itself is given as an IndexedTriangleSet.
attribute vec3 position;
attribute vec3 normal;
attribute vec2 texcoord;
attribute vec3 tangent;
attribute vec3 binormal;
uniform mat4 modelViewMatrix;
uniform mat4 modelViewMatrixInverse;
uniform mat4 modelViewProjectionMatrix;
varying vec3 fragNormal;
varying vec3 fragEyeVector;
varying vec2 fragTexCoord;
varying vec3 fragTangent;
varying vec3 fragBinormal;
void main()
{
vec4 eye = vec4(modelViewMatrixInverse * vec4(0.,0.,0.,1.));
fragEyeVector = position - eye.xyz;
fragNormal = normal;
fragTangent = tangent;
fragBinormal = binormal;
//mat3 matrix = mat3(normalize(normal), normalize(tangent), normalize(binormal));
//fragNormalTS = transpose(matrix) * fragNormal;
//fragEyeTS = transpose(matrix) * fragEyeVector;
fragTexCoord = vec2(texcoord.x, 1.0 - texcoord.y);
gl_Position = modelViewProjectionMatrix * vec4(position, 1.0);
}
#ifdef GL_ES
precision highp float;
#endif
uniform sampler2D tex;
uniform samplerCube cube;
uniform sampler2D bump;
varying vec3 fragNormal;
varying vec3 fragEyeVector;
varying vec2 fragTexCoord;
varying vec3 fragTangent;
varying vec3 fragBinormal;
void main()
{
vec3 eye = normalize(fragEyeVector);
vec3 normal = normalize(fragNormal);
vec3 tangent = normalize(fragTangent);
vec3 binormal = normalize(fragBinormal);
vec4 texCol = texture2D(tex, fragTexCoord);
vec3 bumpCol = texture2D(bump, fragTexCoord).rgb;
vec3 tsn = 2.0 * (normalize(bumpCol) - 0.5);
tsn = tsn.z * normal + tsn.y * tangent + tsn.x * binormal;
normal = -normalize(tsn);
vec3 cubecoord = reflect(eye, normal);
vec4 cubeCol = textureCube(cube, cubecoord);
float p = max(0.1, dot(normal, eye));
texCol.rgb *= p;
texCol.rgb += max(0.0, pow(p, 128.0)) * vec3(0.8);
texCol.rgb = clamp(texCol.rgb, 0.0, 1.0);
gl_FragColor = mix(texCol, cubeCol, 0.35);
}