-
Notifications
You must be signed in to change notification settings - Fork 68
/
cloth.html
809 lines (632 loc) · 28.8 KB
/
cloth.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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
<!DOCTYPE html>
<html>
<head>
<title>PicoGL.js: Cloth</title>
<meta charset="utf-8">
<script src="utils/compatibility.js"></script>
<script src="utils/gl-matrix.js"></script>
<script src="utils/utils.js"></script>
<link rel="stylesheet" href="../site/css/picogl-example.css">
</head>
<!--
The MIT License (MIT)
Copyright (c) 2017 Tarek Sherif
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-->
<!--
Algorithm based on:
- Mosegaard's Cloth Simulation Coding Tutorial (CPU implementation)
https://viscomp.alexandra.dk/?p=147
- Cloth Simulation on the GPU by Cyril Zeller
http://http.download.nvidia.com/developer/presentations/2005/SIGGRAPH/ClothSimulationOnTheGPU.pdf
-->
<body>
<div id="example-title">
PicoGL.js Example: Cloth<BR>
(Use mouse to control ball)
<div>
<a href="https://github.com/tsherif/picogl.js/blob/master/examples/cloth.html">Source code</a>
</div>
</div>
<canvas id="gl-canvas"></canvas>
<script id="quad-vs" type="x-shader/vs">
#version 300 es
layout(location=0) in vec4 aPosition;
out vec2 vScreenUV;
void main() {
vScreenUV = aPosition.xy * 0.5 + 0.5;
gl_Position = aPosition;
}
</script>
<script id="update-force-fs" type="x-fragment-shader">
#version 300 es
precision highp float;
#define GRAVITY vec3(0.0, -0.0003, 0.0)
#define WIND vec3(0.0, 0.0, 0.00008)
#define DAMPING 0.99
in vec2 vScreenUV;
uniform sampler2D uPositionBuffer;
uniform sampler2D uNormalBuffer;
uniform sampler2D uOldPositionBuffer;
layout(location=0) out vec3 outPosition;
layout(location=1) out vec3 outOldPosition;
void main() {
ivec2 dimensions = textureSize(uPositionBuffer, 0);
ivec2 maxTexelCoord = dimensions - 1;
ivec2 texelCoord = ivec2(vScreenUV * vec2(dimensions));
vec3 position = texelFetch(uPositionBuffer, texelCoord, 0).xyz;
vec3 normal = texelFetch(uNormalBuffer, texelCoord, 0).xyz;
vec3 oldPosition = texelFetch(uOldPositionBuffer, texelCoord, 0).xyz;
vec3 temp = position;
if (texelCoord != ivec2(0, 0) && texelCoord != ivec2(maxTexelCoord.x, 0)) {
position += (position - oldPosition) * DAMPING + GRAVITY;
float wDotN = dot(WIND, normal);
position += abs(wDotN) * sign(wDotN) * normal;
}
outPosition = position;
outOldPosition = temp;
}
</script>
<script id="update-constraint-fs" type="x-fragment-shader">
#version 300 es
precision highp float;
in vec2 vScreenUV;
// uModVal and dir used to select the direction
// to look for the neighbour we're going to check
layout(std140) uniform ConstraintUniforms {
ivec2 uDir;
int uModVal;
float uRestDistance;
};
uniform sampler2D uPositionBuffer;
out vec3 outPosition;
void main() {
ivec2 dimensions = textureSize(uPositionBuffer, 0);
ivec2 texelCoord = ivec2(vScreenUV * vec2(dimensions));
vec3 position = texelFetch(uPositionBuffer, texelCoord, 0).xyz;
int iDot = abs(texelCoord.x * uDir.x) + abs(texelCoord.y * uDir.y);
int neg = iDot % 2 == uModVal ? 1 : -1;
bool otherPin = false;
if (texelCoord != ivec2(0, 0) && texelCoord != ivec2(dimensions.x - 1, 0)) {
ivec2 otherCoord = texelCoord + uDir * neg;
if (otherCoord == ivec2(0, 0) || otherCoord == ivec2(dimensions.x - 1, 0)) {
otherPin = true;
}
if (all(greaterThanEqual(otherCoord, ivec2(0, 0))) && all(lessThan(otherCoord, dimensions))) {
vec3 otherPosition = texelFetch(uPositionBuffer, otherCoord, 0).xyz;
vec3 diffVec = otherPosition - position;
float dist = length(diffVec);
if (dist > uRestDistance) {
position += diffVec * (1.0 - uRestDistance / dist) * (otherPin ? 1.0 : 0.5);
}
}
}
outPosition = position;
}
</script>
<script id="update-collision-fs" type="x-fragment-shader">
#version 300 es
precision highp float;
in vec2 vScreenUV;
layout(std140) uniform BallUniforms {
vec4 position;
float radius;
} ball;
uniform sampler2D uPositionBuffer;
out vec3 outPosition;
void main() {
vec2 dimensions = vec2(textureSize(uPositionBuffer, 0));
ivec2 texelCoord = ivec2(vScreenUV * dimensions);
vec3 position = texelFetch(uPositionBuffer, texelCoord, 0).xyz;
vec3 diff = position - ball.position.xyz;
float dist = length(diff);
if (dist < ball.radius + 0.01) {
position += (ball.radius + 0.01 - dist) * normalize(diff);
}
outPosition = position;
}
</script>
<script id="update-normal-fs" type="x-fragment-shader">
#version 300 es
precision highp float;
in vec2 vScreenUV;
uniform sampler2D uPositionBuffer;
out vec3 outNormal;
void main() {
ivec2 dimensions = textureSize(uPositionBuffer, 0);
ivec2 texelCoord = ivec2(vScreenUV * vec2(dimensions));
vec3 position = texelFetch(uPositionBuffer, texelCoord, 0).xyz;
vec3 normal = vec3(0.0);
if (texelCoord.x > 0) {
vec3 left = texelFetch(uPositionBuffer, texelCoord - ivec2(1, 0), 0).xyz;
if (texelCoord.y > 0) {
vec3 down = texelFetch(uPositionBuffer, texelCoord - ivec2(0, 1), 0).xyz;
normal += normalize(cross(left - position, down - position));
}
if (texelCoord.y < dimensions.y - 1) {
vec3 up = texelFetch(uPositionBuffer, texelCoord + ivec2(0, 1), 0).xyz;
normal += normalize(cross(up - position, left - position));
}
}
if (texelCoord.x < dimensions.x - 1) {
vec3 right = texelFetch(uPositionBuffer, texelCoord + ivec2(1, 0), 0).xyz;
if (texelCoord.y > 0) {
vec3 down = texelFetch(uPositionBuffer, texelCoord - ivec2(0, 1), 0).xyz;
normal += normalize(cross(down - position, right - position));
}
if (texelCoord.y < dimensions.y - 1) {
vec3 up = texelFetch(uPositionBuffer, texelCoord + ivec2(0, 1), 0).xyz;
normal += normalize(cross(right - position, up - position));
}
}
outNormal = normalize(normal);
}
</script>
<script id="ball-vs" type="x-vertex-shader">
#version 300 es
layout(location=0) in vec3 aPosition;
layout(location=1) in vec3 aNormal;
layout(std140) uniform;
uniform BallUniforms {
vec4 position;
float radius;
} ball;
uniform SceneUniforms {
mat4 viewProj;
vec4 lightPosition;
};
out vec3 vPosition;
out vec2 vUV;
out vec3 vNormal;
void main() {
vPosition = aPosition + ball.position.xyz;
vUV = vec2(0.0, 0.0);
vNormal = aNormal;
gl_Position = viewProj * vec4(vPosition, 1.0);
}
</script>
<script id="cloth-vs" type="x-vertex-shader">
#version 300 es
layout(location=0) in ivec2 aTexelCoord;
layout(location=1) in vec2 aUV;
uniform sampler2D uPositionBuffer;
uniform sampler2D uNormalBuffer;
layout(std140, column_major) uniform SceneUniforms {
mat4 viewProj;
vec4 lightPosition;
};
out vec3 vPosition;
out vec2 vUV;
out vec3 vNormal;
void main() {
vec3 position = texelFetch(uPositionBuffer, aTexelCoord, 0).xyz;
vPosition = position;
vNormal = texelFetch(uNormalBuffer, aTexelCoord, 0).xyz;
vUV = aUV;
gl_PointSize = 4.0;
gl_Position = viewProj * vec4(position, 1.0);
}
</script>
<script id="phong-fs" type="x-fragment-shader">
#version 300 es
precision highp float;
layout(std140, column_major) uniform SceneUniforms {
mat4 viewProj;
vec4 lightPosition;
};
uniform sampler2D uDiffuse;
in vec3 vPosition;
in vec2 vUV;
in vec3 vNormal;
out vec4 fragColor;
void main() {
vec3 color = texture(uDiffuse, vUV).rgb;
vec3 normal = normalize(vNormal);
vec3 lightVec = -normalize(vPosition - lightPosition.xyz);
float diffuse = abs(dot(lightVec, normal));
float ambient = 0.1;
fragColor = vec4(color * (diffuse + ambient), 1.0);
}
</script>
<script type="module">
import { PicoGL } from "../src/picogl.js";
utils.addTimerElement();
utils.instrumentAnimationFrame();
if (!testExtension("EXT_color_buffer_float")) {
document.body.innerHTML = "This example requires extension <b>EXT_color_buffer_float</b> which is not supported on this system."
}
let canvas = document.getElementById("gl-canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
let app = PicoGL.createApp(canvas)
.clearColor(0.0, 0.0, 0.0, 1.0)
.enable(PicoGL.DEPTH_TEST);
let timer = app.createTimer();
const CONSTRAINT_ITERATIONS = 20;
const DATA_TEXTURE_DIM = 60;
const NUM_PARTICLES = DATA_TEXTURE_DIM * DATA_TEXTURE_DIM;
const STRUCTURAL_REST = 1 / DATA_TEXTURE_DIM;
const SHEAR_REST = Math.sqrt(2 * STRUCTURAL_REST * STRUCTURAL_REST);
const BALL_RADIUS = 0.15;
const BALL_RANGE = 0.9;
let ballSpeed = 0.004;
///////////////////
// PROGRAMS
///////////////////
// Generic quad vertex shader
let quadVsSource = document.getElementById("quad-vs").text;
let quadShader = app.createShader(PicoGL.VERTEX_SHADER, quadVsSource);
// Update wind and gravity forces
let updateForceFsSource = document.getElementById("update-force-fs").text;
// Apply structural and shear constraints
let updateConstraintFsSource = document.getElementById("update-constraint-fs").text;
// Check for collision with ball
let updateCollisionFsSource = document.getElementById("update-collision-fs").text;
// Calculate normals
let updateNormalFsSource = document.getElementById("update-normal-fs").text;
// Generic phong shader used for drawing
let phongSource = document.getElementById("phong-fs").text;
let phongShader = app.createShader(PicoGL.FRAGMENT_SHADER, phongSource);
// Draw ball
let ballVsSource = document.getElementById("ball-vs").text;
// Draw cloth
let clothVsSource = document.getElementById("cloth-vs").text;
////////////////////
// FRAME BUFFERS
////////////////////
// Store results of force update
let forceTarget1 = app.createTexture2D(DATA_TEXTURE_DIM, DATA_TEXTURE_DIM, {
internalFormat: PicoGL.RGBA32F
});
let forceTarget2 = app.createTexture2D(DATA_TEXTURE_DIM, DATA_TEXTURE_DIM, {
internalFormat: PicoGL.RGBA32F
});
let updateForceFramebuffer = app.createFramebuffer(DATA_TEXTURE_DIM, DATA_TEXTURE_DIM)
.colorTarget(0, forceTarget1)
.colorTarget(1, forceTarget2);
// Results of constraint satisfaction passes
let updateTarget = app.createTexture2D(DATA_TEXTURE_DIM, DATA_TEXTURE_DIM, {
internalFormat: PicoGL.RGBA32F
});
let updateFramebuffer = app.createFramebuffer(DATA_TEXTURE_DIM, DATA_TEXTURE_DIM)
.colorTarget(0, updateTarget);
///////////////////////////
// CLOTH GEOMETRY DATA
///////////////////////////
let clothPositionData = new Float32Array(NUM_PARTICLES * 4);
let clothNormalData = new Float32Array(NUM_PARTICLES * 4);
let uvData = new Float32Array(NUM_PARTICLES * 2);
let dataTextureIndex = new Int16Array(NUM_PARTICLES * 2);
let indexData = new Uint16Array((DATA_TEXTURE_DIM - 1) * (DATA_TEXTURE_DIM - 1) * 6);
let indexI = 0;
for (let i = 0; i < NUM_PARTICLES; ++i) {
let vec4i = i * 4;
let vec2i = i * 2;
let x = (i % DATA_TEXTURE_DIM);
let y = Math.floor(i / DATA_TEXTURE_DIM);
let u = x / DATA_TEXTURE_DIM;
let v = y / DATA_TEXTURE_DIM;
clothPositionData[vec4i] = u - 0.5;
clothPositionData[vec4i + 1] = v + 0.8;
clothNormalData[vec4i + 2] = 1;
uvData[vec2i] = u;
uvData[vec2i + 1] = v;
dataTextureIndex[vec2i] = (i % DATA_TEXTURE_DIM);
dataTextureIndex[vec2i + 1] = Math.floor(i / DATA_TEXTURE_DIM);
if (x < DATA_TEXTURE_DIM - 1 && y < DATA_TEXTURE_DIM - 1) {
indexData[indexI] = i;
indexData[indexI + 1] = i + DATA_TEXTURE_DIM;
indexData[indexI + 2] = i + DATA_TEXTURE_DIM + 1;
indexData[indexI + 3] = i;
indexData[indexI + 4] = i + DATA_TEXTURE_DIM + 1;
indexData[indexI + 5] = i + 1;
indexI += 6;
}
}
///////////////////////////
// SIM DATA TEXTURES
///////////////////////////
let positionTextureA = app.createTexture2D(clothPositionData, DATA_TEXTURE_DIM, DATA_TEXTURE_DIM, {
internalFormat: PicoGL.RGBA32F,
minFilter: PicoGL.NEAREST,
magFilter: PicoGL.NEAREST,
wrapS: PicoGL.CLAMP_TO_EDGE,
wrapT: PicoGL.CLAMP_TO_EDGE
});
let oldPositionTextureA = app.createTexture2D(clothPositionData, DATA_TEXTURE_DIM, DATA_TEXTURE_DIM, {
internalFormat: PicoGL.RGBA32F,
minFilter: PicoGL.NEAREST,
magFilter: PicoGL.NEAREST,
wrapS: PicoGL.CLAMP_TO_EDGE,
wrapT: PicoGL.CLAMP_TO_EDGE
});
let positionTextureB = updateForceFramebuffer.colorAttachments[0];
let oldPositionTextureB = updateForceFramebuffer.colorAttachments[1];
let normalTexture = app.createTexture2D(clothNormalData, DATA_TEXTURE_DIM, DATA_TEXTURE_DIM, {
internalFormat: PicoGL.RGBA32F,
minFilter: PicoGL.NEAREST,
magFilter: PicoGL.NEAREST,
wrapS: PicoGL.CLAMP_TO_EDGE,
wrapT: PicoGL.CLAMP_TO_EDGE
});
/////////////////////////
// GEOMETRY FOR DRAWING
/////////////////////////
// Quad for simulation passes
let quadPositions = app.createVertexBuffer(PicoGL.FLOAT, 2, new Float32Array([
-1, 1,
-1, -1,
1, -1,
-1, 1,
1, -1,
1, 1,
]));
let quadArray = app.createVertexArray()
.vertexAttributeBuffer(0, quadPositions);
// Cloth geometry for drawing
let dataIndex = app.createVertexBuffer(PicoGL.SHORT, 2, dataTextureIndex);
let uv = app.createVertexBuffer(PicoGL.FLOAT, 2, uvData);
let indices = app.createIndexBuffer(PicoGL.UNSIGNED_SHORT, indexData);
let clothArray = app.createVertexArray()
.vertexAttributeBuffer(0, dataIndex)
.vertexAttributeBuffer(1, uv)
.indexBuffer(indices);
// Ball geometry
let ballGeo = utils.createSphere({radius: BALL_RADIUS});
let ballPositions = app.createVertexBuffer(PicoGL.FLOAT, 3, ballGeo.positions);
let ballNormals = app.createVertexBuffer(PicoGL.FLOAT, 3, ballGeo.normals);
let ballIndices = app.createIndexBuffer(PicoGL.UNSIGNED_SHORT, ballGeo.indices);
let ballArray = app.createVertexArray()
.vertexAttributeBuffer(0, ballPositions)
.vertexAttributeBuffer(1, ballNormals)
.indexBuffer(ballIndices);
////////////////
// UNIFORMS
////////////////
// Constraint uniforms
let updateHorizontal1Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([1, 0]))
.set(1, 0)
.set(2, STRUCTURAL_REST)
.update();
let updateHorizontal2Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([1, 0]))
.set(1, 1)
.set(2, STRUCTURAL_REST)
.update();
let updateVertical1Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([0, 1]))
.set(1, 0)
.set(2, STRUCTURAL_REST)
.update();
let updateVertical2Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([0, 1]))
.set(1, 1)
.set(2, STRUCTURAL_REST)
.update();
let updateShear1Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([1, 1]))
.set(1, 0)
.set(2, SHEAR_REST)
.update();
let updateShear2Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([1, 1]))
.set(1, 1)
.set(2, SHEAR_REST)
.update();
let updateShear3Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([1, -1]))
.set(1, 0)
.set(2, SHEAR_REST)
.update();
let updateShear4Uniforms = app.createUniformBuffer([
PicoGL.INT_VEC2,
PicoGL.INT,
PicoGL.FLOAT
])
.set(0, new Int32Array([1, -1]))
.set(1, 1)
.set(2, SHEAR_REST)
.update();
// Draw uniforms
let ballPosition = vec3.fromValues(0, 0.15, -0.8);
let ballUniforms = app.createUniformBuffer([
PicoGL.FLOAT_VEC4,
PicoGL.FLOAT
])
.set(0, ballPosition)
.set(1, BALL_RADIUS)
.update();
let ballColor = new Uint8Array([255, 20, 20]);
let ballTexture = app.createTexture2D(ballColor, 1, 1, { internalFormat: PicoGL.RGB8 });
let projMatrix = mat4.create();
mat4.perspective(projMatrix, Math.PI / 2, canvas.width / canvas.height, 0.1, 3.0);
let viewMatrix = mat4.create();
let eyePosition = vec3.fromValues(0.5, 0.7, 1.2);
mat4.lookAt(viewMatrix, eyePosition, vec3.fromValues(0, 0.1, 0), vec3.fromValues(0, 1, 0));
let viewProjMatrix = mat4.create();
mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);
let lightPosition = vec3.fromValues(1, 1, 1);
let sceneUniformBuffer = app.createUniformBuffer([
PicoGL.FLOAT_MAT4,
PicoGL.FLOAT_VEC4
])
.set(0, viewProjMatrix)
.set(1, lightPosition)
.update();
window.onresize = function() {
app.resize(window.innerWidth, window.innerHeight);
mat4.perspective(projMatrix, Math.PI / 2, app.width / app.height, 0.1, 3.0);
mat4.multiply(viewProjMatrix, projMatrix, viewMatrix);
sceneUniformBuffer.set(0, viewProjMatrix).update();
};
let targetZ = null;
let targetY = null;
canvas.addEventListener("mousemove", function(event) {
targetZ = -((event.clientX / canvas.width) * 2 - 1);
targetY = ((canvas.height - event.clientY) / canvas.height) * 2 - 1;
});
Promise.all([
app.createPrograms(
[quadShader, updateForceFsSource],
[quadShader, updateConstraintFsSource],
[quadShader, updateCollisionFsSource],
[quadShader, updateNormalFsSource],
[ballVsSource, phongShader],
[clothVsSource, phongShader]
),
utils.loadImages(["img/webgl-logo.png"])
]).then(([
[updateForceProgram, updateConstraintProgram, updateCollisionProgram, updateNormalProgram, ballProgram, clothProgram],
[image]
]) => {
let texture = app.createTexture2D(image, {
maxAnisotropy: PicoGL.WEBGL_INFO.MAX_TEXTURE_ANISOTROPY
});
///////////////
// DRAW CALLS
///////////////
// Update forces
let updateForceDrawCall = app.createDrawCall(updateForceProgram, quadArray)
.texture("uPositionBuffer", positionTextureA)
.texture("uNormalBuffer", normalTexture);
// Structural constraints
let updateHorizontal1DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateHorizontal1Uniforms);
let updateHorizontal2DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateHorizontal2Uniforms);
let updateVertical1DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateVertical1Uniforms);
let updateVertical2DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateVertical2Uniforms);
// Shear constraints
let updateShear1DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateShear1Uniforms);
let updateShear2DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateShear2Uniforms);
let updateShear3DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateShear3Uniforms);
let updateShear4DrawCall = app.createDrawCall(updateConstraintProgram, quadArray)
.uniformBlock("ConstraintUniforms", updateShear4Uniforms);
let updateCollisionDrawCall = app.createDrawCall(updateCollisionProgram, quadArray)
.uniformBlock("BallUniforms", ballUniforms);
let updateNormalDrawCall = app.createDrawCall(updateNormalProgram, quadArray);
let clothDrawCall = app.createDrawCall(clothProgram, clothArray)
.uniformBlock("SceneUniforms", sceneUniformBuffer)
.texture("uDiffuse", texture)
.texture("uNormalBuffer", normalTexture);
let ballDrawCall = app.createDrawCall(ballProgram, ballArray)
.uniformBlock("SceneUniforms", sceneUniformBuffer)
.uniformBlock("BallUniforms", ballUniforms)
.texture("uDiffuse", ballTexture);
/////////
// DRAW
/////////
function draw() {
if (timer.ready()) {
utils.updateTimerElement(timer.cpuTime, timer.gpuTime);
}
timer.start();
updateForceDrawCall.texture("uPositionBuffer", positionTextureA);
updateForceDrawCall.texture("uOldPositionBuffer", oldPositionTextureA);
updateForceFramebuffer.colorTarget(0, positionTextureB);
updateForceFramebuffer.colorTarget(1, oldPositionTextureB);
app.viewport(0, 0, DATA_TEXTURE_DIM, DATA_TEXTURE_DIM);
app.drawFramebuffer(updateForceFramebuffer);
updateForceDrawCall.draw();
for (let i = 0; i < CONSTRAINT_ITERATIONS; ++i) {
app.drawFramebuffer(updateFramebuffer);
updateFramebuffer.colorTarget(0, positionTextureA);
updateHorizontal1DrawCall.texture("uPositionBuffer", positionTextureB).draw();
updateFramebuffer.colorTarget(0, positionTextureB);
updateHorizontal2DrawCall.texture("uPositionBuffer", positionTextureA).draw();
updateFramebuffer.colorTarget(0, positionTextureA);
updateVertical1DrawCall.texture("uPositionBuffer", positionTextureB).draw();
updateFramebuffer.colorTarget(0, positionTextureB);
updateVertical2DrawCall.texture("uPositionBuffer", positionTextureA).draw();
updateFramebuffer.colorTarget(0, positionTextureA);
updateShear1DrawCall.texture("uPositionBuffer", positionTextureB).draw();
updateFramebuffer.colorTarget(0, positionTextureB);
updateShear2DrawCall.texture("uPositionBuffer", positionTextureA).draw();
updateFramebuffer.colorTarget(0, positionTextureA);
updateShear3DrawCall.texture("uPositionBuffer", positionTextureB).draw();
updateFramebuffer.colorTarget(0, positionTextureB);
updateShear4DrawCall.texture("uPositionBuffer", positionTextureA).draw();
}
if (targetZ === null) {
ballPosition[2] += ballSpeed;
if (ballPosition[2] > BALL_RANGE || ballPosition[2] < -BALL_RANGE) {
ballSpeed *= -1;
}
ballUniforms.set(0, ballPosition).update();
} else {
let zDiff = targetZ - ballPosition[2];
let yDiff = targetY - ballPosition[1];
if (Math.abs(zDiff) > 0.001 || Math.abs(yDiff) > 0.001) {
ballPosition[2] += zDiff * 0.02;
ballPosition[1] += yDiff * 0.02;
ballUniforms.set(0, ballPosition).update();
}
}
updateFramebuffer.colorTarget(0, positionTextureA);
updateCollisionDrawCall.texture("uPositionBuffer", positionTextureB).draw();
updateFramebuffer.colorTarget(0, normalTexture);
updateNormalDrawCall.texture("uPositionBuffer", positionTextureA).draw();
clothDrawCall.texture("uPositionBuffer", positionTextureA);
app.defaultViewport().defaultDrawFramebuffer().clear();
clothDrawCall.draw();
ballDrawCall.draw();
let temp = oldPositionTextureA;
oldPositionTextureA = oldPositionTextureB;
oldPositionTextureB = temp;
timer.end();
requestAnimationFrame(draw);
}
requestAnimationFrame(draw);
});
</script>
<a href="https://github.com/tsherif/picogl.js" id="github-ribbon"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/365986a132ccd6a44c23a9169022c0b5c890c387/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f7265645f6161303030302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png"></a>
<script src="../site/js/iframe.js"></script>
</body>
</html>