-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVectorFields.html
287 lines (233 loc) · 9.08 KB
/
VectorFields.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - mesh - batch</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 {
background-color: #fff;
color: #444;
margin: 0;
padding: 0;
width: 100vw;
height: 100vh;
overflow: hidden;
}
</style>
<body>
<script type="importmap">
{
"imports": {
"three": "./three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from "three";
import { OrbitControls } from 'three/addons/OrbitControls.js';
let w = window.innerWidth;
let h = window.innerHeight;
const vertexShader = textureSize => `
varying vec3 vViewPosition;
varying vec2 vUv;
attribute vec4 instanceQuaternion;
// custom code:
uniform float time;
// uniform vec4 cameraProps;
// uniform sampler2D levelImageMap;
// attribute float offset;
attribute vec4 orientation;
attribute vec3 offset;
attribute vec3 gridXYZ;
attribute float scale;
// uniform vec3 floorOffset;
attribute vec3 color;
varying vec4 vColor;
vec3 applyQuaternionToVector( vec4 q, vec3 v ){
return v + 2.0 * cross( q.xyz, cross( q.xyz, v ) + q.w * v );
}
void main() {
vUv = uv;
vec3 transformed = position;
// vColor = vec4(color, (1.0 + sin(transformed.z * 1000.0 + time)) / 2.0);
// float a = mod(time - gridXYZ.z / ${textureSize}.0, 1.0);
// if (a > 1.0 / ${textureSize}.0) {
// a = 0.8;
// } else {
// a = 1.0;
// }
// vColor = vec4(color, a);
// vColor = vec4(color, a);
vColor = vec4(
gridXYZ.x / ${textureSize}.0,
0.3,
// gridXYZ.y / ${textureSize}.0,
0.7,
// gridXYZ.z / ${textureSize}.0,
0.6);
// a);
transformed = applyQuaternionToVector(orientation, transformed);
transformed += offset;
// vColor = vec4(color, 1.0); //max(0.75, sin(position.y * 0.005 - time * 0.5)));
// transformed *= vec3(scale, scale, 1.0);
vec3 pos = position;
pos += offset ;
gl_Position = projectionMatrix * modelViewMatrix * vec4(transformed , 1.0);
}
`
const fragmentShader = `
void main() {
gl_FragColor = vec4(1.,0.,0.,1.);
}
`
const scene = new THREE.Scene();
scene.fog = new THREE.FogExp2(0x000000, 0.06);
const camera = new THREE.PerspectiveCamera(75, w / h, 0.1, 1000);
camera.position.z = 100;
const renderer = new THREE.WebGLRenderer();
renderer.setSize(w, h);
document.body.appendChild(renderer.domElement);
// Orbit Controls
const controls = new OrbitControls(camera, renderer.domElement);
controls.target.set(0, 0, 0);
controls.update();
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); // Thêm ánh sáng môi trường
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
scene.add(directionalLight);
let geometry, material
const textureSize = 10
initVectorSample()
function initVectorSample() {
geometry = new THREE.InstancedBufferGeometry()
geometry.copy(new THREE.ConeGeometry(0.5, 4, 3))
const offsets = []
const orientations = []
const scales = []
const indexes = []
const colors = []
const gridXYZs = []
const lifetimes = []
let _q = new THREE.Quaternion()
const numberOfRows = textureSize
const numberOfColumns = textureSize
const numberOfBuckets = textureSize
const stepSize = 8
const halfStep = stepSize / 2
let i = 0
const offsetX = stepSize * numberOfColumns / 2
const offsetY = stepSize * numberOfRows / 2
const offsetZ = stepSize * numberOfBuckets / 2
for (let z = 0; z < numberOfBuckets; z += 1) {
for (let y = 0; y < numberOfRows; y += 1) {
for (let x = 0; x < numberOfColumns; x += 1) {
// for (let i = 0; i < numberOfParticles; i += 1) {
offsets.push(
-offsetX + x * stepSize + halfStep,
-offsetY + y * stepSize + halfStep,
-offsetZ + z * stepSize + halfStep
)
_q.set(0, 0, 0, 0).normalize();
orientations.push(
_q.x, _q.y, _q.z, _q.w
)
// mats.push(
// 0,0,0,1
// )
gridXYZs.push(x, y, z)
colors.push(
0.1, 0.1, 0.1
// 0.5 + x / numberOfBuckets / 2,
// 0.5 - y / numberOfBuckets / 2,
// 1 - z / numberOfBuckets / 2
// y / numberOfRows
// Math.random(),
// Math.random(),
// Math.random()
)
scales.push(stepSize / 8)
// indexes.push(i + 1)
// i += 1
// lifetimes.push(0, 0.5 + Math.random() / 2)
// lifetimes.push(0, 1)
}
}
}
console.log(offsets)
geometry.setAttribute('offset', new THREE.InstancedBufferAttribute(new Float32Array(offsets), 3))
geometry.setAttribute('orientation', new THREE.InstancedBufferAttribute(new Float32Array(orientations), 4))
geometry.setAttribute('gridXYZ', new THREE.InstancedBufferAttribute(new Float32Array(gridXYZs), 3))
geometry.setAttribute('scale', new THREE.InstancedBufferAttribute(new Float32Array(scales), 1))
geometry.setAttribute('color', new THREE.InstancedBufferAttribute(new Float32Array(colors), 3))
geometry.setAttribute('particleIndex', new THREE.InstancedBufferAttribute(new Float32Array(indexes), 1))
geometry.setAttribute('lifetime', new THREE.InstancedBufferAttribute(new Float32Array(lifetimes), 2))
material = new THREE.ShaderMaterial({
uniforms: {
time: {
value: 0
}
},
vertexShader: vertexShader(textureSize),
fragmentShader: fragmentShader,
side: THREE.FrontSide,
})
material.receiveShadow = true
material.castShadow = true
const mesh = new THREE.InstancedMesh(geometry, material, 1000)
scene.add(mesh)
}
let t = 0
function animate() {
requestAnimationFrame(animate);
updateFunctions()
t++
if (material) material.uniforms.time.value = t
renderer.render(scene, camera);
}
function updateFunctions() {
const orientations = geometry.attributes.orientation.array
const numberOfRows = textureSize
const numberOfColumns = textureSize
const numberOfBuckets = textureSize
let i = 0
let i4 = 0
let _q = new THREE.Quaternion()
const vec4 = new THREE.Vector4()
const vec3 = new THREE.Vector3()
const mat = new THREE.Matrix4()
const quat = new THREE.Quaternion()
for (let z = 0; z < numberOfBuckets; z += 1) {
for (let y = 0; y < numberOfRows; y += 1) {
for (let x = 0; x < numberOfColumns; x += 1) {
const res1 = Math.cos((x - t) * 0.05) - Math.sin((y - numberOfRows / 2) * 0.1)
const res2 = Math.sin((x - numberOfColumns / 2) * 0.1) + Math.sin((y - numberOfRows / 2) * 0.1)
const res3 = Math.sin((x - numberOfColumns / 2) * 0.1) + Math.sin((z - numberOfBuckets / 2) * 0.1)
const pitch = Math.asin(res3);
const yaw = -Math.atan2(res1, res2)
_q.setFromEuler(new THREE.Euler(pitch, 0, yaw, 'XYZ'))
orientations[i4 + 0] = _q.x
orientations[i4 + 1] = _q.y
orientations[i4 + 2] = _q.z
orientations[i4 + 3] = _q.w
i += 1
i4 += 4
}
}
}
geometry.attributes.orientation.needsUpdate = true
}
animate();
function handleWindowResize() {
w = window.innerWidth;
h = window.innerHeight;
camera.aspect = w / h;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
window.addEventListener('resize', handleWindowResize, false);
</script>
</body>
</html>