-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
603 lines (507 loc) · 20.9 KB
/
Copy pathindex.html
File metadata and controls
603 lines (507 loc) · 20.9 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shading Visualization using WebGL</title>
<link rel="icon" type="image/jpg" sizes="32x32" href="asset/stanford_bunny.jpg">
<link href='css/style.css' type='text/css' rel='stylesheet' />
<link href='css/colorpicker.css' type='text/css' rel='stylesheet'/>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.13.2/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/theme/abbott.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/codemirror.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/codemirror.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.5/mode/clike/clike.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/gl-matrix@3.4.3/gl-matrix-min.js"></script>
<script src="js/renderer.js"></script>
<script src="js/colorpicker.js"></script>
<script>
var renderer = null;
var interval = null;
var editorVert, editorFrag;
function initEditors() {
editorVert = CodeMirror(document.getElementById("code_vert"), {
value: document.getElementById("GouraudVert").value.trim(),
mode: "x-shader/x-vertex",
theme: "abbott",
lineNumbers: true,
tabSize: 2,
indentUnit: 2
});
editorVert.setSize(425, 650);
editorFrag = CodeMirror(document.getElementById("code_frag"), {
value: document.getElementById("GouraudFrag").value.trim(),
mode: "x-shader/x-vertex",
theme: "abbott",
lineNumbers: true,
tabSize: 2,
indentUnit: 2
});
editorFrag.setSize(420, 650);
}
function run() {
var id = document.getElementById("select_example_id").value;
var vertexSrc = document.getElementById("GouraudVert").value;
var fragmentSrc = document.getElementById("GouraudFrag").value;
initEditors();
editorVert.setValue(vertexSrc);
editorFrag.setValue(fragmentSrc);
renderer = new Renderer("canvas", vertexSrc, fragmentSrc);
renderer.t = 0;
renderer.init();
if (!interval) {
interval = setInterval(timerFunc, 40);
}
}
function updateRenderer() {
var vertexSrc = editorVert.getValue();
var fragmentSrc = editorFrag.getValue();
if (renderer) {
renderer.updateShader(vertexSrc, fragmentSrc);
renderer.t = 0;
renderer.init(); // Reinitialize the renderer with new shaders
} else {
console.error("Renderer is not initialized.");
}
}
function timerFunc() {
++renderer.t;
renderer.display();
}
function exampleChanged() {
var d = parseInt(document.getElementById("select_example_id").value);
switch(d)
{
default:
case 0:
editorFrag.setValue(document.getElementById("GouraudFrag").value.trim());
editorVert.setValue(document.getElementById("GouraudVert").value.trim());
break;
case 1:
editorFrag.setValue(document.getElementById("PhongFrag").value.trim());
editorVert.setValue(document.getElementById("PhongVert").value.trim());
break;
case 2:
editorFrag.setValue(document.getElementById("FlatFrag").value.trim());
editorVert.setValue(document.getElementById("FlatVert").value.trim());
break;
}
updateRenderer();
}
function modelChanged() {
var d = document.getElementById("select_id2").value;
renderer.updateModel(d);
renderer.display();
}
function modeChanged() {
var d = document.getElementById("select_id").value;
renderer.modeVal = d;
}
$( function() { $('#slider-ka').slider({value:1, max:1, step:0.01, range:"min", slide:updateLightAmbientTerm}); } );
$( function() { $('#slider-kd').slider({value:1, max:1, step:0.01, range:"min", slide:updateLightDiffuseTerm}); } );
$( function() { $('#slider-ks').slider({value:1, max:1, step:0.01, range:"min", slide:updateLightSpecularTerm}); } );
$( function() { $('#slider-s').slider({value:80, min:1, max:128, step:1, range:"min", slide:updateShininess});});
$( function() { $('#slider-x').slider({value:1, min:-10, max:10, step:0.1, slide:updateLight}); });
$( function() { $('#slider-y').slider({value:1, min:-10, max:10, step:0.1, slide:updateLight});});
$( function() { $('#slider-z').slider({value:-1, min:-10, max:10, step:0.1, slide:updateLight})});
function updateLightAmbientTerm(event, ui){
renderer.kaVal = ui.value;
$('#slider-ka-value').html(ui.value);
}
function updateLightDiffuseTerm(event, ui){
renderer.kdVal = ui.value;
$('#slider-kd-value').html(ui.value);
}
function updateLightSpecularTerm(event, ui){
renderer.ksVal = ui.value;
$('#slider-ks-value').html(ui.value);
}
function updateShininess(event, ui){
renderer.shininess = ui.value;
$('#slider-s-value').html(ui.value);
}
function updateLight(event, ui) {
// Update the value for the slider that triggered the event
if (event.target.id === 'slider-x') $('#slider-x-value').html(ui.value);
if (event.target.id === 'slider-y') $('#slider-y-value').html(ui.value);
if (event.target.id === 'slider-z') $('#slider-z-value').html(ui.value);
// Read all current values just once
renderer.lightPos = [
$('#slider-x').slider("value"),
$('#slider-y').slider("value"),
$('#slider-z').slider("value")
];
}
$( function() { $('#colorSelectorAmbient').ColorPicker({
onSubmit: function(hsb, hex, rgb, el) {
$(el).val(hex);
$(el).ColorPickerHide();
},
color: '#341900',
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
$('#colorSelectorAmbient div').css('backgroundColor', '#' + hex);
renderer.ambientColor = [rgb.r/256, rgb.g/256, rgb.b/256];
},
onBeforeShow: function (colpkr) {
$(colpkr).ColorPickerSetColor('rgb(0.2,0.1,0.0)');
}
}) });
$( function() { $('#colorSelectorDiffuse').ColorPicker({
onSubmit: function(hsb, hex, rgb, el) {
$(el).val(hex);
$(el).ColorPickerHide();
},
color: '#cc6600',
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
$('#colorSelectorDiffuse div').css('backgroundColor', '#' + hex);
renderer.diffuseColor = [rgb.r/256,rgb.g/256,rgb.b/256];
},
onBeforeShow: function (colpkr) {
$(colpkr).ColorPickerSetColor('rgb(0.8,0.4,0.0)');
}
}) });
$( function() { $('#colorSelectorSpecular').ColorPicker({
onSubmit: function(hsb, hex, rgb, el) {
$(el).val(hex);
$(el).ColorPickerHide();
},
color: '#ffffff',
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
$('#colorSelectorSpecular div').css('backgroundColor', '#' + hex);
renderer.specularColor = [rgb.r/256,rgb.g/256,rgb.b/256];
},
onBeforeShow: function (colpkr) {
$(colpkr).ColorPickerSetColor('rgb(1.0,1.0,1.0)');
}
}) });
$( function() { $('#colorSelectorBg').ColorPicker({
onSubmit: function(hsb, hex, rgb, el) {
$(el).val(hex);
$(el).ColorPickerHide();
},
color: '#0065b3',
onShow: function (colpkr) {
$(colpkr).fadeIn(500);
return false;
},
onHide: function (colpkr) {
$(colpkr).fadeOut(500);
return false;
},
onChange: function (hsb, hex, rgb) {
$('#colorSelectorBg div').css('backgroundColor', '#' + hex);
renderer.clearColor = [rgb.r/256,rgb.g/256,rgb.b/256];
},
onBeforeShow: function (colpkr) {
$(colpkr).ColorPickerSetColor('rgb(0.0,0.4,0.7)');
}
}) });
</script>
</div>
</head>
<body onload="run()">
<p id="code_vert_error"></p>
<p id="code_frag_error"></p>
<table>
<tbody>
<tr style="vertical-align:top;">
<td>
<canvas id="canvas" width="600" height="400" style="padding-top:20px;">Your browser does not support the canvas element</canvas><br>
<select onchange="exampleChanged()" id="select_example_id">>
<option value="0">Gouraud Shading</option>
<option value="1">Phong Shading</option>
<option value="2">Flat Shading</option>
</select>
<select onchange="modelChanged()" id="select_id2">
<option value="./obj/teapot.txt">Teapot</option>
<option value="./obj/sphere.txt">Sphere</option>
</select>
<select onchange="modeChanged()" id="select_id">
<option value="0">normal mode</option>
<option value="1">ambient only</option>
<option value="2">diffuse only</option>
<option value="3">specular only</option>
</select>
<button onclick="updateRenderer()">Reload Shader Code</button>
<table>
<tr>
<td >Ambient Reflection (ka):</td>
<td id='slider-ka-value' style='width:30px'>1</td>
<td><div id='slider-ka'style='width: 150px'></div></td>
<td style='padding-left: 20px;'>Ambient color:</td>
<td colspan='2'>
<div id='colorSelectorAmbient' class='colorSelector'>
<div style='background-color:rgb(52,26,0)'>
</div>
</div>
</td>
</tr>
<tr>
<td>Diffuse Reflection (kd):</td>
<td id='slider-kd-value' style='width:30px'>1</td>
<td><div id='slider-kd'style='width: 150px'></div></td>
<td style='padding-left: 20px;'>Diffuse color:</td>
<td colspan='2'>
<div id='colorSelectorDiffuse' class='colorSelector'>
<div style='background-color:rgb(204,102,0)'>
</div>
</div>
</td>
</tr>
<tr>
<td>Specular Reflection (ks):</td>
<td id='slider-ks-value' style='width:30px'>1</td>
<td><div id='slider-ks'style='width: 150px'></div></td>
<td style='padding-left: 20px;'>Specular color:</td>
<td ><div id='colorSelectorSpecular' class='colorSelector'>
<div style='background-color:rgb(256,256,256)'>
</div>
</div>
</td>
</tr>
<tr>
<td>Shininess:</td>
<td id='slider-s-value' style='width:30px'>80</td>
<td><div id='slider-s'style='width: 150px'></div></td>
<td style='padding-left: 20px;'>Background Color:</td>
<td colspan='2'>
<div id='colorSelectorBg' class='colorSelector'>
<div style='background-color:rgb(0,0,0)'>
</div></div></td>
</tr>
<tr>
<td>Light Position X:</td>
<td id="slider-x-value" style='width:30px'>1</td>
<td><div id='slider-x'style='width: 150px'></div></td>
</tr>
<tr>
<td>Light Position Y:</td>
<td id="slider-y-value" style='width:30px'>1</td>
<td><div id='slider-y'style='width: 150px'></div></td>
</tr>
<tr>
<td>Light Position Z:</td>
<td id="slider-z-value" style='width:30px'>-1</td>
<td><div id='slider-z'style='width: 150px'></div></td>
</tr>
</table>
<div class="social-links">
<a href="https://github.com/shInNei/webgl-shading" target="_blank" aria-label="GitHub">
<img src="https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/github.svg" alt="GitHub" width="24" height="24" style="filter: invert(20%);">
</a>
<a href="https://www.linkedin.com/in/nvnhn/" target="_blank" aria-label="LinkedIn">
<img src="https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/linkedin.svg" alt="LinkedIn" width="24" height="24" style="filter: invert(20%);">
</a>
<!-- <a href="https://yourportfolio.com" target="_blank" aria-label="Portfolio">
<img src="https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/webflow.svg" alt="Portfolio" width="24" height="24" style="filter: invert(20%);">
</a> -->
</div>
</td>
<td style="display: flex; gap: 10px;">
<div id="vert_area">
<h3 style="margin:0%;padding:0%;text-align: center;font-size: 100%;font-weight:bold">Vertex Shader</h3>
<div id="code_vert"></div> <!-- Changed from textarea to div -->
</div>
<div id="frag_area">
<h3 style="margin:0%;padding:0%;text-align: center;font-size: 100%;font-weight:bold">Fragment Shader</h3>
<div id="code_frag"></div> <!-- Changed from textarea to div -->
</div>
</td>
</tr>
</tbody>
</table>
<textarea id="PhongFrag" style="display:none;">
#version 300 es
precision highp float;
in vec3 normalInterp; // Surface normal
in vec3 vertPos; // Vertex position
uniform int mode;
uniform float ka; // Ambient reflection coefficient
uniform float kd; // Diffuse reflection coefficient
uniform float ks; // Specular reflection coefficient
uniform float shininess; // Shininess coefficient
uniform vec3 ambientColor; // Ambient color
uniform vec3 diffuseColor; // Diffuse color
uniform vec3 specularColor; // Specular color
uniform vec3 lightPos; // Light position
out vec4 fragColor;
void main() {
vec3 N = normalize(normalInterp);
vec3 L = normalize(lightPos - vertPos);
// 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(-vertPos); // Vector to viewer
// Compute the specular term
float specAngle = max(dot(R, V), 0.0);
specular = pow(specAngle, shininess);
}
fragColor = vec4(ka * ambientColor +
kd * lambertian * diffuseColor +
ks * specular * specularColor, 1.0);
// only ambient
if(mode == 2) fragColor = vec4(ka * ambientColor, 1.0);
// only diffuse
if(mode == 3) fragColor = vec4(kd * lambertian * diffuseColor, 1.0);
// only specular
if(mode == 4) fragColor = vec4(ks * specular * specularColor, 1.0);
}
</textarea>
<textarea id="PhongVert" style="display:none;">
#version 300 es
precision highp float;
in vec3 position;
in vec3 normal;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 normalMatrix;
out vec3 normalInterp;
out vec3 vertPos;
void main(){
vec4 viewVertVec4 = view * vec4(position, 1.0);
vertPos = vec3(viewVertVec4) / viewVertVec4.w;
normalInterp = vec3(normalMatrix * vec4(normal, 0.0));
gl_Position = projection * viewVertVec4 ;
}
</textarea>
<textarea id="GouraudVert" style="display:none;">
#version 300 es
precision highp float;
in vec3 position;
in vec3 normal;
uniform mat4 projection;
uniform mat4 view;
uniform mat4 normalMatrix;
uniform int mode;
uniform float ka; // Ambient reflection coefficient
uniform float kd; // Diffuse reflection coefficient
uniform float ks; // Specular reflection coefficient
uniform float shininess; // Shininess coefficient
uniform vec3 ambientColor; // Ambient color
uniform vec3 diffuseColor; // Diffuse color
uniform vec3 specularColor; // Specular color
uniform vec3 lightPos; // Light position
out vec4 vColor;
void main() {
// Transform position to View Space
vec4 viewVertVec4 = view * vec4(position, 1.0);
vec3 viewVertPos = vec3(viewVertVec4) / viewVertVec4.w;
// Transform normal to View Space
vec3 normalView = vec3(normalMatrix * vec4(normal, 0.0));
// Standard transformation to clip space
gl_Position = projection * viewVertVec4;
vec3 N = normalize(normalView); // The Surface Normal
vec3 L = normalize(lightPos - viewVertPos); // The Light Direction From Shading Point -> Light Source
float lambertian = max(dot(N,L), 0.0);
float specular = 0.0;
if (lambertian > 0.0) {
vec3 R = reflect(-L, N);
vec3 V = normalize(-viewVertPos);
float specAngle = max(dot(R, V), 0.0);
specular = pow(specAngle, shininess);
}
vColor = vec4(ka * ambientColor +
kd * lambertian * diffuseColor +
ks * specular * specularColor, 1.0);
// only ambient
if(mode == 1) vColor = vec4(ka * ambientColor, 1.0);
// only diffuse
if(mode == 2) vColor = vec4(kd * lambertian * diffuseColor, 1.0);
// only specular
if(mode == 3) vColor = vec4(ks * specular * specularColor, 1.0);
}
</textarea>
<textarea id ="GouraudFrag" style="display:none;">
#version 300 es
precision highp float;
in vec4 vColor;
out vec4 fragColor;
void main() {
fragColor = vColor;
}
</textarea>
<textarea id="FlatVert" style="display:none;">
#version 300 es
precision highp float;
in vec3 position;
in vec3 normal;
uniform mat4 projection;
uniform mat4 view;
out vec3 vViewPos;
void main() {
vec4 viewPos4 = view * vec4(position, 1.0);
vViewPos = viewPos4.xyz / viewPos4.w;
gl_Position = projection * viewPos4;
}
</textarea>
<textarea id="FlatFrag" style="display:none;">
#version 300 es
precision highp float;
in vec3 vViewPos;
uniform int mode;
uniform vec3 lightPos;
uniform vec3 ambientColor;
uniform vec3 diffuseColor;
uniform vec3 specularColor;
uniform float ka;
uniform float kd;
uniform float ks;
uniform float shininess;
out vec4 fragColor;
void main() {
vec3 dx = dFdx(vViewPos);
vec3 dy = dFdy(vViewPos);
vec3 N = normalize(cross(dx, dy));
if (!gl_FrontFacing) N = -N;
vec3 L = normalize(lightPos - vViewPos);
float lambertian = max(dot(N,L), 0.0);
float specular = 0.0;
if (lambertian > 0.0) {
vec3 V = normalize(-vViewPos);
vec3 R = reflect(-L, N);
float specAngle = max(dot(R, V), 0.0);
specular = pow(specAngle, shininess);
}
fragColor = vec4(
ka * ambientColor +
kd * lambertian * diffuseColor +
ks * specular * specularColor,
1.0
);
// only ambient
if(mode == 1) fragColor = vec4(ka * ambientColor, 1.0);
// only diffuse
if(mode == 2) fragColor = vec4(kd * lambertian * diffuseColor, 1.0);
// only specular
if(mode == 3) fragColor = vec4(ks * specular * specularColor, 1.0);
}
</textarea>
</body>
</html>