119 lines
2.6 KiB
HTML
119 lines
2.6 KiB
HTML
<!DOCTYPE html>
|
|
<html>
|
|
|
|
<script id="vertex-shader-1" type="x-shader/x-vertex">
|
|
#version 300 es
|
|
|
|
// first vertex shader used to compute view from camera
|
|
|
|
in vec4 aPosition;
|
|
|
|
uniform mat4 uInstanceMatrix;
|
|
uniform mat4 uProjectionMatrix;
|
|
uniform mat4 uModelViewMatrix;
|
|
|
|
void main()
|
|
{
|
|
|
|
gl_Position = uProjectionMatrix*uModelViewMatrix*uInstanceMatrix*aPosition;
|
|
|
|
}
|
|
</script>
|
|
|
|
<script id="fragment-shader-1" type="x-shader/x-fragment">
|
|
#version 300 es
|
|
|
|
// fragment shader outputs fragment depth in rgb components.
|
|
|
|
precision highp float;
|
|
|
|
out vec4 fColor;
|
|
|
|
void
|
|
main()
|
|
{
|
|
fColor = vec4(gl_FragCoord.z, gl_FragCoord.z, gl_FragCoord.z , 1.0);
|
|
|
|
}
|
|
</script>
|
|
|
|
<script id="vertex-shader-2" type="x-shader/x-vertex">
|
|
#version 300 es
|
|
|
|
in vec4 aPosition;
|
|
in vec4 aColor;
|
|
|
|
uniform mat4 uInstanceMatrix;
|
|
uniform mat4 uProjectionMatrix;
|
|
uniform mat4 uModelViewMatrix;
|
|
|
|
uniform mat4 uLightProjectionMatrix;
|
|
uniform mat4 uLightViewMatrix;
|
|
|
|
out vec4 vColor;
|
|
out vec4 vLightViewPosition;
|
|
|
|
void main()
|
|
{
|
|
// shader computes position both from camera and light source
|
|
|
|
gl_Position = uProjectionMatrix*uModelViewMatrix*uInstanceMatrix*aPosition;
|
|
vLightViewPosition = uLightProjectionMatrix*uLightViewMatrix*uInstanceMatrix*aPosition;
|
|
vColor = aColor;
|
|
}
|
|
</script>
|
|
|
|
<script id="fragment-shader-2" type="x-shader/x-fragment">
|
|
#version 300 es
|
|
|
|
precision mediump float;
|
|
|
|
in vec4 vColor;
|
|
in vec4 vLightViewPosition;
|
|
out vec4 fColor;
|
|
|
|
uniform sampler2D uTextureMap;
|
|
|
|
void main()
|
|
{
|
|
vec4 shadowColor = vec4(0.0, 0.0, 0.0, 1.0); //black
|
|
|
|
// rescale depths from [-1, 1] to texture coords in range [0, 1]
|
|
// convert from (x, y, z, w) values to (x/w, y/w, z/w)
|
|
|
|
vec3 shadowCoord = 0.5*vLightViewPosition.xyz/vLightViewPosition.w + 0.5;
|
|
|
|
// get depth from texture map
|
|
|
|
float depth = texture(uTextureMap, shadowCoord.xy).x;
|
|
|
|
//compare depth from camera with depth of fragment in camera space
|
|
// add small factor to control some of the aliasing
|
|
|
|
if(shadowCoord.z < depth + 0.005) fColor = vColor;
|
|
else fColor = shadowColor;
|
|
|
|
// use line below to display depth map
|
|
// gl_FragColor = vec4(depth, depth, depth, 1.0);
|
|
}
|
|
</script>
|
|
|
|
<script type="text/javascript" src="../Common/initShaders.js"></script>
|
|
<script type="text/javascript" src="../Common/MVnew.js"></script>
|
|
<script type="text/javascript" src="shadowMap.js"></script>
|
|
|
|
|
|
<body>
|
|
<canvas id="gl-canvas" width="1024" height="1024">
|
|
Oops ... your browser doesn't support the HTML5 canvas element
|
|
</canvas>
|
|
<div>
|
|
<button id = "ButtonX">Rotate X</button>
|
|
<button id = "ButtonY">Rotate Y</button>
|
|
<button id = "ButtonZ">Rotate Z</button>
|
|
<button id = "ButtonT">Toggle Cube Rotation</button>
|
|
|
|
</div>
|
|
</body>
|
|
</html>
|