-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuildSim_Restyle.html
455 lines (339 loc) · 15 KB
/
FuildSim_Restyle.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>FuildSim_Restyle</title>
</head>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
background-color: rgb(255, 255, 255);
}
h1 {
color: rgb(0, 0, 0);
position: absolute;
}
</style>
<body>
</body>
<div id="canvas"></div>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from "three";
import Renderer from "./fuild/modules_restyle/Renderer.js";
import Simulation from "./fuild/modules_restyle/Simulation.js";
import Mouse from "./fuild/modules_restyle/Mouse.js";
import { OrbitControls } from 'three/addons/OrbitControls.js';
import { calcSizeFitCamPerspective } from './utils/calcSizeFitCamPerspective.js'
Renderer.init();
Mouse.init();
let sceneMain, cameraMain, controlsMain, instancedMesh
let sceneOutSim, cameraOutSim, planeMatOutSim
//view texture
let matPLane1
let fboMain, fboOutSim
const widthView = window.innerWidth;
const heightView = window.innerHeight;
const dummy = new THREE.Object3D();
const countInstanced = 420;
const simulation = new Simulation({
options: {
iterations_poisson: 1,
iterations_viscous: 32,
mouse_force: 100,
resolution: 0.5,
cursor_size: 42,
viscous: 30,
isBounce: false,
dt: 0.014,
isViscous: false,
BFECC: false
}
});
const textureImg = new THREE.TextureLoader().load('textures/cat.jpg');
initFbos()
function initFbos() {
const renderTargetMain = new THREE.WebGLRenderTarget(widthView, heightView, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat
});
const renderTargetOutSim = new THREE.WebGLRenderTarget(widthView, heightView, {
minFilter: THREE.LinearFilter,
magFilter: THREE.LinearFilter,
format: THREE.RGBAFormat
});
// Biến để theo dõi Render Target hiện tại
fboMain = renderTargetMain;
fboOutSim = renderTargetOutSim;
}
initSceneMain()
function initSceneMain() {
sceneMain = new THREE.Scene();
sceneMain.background = 0xffffff
const light = new THREE.AmbientLight(0xffffff, 2.5);
const directionalLight = new THREE.DirectionalLight(0xffffff, 5.5);
sceneMain.add(light, directionalLight);
cameraMain = new THREE.PerspectiveCamera(75, widthView / heightView, 0.1, 1000);
cameraMain.position.z = 5
controlsMain = new OrbitControls(cameraMain, Renderer.renderer.domElement);
// Tạo BoxGeometry
const geometry = new THREE.BoxGeometry(.1, .1, .1);
// Tạo Material. Sử dụng MeshBasicMaterial cho đơn giản, bạn có thể sử dụng các material khác tùy nhu cầu.
const material = new THREE.MeshPhongMaterial({ vertexColors: false });
// Tạo InstancedMesh
instancedMesh = new THREE.InstancedMesh(geometry, material, countInstanced);
// Tạo một BufferAttribute để lưu trữ màu sắc cho mỗi instance
const colors = [];
for (let i = 0; i < countInstanced; i++) {
// Tạo ma trận transform cho mỗi instance
const matrix = new THREE.Matrix4();
// Tạo vị trí ngẫu nhiên trong phạm vi từ -50 đến 50
const position = new THREE.Vector3(
(Math.random() - 0.5) * 2,
(Math.random() - 0.5) * 2,
(Math.random() - 0.5) * 2
);
// Tạo tỷ lệ ngẫu nhiên từ 0.5 đến 1.5
const scale = new THREE.Vector3(
Math.random() + 0.5,
Math.random() + 0.5,
Math.random() + 0.5
);
// Tạo rotation ngẫu nhiên
const rotation = new THREE.Euler(
Math.random() * 2 * Math.PI,
Math.random() * 2 * Math.PI,
Math.random() * 2 * Math.PI
);
// Áp dụng transform vào ma trận
matrix.makeRotationFromEuler(rotation);
matrix.setPosition(position);
matrix.scale(scale);
// Gán ma trận cho instance thứ i
instancedMesh.setMatrixAt(i, matrix);
// Tạo màu ngẫu nhiên cho mỗi instance
const color = new THREE.Color(Math.random(), Math.random(), Math.random());
colors.push(color.r, color.g, color.b);
}
// Tạo BufferAttribute từ mảng màu sắc và gán vào geometry của InstancedMesh
const colorAttribute = new THREE.InstancedBufferAttribute(new Float32Array(colors), 3);
instancedMesh.instanceColor = colorAttribute;
const vertexShader = `
varying vec2 vUv;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vUv = uv;
}
`;
const fragmentShader = `
varying vec2 vUv;
uniform sampler2D texView;
void main() {
vec4 out1 = texture2D(texView,vUv);
gl_FragColor = out1 +vec4(1.,0.,0.,1.);
}
`
matPLane1 = new THREE.ShaderMaterial({
vertexShader: vertexShader,
fragmentShader: fragmentShader,
uniforms: {
texView: {
value: null
}
}
});
const plane1 = new THREE.Mesh(new THREE.PlaneGeometry(1, 1), matPLane1)
plane1.position.z = 2
sceneMain.add(instancedMesh)
}
initFboOutSim()
function initFboOutSim() {
sceneOutSim = new THREE.Scene();
const light = new THREE.AmbientLight(0x404040);
const directionalLight = new THREE.DirectionalLight(0xffffff);
sceneOutSim.add(light, directionalLight);
cameraOutSim = new THREE.PerspectiveCamera(75, widthView / heightView, 0.1, 1000);
cameraOutSim.position.z = .1
const [widthFit, heightFit] = calcSizeFitCamPerspective(cameraOutSim)
const vertexShader = `
varying vec2 vUv;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
vUv = uv;
}
`;
const fragmentShader = `
varying vec2 vUv;
uniform sampler2D fuildVel;
uniform sampler2D fuildPress;
uniform sampler2D fuildDiv;
uniform sampler2D outSceneMain;
uniform sampler2D textureImg;
uniform float time;
uniform float ratio;
uniform vec2 force;
uniform vec2 center;
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) );
}
vec3 circularColor(float t) {
float angle = t * 6.28318; // 2π
return vec3(0.5 + 0.5 * cos(angle + vec3(0.0, 2.0, 4.0))); // Tạo 3 màu khác nhau
}
float random (vec2 st) {
return fract(sin(dot(st.xy,
vec2(12.9898,78.233)))*
43758.5453123);
}
float blendDarken(float base, float blend) {
return min(blend,base);
}
vec3 blendDarken(vec3 base, vec3 blend) {
return vec3(blendDarken(base.r,blend.r),blendDarken(base.g,blend.g),blendDarken(base.b,blend.b));
}
vec3 blendDarken(vec3 base, vec3 blend, float opacity) {
return (blendDarken(base, blend) * opacity + base * (1.0 - opacity));
}
void main() {
vec3 cat = texture2D(textureImg,vec2((vUv.x* ratio ) - .5,vUv.y)).xyz;
vec2 vel = texture2D(fuildVel,vUv).xy;
vec2 velD = vel;
vec2 press = texture2D(fuildPress,vUv).xy;
vec2 pressD = press;
float len = length(vec2(vel.y,vel.x));
vel = vel * 0.5 + 0.5;
vec2 velO = vel * 0.5 + 0.5;
float angle = time * 6.28318; // 2π
vec3 colVel = pal(vel.x * 50.2 , vec3(random(vUv),0.5,0.5),vec3(0.5,cos(time),0.5),vec3(1.0,1.0,0.5),vec3(sin(time),0.90,0.30) );
colVel = pal(atan(vel.y ,vel.x ) * 50.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 saveC = pal(atan(vUv.y - .5 ,vUv.x - .5) * 1.2 , vec3(.5,0.5,0.5),vec3(0.5,.1,0.5),vec3(1.0,0.5,0.5),vec3(.9,0.1,0.1) );
saveC += pal(atan(vel.y ,vel.x ) * 50.2 , vec3(.5,0.5,0.5),vec3(0.5,.1,0.5),vec3(1.0,0.5,0.5),vec3(.9,0.1,0.1) );
vec3 color = mix(vec3(1.0),colVel, len);
color = mix(vec3(1.0),vec3(vel.y), len);
vec3 rls = 1.-color;
vec3 outScene = texture2D(outSceneMain,vUv - texture2D(fuildVel,vUv).xy * .4).xyz;
if(rls.x > 0.02) {
outScene = texture2D(outSceneMain,vUv ).xyz;
}else {
outScene = vec3(cat);
}
vec4 outF = vec4(rls * 2. + outScene ,1.);
//outF = vec4(rls * 2. + vec3(outScene.x) ,1.);
// Kiểm tra độ sáng của outF
float brightness = (outF.r + outF.g + outF.b) / 3.0;
if (brightness < .06) {
// outF = vec4(0.0, 0.0, 0.0, 1.0); // Chuyển thành màu đen
}
vec3 blend = blendDarken(outF.xyz,vec3(0.),.4);
gl_FragColor = vec4(vec3(outF.x),1.);
gl_FragColor = vec4(blend,1.);
}
`
planeMatOutSim = new THREE.ShaderMaterial({
vertexShader: vertexShader,
fragmentShader: fragmentShader,
transparent: false,
depthWrite: false,
blendSrcAlpha: 1,
uniforms: {
force: {
value: new THREE.Vector2()
},
center: {
value: new THREE.Vector2()
},
outSceneMain: {
value: null
},
time: {
value: 0
},
fuildVel: {
value: simulation.fbos.vel_0.texture
},
fuildPress: {
value: simulation.fbos.pressure_1.texture
},
fuildDiv: {
value: simulation.fbos.div.texture
},
sizeView: {
value: new THREE.Vector2(widthFit, heightFit)
},
textureImg : {
value:textureImg
},
ratio : {
value: widthView/heightView
}
}
});
let mesh = new THREE.Mesh(new THREE.PlaneGeometry(widthFit, heightFit), planeMatOutSim);
sceneOutSim.add(mesh)
}
function checkTexture() {
}
function animate() {
Mouse.update();
Renderer.update();
simulation.update()
// console.log(simulation.more.externalForceFbo.force)
if (1 === 1) {
// Render sceneOutSim vào Render Target hiện tại
Renderer.renderer.autoClear = true;
Renderer.renderer.setRenderTarget(fboMain);
Renderer.renderer.render(sceneMain, cameraMain);
planeMatOutSim.uniforms.time.value = Renderer.time
planeMatOutSim.uniforms.outSceneMain.value = fboMain.texture
planeMatOutSim.uniforms.force.value = simulation.more.externalForceFbo.force
planeMatOutSim.uniforms.center.value = simulation.more.externalForceFbo.center
Renderer.renderer.setRenderTarget(null);
Renderer.renderer.autoClear = false;
// Render sceneMain vào màn hình
Renderer.renderer.render(sceneOutSim, cameraOutSim);
// let temp = fboMain;
// fboMain = fboOutSim;
// fboOutSim = temp;
} else {
Renderer.renderer.setRenderTarget(null);
Renderer.renderer.render(sceneMain, cameraMain);
matPLane1.uniforms.texView.value = simulation.more.externalForceFbo
}
if (instancedMesh) {
instancedMesh.rotation.x = Math.sin(Renderer.time / 4);
instancedMesh.rotation.y = Math.sin(Renderer.time / 2);
}
// if (instancedMesh) {
// const time = Date.now() * 0.001;
// let i = 0;
// const offset = (countInstanced - 1) / 2;
// for (let x = 0; x < countInstanced; x++) {
// for (let y = 0; y < countInstanced; y++) {
// for (let z = 0; z < countInstanced; z++) {
// dummy.position.set(offset - x, offset - y, offset - z);
// dummy.rotation.y = (Math.sin(x / 4 + time) + Math.sin(y / 4 + time) + Math.sin(z / 4 + time));
// dummy.rotation.z = dummy.rotation.y * 2;
// dummy.updateMatrix();
// instancedMesh.setMatrixAt(i++, dummy.matrix);
// }
// }
// }
// instancedMesh.instanceMatrix.needsUpdate = true;
// instancedMesh.computeBoundingSphere();
// }
}
Renderer.renderer.setAnimationLoop(animate);
</script>
</html>