forked from gpjt/webgl-lessons
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path07-lighting.html
122 lines (103 loc) · 5.1 KB
/
07-lighting.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<html>
<head>
<title>WebGL - 07 Lighting</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="./style/webgl.css" type="text/css">
<script type="text/javascript" src="./scripts/glMatrix-0.9.5.min.js"></script>
<script src="./scripts/07-lighting.js" type="text/javascript"></script>
<!-- Fragment shader program -->
<script id="shader-fs" type="x-shader/x-fragment">
precision mediump float;
// uniform attribute for setting texture coordinates
varying vec2 vTextureCoord;
// uniform attribute for setting lighting
varying vec3 vLightWeighting;
// uniform attribute for setting 2D sampler
uniform sampler2D uSampler;
void main(void) {
// sample the fragment color from texture
vec4 textureColor = texture2D(uSampler, vec2(vTextureCoord.s, vTextureCoord.t));
gl_FragColor = vec4(textureColor.rgb * vLightWeighting, textureColor.a);
}
</script>
<!-- Vertex shader program -->
<script id="shader-vs" type="x-shader/x-vertex">
// atributes for setting vertex position, normals and texture coordinates
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix; // model-view matrix
uniform mat4 uPMatrix; // projection matrix
uniform mat3 uNMatrix; // normal matrix
uniform vec3 uAmbientColor; // ambient color uniform
uniform vec3 uLightingDirection; // light direction uniform
uniform vec3 uDirectionalColor; // light color
uniform bool uUseLighting; // lighting switch
// variable for passing texture coordinates and lighting weights
// from vertex shader to fragment shader
varying vec2 vTextureCoord;
varying vec3 vLightWeighting;
void main(void) {
// calculate the vertex position
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
if (!uUseLighting) {
vLightWeighting = vec3(1.0, 1.0, 1.0);
} else {
// transform normals
vec3 transformedNormal = uNMatrix * aVertexNormal;
// calculate weight for directional light
float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
// calculate lighting
vLightWeighting = uAmbientColor + uDirectionalColor * directionalLightWeighting;
}
}
</script>
<script type="text/javascript">
function showValue(id, newValue)
{
document.getElementById(id+"span").innerHTML = " " + newValue;
}
</script>
</head>
<body onload="start()">
<h1>WebGL - 07 Lighting</h1>
<div id="content">
<canvas id="glcanvas" width="1280px" height="720px">
No <code><canvas></code> suppport in your browser.
</canvas>
<br />
<br />
<input type="checkbox" id="lighting" checked /> Use lighting<br/>
(Use cursor keys to spin the box and <code>Page Up</code>/<code>Page Down</code> to zoom out/in)
<br/>
<h2>Directional light:</h2>
<table>
<tr>
<td class="table1st"><b>Direction:</b></td>
<td>X: <input type="range" min="-1" max="1" step="0.05" id="lightDirectionX" value="-0.25" oninput="showValue(this.id, this.value)" /><span id="lightDirectionXspan">-0.25</span></td>
<td>Y: <input type="range" min="-1" max="1" step="0.05" id="lightDirectionY" value="-0.25" oninput="showValue(this.id, this.value)" /><span id="lightDirectionYspan">-0.25</span></td>
<td>Z: <input type="range" min="-1" max="1" step="0.05" id="lightDirectionZ" value="-1.0" oninput="showValue(this.id, this.value)" /><span id="lightDirectionZspan">-1.0</span></td>
</tr>
<tr>
<td><b>Color:</b></td>
<td>R: <input type="range" min="0" max="1" step="0.01" id="directionalR" value="0.8" oninput="showValue(this.id, this.value)" /><span id="directionalRspan">0.8</span></td>
<td>G: <input type="range" min="0" max="1" step="0.01" id="directionalG" value="0.8" oninput="showValue(this.id, this.value)" /><span id="directionalGspan">0.8</span></td>
<td>B: <input type="range" min="0" max="1" step="0.01" id="directionalB" value="0.8" oninput="showValue(this.id, this.value)" /><span id="directionalBspan">0.8</span></td>
</tr>
</table>
<h2>Ambient light:</h2>
<table>
<tr>
<td class="table1st"><b>Color:</b></td>
<td>R: <input type="range" min="0" max="1" step="0.01" id="ambientR" value="0.2" oninput="showValue(this.id, this.value)" /><span id="ambientRspan">0.2</span></td>
<td>G: <input type="range" min="0" max="1" step="0.01" id="ambientG" value="0.2" oninput="showValue(this.id, this.value)" /><span id="ambientGspan">0.2</span></td>
<td>B: <input type="range" min="0" max="1" step="0.01" id="ambientB" value="0.2" oninput="showValue(this.id, this.value)" /><span id="ambientBspan">0.2</span></td>
</tr>
</table>
<br />
<br />
<a href="./"><- Back</a>
</div>
</body>
</html>