ch10/Fog
This is an example from the book "WebGL Programming Guide" by Kouichi Matsuda and Rodger Lea
by Ivan Enzhaev
HTML
<p>↑↓: Increase/decrease the fog distance</p></p>
<canvas id="webgl" width="400" height="400">
Please use a browser that supports "canvas"
</canvas>
<script type="x-shader/x-vertex" id="VertexShader">
attribute vec4 a_Position;
attribute vec4 a_Color;
uniform mat4 u_MvpMatrix;
uniform mat4 u_ModelMatrix;
uniform vec4 u_Eye; // Position of eye point (world coordinates)
varying vec4 v_Color;
varying float v_Dist;
void main()
{
gl_Position = u_MvpMatrix * a_Position;
v_Color = a_Color;
// Calculate the distance to each vertex from eye point
v_Dist = distance(u_ModelMatrix * a_Position, u_Eye);
}
</script>
<script type="x-shader/x-fragment" id="FragmentShader">
precision mediump float;
uniform vec3 u_FogColor; // Color of Fog
uniform vec2 u_FogDist; // Distance of Fog (starting
// point, end point)
varying vec4 v_Color;
varying float v_Dist;
void main()
{
// Calculation of fog factor (factor becomes smaller
// as it goes further away from eye point)
float fogFactor = clamp((u_FogDist.y - v_Dist) /
(u_FogDist.y - u_FogDist.x), 0.0, 1.0);
// Stronger fog as it gets further: u_FogColor *
// (1 - fogFactor) + v_Color * fogFactor
vec3 color = mix(u_FogColor, vec3(v_Color), fogFactor);
gl_FragColor = vec4(color, v_Color.a);
}
</script>
<script src="https://dl.dropboxusercontent.com/s/ylw7v7a44xtrwv0/webgl-utils.js"></script>
<script src="https://dl.dropboxusercontent.com/s/iz1n21c8xafd76n/webgl-debug.js"></script>
<script src="https://dl.dropboxusercontent.com/s/subm1yzylxuao8v/cuon-utils.js"></script>
<script...
CSS
body {
overflow: hidden;
}
JavaScript
// Fog.js (c) 2012 matsuda and ohnishi
// This is an example from the book "WebGL Programming Guide"
// by Kouichi Matsuda and Rodger Lea
main();
//window.onload = main;
function main()
{
// Retrieve <canvas> element
var canvas = document.getElementById('webgl');
// Get the rendering context for WebGL
var gl = getWebGLContext(canvas);
if (!gl)
{
console.log('Failed to get the rendering context for WebGL');
return;
}
// Get shader elements
var vShaderElement = document.getElementById("VertexShader");
var fShaderElement = document.getElementById("FragmentShader");
if (vShaderElement == null)
{
console.log("Failed to get the vertex shader element");
return;
}
if (fShaderElement == null)
{
console.log("Failed to get the fragment shader element");
return;
}
// Get shader sources
var vShaderSource = vShaderElement.firstChild.textContent;
var fShaderSource = fShaderElement.firstChild.textContent;
// Initialize shaders
if (!initShaders(gl, vShaderSource, fShaderSource))
{
console.log('Failed to intialize shaders');
return;
}
// Set the vertex information
var n = initVertexBuffers(gl);
if (n < 1)
{
console.log('Failed to set the vertex information');
return;
}
// Color of Fog
var fogColor = new Float32Array([0.137, 0.231, 0.423]);
// Distance of fog [where fog starts, where fog completely covers object]
var fogDist = new Float32Array([55, 80]);
// Position of eye point (world coordinates)
var eye = new Float32Array([25, 65, 35, 1.0]);
// Get the storage locations of uniform variables
var u_MvpMatrix = gl.getUniformLocation(gl.program, 'u_MvpMatrix');
var u_ModelMatrix = gl.getUniformLocation(gl.program, 'u_ModelMatrix');
var u_Eye = gl.getUniformLocation(gl.program, 'u_Eye');
var u_FogColor = gl.getUniformLocation(gl.program,...