-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFatline.html
251 lines (216 loc) · 9.64 KB
/
Fatline.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Fatline</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>
<p>https://codepen.io/prisoner849/pen/wvdBerm</p>
<div id="container"></div>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls";
import { GUI } from "three/addons/libs/lil-gui.module.min.js";
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';
import { LineSegments2 } from 'three/addons/lines/LineSegments2.js';
import { LineSegmentsGeometry } from 'three/addons/lines/LineSegmentsGeometry.js';
console.clear();
const sizeTexture = 100
class InstDataTexture extends THREE.DataTexture {
constructor(verticesPerInstance, instanceCount) {
const totalAmountPerInstance = verticesPerInstance + 1; // vertices + color (vec4 * 1)
const instancePerRow = Math.floor(sizeTexture / totalAmountPerInstance);
const rows = Math.ceil(instanceCount / instancePerRow);
const w = totalAmountPerInstance * instancePerRow;
const h = rows;
const totalData = new Float32Array(w * h * 4).fill(1);
super(totalData, w, h, THREE.RGBAFormat, THREE.FloatType);
this.needsUpdate = true;
this.instanceCount = instanceCount;
this.totalAmountPerInstance = totalAmountPerInstance;
this.instancePerRow = instancePerRow;
this.rows = rows;
this.uniforms = {
totalAmountPerInstance: { value: totalAmountPerInstance },
instancePerRow: { value: instancePerRow },
width: { value: w },
height: { value: h }
};
}
setDataAt(idx, data) {
// data - array of Vector3
if (idx >= this.instanceCount) {
return;
}
let sourceData = this.source.data.data;
let startIdx = this.totalAmountPerInstance * idx;
data.forEach((p, pIdx) => {
let currIdx = startIdx + pIdx;
sourceData[currIdx * 4 + 0] = p.x;
sourceData[currIdx * 4 + 1] = p.y;
sourceData[currIdx * 4 + 2] = p.z;
sourceData[currIdx * 4 + 3] = 1;
});
}
setColorAt(idx, color, alpha = 1) {
let sourceData = this.source.data.data;
let startIdx = this.totalAmountPerInstance * (idx + 1) - 1;
sourceData[startIdx * 4 + 0] = color.r;
sourceData[startIdx * 4 + 1] = color.g;
sourceData[startIdx * 4 + 2] = color.b;
// sourceData[startIdx * 4 + 0] = 0;
// sourceData[startIdx * 4 + 1] = 0;
// sourceData[startIdx * 4 + 2] = 0;
sourceData[startIdx * 4 + 3] = alpha;
}
}
let scene = new THREE.Scene();
scene.background = new THREE.Color().multiplyScalar(0.1);
let camera = new THREE.PerspectiveCamera(45, innerWidth / innerHeight, 1, 1000);
camera.position.set(5, 8, 13).setLength(21);
let renderer = new THREE.WebGLRenderer({ antialias: false });
renderer.setSize(innerWidth, innerHeight);
document.body.appendChild(renderer.domElement);
window.addEventListener("resize", (event) => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
let controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
//controls.enablePan = false;
let box = new THREE.Box3Helper(new THREE.Box3().setFromCenterAndSize(new THREE.Vector3(), new THREE.Vector3().setScalar(10)), 0x7f7f7f);
scene.add(box);
let meshCheck = new THREE.Mesh(
new THREE.PlaneGeometry(4, 4),
new THREE.MeshBasicMaterial({ map: null })
)
meshCheck.position.z = -1
scene.add(meshCheck)
let savePts
const instCount = 2;
const verticesPerInstance = 3;
let dataTexture = new InstDataTexture(verticesPerInstance, instCount);
initFatLine()
function initFatLine() {
let curve = new THREE.CatmullRomCurve3(
new Array(5).fill().map((p) => {
return new THREE.Vector3();
}), false, "catmullrom", 2
);
let color = new THREE.Color();
let curves = [];
for (let i = 0; i < instCount; i++) {
curve.points.forEach((p) => {
p.random().subScalar(0.5).multiplyScalar(10);
});
let crv = curve.clone();
crv.userData = {
phase: (i / instCount)
}
crv.tension = getTension(crv.userData.phase);
crv.updateArcLengths();
curves.push(crv);
let pts = crv.getPoints(verticesPerInstance - 1);
console.log(pts)
savePts = pts
dataTexture.setDataAt(i, pts);
dataTexture.setColorAt(
i,
color.setHSL(i / instCount, 1, 0.5)
);
}
dataTexture.needsUpdate = true;
let dt = { value: dataTexture };
console.log(curves);
let flg = new LineSegmentsGeometry();
flg.instanceCount = instCount * (verticesPerInstance + 1);
let flm = new LineMaterial({
linewidth: 0.105,
worldUnits: true,
vertexColors: true,
wireframe: true,
// alphaToCoverage: true,
onBeforeCompile: shader => {
shader.uniforms.data = dt;
shader.uniforms.totalAmountPerInstance = dataTexture.uniforms.totalAmountPerInstance;
shader.uniforms.instancePerRow = dataTexture.uniforms.instancePerRow;
shader.vertexShader = `
uniform sampler2D data;
uniform float totalAmountPerInstance;
uniform float instancePerRow;
${shader.vertexShader}
`.replace(
`#ifdef USE_COLOR
vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd;
#endif`,
`
ivec2 tSize = textureSize(data, 0);
int vertID = int(mod(float(gl_InstanceID), totalAmountPerInstance - 2.));
int instID = int(floor(float(gl_InstanceID) / totalAmountPerInstance));
int fetchY = tSize.y - 1 - int(instID / int(instancePerRow));
int fetchX = int(mod(float(instID), instancePerRow) * totalAmountPerInstance);
#ifdef USE_COLOR
vColor.xyz = vec3(texelFetch(data, ivec2(fetchX + int(totalAmountPerInstance) - 1, fetchY), 0));
#endif`
)
.replace(
`// camera space
vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 );
vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 );`,
`// camera space
vec4 instStart = texelFetch(data, ivec2(fetchX + vertID, fetchY), 0);
vec4 instEnd = texelFetch(data, ivec2(fetchX + vertID + 1, fetchY), 0);
vec4 start = modelViewMatrix * instStart;
vec4 end = modelViewMatrix * instEnd;`
);
// console.log(shader.vertexShader);
}
});
let fl = new LineSegments2(flg, flm);
scene.add(fl);
}
let clock = new THREE.Clock();
setInterval(() => {
console.log("run", dataTexture)
for (let i = 0; i < instCount; i++) {
dataTexture.setDataAt(i, [new THREE.Vector3(0, Math.random(), 0), new THREE.Vector3(0, Math.random(), 0), new THREE.Vector3(0, Math.random(), 0)]);
dataTexture.needsUpdate = true;
}
}, 1000);
renderer.setAnimationLoop(() => {
let t = clock.getElapsedTime();
controls.update();
// curves.forEach((crv, cIdx) => {
// let ud = crv.userData;
// crv.tension = getTension(ud.phase + t * 0.1);
// crv.updateArcLengths();
// let pts = crv.getPoints(verticesPerInstance - 1);
// dataTexture.setDataAt(cIdx, pts);
// })
// dataTexture.needsUpdate = true;
meshCheck.material.map = dataTexture
renderer.render(scene, camera);
});
function getTension(phase) {
return (Math.sin(phase * Math.PI * 2) * 0.5 + 0.5) * 2;
}
</script>
</body>
</html>