forked from webaverse/app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infobox.js
352 lines (314 loc) · 10.2 KB
/
infobox.js
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
import * as THREE from 'three';
// import {world} from './world.js';
import {getRenderer} from './renderer.js';
// import easing from './easing.js';
// import {createObjectSprite} from './object-spriter.js';
// const cubicBezier = easing(0, 1, 0, 1);
const fullscreenVertexShader = `\
varying vec2 vUv;
void main() {
vUv = uv;
gl_Position = vec4(position.xy, 1.0, 1.0);
}
`;
const fullscreenFragmentShader = `\
uniform sampler2D uTex;
uniform float uTexEnabled;
uniform float uSelected;
uniform float uSelectFactor;
uniform float uTime;
uniform float numFrames;
uniform float numFramesPerRow;
uniform float outline_thickness;
varying vec2 vUv;
#define PI 3.1415926535897932384626433832795
//---------------------------------------------------------------------------
//1D Perlin noise implementation
//---------------------------------------------------------------------------
#define HASHSCALE 0.1031
float hash(float p) {
vec3 p3 = fract(vec3(p) * HASHSCALE);
p3 += dot(p3, p3.yzx + 19.19);
return fract((p3.x + p3.y) * p3.z);
}
float fade(float t) { return t*t*t*(t*(6.*t-15.)+10.); }
float grad(float hash, float p) {
int i = int(1e4*hash);
return (i & 1) == 0 ? p : -p;
}
float perlinNoise1D(float p) {
float pi = floor(p), pf = p - pi, w = fade(pf);
return mix(grad(hash(pi), pf), grad(hash(pi + 1.0), pf - 1.0), w) * 2.0;
}
float fbm(float pos, int octaves, float persistence) {
float total = 0., frequency = 1., amplitude = 1., maxValue = 0.;
for(int i = 0; i < octaves; ++i) {
total += perlinNoise1D(pos * frequency) * amplitude;
maxValue += amplitude;
amplitude *= persistence;
frequency *= 2.;
}
return total / maxValue;
}
struct Tri {
vec2 a;
vec2 b;
vec2 c;
};
vec2 rotateCCW(vec2 pos, float angle) {
float ca = cos(angle), sa = sin(angle);
return pos * mat2(ca, sa, -sa, ca);
}
vec2 rotateCCW(vec2 pos, vec2 around, float angle) {
pos -= around;
pos = rotateCCW(pos, angle);
pos += around;
return pos;
}
// return 1 if v inside the box, return 0 otherwise
bool insideAABB(vec2 v, vec2 bottomLeft, vec2 topRight) {
vec2 s = step(bottomLeft, v) - step(topRight, v);
return s.x * s.y > 0.;
}
bool isPointInTriangle(vec2 point, vec2 a, vec2 b, vec2 c) {
vec2 v0 = c - a;
vec2 v1 = b - a;
vec2 v2 = point - a;
float dot00 = dot(v0, v0);
float dot01 = dot(v0, v1);
float dot02 = dot(v0, v2);
float dot11 = dot(v1, v1);
float dot12 = dot(v1, v2);
float invDenom = 1. / (dot00 * dot11 - dot01 * dot01);
float u = (dot11 * dot02 - dot01 * dot12) * invDenom;
float v = (dot00 * dot12 - dot01 * dot02) * invDenom;
return (u >= 0.) && (v >= 0.) && (u + v < 1.);
}
bool isPointInTriangle(vec2 point, Tri tri) {
return isPointInTriangle(point, tri.a, tri.b, tri.c);
}
bool isInsideChevron(vec2 point, vec2 center, float width, float height, float skew) {
point -= center;
vec2 a = vec2(-width/2., height/2. - skew);
vec2 b = vec2(-width/2., -height/2. - skew);
vec2 c = vec2(0., height/2. + skew);
vec2 d = vec2(0., -height/2. + skew);
vec2 e = vec2(width/2., height/2. - skew);
vec2 f = vec2(width/2., -height/2. - skew);
Tri t1 = Tri(a, b, c);
Tri t2 = Tri(b, d, c);
Tri t3 = Tri(e, f, c);
Tri t4 = Tri(f, d, c);
return isPointInTriangle(point, t1) ||
isPointInTriangle(point, t2) ||
isPointInTriangle(point, t3) ||
isPointInTriangle(point, t4);
}
void main() {
// compute uv
float f = mod(uTime / 1000. * 0.5, 1.);
float frameIndex = floor(f * numFrames);
float x = mod(frameIndex, numFramesPerRow);
float y = floor(frameIndex / numFramesPerRow);
float frameSize = 1. / numFramesPerRow;
float xOffset = x * frameSize;
float yOffset = y * frameSize;
vec2 uv = vUv;
uv.x = xOffset + uv.x * frameSize;
uv.y = yOffset + uv.y * frameSize;
// gl_FragColor.gb = uv;
// gl_FragColor.a = 1.;
// sample texture
vec4 s;
if (uTexEnabled > 0.) {
s = texture2D(uTex, uv);
} else {
s = vec4(0.);
}
// outline
if (uTexEnabled > 0.) {
float sum = 0.0;
int passes = 32;
float passesFloat = float(passes);
float angleStep = 2.0 * PI / passesFloat;
for (int i = 0; i < passes; ++i) {
float n = float(i);
float angle = angleStep * n;
vec2 angleOffset = vec2(cos(angle), sin(angle)) * outline_thickness;
vec2 targetUv = vUv + angleOffset;
if (targetUv.x >= 0. && targetUv.x <= 1. && targetUv.y >= 0. && targetUv.y <= 1.) {
vec2 uv2 = uv + angleOffset / numFramesPerRow;
sum += texture(uTex, uv2).a;
}
}
if (sum > 0.) {
s.rgb = mix(
mix(vec3(0.8), vec3(1.), vUv.y),
s.rgb,
s.a
);
// s.a = max(s.a, 0.5);
s.a = 1.;
}
}
gl_FragColor = s;
/* vec3 c = vec3(0.1);
c = c * (1. - s.a) + s.rgb * s.a;
float backgroundAlpha = (1. - vUv.y * 2.) * 0.7;
float a = max(backgroundAlpha, s.a);
// result
gl_FragColor.rgb = c;
gl_FragColor.a = backgroundAlpha; */
}
`;
const localVector2D = new THREE.Vector2();
const localVector4D = new THREE.Vector4();
const _makeInfoboxRendererScene = () => {
const scene = new THREE.Scene();
const fullScreenQuadMesh = new THREE.Mesh(
new THREE.PlaneBufferGeometry(2, 2),
new THREE.ShaderMaterial({
uniforms: {
uTex: {
value: null,
needsUpdate: false,
},
uTexEnabled: {
value: 0,
needsUpdate: true,
},
uTime: {
value: 0,
needsUpdate: true,
},
numFrames: {
value: 0,
needsUpdate: true,
},
numFramesPerRow: {
value: 0,
needsUpdate: true,
},
outline_thickness: {
value: 0.02,
needsUpdate: true,
},
},
vertexShader: fullscreenVertexShader,
fragmentShader: fullscreenFragmentShader,
depthTest: false,
transparent: true,
}),
);
fullScreenQuadMesh.frustumCulled = false;
scene.add(fullScreenQuadMesh);
scene.fullScreenQuadMesh = fullScreenQuadMesh;
return scene;
};
class InfoboxRenderer {
constructor(width, height, selected) {
this.width = width;
this.height = height;
this.scene = _makeInfoboxRendererScene();
this.camera = new THREE.OrthographicCamera(
-1,
1,
1,
-1,
0,
1000
);
this.canvases = [];
this.selected = selected;
this.selectFactor = +selected;
this.needsUpdate = false;
}
addCanvas(canvas) {
const ctx = canvas.getContext('2d');
canvas.ctx = ctx;
this.canvases.push(canvas);
this.needsUpdate = true;
}
removeCanvas(canvas) {
this.canvases.splice(this.canvases.indexOf(canvas), 1);
this.needsUpdate = true;
}
setSpritesheet(spritesheet) {
if (spritesheet) {
const {
result,
numFrames,
// frameSize,
numFramesPerRow,
} = spritesheet;
this.scene.fullScreenQuadMesh.material.uniforms.uTex.value = result;
this.scene.fullScreenQuadMesh.material.uniforms.uTex.needsUpdate = true;
this.scene.fullScreenQuadMesh.material.uniforms.uTexEnabled.value = 1;
this.scene.fullScreenQuadMesh.material.uniforms.uTexEnabled.needsUpdate = true;
this.scene.fullScreenQuadMesh.material.uniforms.numFrames.value = numFrames;
this.scene.fullScreenQuadMesh.material.uniforms.numFrames.needsUpdate = true;
this.scene.fullScreenQuadMesh.material.uniforms.numFramesPerRow.value = numFramesPerRow;
this.scene.fullScreenQuadMesh.material.uniforms.numFramesPerRow.needsUpdate = true;
} else {
/* this.scene.fullScreenQuadMesh.material.uniforms.uTex.value = null;
this.scene.fullScreenQuadMesh.material.uniforms.uTex.needsUpdate = true; */
this.scene.fullScreenQuadMesh.material.uniforms.uTexEnabled.value = 0;
this.scene.fullScreenQuadMesh.material.uniforms.uTexEnabled.needsUpdate = true;
/* this.scene.fullScreenQuadMesh.material.uniforms.numFrames.value = numFrames;
this.scene.fullScreenQuadMesh.material.uniforms.numFrames.needsUpdate = true;
this.scene.fullScreenQuadMesh.material.uniforms.numFramesPerRow.value = numFramesPerRow;
this.scene.fullScreenQuadMesh.material.uniforms.numFramesPerRow.needsUpdate = true; */
}
}
update(timestamp, timeDiff) {
const renderer = getRenderer();
const size = renderer.getSize(localVector2D);
const pixelRatio = renderer.getPixelRatio();
const _render = () => {
// push old state
// const oldRenderTarget = renderer.getRenderTarget();
const oldViewport = renderer.getViewport(localVector4D);
{
// const smoothedSelectFactor = this.selected ? cubicBezier(this.selectFactor) : 1 - cubicBezier(1 - this.selectFactor);
/* this.scene.fullScreenQuadMesh.material.uniforms.uSelected.value = +this.selected;
this.scene.fullScreenQuadMesh.material.uniforms.uSelected.needsUpdate = true;
this.scene.fullScreenQuadMesh.material.uniforms.uSelectFactor.value = smoothedSelectFactor;
this.scene.fullScreenQuadMesh.material.uniforms.uSelectFactor.needsUpdate = true; */
this.scene.fullScreenQuadMesh.material.uniforms.uTime.value = timestamp;
this.scene.fullScreenQuadMesh.material.uniforms.uTime.needsUpdate = true;
renderer.setViewport(0, 0, this.width, this.height);
renderer.clear();
renderer.render(this.scene, this.camera);
}
// pop old state
// renderer.setRenderTarget(oldRenderTarget);
renderer.setViewport(oldViewport);
};
_render();
const _copyToCanvases = () => {
for (const canvas of this.canvases) {
const {
width,
height,
ctx
} = canvas;
ctx.clearRect(0, 0, width, height);
ctx.drawImage(
renderer.domElement,
0,
size.y * pixelRatio - this.height * pixelRatio,
this.width * pixelRatio,
this.height * pixelRatio,
0,
0,
width,
height
);
}
};
_copyToCanvases();
}
}
export {
InfoboxRenderer,
};