-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
258 lines (230 loc) · 10.5 KB
/
index.html
File metadata and controls
258 lines (230 loc) · 10.5 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<!DOCTYPE html>
<html>
<head>
<title>WebGL basics</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<!-- Jquery, Bootstrap and FontAwesome -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css"
integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS"
crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"
integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"
integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k"
crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<!-- WebGL libs -->
<script src="js/libs/gl-matrix/gl-matrix.js"></script>
<!-- FONT -->
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Ubuntu" />
<!-- Our code -->
<script src="js/glDrawable.js"></script>
<script src="js/uiController.js"></script>
<script src="js/glController.js"></script>
<link rel="stylesheet" href="css/style.css">
<!-- Shaders -->
<script id="vertex-shader" type="x-shader/x-vertex">
#version 100
precision mediump float;
uniform mat4 PVM;
uniform mat4 M_INVERSE_TRANSPOSE;
uniform mat4 M;
uniform float texture_mode;
uniform float texture_axis;
uniform vec3 b_max;
uniform vec3 b_min;
uniform vec3 d;
attribute vec3 vertex;
attribute vec3 normal;
attribute vec2 uv;
varying vec2 texture_coordinates;
varying vec3 normal_v;
varying vec3 vert_pos;
void main(){
// Calculate uvs based on mode
texture_coordinates = uv; // default
// Plane
if(texture_mode == 1.0)
{
// X axis
if(texture_axis == 0.0)
{
texture_coordinates[0] = (vertex[2] - b_min[2]) / d[2];
texture_coordinates[1] = (vertex[1] - b_min[1]) / d[1];
}
// Y axis
else if(texture_axis == 1.0)
{
texture_coordinates[0] = (vertex[0] - b_min[0]) / d[0];
texture_coordinates[1] = (vertex[2] - b_min[2]) / d[2];
}
// Z axis
else if(texture_axis == 2.0)
{
texture_coordinates[0] = (vertex[0] - b_min[0]) / d[0];
texture_coordinates[1] = (vertex[1] - b_min[1]) / d[1];
}
}
// Cylindrical
else if(texture_mode == 2.0)
{
vec3 center = b_min + d/2.0;
vec3 v = vertex - center;
float angle = 0.0;
// X axis
if(texture_axis == 0.0)
{
angle = degrees(atan(v[1] , v[2])) + 180.0;
texture_coordinates[1] = v[0]/d[0] + 1.0/2.0;
}
// Y axis
else if(texture_axis == 1.0)
{
angle = degrees(atan(v[2] , v[0])) + 180.0;
texture_coordinates[1] = v[1]/d[1] + 1.0/2.0;
}
// Z axis
else if(texture_axis == 2.0)
{
angle = degrees(atan(v[1] , v[0])) + 180.0;
texture_coordinates[1] = v[2]/d[2] + 1.0/2.0;
}
texture_coordinates[0] = angle / 360.0;
}
// Spherical
else if(texture_mode == 3.0)
{
vec3 center = b_min + d/2.0;
vec3 v = vertex - center;
float angle_u = 0.0;
float angle_v = 0.0;
// X axis
if(texture_axis == 0.0)
{
angle_u = degrees(atan(v[1] , v[2])) + 180.0;
angle_v = degrees(asin(v[0]/length(v)));
}
// Y axis
else if(texture_axis == 1.0)
{
angle_u = degrees(atan(v[2] , v[0])) + 180.0;
angle_v = degrees(asin(v[1]/length(v)));
}
// Z axis
else if(texture_axis == 2.0)
{
angle_u = degrees(atan(v[1] , v[0])) + 180.0;
angle_v = degrees(asin(v[2]/length(v)));
}
texture_coordinates[0] = angle_u / 360.0;
texture_coordinates[1] = angle_v / 180.0 + 1.0/2.0;
}
// Set vertex position
gl_Position= PVM * vec4(vertex,1);
// Compute normal
normal_v = vec3(M_INVERSE_TRANSPOSE * vec4(normal, 0.0));
// Compute the world position of the surface
vec4 vertex4 = M * vec4(vertex, 1.0);
vert_pos = vec3(vertex4) / vertex4.w;
}
</script>
<script id="fragment-shader" type="x-shader/x-fragment">
#version 100
#extension GL_EXT_shader_texture_lod : enable
#extension GL_OES_standard_derivatives : enable
precision mediump float;
varying vec3 normal_v; // Surface normal
varying vec3 vert_pos; // Vertex position
varying vec2 texture_coordinates;
uniform float KA; // Ambient reflection coefficient
uniform float KD; // Diffuse reflection coefficient
uniform float KS; // Specular reflection coefficient
// Material color
uniform vec3 ambient_color;
uniform vec3 diffuse_color;
uniform vec3 specular_color;
uniform vec3 LIGHT; // Light position
uniform sampler2D sampler;
uniform sampler2D normal_sampler;
vec3 BumpMappingFromHeight(vec3 n, float b, vec3 p){
float bu=dFdx(b);
float bv=dFdy(b);
vec3 su=dFdx(p);
vec3 sv=dFdy(p);
vec3 d=bu*normalize(cross(n,sv)) + bv*normalize(cross(su,n));
return normalize(n+d);
}
void main() {
// Calculate new normal from normal and normal map
vec4 normal_texture = texture2D(normal_sampler, texture_coordinates);
float b = length(normal_texture.xyz);
vec3 new_normal = BumpMappingFromHeight(normal_v, b, vert_pos);
vec3 N = normalize(new_normal);
vec3 L = normalize(LIGHT - vert_pos);
// Lambert's cosine law
float lambertian = max(dot(N, L), 0.0);
float specular = 0.0;
if(lambertian > 0.0) {
vec3 R = reflect(-L, N); // Reflected light vector
vec3 V = normalize(-vert_pos); // Vector to viewer
// Compute the specular term
float specAngle = max(dot(R, V), 0.0);
specular = pow(specAngle, 1.0);
}
vec3 light_weight = KA * ambient_color +
KD * lambertian * diffuse_color +
KS * specular * specular_color;
vec4 texture = texture2D(sampler, texture_coordinates);
// modulate texture color with light_weigthing and write as final color
gl_FragColor = vec4(light_weight.rgb * texture.rgb, texture.a);
}
</script>
</head>
<body>
<div id="app" class="container">
<div class="row">
<canvas id="gl_canvas" class="col">
Your web browser does not support Canvas.
</canvas>
</div>
<div id="base_instructions" class="row">
<div id="camera_instructions" class="col">
<h3>Camera</h3>
<p>
<b>Move:</b> W A S D + Q and E<br>
<b>Rotate:</b> I J K L + U and O<br>
<b>Perspective change:</b> Space<br>
</p>
</div>
<div class="col form-group">
<h3> Light coordinates </h3>
<label for="x_pos">X: </label>
<input class="form-control-static" id="x_pos" type=number step=0.01 onchange="captureLightChange()"/>
<br>
<label for="y_pos">Y: </label>
<input class="form-control-static" id="y_pos" type=number step=0.01 onchange="captureLightChange()"/>
<br>
<label for="z_pos">Z: </label>
<input class="form-control-static" id="z_pos" type=number step=0.01 onchange="captureLightChange()"/>
</div>
<div id="button_box" class="col">
<button class="btn btn-primary" style="margin: 5px; float: right"
onclick="selectObj()">
<span class="fa fa-plus"></span>
</button>
<button class="btn btn-warning" style="margin: 5px; float: right"
onclick="location.reload();">
<span class="fa fa-refresh"></span>
</button>
</div>
</div>
</div>
<div id="object_container" class="container">
</div>
</body>
</html>