-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fuild_ParticleCurlNoise3D_GPGPU.html
790 lines (608 loc) · 26.4 KB
/
Fuild_ParticleCurlNoise3D_GPGPU.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fuild_ParticleCurlNoise2D_GPGPU</title>
</head>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: black;
}
.monitor {
position: fixed;
top: 0;
left: 0;
z-index: 9999;
}
.monitor button {
padding: 1rem 2rem;
}
</style>
<body>
</body>
<div id="canvas"></div>
<div class="monitor">
<button id="stop">stop delta</button>
<button id="start">start</button>
</div>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script id="frag_Pos_GPGPU" type="x-shader/x-fragment">
uniform float time;
uniform float delta;
uniform float forceMouse;
uniform sampler2D fuildMap;
uniform vec2 sizeView;
uniform bool stopDelta;
uniform bool start;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrixOrbit;
vec2 rotate(vec2 v, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return m * v;
}
float random (vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
mat4 rotationMatrix(vec3 axis, float angle) {
axis = normalize(axis);
float s = sin(angle);
float c = cos(angle);
float oc = 1.0 - c;
return mat4(oc * axis.x * axis.x + c, oc * axis.x * axis.y - axis.z * s, oc * axis.z * axis.x + axis.y * s, 0.0,
oc * axis.x * axis.y + axis.z * s, oc * axis.y * axis.y + c, oc * axis.y * axis.z - axis.x * s, 0.0,
oc * axis.z * axis.x - axis.y * s, oc * axis.y * axis.z + axis.x * s, oc * axis.z * axis.z + c, 0.0,
0.0, 0.0, 0.0, 1.0);
}
vec3 rotate(vec3 v, vec3 axis, float angle) {
mat4 m = rotationMatrix(axis, angle);
return (m * vec4(v, 1.0)).xyz;
}
#define PI 3.141592653
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 tmpPos = texture2D( texturePosition, uv );
vec3 posSelf = tmpPos.xyz;
vec3 velSelf = texture2D( textureVelocity, uv ).xyz;
vec3 extraSelf = texture2D( textureExtra, uv ).xyz;
float life = tmpPos.w;
float maxYSet = 2.;
float activeOnYSet = 1.7;
if(posSelf.y > maxYSet) {
posSelf.y = -maxYSet;
}
if (posSelf.y >= activeOnYSet && posSelf.y < maxYSet) {
life = 1.0 - ((posSelf.y - activeOnYSet) / 2.0);
} else if (posSelf.y >= maxYSet) {
life = 0.0;
//posSelf.y = -maxYSet;
} else if (posSelf.y >= -maxYSet && posSelf.y < -activeOnYSet) {
life = (posSelf.y + maxYSet) / 2.0;
}
float decreaseVel = 0.1;
if(stopDelta) decreaseVel = 0.;
posSelf += velSelf * decreaseVel;
mat4 invertGG = inverse(projectionMatrix * viewMatrix);
vec4 viewPos = vec4(posSelf,1.);
vec2 uvPos = vec2(
viewPos.x / sizeView.x + 0.5,
viewPos.y / sizeView.y + 0.5
);
vec4 projCoord = vec4(vec3(uvPos,posSelf.z), 1.0);
vec4 fluidColor = texture2DProj(fuildMap, projCoord);
vec2 velFuild = texture2D(fuildMap,uvPos).xy;
// posSelf += vec3(velFuild,0.) * 2.;
posSelf += vec3(fluidColor.xyz) * .0001;
if(posSelf.y > sizeView.y) {
posSelf.y = -sizeView.y;
}else if(posSelf.y < -sizeView.y) {
posSelf.y = -sizeView.y;
}
if(posSelf.x > 2.) {
posSelf.x = -2.;
}else if(posSelf.x < -2.) {
posSelf.x = -2.;
}
if(!start) posSelf = tmpPos.xyz;
gl_FragColor = vec4( posSelf, 1. );
}
</script>
<script id="frag_Vel_GPGPU" type="x-shader/x-fragment">
uniform float time;
uniform float delta;
uniform vec2 mouse;
uniform bool stopDelta;
uniform bool start;
vec3 mod289(vec3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 mod289(vec4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
vec4 permute(vec4 x) { return mod289(((x*34.0)+1.0)*x); }
vec4 taylorInvSqrt(vec4 r) { return 1.79284291400159 - 0.85373472095314 * r;}
float snoise(vec3 v) {
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ;
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy );
vec3 x1 = x0 - i1 + C.xxx;
vec3 x2 = x0 - i2 + C.yyy; // 2.0*C.x = 1/3 = C.y
vec3 x3 = x0 - D.yyy; // -1.0+3.0*C.x = -0.5 = -D.y
i = mod289(i);
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
float n_ = 0.142857142857; // 1.0/7.0
vec3 ns = n_ * D.wyz - D.xzx;
vec4 j = p - 49.0 * floor(p * ns.z * ns.z); // mod(p,7*7)
vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);
vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw );
vec4 s0 = floor(b0)*2.0 + 1.0;
vec4 s1 = floor(b1)*2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));
vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
vec3 p0 = vec3(a0.xy,h.x);
vec3 p1 = vec3(a0.zw,h.y);
vec3 p2 = vec3(a1.xy,h.z);
vec3 p3 = vec3(a1.zw,h.w);
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
m = m * m;
return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) );
}
vec3 snoiseVec3( vec3 x ){
float s = snoise(vec3( x ));
float s1 = snoise(vec3( x.y - 19.1 , x.z + 33.4 , x.x + 47.2 ));
float s2 = snoise(vec3( x.z + 74.2 , x.x - 124.5 , x.y + 99.4 ));
vec3 c = vec3( s , s1 , s2 );
return c;
}
vec3 curlNoise( vec3 p ){
const float e = .1;
vec3 dx = vec3( e , 0.0 , 0.0 );
vec3 dy = vec3( 0.0 , e , 0.0 );
vec3 dz = vec3( 0.0 , 0.0 , e );
vec3 p_x0 = snoiseVec3( p - dx );
vec3 p_x1 = snoiseVec3( p + dx );
vec3 p_y0 = snoiseVec3( p - dy );
vec3 p_y1 = snoiseVec3( p + dy );
vec3 p_z0 = snoiseVec3( p - dz );
vec3 p_z1 = snoiseVec3( p + dz );
float x = p_y1.z - p_y0.z - p_z1.y + p_z0.y;
float y = p_z1.x - p_z0.x - p_x1.z + p_x0.z;
float z = p_x1.y - p_x0.y - p_y1.x + p_y0.x;
const float divisor = 1.0 / ( 2.0 * e );
return normalize( vec3( x , y , z ) * divisor );
}
vec2 rotate(vec2 v, float a) {
float s = sin(a);
float c = cos(a);
mat2 m = mat2(c, -s, s, c);
return m * v;
}
#define PI 3.141592653
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec3 posSelf = texture2D( texturePosition, uv ).xyz;
vec3 velSelf = texture2D( textureVelocity, uv ).xyz;
vec3 pvelSelf = velSelf;
vec3 extraSelf = texture2D( textureExtra, uv ).xyz;
float posOffset = mix(posSelf.b, 1.0, .9);
vec3 acc = curlNoise(posSelf * posOffset + delta * 0.5);
acc.y += .5;
acc.y *= 0.85;
float speedOffset = mix(extraSelf.g, 1.0, .9);
// rotation
vec2 dir = normalize(posSelf.xz);
dir = rotate(dir, PI * 0.75);
acc.xz += dir * 0.7; // > 4 to have tornal
velSelf += acc * .01 * speedOffset;
float decrease = .96;
if(stopDelta) decrease = 0.;
velSelf *= decrease;
if(!start) velSelf = pvelSelf;
gl_FragColor = vec4( velSelf,1.);
}
</script>
<script id="frag_Extra_GPGPU" type="x-shader/x-fragment">
void main() {
vec2 uv = gl_FragCoord.xy / resolution.xy;
vec4 extra = texture2D( textureExtra, uv );
gl_FragColor = vec4(extra.rgb ,1.);
}
</script>
<script type="module">
import * as THREE from "three";
import Renderer from "./fuild/modules_curlNoise3d/Renderer.js";
import Simulation from "./fuild/modules_curlNoise3d/Simulation.js";
import Mouse from "./fuild/modules_curlNoise3d/Mouse.js";
import { calcSizeFitCamPerspective } from './utils/calcSizeFitCamPerspective.js'
import { GPUComputationRenderer } from 'three/addons/GPUComputationRenderer.js';
import { OrbitControls } from 'three/addons/OrbitControls.js';
import { curl4Noise } from './glsl/curl4Noise.js'
let controlsMain
//GPGPU
let gpuCompute
let widthTexture = 258
let velocityVariable, positionVariable, extraVariable
let positionUniforms, velocityUniforms, extraUniforms
let now, last, delta
let wView = window.innerWidth
let hView = window.innerHeight
let ratioView = window.innerWidth / window.innerHeight
let PointMat;
function initComputeRenderer(renderer) {
gpuCompute = new GPUComputationRenderer(widthTexture, widthTexture, renderer);
const dtPosition = gpuCompute.createTexture();
const dtVelocity = gpuCompute.createTexture();
const dtExtra = gpuCompute.createTexture();
fillPosTexture(dtPosition);
fillVelTexture(dtVelocity);
fillExtraTexture(dtExtra)
velocityVariable = gpuCompute.addVariable('textureVelocity', document.getElementById('frag_Vel_GPGPU').textContent, dtVelocity);
positionVariable = gpuCompute.addVariable('texturePosition', document.getElementById('frag_Pos_GPGPU').textContent, dtPosition);
extraVariable = gpuCompute.addVariable('textureExtra', document.getElementById('frag_Extra_GPGPU').textContent, dtExtra);
velocityVariable.wrapS = THREE.RepeatWrapping;
velocityVariable.wrapT = THREE.RepeatWrapping;
positionVariable.wrapS = THREE.RepeatWrapping;
positionVariable.wrapT = THREE.RepeatWrapping;
extraVariable.wrapS = THREE.RepeatWrapping;
extraVariable.wrapT = THREE.RepeatWrapping;
gpuCompute.setVariableDependencies(velocityVariable, [positionVariable, velocityVariable, extraVariable]);
gpuCompute.setVariableDependencies(positionVariable, [positionVariable, velocityVariable, extraVariable]);
gpuCompute.setVariableDependencies(extraVariable, [positionVariable, velocityVariable, extraVariable]);
positionUniforms = positionVariable.material.uniforms;
velocityUniforms = velocityVariable.material.uniforms;
extraUniforms = extraVariable.material.uniforms;
positionUniforms['time'] = { value: 0.0 };
positionUniforms['sizeView'] = { value: new THREE.Vector2(0, 0) };
positionUniforms['delta'] = { value: 0.0 };
velocityUniforms['time'] = { value: 0.0 };
velocityUniforms['delta'] = { value: 0.0 };
extraUniforms['time'] = { value: 0.0 };
extraUniforms['delta'] = { value: 0.0 };
positionUniforms['fuildMap'] = { value: null }
velocityUniforms['fuildMap'] = { value: null }
positionUniforms['start'] = { value: false }
velocityUniforms['start'] = { value: false };
positionUniforms['stopDelta'] = { value: false }
velocityUniforms['stopDelta'] = { value: false };
positionUniforms['forceMouse'] = { value: 0.0 };
positionUniforms['projectionMatrix'] = { value: new THREE.Matrix4() };
positionUniforms['viewMatrixOrbit'] = { value: new THREE.Matrix4() };
const error = gpuCompute.init();
if (error !== null) {
console.error(error);
}
}
function fillPosTexture(texture) {
const arrT = texture.image.data;
for (let k = 0, kl = arrT.length; k < kl; k += 4) {
const x = Math.random() * 2 - 1;
const y = Math.random() * 2 - 1;
const z = Math.random() * 2 - 1;
let vp = new THREE.Vector3(x, y, z)
vp.multiplyScalar(0.4)
arrT[k + 0] = vp.x
arrT[k + 1] = vp.y
arrT[k + 2] = vp.z
arrT[k + 3] = 1
}
}
function fillVelTexture(texture) {
const arrT = texture.image.data;
for (let k = 0, kl = arrT.length; k < kl; k += 4) {
const x = Math.random() * 2 - 1;
const y = Math.random() * 2 - 1;
const z = Math.random() * 2 - 1;
let vv = new THREE.Vector3(x, y, z)
arrT[k + 0] = vv.x
arrT[k + 1] = vv.y
arrT[k + 2] = vv.z
arrT[k + 3] = 1
}
}
function fillExtraTexture(texture) {
const arrT = texture.image.data;
for (let k = 0, kl = arrT.length; k < kl; k += 4) {
const x = Math.random() * 2 - 1;
const y = Math.random() * 2 - 1;
const z = Math.random() * 2 - 1;
arrT[k + 0] = x
arrT[k + 1] = y
arrT[k + 2] = z
arrT[k + 3] = 1
}
}
Renderer.init();
Mouse.init();
const buttonStop = document.getElementById("stop")
const buttonStart = document.getElementById("start")
buttonStop.onclick = () => {
velocityUniforms['stopDelta'].value = !velocityUniforms['stopDelta'].value;
positionUniforms['stopDelta'].value = !positionUniforms['stopDelta'].value;
};
buttonStart.onclick = () => {
velocityUniforms['start'].value = !velocityUniforms['start'].value;
positionUniforms['start'].value = !positionUniforms['start'].value;
};
initComputeRenderer(Renderer.renderer)
const scene = new THREE.Scene();
scene.background = 0x000000
const light = new THREE.AmbientLight(0x404040); // soft white light
scene.add(light);
const directionalLight = new THREE.DirectionalLight(0xffffff, 2.5);
scene.add(directionalLight);
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 2.5
controlsMain = new OrbitControls(camera, Renderer.renderer.domElement);
const [widthFit, heightFit] = calcSizeFitCamPerspective(camera)
positionUniforms['sizeView'] = { value: new THREE.Vector2(widthFit, heightFit) };
const simulation = new Simulation({
options: {
iterations_poisson: 1,
iterations_viscous: 32,
mouse_force: 10,
resolution: 0.3,
cursor_size: 20,
viscous: 30,
isBounce: false,
dt: 0.1,
isViscous: false,
BFECC: false
}
});
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
const textureImg = new THREE.TextureLoader().load('textures/cat.jpg');
initPoint()
function initPoint() {
const gridSize = widthTexture; // Số điểm theo mỗi chiều
const spacing = 1.0 / (gridSize - 1); // Khoảng cách giữa các điểm
// Tạo dữ liệu điểm cho lưới
const vertices = [];
const extra = [];
const uvs = [];
const radius = 1.0;
for (let x = 0; x < gridSize; x++) {
for (let y = 0; y < gridSize; y++) {
// Tính toán tọa độ UV cho điểm
const u = x * spacing;
const v = y * spacing;
//// rando
// let posX = u * 2.0 - 1.0;
// let posY = v * 2.0 - 1.0;
// vertices.push(posX * 2., posY * 2., 0);
//// sphere
const theta = u * Math.PI * 2; // Góc xoay quanh trục Y (longitude)
const phi = v * Math.PI; // Góc từ trên xuống dưới (latitude)
// Tính toán tọa độ x, y, z trên bề mặt hình cầu
let posX = radius * Math.sin(phi) * Math.cos(theta);
let posY = radius * Math.sin(phi) * Math.sin(theta);
let posZ = radius * Math.cos(phi);
vertices.push(posX, posY, posZ);
let extraX = Math.random() * 2 - 1
let extraY = Math.random() * 2 - 1
extra.push(extraX, extraY, 0);
uvs.push(u, v);
}
}
const PointGeo = new THREE.BufferGeometry();
const positionAttribute = new THREE.BufferAttribute(new Float32Array(vertices), 3);
const extraAttribute = new THREE.BufferAttribute(new Float32Array(extra), 3);
const uvAttribute = new THREE.BufferAttribute(new Float32Array(uvs), 2);
PointGeo.setAttribute('position', positionAttribute);
PointGeo.setAttribute('extra', extraAttribute);
PointGeo.setAttribute('uv', uvAttribute);
const vertexShader = `
precision highp float;
uniform float time;
uniform sampler2D fuildMap;
uniform sampler2D fuildPress;
uniform sampler2D tPos_gpu;
uniform sampler2D tVel_gpu;
uniform float speedmouse;
uniform mat4 p;
uniform mat4 m;
uniform vec2 sizeView;
uniform vec2 tSize;
varying vec2 vUv;
varying vec3 vPos;
varying vec2 vFuild;
${curl4Noise}
void main() {
vec2 uvT = vec2(
mod(float(gl_InstanceID), tSize.x) / tSize.x, // Tính chỉ số cột
floor(float(gl_InstanceID) / tSize.x) / tSize.y // Tính chỉ số hàng
);
vec3 posgpu = texture2D(tPos_gpu,uv).xyz;
vec3 pos = posgpu;
vec2 uvPos = vec2(
pos.x / sizeView.x + 0.5,
pos.y / sizeView.y + 0.5
);
vec2 velfuild = texture2D(fuildMap,uvPos).xy;
gl_Position = p * m * vec4(pos, 1.0);
vUv = uvPos;
vPos = pos;
vFuild = 1.-velfuild;
gl_PointSize = max(2.,200. * abs(velfuild.y - velfuild.x));
}
`;
const fragmentShader = `
varying vec3 vPos;
varying vec2 vFuild;
varying vec2 vUv;
uniform sampler2D fuildMap;
uniform sampler2D fuildPress;
vec3 pal( in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d )
{
return a + b*cos( 6.28318*(c*t+d) );
}
void main() {
vec2 vel = texture2D(fuildMap,vUv).xy;
vec2 press = texture2D(fuildPress,vUv).xy;
float len = length(vel);
vec2 velO = vel * 0.5 + 0.5;
vec3 colVel = pal( length(vel) * 10.2, vec3(0.5,0.5,0.5),vec3(0.5,0.5,0.5),vec3(1.0,1.0,0.5),vec3(0.8,0.90,0.30) );
gl_FragColor = vec4(colVel ,1.);
}
`
PointMat = new THREE.ShaderMaterial({
vertexShader: vertexShader,
fragmentShader: fragmentShader,
transparent: true,
depthWrite: false,
blendSrcAlpha: 1,
uniforms: {
speedmouse: {
value: 0
},
time: {
value: 0
},
fuildMap: {
value: simulation.fbos.vel_0.texture
},
fuildPress: {
value: simulation.fbos.pressure_1.texture
},
sizeView: {
value: new THREE.Vector2(widthFit, heightFit)
},
tPos_gpu: { value: null },
tVel_gpu: { value: null },
tSize: { value: new THREE.Vector2(widthTexture, widthTexture) },
p : {value:new THREE.Matrix4()},
m : {value:new THREE.Matrix4()},
}
});
let points = new THREE.Points(PointGeo, PointMat);
let box = new THREE.Mesh(
new THREE.BoxGeometry(2, 2, 2),
new THREE.MeshBasicMaterial({ color: 'blue', transparent: true, opacity: 1 })
)
scene.add(points)
}
function runGPGPU() {
positionUniforms['time'].value = now / 1000;
positionUniforms['delta'].value = delta;
velocityUniforms['time'].value = now / 1000;
velocityUniforms['delta'].value = delta;
// positionUniforms['fuildMap'].value = simulation.fbos.vel_0.texture
gpuCompute.compute();
const texturePosOut = gpuCompute.getCurrentRenderTarget(positionVariable).texture;
const textureVelOut = gpuCompute.getCurrentRenderTarget(velocityVariable).texture;
if (PointMat) {
PointMat.uniforms.tPos_gpu.value = texturePosOut
PointMat.uniforms.tVel_gpu.value = textureVelOut
}
}
const targetFboOrtho = new THREE.WebGLRenderTarget(wView, hView, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat
});
let sceneOrtho, cameraOrtho, planeOrtho
SceneOrtho()
function SceneOrtho() {
sceneOrtho = new THREE.Scene()
cameraOrtho = new THREE.OrthographicCamera(- 2, 2, 2, -2, 0, 1);
let planeMat = new THREE.ShaderMaterial({
vertexShader: `
varying vec2 vUv;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vUv = uv;
}
`,
fragmentShader: `
varying vec2 vUv;
uniform sampler2D fuildMap;
uniform sampler2D externalForce;
vec3 pal( in float t, in vec3 a, in vec3 b, in vec3 c, in vec3 d )
{
return a + b*cos( 6.28318*(c*t+d) );
}
void main() {
vec3 force = texture2D(externalForce,vUv).xyz;
vec3 vel = texture2D(fuildMap,vUv).xyz;
float len = length(vel);
vel = vel * 0.5 + 0.5;
vec3 colVel = pal(len * 10.2, vec3(0.5,0.5,0.5),vec3(0.5,0.5,0.5),vec3(1.0,1.0,0.5),vec3(0.8,0.90,0.30) );
vec3 color = vec3(vel.x, vel.y, 1.0);
color = mix(vec3(1.0), colVel, len);
gl_FragColor = vec4(1.-colVel,1.);
}
`,
uniforms: {
externalForce: {
value: simulation.externalForce.props.output.texture
},
fuildMap: {
value: simulation.fbos.vel_0.texture
},
fuildPress: {
value: simulation.fbos.pressure_1.texture
},
},
wireframe: false,
transparent: true,
blending: THREE.AdditiveBlending,
})
let planeCheck = new THREE.Mesh(
new THREE.PlaneGeometry(4, 4),
planeMat,
)
sceneOrtho.add(planeCheck)
}
Renderer.renderer.setAnimationLoop(animate);
function animate() {
Mouse.update();
Renderer.update();
simulation.update()
runGPGPU()
Renderer.renderer.setRenderTarget(null);
Renderer.renderer.render(scene, camera);
PointMat.uniforms.time.value = Renderer.time
PointMat.uniforms.speedmouse.value = simulation.externalForce.speed
positionUniforms['forceMouse'] = { value: simulation.externalForce.speed };
positionUniforms['projectionMatrix'] = { value: controlsMain.object.projectionMatrix };
positionUniforms['viewMatrixOrbit'] = { value: controlsMain.object.matrixWorldInverse };
PointMat.uniforms.p.value = controlsMain.object.projectionMatrix
PointMat.uniforms.m.value = controlsMain.object.matrixWorldInverse
Renderer.renderer.setRenderTarget(null);
Renderer.renderer.setRenderTarget(targetFboOrtho);
Renderer.renderer.render(sceneOrtho, cameraOrtho);
positionUniforms['fuildMap'] = { value: targetFboOrtho.texture }
// PointMat.uniforms.externalForce.value = simulation.externalForce.props.output.texture
}
</script>
</html>