-
Notifications
You must be signed in to change notification settings - Fork 0
/
FishSurface.html
379 lines (280 loc) · 11.1 KB
/
FishSurface.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - decal splatter</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
</head>
<style>
body{margin:0}
</style>
<body>
<div id="container"></div>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import Stats from 'three/addons/stats.module.js';
import { GUI } from 'three/addons/lil-gui.module.min.js';
import { OBJLoader } from 'three/addons/OBJLoader.js';
import { OrbitControls } from 'three/addons/OrbitControls.js';
let objUniforms,stats
let shaderChunk,shaderChunk2
let texDataCurve,curve
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(60, innerWidth / innerHeight, 0.1, 11500);
camera.position.set(0, -25, 50);
const renderer = new THREE.WebGLRenderer({ antialias: false });
renderer.setClearColor(0x181005);
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
stats = new Stats();
document.body.appendChild(stats.dom);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
const axesHelper = new THREE.AxesHelper( 5 );
scene.add( axesHelper );
function getRandomHexColor() {
return '#' + Math.floor(Math.random()*16777215).toString(16);
}
// path
console.time()
const countCur = 1
const data = [];
const numPoints = 250;
const cSegments = 6;
const cPts = [];
for (let ii = 0; ii < countCur; ii++) {
for (let i = 0; i < cSegments; i++) {
cPts.push(
new THREE.Vector3(Math.random()*50,-Math.random()*50,Math.random()*50)
);
}
curve = new THREE.CatmullRomCurve3(cPts);
curve.verticesNeedUpdate = true;
curve.closed = true;
const cPoints = curve.getSpacedPoints(numPoints);
const cObjects = curve.computeFrenetFrames(numPoints, true);
console.log("cPts",cPts,"curve",curve,"cPoints",cPoints,"cObjects",cObjects);
cPoints.forEach(v => { data.push(v.x, v.y, v.z,1.); });
cObjects.binormals.forEach(v => { data.push(v.x, v.y, v.z,1.); });
cObjects.normals.forEach(v => { data.push(v.x, v.y, v.z,1.); });
cObjects.tangents.forEach(v => { data.push(v.x, v.y, v.z,1.); });
const pGeom = new THREE.BufferGeometry().setFromPoints(cPoints);
const pMat = new THREE.LineBasicMaterial({ color: getRandomHexColor() });
const pathLine = new THREE.Line(pGeom, pMat);
scene.add(pathLine);
}
const dataArray = new Float32Array(data);
texDataCurve = new THREE.DataTexture(dataArray, numPoints + 1, 4 * countCur, THREE.RGBAFormat, THREE.FloatType);
texDataCurve.magFilter = THREE.NearestFilter;
texDataCurve.needsUpdate = true
console.log(texDataCurve);
console.timeEnd()
// setInterval(()=>{
// updateCurve(curve, cPts, dataArray, texDataCurve)
// },2000)
const planeCheck = new THREE.Mesh(
new THREE.PlaneGeometry(25,25),
new THREE.MeshBasicMaterial({map:texDataCurve})
)
scene.add(planeCheck);
// Koi
function updateCurve(curve, cPts, dataArray, texDataCurve) {
// Xóa điểm đầu tiên
// cPts.shift();
// Thêm điểm mới vào cuối
const newPoint = new THREE.Vector3(Math.random() * 50, -Math.random() * 50, Math.random() * 50);
cPts.push(newPoint);
// Tạo lại đường cong và các dữ liệu
const numPoints = 250;
const countCur = cPts.length; // Đảm bảo số điểm tương thích
const curveUpdated = new THREE.CatmullRomCurve3(cPts);
curveUpdated.closed = true;
// Tính lại các điểm
const cPoints = curveUpdated.getSpacedPoints(numPoints);
const cObjects = curveUpdated.computeFrenetFrames(numPoints, true);
// Xóa data cũ và thêm data mới
const data = [];
cPoints.forEach(v => { data.push(v.x, v.y, v.z, 1.); });
cObjects.binormals.forEach(v => { data.push(v.x, v.y, v.z, 1.); });
cObjects.normals.forEach(v => { data.push(v.x, v.y, v.z, 1.); });
cObjects.tangents.forEach(v => { data.push(v.x, v.y, v.z, 1.); });
// Cập nhật lại dataArray
const newDataArray = new Float32Array(data);
// Cập nhật lại DataTexture
texDataCurve.image.data = newDataArray;
texDataCurve.needsUpdate = true;
// In ra thông tin cập nhật
console.log("Cập nhật điểm: ", cPts);
console.log("Cập nhật texture: ", texDataCurve);
const pGeom = new THREE.BufferGeometry().setFromPoints(cPoints);
const pMat = new THREE.LineBasicMaterial({ color: getRandomHexColor() });
const pathLine = new THREE.Line(pGeom, pMat);
scene.add(pathLine);
}
const oUs = [];
const loader = new OBJLoader();
loader.load("/models/fish.obj", obj => {
let objGeom = obj.children[0].geometry
objGeom.center();
objGeom.rotateX(-Math.PI * 0.5);
objGeom.scale(0.05, 0.05, 0.05);
const objBox = new THREE.Box3().setFromBufferAttribute(objGeom.getAttribute("position"));
const objSize = new THREE.Vector3();
objBox.getSize(objSize);
//objGeom.translate(0, 0, objBox.z);
const objUniforms = {
uSpatialTexture: { value: texDataCurve },
uTextureSize: { value: new THREE.Vector2(numPoints + 1, 4) },
uTime: { value: 0 },
stt: { value: new THREE.Vector2(countCur, 0) },
uLengthRatio: { value: new THREE.Vector2( .02, .02) }, // objSize.z / curve.cacheArcLengths[200] more or less real lenght along the path
uObjSize: { value: objSize } // lenght
}
oUs.push(objUniforms);
console.log(objUniforms)
const objMat = new THREE.MeshBasicMaterial({ color: 0xff6600, wireframe: true });
objMat.onBeforeCompile = (shader) => {
shader.uniforms.uSpatialTexture = objUniforms.uSpatialTexture;
shader.uniforms.uTextureSize = objUniforms.uTextureSize;
shader.uniforms.uTime = objUniforms.uTime;
shader.uniforms.stt = objUniforms.stt;
shader.uniforms.uLengthRatio = objUniforms.uLengthRatio;
shader.uniforms.uObjSize = objUniforms.uObjSize;
shader.vertexShader = `
uniform sampler2D uSpatialTexture;
uniform vec2 uTextureSize;
uniform float uTime;
uniform vec2 stt;
uniform vec2 uLengthRatio;
uniform vec3 uObjSize;
struct splineData {
vec3 point;
vec3 binormal;
vec3 normal;
};
splineData getSplineData(float t){
float step = (1. / uTextureSize.y) / stt.x ;
float halfStep = step * 0.5;
splineData sd;
sd.point = texture2D(uSpatialTexture, vec2(t, step * (0.+4.*stt.y) + halfStep)).rgb;
sd.binormal = texture2D(uSpatialTexture, vec2(t, step * (1.+4.*stt.y) + halfStep)).rgb;
sd.normal = texture2D(uSpatialTexture, vec2(t, step * (2.+4.*stt.y) + halfStep)).rgb;
return sd;
}
` + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace(
`#include <begin_vertex>`,
`#include <begin_vertex>
vec3 pos = position;
float wStep = 1. / uTextureSize.x;
float hWStep = wStep * 0.5;
float d = pos.z / uObjSize.z;
float t = fract((uTime * 0.1) + (d * uLengthRatio.x));
float numPrev = floor(t / wStep);
float numNext = numPrev + 1.;
//numNext = numNext > (uTextureSize.x - 1.) ? 0. : numNext;
float tPrev = numPrev * wStep + hWStep;
float tNext = numNext * wStep + hWStep;
//float tDiff = tNext - tPrev;
splineData splinePrev = getSplineData(tPrev);
splineData splineNext = getSplineData(tNext);
float f = (t - tPrev) / wStep;
vec3 P = mix(splinePrev.point, splineNext.point, f);
vec3 B = mix(splinePrev.binormal, splineNext.binormal, f);
vec3 N = mix(splinePrev.normal, splineNext.normal, f);
transformed = P + (N * pos.x) + (B * pos.y) ;
`
);
shaderChunk = shader
console.log(shader)
}
const model = new THREE.Mesh(objGeom, objMat);
// scene.add(model);
const objMat2 = new THREE.MeshBasicMaterial({ color: 0xff6600, wireframe: true });
objMat2.onBeforeCompile = (shader) => {
shader.uniforms.uSpatialTexture = objUniforms.uSpatialTexture;
shader.uniforms.uTextureSize = objUniforms.uTextureSize;
shader.uniforms.uTime = objUniforms.uTime;
shader.uniforms.stt = objUniforms.stt;
shader.uniforms.uLengthRatio = objUniforms.uLengthRatio;
shader.uniforms.uObjSize = objUniforms.uObjSize;
shader.vertexShader = `
uniform sampler2D uSpatialTexture;
uniform vec2 uTextureSize;
uniform float uTime;
uniform vec2 stt;
uniform vec2 uLengthRatio;
uniform vec3 uObjSize;
struct splineData {
vec3 point;
vec3 binormal;
vec3 normal;
};
splineData getSplineData(float t){
float step = (1. / uTextureSize.y) / stt.x ;
float halfStep = step * 0.5;
splineData sd;
sd.point = texture2D(uSpatialTexture, vec2(t, step * (0.+4.*float(gl_InstanceID)) + halfStep)).rgb;
sd.binormal = texture2D(uSpatialTexture, vec2(t, step * (1.+4.*float(gl_InstanceID)) + halfStep)).rgb;
sd.normal = texture2D(uSpatialTexture, vec2(t, step * (2.+4.*float(gl_InstanceID)) + halfStep)).rgb;
return sd;
}
` + shader.vertexShader;
shader.vertexShader = shader.vertexShader.replace(
`#include <begin_vertex>`,
`#include <begin_vertex>
vec3 pos = position;
float wStep = 1. / uTextureSize.x;
float hWStep = wStep * 0.5;
float d = pos.z / uObjSize.z;
float t = fract((uTime * 0.1) + (d * uLengthRatio.x));
float numPrev = floor(t / wStep);
float numNext = numPrev + 1.;
//numNext = numNext > (uTextureSize.x - 1.) ? 0. : numNext;
float tPrev = numPrev * wStep + hWStep;
float tNext = numNext * wStep + hWStep;
//float tDiff = tNext - tPrev;
splineData splinePrev = getSplineData(tPrev);
splineData splineNext = getSplineData(tNext);
float f = (t - tPrev) / wStep;
vec3 P = mix(splinePrev.point, splineNext.point, f);
vec3 B = mix(splinePrev.binormal, splineNext.binormal, f);
vec3 N = mix(splinePrev.normal, splineNext.normal, f);
transformed = P + (N * pos.x) + (B * pos.y) ;
`
);
shaderChunk2 = shader
console.log(shader)
}
const cInstancedMesh = new THREE.InstancedMesh(objGeom,objMat2 /* new THREE.MeshBasicMaterial({color:'blue'}) */, countCur);
scene.add(cInstancedMesh);
});
initGui()
function initGui() {
const parameters = { stt: 0 };
function update() {
if(shaderChunk) shaderChunk.uniforms.stt.value.y = parameters.stt
}
const gui = new GUI();
gui.add(parameters, 'stt', 0, countCur -1, 1).onChange(update);
}
const clock = new THREE.Clock();
renderer.setAnimationLoop(() => {
stats.update();
controls.update();
const t = clock.getElapsedTime();
if(shaderChunk) shaderChunk.uniforms.uTime.value = t
if(shaderChunk2) shaderChunk2.uniforms.uTime.value = t/5
renderer.render(scene, camera);
});
</script>
</body>
</html>