@yaks/voxel 0.0.0-stage → 0.2.36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +79 -2
- package/_dist/boxes.d.ts +80 -0
- package/_dist/boxes.d.ts.map +1 -0
- package/_dist/chartbook.d.ts +14 -0
- package/_dist/chartbook.d.ts.map +1 -0
- package/_dist/chartsheet.d.ts +8 -0
- package/_dist/chartsheet.d.ts.map +1 -0
- package/_dist/light.d.ts +18 -0
- package/_dist/light.d.ts.map +1 -0
- package/_dist/mesh.d.ts +120 -0
- package/_dist/mesh.d.ts.map +1 -0
- package/_dist/mod.d.ts +7 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/stream.d.ts +33 -0
- package/_dist/stream.d.ts.map +1 -0
- package/_dist/three/mod.d.ts +5 -0
- package/_dist/three/mod.d.ts.map +1 -0
- package/_dist/three/parts.d.ts +59 -0
- package/_dist/three/parts.d.ts.map +1 -0
- package/_dist/three/puppet.d.ts +93 -0
- package/_dist/three/puppet.d.ts.map +1 -0
- package/_dist/three/soft.d.ts +27 -0
- package/_dist/three/soft.d.ts.map +1 -0
- package/boxes.js +265 -0
- package/boxes.js.map +1 -0
- package/boxes.ts +266 -0
- package/chartbook.js +63 -0
- package/chartbook.js.map +1 -0
- package/chartbook.ts +88 -0
- package/chartsheet.js +31 -0
- package/chartsheet.js.map +1 -0
- package/chartsheet.ts +32 -0
- package/deno.json +1 -0
- package/light.js +45 -0
- package/light.js.map +1 -0
- package/light.ts +49 -0
- package/mesh.js +577 -0
- package/mesh.js.map +1 -0
- package/mesh.ts +624 -0
- package/mod.js +7 -0
- package/mod.js.map +1 -0
- package/mod.ts +7 -0
- package/package.json +45 -4
- package/stream.js +80 -0
- package/stream.js.map +1 -0
- package/stream.ts +86 -0
- package/three/mod.js +7 -0
- package/three/mod.js.map +1 -0
- package/three/mod.ts +7 -0
- package/three/parts.js +181 -0
- package/three/parts.js.map +1 -0
- package/three/parts.ts +185 -0
- package/three/puppet.js +311 -0
- package/three/puppet.js.map +1 -0
- package/three/puppet.ts +471 -0
- package/three/soft.js +338 -0
- package/three/soft.js.map +1 -0
- package/three/soft.ts +382 -0
package/three/soft.ts
ADDED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
// The soft look. Every voxel in a scene is drawn by this one material: three's
|
|
2
|
+
// Lambert, taught three things in its shader.
|
|
3
|
+
//
|
|
4
|
+
// Rounded edges. A face knows which of its edges are convex and where
|
|
5
|
+
// across it a pixel lies (mesh.ts `edge`). Near a convex edge the normal
|
|
6
|
+
// leans outward and the colour brightens a touch, so a hard cube reads as a
|
|
7
|
+
// soft one without a single extra triangle.
|
|
8
|
+
//
|
|
9
|
+
// Voxels on merged faces. A pixel is tinted by the voxel cell it falls in,
|
|
10
|
+
// so a large flat quad of ground still shows its voxels.
|
|
11
|
+
//
|
|
12
|
+
// Metal. A face made of metal (mesh.ts `METAL`) shows less of its own
|
|
13
|
+
// colour lit and more of what it mirrors: the sky above its horizon and the
|
|
14
|
+
// ground below it, as the scene's hemisphere light colours them, whiter
|
|
15
|
+
// toward a grazing edge; and each light that reaches it, the sun and the
|
|
16
|
+
// fire, is a tight bright highlight on it that slides as it turns. Both
|
|
17
|
+
// take the metal's own colour, so a tinted metal shines its tint, and both
|
|
18
|
+
// follow the day's light as the scene moves it. A few sums a pixel, and none
|
|
19
|
+
// for anything matte.
|
|
20
|
+
//
|
|
21
|
+
// A flash. A mesh glows for a moment when struck.
|
|
22
|
+
//
|
|
23
|
+
// A line of sight. The ground and what stands on it thin away where, as the
|
|
24
|
+
// camera sees them, they cover the subject or nearby attackers, and right
|
|
25
|
+
// around the camera (`see`). The clearance is round but ends above the
|
|
26
|
+
// ground, so a tree in the way shows the fight without hollowing the land.
|
|
27
|
+
// It is a stipple, pixels left out in an even pattern, so nothing needs
|
|
28
|
+
// sorting. The roof and upper floors of a building the subject is in or behind
|
|
29
|
+
// thin away the same way, over a height within its box (`cut`).
|
|
30
|
+
//
|
|
31
|
+
// No seams. A vertex is placed in the world before it is seen from the
|
|
32
|
+
// camera, never through the two at once, so where two chunks meet, the same
|
|
33
|
+
// point in each lands on the same pixel: a chunk stands at whole metres and
|
|
34
|
+
// its corners at whole voxels, which float32 adds exactly.
|
|
35
|
+
// @ts-types="npm:@types/three@^0.186.0"
|
|
36
|
+
import * as THREE from 'three'
|
|
37
|
+
import type { Packed } from '../mesh.ts'
|
|
38
|
+
|
|
39
|
+
/** How many boxes a see-through material fades at once. */
|
|
40
|
+
export let CUTS = 3
|
|
41
|
+
/** The nearest subjects whose silhouettes the woods also leave clear. */
|
|
42
|
+
export let FOES = 3
|
|
43
|
+
|
|
44
|
+
/** A geometry over what a mesher wrote, packed (mesh.ts `pack`): the arrays
|
|
45
|
+
* become the geometry's own, uncopied. */
|
|
46
|
+
export let geometry = (p: Packed): THREE.BufferGeometry => {
|
|
47
|
+
let g = new THREE.BufferGeometry()
|
|
48
|
+
g.setAttribute('position', new THREE.BufferAttribute(p.pos, 3))
|
|
49
|
+
g.setAttribute('normal', new THREE.BufferAttribute(p.nrm, 4))
|
|
50
|
+
g.setAttribute('color', new THREE.BufferAttribute(p.col, 4, true))
|
|
51
|
+
g.setAttribute('edge', new THREE.BufferAttribute(p.edge, 4))
|
|
52
|
+
g.setAttribute('bw', new THREE.BufferAttribute(p.bw, 3))
|
|
53
|
+
g.setIndex(new THREE.BufferAttribute(p.idx, 1))
|
|
54
|
+
g.computeBoundingSphere()
|
|
55
|
+
g.computeBoundingBox()
|
|
56
|
+
return g
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
let VERTEX_PARS = /* glsl */ `
|
|
60
|
+
attribute vec4 edge;
|
|
61
|
+
attribute vec3 bw;
|
|
62
|
+
// The colours come as sRGB bytes (mesh.ts), and light blends in linear.
|
|
63
|
+
vec3 unsrgb(vec3 c) {
|
|
64
|
+
return mix(c / 12.92, pow((c + .055) / 1.055, vec3(2.4)), step(.04045, c));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
varying vec2 vFace;
|
|
68
|
+
varying vec4 vRim;
|
|
69
|
+
varying vec2 vBw;
|
|
70
|
+
varying vec3 vTu;
|
|
71
|
+
varying vec3 vTv;
|
|
72
|
+
varying vec3 vCell;
|
|
73
|
+
varying vec3 vAt;
|
|
74
|
+
varying float vMaterial;
|
|
75
|
+
varying float vWarmth;
|
|
76
|
+
`
|
|
77
|
+
|
|
78
|
+
let VERTEX = /* glsl */ `
|
|
79
|
+
vec4 worldAt = vec4(transformed, 1.);
|
|
80
|
+
#ifdef USE_INSTANCING
|
|
81
|
+
worldAt = instanceMatrix * worldAt;
|
|
82
|
+
#endif
|
|
83
|
+
vAt = (modelMatrix * worldAt).xyz;
|
|
84
|
+
vFace = edge.xy;
|
|
85
|
+
vMaterial = edge.w;
|
|
86
|
+
vWarmth = floor(edge.z / 16.) / 15.;
|
|
87
|
+
vRim = mod(floor(edge.z / vec4(1., 2., 4., 8.)), 2.);
|
|
88
|
+
vBw = bw.xy;
|
|
89
|
+
// The face's own axes, from its normal as it was meshed, turned as the mesh
|
|
90
|
+
// is turned: by its instance, or by the bone that carries it.
|
|
91
|
+
vec3 an = abs(normal);
|
|
92
|
+
vec3 tu = an.x > .5 ? vec3(0., 0., 1.) : vec3(1., 0., 0.);
|
|
93
|
+
vec3 tv = an.y > .5 ? vec3(0., 0., 1.) : vec3(0., 1., 0.);
|
|
94
|
+
#ifdef USE_INSTANCING
|
|
95
|
+
tu = mat3(instanceMatrix) * tu;
|
|
96
|
+
tv = mat3(instanceMatrix) * tv;
|
|
97
|
+
#endif
|
|
98
|
+
#ifdef USE_SKINNING
|
|
99
|
+
tu = (skinMatrix * vec4(tu, 0.)).xyz;
|
|
100
|
+
tv = (skinMatrix * vec4(tv, 0.)).xyz;
|
|
101
|
+
#endif
|
|
102
|
+
vTu = normalize(normalMatrix * tu);
|
|
103
|
+
vTv = normalize(normalMatrix * tv);
|
|
104
|
+
vCell = (position - normal * (bw.z * .5)) / bw.z;
|
|
105
|
+
`
|
|
106
|
+
|
|
107
|
+
// three's project_vertex, but for the world before the view.
|
|
108
|
+
let PROJECT = /* glsl */ `
|
|
109
|
+
vec4 mvPosition = vec4(transformed, 1.);
|
|
110
|
+
#ifdef USE_INSTANCING
|
|
111
|
+
mvPosition = instanceMatrix * mvPosition;
|
|
112
|
+
#endif
|
|
113
|
+
mvPosition = viewMatrix * (modelMatrix * mvPosition);
|
|
114
|
+
gl_Position = projectionMatrix * mvPosition;
|
|
115
|
+
`
|
|
116
|
+
|
|
117
|
+
let FRAGMENT_PARS = /* glsl */ `
|
|
118
|
+
uniform vec3 flash;
|
|
119
|
+
uniform float flashing;
|
|
120
|
+
uniform float speckle;
|
|
121
|
+
uniform float night;
|
|
122
|
+
varying vec2 vFace;
|
|
123
|
+
varying vec4 vRim;
|
|
124
|
+
varying vec2 vBw;
|
|
125
|
+
varying vec3 vTu;
|
|
126
|
+
varying vec3 vTv;
|
|
127
|
+
varying vec3 vCell;
|
|
128
|
+
varying vec3 vAt;
|
|
129
|
+
varying float vMaterial;
|
|
130
|
+
varying float vWarmth;
|
|
131
|
+
uniform vec3 seeFrom;
|
|
132
|
+
uniform vec3 seeFeet;
|
|
133
|
+
uniform float seeTall;
|
|
134
|
+
uniform vec4 seeFoes[FOES];
|
|
135
|
+
uniform vec4 cutLo[CUTS];
|
|
136
|
+
uniform vec3 cutHi[CUTS];
|
|
137
|
+
float edge(float flag, float d, float w) {
|
|
138
|
+
return flag * (1. - smoothstep(0., w, d));
|
|
139
|
+
}
|
|
140
|
+
float b2(vec2 p) {
|
|
141
|
+
float x = mod(p.x, 2.), y = mod(p.y, 2.);
|
|
142
|
+
return 2. * abs(x - y) + y;
|
|
143
|
+
}
|
|
144
|
+
// A 4 by 4 ordered dither: how far through the pattern this pixel is, from
|
|
145
|
+
// 1/32 to 31/32. A pixel's coordinates are its middle, so they are floored
|
|
146
|
+
// first, or some pixels would be past 1 and never left out.
|
|
147
|
+
float bayer(vec2 p) {
|
|
148
|
+
p = floor(p);
|
|
149
|
+
return (4. * b2(p) + b2(floor(p / 2.)) + .5) / 16.;
|
|
150
|
+
}
|
|
151
|
+
`
|
|
152
|
+
|
|
153
|
+
// A pixel's ray meets a disc at the subject's distance from the camera.
|
|
154
|
+
// Keep the lower edge above the ground and the last metre before the subject
|
|
155
|
+
// whole, so only the intervening woods thin away.
|
|
156
|
+
let SIGHT = /* glsl */ `
|
|
157
|
+
#ifdef SEE
|
|
158
|
+
vec3 sl = seeFeet + vec3(0., seeTall * .5, 0.) - seeFrom;
|
|
159
|
+
float sL = max(length(sl), 1e-3);
|
|
160
|
+
vec3 ahead = sl / sL;
|
|
161
|
+
float along = dot(vAt - seeFrom, ahead);
|
|
162
|
+
vec3 upward = normalize(vec3(0., 1., 0.) - ahead * ahead.y);
|
|
163
|
+
vec3 met = (vAt - seeFrom) * (sL / max(along, 1e-3)) - sl;
|
|
164
|
+
float up = dot(met, upward);
|
|
165
|
+
float body = (1. - smoothstep(1.7, 2.4, length(met))) *
|
|
166
|
+
smoothstep(-seeTall * .5 - .35, -seeTall * .5 - .1, up);
|
|
167
|
+
float thin = along > 0. && along < sL - 1. ? body : 0.;
|
|
168
|
+
for (int i = 0; i < FOES; i++) {
|
|
169
|
+
vec4 foe = seeFoes[i];
|
|
170
|
+
if (foe.w <= 0.) continue;
|
|
171
|
+
vec3 toFoe = foe.xyz - seeFrom;
|
|
172
|
+
float distanceToFoe = max(length(toFoe), 1e-3);
|
|
173
|
+
float alongFoe = dot(vAt - seeFrom, toFoe / distanceToFoe);
|
|
174
|
+
if (alongFoe <= 0. || alongFoe >= distanceToFoe - .5) continue;
|
|
175
|
+
vec3 metFoe = (vAt - seeFrom) *
|
|
176
|
+
(distanceToFoe / max(alongFoe, 1e-3)) - toFoe;
|
|
177
|
+
float upFoe = dot(metFoe,
|
|
178
|
+
normalize(vec3(0., 1., 0.) - toFoe *
|
|
179
|
+
(toFoe.y / (distanceToFoe * distanceToFoe))));
|
|
180
|
+
float clearFoe = (1. - smoothstep(foe.w * .7, foe.w, length(metFoe))) *
|
|
181
|
+
smoothstep(-foe.w * .8, -foe.w * .55, upFoe);
|
|
182
|
+
thin = max(thin, clearFoe);
|
|
183
|
+
}
|
|
184
|
+
float near = 1. - smoothstep(1.2, 2., length(vAt - seeFrom));
|
|
185
|
+
thin = max(thin, near * smoothstep(seeFrom.y - 1., seeFrom.y - .5, vAt.y));
|
|
186
|
+
float gone = thin * .8;
|
|
187
|
+
for (int i = 0; i < CUTS; i++) {
|
|
188
|
+
vec3 lo = cutLo[i].xyz;
|
|
189
|
+
if (all(greaterThanEqual(vAt, lo)) && all(lessThanEqual(vAt, cutHi[i]))) {
|
|
190
|
+
gone = max(gone, cutLo[i].w);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (bayer(gl_FragCoord.xy) < gone) discard;
|
|
194
|
+
#endif
|
|
195
|
+
`
|
|
196
|
+
|
|
197
|
+
let COLOR = /* glsl */ `
|
|
198
|
+
float r0 = edge(vRim.x, vFace.x, vBw.x);
|
|
199
|
+
float r1 = edge(vRim.y, 1. - vFace.x, vBw.x);
|
|
200
|
+
float r2 = edge(vRim.z, vFace.y, vBw.y);
|
|
201
|
+
float r3 = edge(vRim.w, 1. - vFace.y, vBw.y);
|
|
202
|
+
float rounded = max(max(r0, r1), max(r2, r3));
|
|
203
|
+
vec3 cellOf = floor(vCell + 1e-3);
|
|
204
|
+
float speck = fract(sin(dot(cellOf, vec3(12.9898, 78.233, 37.719))) * 43758.5453);
|
|
205
|
+
diffuseColor.rgb *= 1. + speckle * (speck - .5);
|
|
206
|
+
diffuseColor.rgb *= 1. + .14 * rounded;
|
|
207
|
+
diffuseColor.rgb = mix(diffuseColor.rgb, flash, flashing);
|
|
208
|
+
`
|
|
209
|
+
|
|
210
|
+
let NORMAL = /* glsl */ `
|
|
211
|
+
normal = normalize(normal + (vTu * (r1 - r0) + vTv * (r3 - r2)) * .95);
|
|
212
|
+
`
|
|
213
|
+
|
|
214
|
+
// Each light as Lambert has it, and on metal its highlight too: a tight core
|
|
215
|
+
// going white in a wider glow of the metal's colour.
|
|
216
|
+
let LIGHT = /* glsl */ `
|
|
217
|
+
bool metal() { return vMaterial > .5 && vMaterial < 1.5; }
|
|
218
|
+
void RE_Direct_Soft(
|
|
219
|
+
const in IncidentLight light,
|
|
220
|
+
const in vec3 at,
|
|
221
|
+
const in vec3 n,
|
|
222
|
+
const in vec3 view,
|
|
223
|
+
const in vec3 coat,
|
|
224
|
+
const in LambertMaterial material,
|
|
225
|
+
inout ReflectedLight reflected
|
|
226
|
+
) {
|
|
227
|
+
RE_Direct_Lambert(light, at, n, view, coat, material, reflected);
|
|
228
|
+
if (!metal()) return;
|
|
229
|
+
float nh = saturate(dot(n, normalize(light.direction + view)));
|
|
230
|
+
float nl = saturate(dot(n, light.direction));
|
|
231
|
+
vec3 glint = mix(material.diffuseColor, vec3(1.), .6) * pow(nh, 90.) * 4.
|
|
232
|
+
+ material.diffuseColor * pow(nh, 12.) * .5;
|
|
233
|
+
reflected.directSpecular += light.color * nl * glint;
|
|
234
|
+
}
|
|
235
|
+
#undef RE_Direct
|
|
236
|
+
#define RE_Direct RE_Direct_Soft
|
|
237
|
+
`
|
|
238
|
+
|
|
239
|
+
// What metal mirrors, and its highlights, over a little of its own light: the
|
|
240
|
+
// sky down to a crisp horizon, paler low and deeper overhead, and the ground
|
|
241
|
+
// dark and grey under it.
|
|
242
|
+
let MIRROR = /* glsl */ `
|
|
243
|
+
if (metal()) {
|
|
244
|
+
vec3 seen = reflect(-geometryViewDir, geometryNormal);
|
|
245
|
+
float grazing = pow(1. - saturate(dot(geometryNormal, geometryViewDir)), 4.);
|
|
246
|
+
vec3 around = vec3(0.);
|
|
247
|
+
#if NUM_HEMI_LIGHTS > 0
|
|
248
|
+
HemisphereLight hemi = hemisphereLights[0];
|
|
249
|
+
float up = dot(seen, hemi.direction);
|
|
250
|
+
vec3 low = hemi.skyColor * 1.1, high = hemi.skyColor * hemi.skyColor;
|
|
251
|
+
vec3 earth = hemi.groundColor;
|
|
252
|
+
earth = mix(vec3(dot(earth, vec3(.3, .6, .1))), earth, .4) * .5;
|
|
253
|
+
around = mix(earth, mix(low, high, saturate(up)), smoothstep(-.03, .03, up));
|
|
254
|
+
#endif
|
|
255
|
+
outgoingLight = outgoingLight * .35
|
|
256
|
+
+ around * mix(diffuseColor.rgb, vec3(1.), grazing) * .7
|
|
257
|
+
+ reflectedLight.directSpecular;
|
|
258
|
+
}
|
|
259
|
+
outgoingLight += vec3(1., .32, .08) * vWarmth * (.18 + .82 * night);
|
|
260
|
+
`
|
|
261
|
+
|
|
262
|
+
/** A soft material: `speckle` is how much each voxel's shade wobbles, and a
|
|
263
|
+
* material that can `see` through (the ground's) thins away between the
|
|
264
|
+
* camera and the subject once `sight` says where both are. */
|
|
265
|
+
export let soft = (
|
|
266
|
+
opts: {
|
|
267
|
+
speckle?: number
|
|
268
|
+
transparent?: boolean
|
|
269
|
+
opacity?: number
|
|
270
|
+
see?: boolean
|
|
271
|
+
} = {},
|
|
272
|
+
): THREE.MeshLambertMaterial => {
|
|
273
|
+
let m = new THREE.MeshLambertMaterial({
|
|
274
|
+
vertexColors: true,
|
|
275
|
+
transparent: opts.transparent ?? false,
|
|
276
|
+
opacity: opts.opacity ?? 1,
|
|
277
|
+
})
|
|
278
|
+
let uniforms = {
|
|
279
|
+
speckle: { value: opts.speckle ?? 0.1 },
|
|
280
|
+
night: { value: 0 },
|
|
281
|
+
flash: { value: new THREE.Color(1, 0.35, 0.3) },
|
|
282
|
+
flashing: { value: 0 },
|
|
283
|
+
seeFrom: { value: new THREE.Vector3() },
|
|
284
|
+
seeFeet: { value: new THREE.Vector3() },
|
|
285
|
+
seeTall: { value: 1 },
|
|
286
|
+
seeFoes: {
|
|
287
|
+
value: Array.from({ length: FOES }, () => new THREE.Vector4()),
|
|
288
|
+
},
|
|
289
|
+
cutLo: { value: Array.from({ length: CUTS }, () => new THREE.Vector4()) },
|
|
290
|
+
cutHi: { value: Array.from({ length: CUTS }, () => new THREE.Vector3()) },
|
|
291
|
+
}
|
|
292
|
+
m.userData.soft = uniforms
|
|
293
|
+
m.defines = { CUTS, FOES, ...opts.see ? { SEE: '' } : {} }
|
|
294
|
+
m.onBeforeCompile = (s) => {
|
|
295
|
+
Object.assign(s.uniforms, uniforms)
|
|
296
|
+
s.vertexShader = s.vertexShader
|
|
297
|
+
.replace('#include <common>', `#include <common>\n${VERTEX_PARS}`)
|
|
298
|
+
.replace('#include <begin_vertex>', `#include <begin_vertex>\n${VERTEX}`)
|
|
299
|
+
.replace('#include <project_vertex>', PROJECT)
|
|
300
|
+
.replace(
|
|
301
|
+
'#include <color_vertex>',
|
|
302
|
+
'#include <color_vertex>\nvColor.rgb = unsrgb(vColor.rgb);',
|
|
303
|
+
)
|
|
304
|
+
s.fragmentShader = s.fragmentShader
|
|
305
|
+
.replace('#include <common>', `#include <common>\n${FRAGMENT_PARS}`)
|
|
306
|
+
.replace(
|
|
307
|
+
'#include <clipping_planes_fragment>',
|
|
308
|
+
`#include <clipping_planes_fragment>\n${SIGHT}`,
|
|
309
|
+
)
|
|
310
|
+
.replace(
|
|
311
|
+
'#include <color_fragment>',
|
|
312
|
+
`#include <color_fragment>\n${COLOR}`,
|
|
313
|
+
)
|
|
314
|
+
.replace(
|
|
315
|
+
'#include <normal_fragment_maps>',
|
|
316
|
+
`#include <normal_fragment_maps>\n${NORMAL}`,
|
|
317
|
+
)
|
|
318
|
+
.replace(
|
|
319
|
+
'#include <lights_lambert_pars_fragment>',
|
|
320
|
+
`#include <lights_lambert_pars_fragment>\n${LIGHT}`,
|
|
321
|
+
)
|
|
322
|
+
.replace(
|
|
323
|
+
'#include <envmap_fragment>',
|
|
324
|
+
`#include <envmap_fragment>\n${MIRROR}`,
|
|
325
|
+
)
|
|
326
|
+
}
|
|
327
|
+
m.customProgramCacheKey = () => opts.see ? 'soft-see' : 'soft'
|
|
328
|
+
return m
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/** The sight a see-through material keeps clear: from the camera (`from`)
|
|
332
|
+
* of the subject standing at `feet`, `tall` metres tall, and the nearest foes
|
|
333
|
+
* aiming at them. A foe's fourth coordinate is its clearance radius. */
|
|
334
|
+
export let sight = (
|
|
335
|
+
m: THREE.Material,
|
|
336
|
+
from: THREE.Vector3,
|
|
337
|
+
feet: THREE.Vector3,
|
|
338
|
+
tall: number,
|
|
339
|
+
foes: THREE.Vector4[] = [],
|
|
340
|
+
): void => {
|
|
341
|
+
let u = m.userData.soft
|
|
342
|
+
if (!u) return
|
|
343
|
+
u.seeFrom.value.copy(from)
|
|
344
|
+
u.seeFeet.value.copy(feet)
|
|
345
|
+
u.seeTall.value = tall
|
|
346
|
+
u.seeFoes.value.forEach((at: THREE.Vector4, i: number) =>
|
|
347
|
+
foes[i] ? at.copy(foes[i]) : at.set(0, 0, 0, 0)
|
|
348
|
+
)
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** The boxes a see-through material fades, `lo` to `hi` in metres, each by
|
|
352
|
+
* `fade` (0 not at all, 1 gone); the rest of its CUTS fade none. */
|
|
353
|
+
export let cut = (
|
|
354
|
+
m: THREE.Material,
|
|
355
|
+
boxes: { lo: THREE.Vector3Tuple; hi: THREE.Vector3Tuple; fade: number }[],
|
|
356
|
+
): void => {
|
|
357
|
+
let u = m.userData.soft
|
|
358
|
+
if (!u) return
|
|
359
|
+
u.cutLo.value.forEach((lo: THREE.Vector4, i: number) => {
|
|
360
|
+
let b = boxes[i]
|
|
361
|
+
lo.set(b?.lo[0] ?? 0, b?.lo[1] ?? 0, b?.lo[2] ?? 0, b?.fade ?? 0)
|
|
362
|
+
u.cutHi.value[i].set(b?.hi[0] ?? -1, b?.hi[1] ?? -1, b?.hi[2] ?? -1)
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/** How strongly a soft material glows its flash colour, 0 to 1. */
|
|
367
|
+
export let flash = (
|
|
368
|
+
m: THREE.Material,
|
|
369
|
+
amount: number,
|
|
370
|
+
color?: THREE.Color,
|
|
371
|
+
): void => {
|
|
372
|
+
let u = m.userData.soft
|
|
373
|
+
if (!u) return
|
|
374
|
+
u.flashing.value = amount
|
|
375
|
+
if (color) u.flash.value.copy(color)
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** How much the day's light has fallen, shared by every soft mesh. */
|
|
379
|
+
export let night = (m: THREE.Material, amount: number): void => {
|
|
380
|
+
let u = m.userData.soft
|
|
381
|
+
if (u) u.night.value = amount
|
|
382
|
+
}
|