forked from gpjt/webgl-lessons
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path12-pointlight.html
124 lines (106 loc) · 5.19 KB
/
12-pointlight.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
123
124
<html>
<head>
<title>WebGL - 12 Point light</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/12-pointlight.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 uPointLightingLocation; // light direction uniform
uniform vec3 uPointLightingColor; // 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
vec4 mvPosition = uMVMatrix * vec4(aVertexPosition, 1.0);
gl_Position = uPMatrix * mvPosition;
vTextureCoord = aTextureCoord;
if (!uUseLighting) {
vLightWeighting = vec3(1.0, 1.0, 1.0);
} else {
// transform normals
vec3 lightDirection = normalize(uPointLightingLocation - mvPosition.xyz);
// calculate weight for directional light
vec3 transformedNormal = uNMatrix * aVertexNormal;
float directionalLightWeighting = max(dot(transformedNormal, lightDirection), 0.0);
// calculate lighting
vLightWeighting = uAmbientColor + uPointLightingColor * directionalLightWeighting;
}
}
</script>
<script type="text/javascript">
function showValue(id, newValue)
{
document.getElementById(id+"span").innerHTML = " " + newValue;
}
</script>
</head>
<body onload="start()">
<h1>WebGL - 12 Point light</h1>
<div id="content">
<canvas id="glcanvas" width="1280px" height="720px">
No <code><canvas></code> suppport in your browser.
</canvas>
<br />
<input type="checkbox" id="lighting" checked /> Use lighting<br/>
<br/>
<h2>Point light:</h2>
<table>
<tr>
<td class="table1st"><b>Point light:</b></td>
<td>X: <input type="range" min="-10" max="10" step="0.05" id="lightPositionX" value="0" oninput="showValue(this.id, this.value)" /><span id="lightPositionXspan">0.0</span></td>
<td>Y: <input type="range" min="-10" max="10" step="0.05" id="lightPositionY" value="0" oninput="showValue(this.id, this.value)" /><span id="lightPositionYspan">0.0</span></td>
<td>Z: <input type="range" min="-20" max="20" step="1" id="lightPositionZ" value="-20" oninput="showValue(this.id, this.value)" /><span id="lightPositionZspan">-20.0</span></td>
</tr>
<tr>
<td><b>Color:</b></td>
<td>R: <input type="range" min="0" max="1" step="0.01" id="pointR" value="0.8" oninput="showValue(this.id, this.value)" /><span id="pointRspan">0.8</span></td>
<td>G: <input type="range" min="0" max="1" step="0.01" id="pointG" value="0.8" oninput="showValue(this.id, this.value)" /><span id="pointGspan">0.8</span></td>
<td>B: <input type="range" min="0" max="1" step="0.01" id="pointB" value="0.8" oninput="showValue(this.id, this.value)" /><span id="pointBspan">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 />
Moon texture courtesy of <a href="http://maps.jpl.nasa.gov/">the Jet Propulsion Laboratory</a>.
<br/>
<br/>
<a href="./"><- Back</a>
</div>
</body>
</html>