Applying a Procedural Texture to a scene
This tutorial shows how to apply a procedural texture to a scene.
// The 4x4 world view projection matrix. uniform mat4 worldViewProjection; // input parameters for our vertex shader attribute vec4 position; attribute vec3 normal; attribute vec2 texCoord0; // input parameters for our pixel shader varying vec4 col; varying vec2 texcoord; /** * Our vertex shader. In the vertex shader, we calculate the lighting. * Then we'll combine it with our checker pattern in the pixel shader. */ void main() { gl_Position = worldViewProjection * position; /** * lightVector - light vector * normal - normal vector * We put the light such that it illuminates our model. */ vec4 diffuseColor = vec4(1, 1, 1, 1); vec3 lightPos = vec3(1000, -1000, 1000); vec3 lightVector = normalize(lightPos - position.xyz); vec3 normal = normalize(normal); vec4 diffuse = dot(normal, lightVector) * diffuseColor; col = diffuse; texcoord = texCoord0; } // #o3d SplitMarker // function for getting the checker pattern vec4 checker(vec2 uv) { float checkSize = 4.0; float fmodResult = mod(floor(checkSize * uv.x) + floor(checkSize * uv.y), 2.0); if (fmodResult < 1.0) { return vec4(0, 1, 1, 1); } else { return vec4(1, 0, 1, 1); } } // input parameters for our pixel shader varying vec4 col; varying vec2 texcoord; /** * Our pixel shader. We take the lighting color we got from the vertex sahder * and combine it with our checker pattern. */ void main() { vec4 check = checker(texcoord); vec4 color = col * check; gl_FragColor = color; } // #o3d MatrixLoadOrder RowMajor