@expofp/renderer 3.49.2 → 3.51.0
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/dist/Renderer.d.ts +2 -2
- package/dist/core/SquareDataTexture.d.ts +5 -0
- package/dist/core/SquareDataTexture.js +6 -0
- package/dist/core/batch/BatchedMesh.d.ts +12 -0
- package/dist/core/batch/BatchedMesh.js +29 -0
- package/dist/core/instanced/InstancedMesh.d.ts +5 -0
- package/dist/core/instanced/InstancedMesh.js +12 -0
- package/dist/core/sprites/BatchedSprites.js +2 -2
- package/dist/core/sprites/RaycastingSprites.d.ts +9 -1
- package/dist/core/sprites/RaycastingSprites.js +23 -3
- package/dist/core/sprites/sprite-material.js +5 -6
- package/dist/core/text/MemberTexture.js +2 -4
- package/dist/events/LayoutDriver.js +17 -1
- package/dist/events/layout-targets.js +30 -5
- package/dist/events/placement/box.d.ts +14 -0
- package/dist/events/placement/box.js +19 -2
- package/dist/events/placement/index.d.ts +1 -0
- package/dist/events/placement/index.js +1 -0
- package/dist/events/placement/pass.js +9 -8
- package/dist/events/pointer.d.ts +27 -6
- package/dist/events/pointer.js +50 -9
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/objects/containers/SlotContainer.d.ts +5 -0
- package/dist/objects/containers/SlotContainer.js +7 -0
- package/dist/objects/images/Images.js +1 -1
- package/dist/objects/lighting/Lighting.d.ts +56 -38
- package/dist/objects/lighting/Lighting.js +136 -344
- package/dist/objects/lighting/lighting-gui.js +32 -18
- package/dist/objects/lighting/studio-environment.d.ts +15 -43
- package/dist/objects/lighting/studio-environment.js +18 -126
- package/dist/objects/lighting/sun-shadow-map.d.ts +39 -15
- package/dist/objects/lighting/sun-shadow-map.js +163 -26
- package/dist/objects/materials/ColorMaterial.d.ts +11 -8
- package/dist/objects/materials/ColorMaterial.js +33 -11
- package/dist/objects/materials/ModelMaterial.js +3 -2
- package/dist/objects/materials/OutlineMaterial.d.ts +21 -0
- package/dist/objects/materials/OutlineMaterial.js +77 -0
- package/dist/objects/materials/TextureMaterial.d.ts +10 -0
- package/dist/objects/materials/TextureMaterial.js +13 -2
- package/dist/objects/materials/effects/ao/index.d.ts +2 -5
- package/dist/objects/materials/effects/ao/index.js +4 -7
- package/dist/objects/materials/effects/contact-shadow.d.ts +21 -10
- package/dist/objects/materials/effects/contact-shadow.js +60 -16
- package/dist/objects/materials/effects/effects-context.d.ts +2 -1
- package/dist/objects/materials/effects/effects-context.js +2 -1
- package/dist/objects/materials/effects/side-shade.d.ts +40 -0
- package/dist/objects/materials/effects/side-shade.js +52 -0
- package/dist/objects/materials/effects/sun-shadow.d.ts +5 -21
- package/dist/objects/materials/effects/sun-shadow.js +56 -89
- package/dist/objects/models/Models.d.ts +3 -2
- package/dist/objects/models/Models.js +46 -8
- package/dist/objects/models/gltf-cache.js +1 -1
- package/dist/objects/models/model-asset.d.ts +84 -20
- package/dist/objects/models/model-asset.js +171 -40
- package/dist/objects/models/model-container.d.ts +46 -10
- package/dist/objects/models/model-container.js +92 -19
- package/dist/objects/shapes/Shapes.js +11 -6
- package/dist/objects/shapes/shape-container.d.ts +3 -2
- package/dist/objects/shapes/shape-container.js +6 -4
- package/dist/objects/shapes/shape-geometry.d.ts +16 -4
- package/dist/objects/shapes/shape-geometry.js +17 -13
- package/dist/types/defs.d.ts +30 -1
- package/dist/utils/math.d.ts +14 -0
- package/dist/utils/math.js +20 -0
- package/package.json +2 -2
|
@@ -1,25 +1,31 @@
|
|
|
1
1
|
import { degToRad } from '@expofp/geometry';
|
|
2
2
|
import { Box3, PCFShadowMap, Vector3, } from 'three';
|
|
3
|
+
import { BatchedMeshImpl } from '../../core/batch/BatchedMesh';
|
|
3
4
|
import { clamp } from '../../utils/math';
|
|
4
5
|
/**
|
|
5
|
-
* The
|
|
6
|
-
*
|
|
6
|
+
* The sun's shadow map: where the sun stands, which area the map covers, and when the map is drawn
|
|
7
|
+
* again.
|
|
7
8
|
*
|
|
8
9
|
* The map is baked. It is fitted to what the camera sees and redrawn when the sun, the visible
|
|
9
10
|
* casters or the fitted area change, so camera motion inside the fitted area reuses it. `Lighting`
|
|
10
|
-
*
|
|
11
|
-
*
|
|
11
|
+
* calls {@link fitShadowCamera} from the scene's `onBeforeRender`; three's own lookup applies the
|
|
12
|
+
* map, with the shadow coordinate from `effects/sun-shadow`.
|
|
13
|
+
*
|
|
14
|
+
* Casters that move every frame draw into a second map instead, which {@link syncMovingSun} gives
|
|
15
|
+
* the same fit and which redraws every frame; {@link routeShadow} picks a caster's map, and
|
|
16
|
+
* `effects/sun-shadow` keeps the darker of the two lookups.
|
|
12
17
|
*/
|
|
13
18
|
/**
|
|
14
|
-
* Aims the
|
|
15
|
-
*
|
|
19
|
+
* Aims the sun from a heading and an elevation. The heading turns counterclockwise on screen from
|
|
20
|
+
* the plan's `+x`; the plan's `+y` points down the screen, hence `-sin`.
|
|
16
21
|
* @param settings - The rig.
|
|
17
22
|
* @param target - The sun's position, a direction in plan space.
|
|
23
|
+
* @returns `target`, unit length.
|
|
18
24
|
*/
|
|
19
25
|
export function aimSun(settings, target) {
|
|
20
26
|
const heading = degToRad(settings.sunShadow.headingDegrees);
|
|
21
27
|
const elevation = degToRad(settings.sunShadow.elevationDegrees);
|
|
22
|
-
target.set(Math.cos(elevation) * Math.cos(heading), -Math.cos(elevation) * Math.sin(heading), Math.sin(elevation));
|
|
28
|
+
return target.set(Math.cos(elevation) * Math.cos(heading), -Math.cos(elevation) * Math.sin(heading), Math.sin(elevation));
|
|
23
29
|
}
|
|
24
30
|
/**
|
|
25
31
|
* Turns shadow maps on, PCF with a blur radius.
|
|
@@ -59,9 +65,10 @@ export function enableShadowMaps(state) {
|
|
|
59
65
|
* 1. {@link readCasters}: what the sun and the visible casters look like this frame.
|
|
60
66
|
* 2. {@link measureCasters}, when that changed: their extent in the sun's view, which sets the
|
|
61
67
|
* depth range, and the floor catcher's size.
|
|
62
|
-
* 3. {@link fitView}: the area the camera sees, kept while the view stays inside it
|
|
63
|
-
*
|
|
64
|
-
* @
|
|
68
|
+
* 3. {@link fitView}: the area the camera sees, kept while the view stays inside it, and the map
|
|
69
|
+
* size that area needs.
|
|
70
|
+
* 4. {@link applyFit}: the shadow camera's frustum, biases and spread, the map size, and the redraw.
|
|
71
|
+
* @param light - The sun.
|
|
65
72
|
* @param scene - The scene, walked for casters.
|
|
66
73
|
* @param space - The scene's space.
|
|
67
74
|
* @param catcher - The floor's shadow catcher, sized to the casters.
|
|
@@ -84,21 +91,75 @@ export function fitShadowCamera(light, scene, space, catcher, viewCamera) {
|
|
|
84
91
|
}
|
|
85
92
|
const texel = fitView(light, space, viewCamera, castersChanged);
|
|
86
93
|
if (texel !== undefined)
|
|
87
|
-
applyFit(light, texel);
|
|
94
|
+
applyFit(light, texel, planScale(space));
|
|
88
95
|
}
|
|
89
96
|
/**
|
|
90
97
|
* Whether any visible mesh casts the sun's shadow.
|
|
91
98
|
* @param scene - The scene.
|
|
99
|
+
* @param moving - Counts only casters routed to the moving map when `true`.
|
|
92
100
|
* @returns Whether one does.
|
|
93
101
|
*/
|
|
94
|
-
export function hasVisibleCaster(scene) {
|
|
102
|
+
export function hasVisibleCaster(scene, moving = false) {
|
|
95
103
|
let found = false;
|
|
96
104
|
scene.traverseVisible((object) => {
|
|
97
|
-
if (
|
|
105
|
+
if (found || !object.isMesh || !object.castShadow)
|
|
106
|
+
return;
|
|
107
|
+
if (!moving || isMovingCaster(object))
|
|
98
108
|
found = true;
|
|
99
109
|
});
|
|
100
110
|
return found;
|
|
101
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Sends a caster's shadow to one of the two maps. A moving caster draws only into the moving sun's
|
|
114
|
+
* map, which redraws every frame, so the still map does not have to. A still caster left unrouted
|
|
115
|
+
* draws into both, which looks the same and costs a redraw every frame.
|
|
116
|
+
* @param mesh - The caster.
|
|
117
|
+
* @param moving - Whether it moves on its own, frame to frame.
|
|
118
|
+
*/
|
|
119
|
+
export function routeShadow(mesh, moving) {
|
|
120
|
+
mesh.userData[MOVING_CASTER] = moving;
|
|
121
|
+
mesh.shadowFilter = (shadowCamera) => (shadowCamera.userData[MOVING_CASTER] === true) === moving;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Makes `moving` the sun for moving casters: the same direction, fit and filtering as `sun`, so one
|
|
125
|
+
* lookup matrix reads both maps, and a redraw each frame while it casts. Hidden while it does not,
|
|
126
|
+
* so three counts no second light in the lit programs of a plan with nothing moving.
|
|
127
|
+
* @param moving - The moving casters' sun.
|
|
128
|
+
* @param sun - The still sun, already fitted for this frame.
|
|
129
|
+
* @param casting - Whether any moving caster is visible.
|
|
130
|
+
*/
|
|
131
|
+
export function syncMovingSun(moving, sun, casting) {
|
|
132
|
+
moving.visible = casting;
|
|
133
|
+
moving.castShadow = casting;
|
|
134
|
+
if (!casting)
|
|
135
|
+
return;
|
|
136
|
+
moving.shadow.autoUpdate = false;
|
|
137
|
+
moving.position.copy(sun.position);
|
|
138
|
+
moving.shadow.camera.copy(sun.shadow.camera);
|
|
139
|
+
moving.shadow.camera.userData[MOVING_CASTER] = true;
|
|
140
|
+
moving.shadow.intensity = sun.shadow.intensity;
|
|
141
|
+
// At most the still map's size, and capped below its largest: clearing and storing a depth map
|
|
142
|
+
// every frame is most of what the moving pass costs, and texels finer than the still map's would
|
|
143
|
+
// sharpen an edge the still fit already chose.
|
|
144
|
+
const size = Math.min(sun.shadow.mapSize.x, MOVING_SHADOW_MAP_SIZE);
|
|
145
|
+
setMapSize(moving, size);
|
|
146
|
+
const texelRatio = sun.shadow.mapSize.x / size;
|
|
147
|
+
// The edge keeps its width in the plan, and stays at least a texel: `applyFit`'s rule.
|
|
148
|
+
moving.shadow.radius = Math.max(sun.shadow.radius / texelRatio, 1);
|
|
149
|
+
// The normal bias is half a texel, and the moving map's texels may be larger. The depth bias is
|
|
150
|
+
// a fixed distance in the plan, the same for both.
|
|
151
|
+
moving.shadow.normalBias = sun.shadow.normalBias * texelRatio;
|
|
152
|
+
moving.shadow.bias = sun.shadow.bias;
|
|
153
|
+
moving.shadow.needsUpdate = true;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Whether a caster draws into the moving map; see {@link routeShadow}.
|
|
157
|
+
* @param object - The caster.
|
|
158
|
+
* @returns Whether it does.
|
|
159
|
+
*/
|
|
160
|
+
function isMovingCaster(object) {
|
|
161
|
+
return object.userData[MOVING_CASTER] === true;
|
|
162
|
+
}
|
|
102
163
|
/**
|
|
103
164
|
* Collects the visible casters into `_casters`, and describes the sun and what each caster shows.
|
|
104
165
|
* Cheap to build every frame, so the heavier steps run only when it changes.
|
|
@@ -114,7 +175,12 @@ function readCasters(light, scene) {
|
|
|
114
175
|
if (!mesh.isMesh || !mesh.castShadow)
|
|
115
176
|
return;
|
|
116
177
|
_casters.push(mesh);
|
|
117
|
-
|
|
178
|
+
// A moving caster's place changes every frame and its own map follows it, so the still map
|
|
179
|
+
// notes only that it exists.
|
|
180
|
+
if (isMovingCaster(mesh))
|
|
181
|
+
signature += `|${mesh.id}`;
|
|
182
|
+
else
|
|
183
|
+
signature += `|${mesh.id}:${mesh.matrixWorld.elements.join(',')}:${instanceSignature(mesh)}`;
|
|
118
184
|
});
|
|
119
185
|
return signature;
|
|
120
186
|
}
|
|
@@ -205,7 +271,7 @@ function fitView(light, space, viewCamera, force) {
|
|
|
205
271
|
return undefined;
|
|
206
272
|
// Wider than the view by `FIT_MARGIN`, which is the room the coverage test spends.
|
|
207
273
|
const fitRadius = stepped * FIT_MARGIN;
|
|
208
|
-
const texel = (2 * fitRadius) /
|
|
274
|
+
const texel = (2 * fitRadius) / mapSizeFor(fitRadius, space);
|
|
209
275
|
// Snapped to whole texels, so the grid stays put under a still shadow.
|
|
210
276
|
_viewFit[0] = Math.round(centerX / texel) * texel;
|
|
211
277
|
_viewFit[1] = Math.round(centerY / texel) * texel;
|
|
@@ -213,12 +279,13 @@ function fitView(light, space, viewCamera, force) {
|
|
|
213
279
|
return texel;
|
|
214
280
|
}
|
|
215
281
|
/**
|
|
216
|
-
* Sets the shadow camera's frustum from the view fit and the casters' depth,
|
|
217
|
-
*
|
|
282
|
+
* Sets the shadow camera's frustum from the view fit and the casters' depth, the biases and the
|
|
283
|
+
* lookup's spread, then asks for a redraw.
|
|
218
284
|
* @param light - The sun.
|
|
219
285
|
* @param texel - The map's texel size, in world units.
|
|
286
|
+
* @param scale - World units per plan unit.
|
|
220
287
|
*/
|
|
221
|
-
function applyFit(light, texel) {
|
|
288
|
+
function applyFit(light, texel, scale) {
|
|
222
289
|
const camera = light.shadow.camera;
|
|
223
290
|
const [centerX, centerY, radius] = _viewFit;
|
|
224
291
|
const [minZ, maxZ] = _depth;
|
|
@@ -231,11 +298,49 @@ function applyFit(light, texel) {
|
|
|
231
298
|
camera.near = -maxZ - margin;
|
|
232
299
|
camera.far = -minZ + margin;
|
|
233
300
|
camera.updateProjectionMatrix();
|
|
234
|
-
// In world units through the normal, and a
|
|
235
|
-
light.shadow.normalBias = texel *
|
|
236
|
-
light.shadow.bias = -
|
|
301
|
+
// In world units through the normal, and a fixed depth offset in the map's own `0`–`1` depth.
|
|
302
|
+
light.shadow.normalBias = texel * NORMAL_BIAS_TEXELS;
|
|
303
|
+
light.shadow.bias = -(DEPTH_BIAS * scale) / (camera.far - camera.near);
|
|
304
|
+
// At least a texel, so the taps' own filtering joins up once the map is too coarse for the
|
|
305
|
+
// softness.
|
|
306
|
+
light.shadow.radius = Math.max((SOFTNESS * scale) / texel, 1);
|
|
307
|
+
setMapSize(light, Math.round((2 * radius) / texel));
|
|
237
308
|
light.shadow.needsUpdate = true;
|
|
238
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* Gives a light's shadow map a new side. three allocates the map only while it has none, so a new
|
|
312
|
+
* size needs the old one gone.
|
|
313
|
+
* @param light - The light.
|
|
314
|
+
* @param size - The side, in texels.
|
|
315
|
+
*/
|
|
316
|
+
function setMapSize(light, size) {
|
|
317
|
+
if (light.shadow.mapSize.x === size)
|
|
318
|
+
return;
|
|
319
|
+
light.shadow.mapSize.set(size, size);
|
|
320
|
+
light.shadow.map?.depthTexture?.dispose();
|
|
321
|
+
light.shadow.map?.dispose();
|
|
322
|
+
light.shadow.map = null;
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* The map's side for a fitted area: the power of two whose texels leave the shadow's edge at most
|
|
326
|
+
* {@link MAX_SPREAD_TEXELS} wide, up to {@link SHADOW_MAP_SIZE}. Zoomed in, a 4096 map spends texels
|
|
327
|
+
* on an edge the lookup blurs away anyway, and a lookup across many texels needs many taps.
|
|
328
|
+
* @param fitRadius - Half the fitted area's side, in world units.
|
|
329
|
+
* @param space - The scene's space.
|
|
330
|
+
* @returns The side, in texels.
|
|
331
|
+
*/
|
|
332
|
+
function mapSizeFor(fitRadius, space) {
|
|
333
|
+
const needed = (2 * fitRadius * MAX_SPREAD_TEXELS) / (SOFTNESS * planScale(space));
|
|
334
|
+
return clamp(2 ** Math.floor(Math.log2(needed)), 1, SHADOW_MAP_SIZE);
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* World units per plan unit: the length the space's matrix gives one plan unit along `x`.
|
|
338
|
+
* @param space - The scene's space.
|
|
339
|
+
* @returns The scale.
|
|
340
|
+
*/
|
|
341
|
+
function planScale(space) {
|
|
342
|
+
return _axis.setFromMatrixColumn(space.matrix, 0).length();
|
|
343
|
+
}
|
|
239
344
|
/**
|
|
240
345
|
* Where a corner of the view meets the plan floor, in plan units, clamped to the casters' extent so
|
|
241
346
|
* a corner above the horizon stays on the plan.
|
|
@@ -268,6 +373,10 @@ function groundCorner(camera, space, corner, limit, target) {
|
|
|
268
373
|
*/
|
|
269
374
|
function instanceSignature(mesh) {
|
|
270
375
|
const instanced = mesh;
|
|
376
|
+
if (mesh instanceof BatchedMeshImpl) {
|
|
377
|
+
// A shape batch: its instances and their geometry change through slot writes the mesh counts.
|
|
378
|
+
return `${mesh.instanceCount}/${mesh.shadowVersion}`;
|
|
379
|
+
}
|
|
271
380
|
if (instanced.instancesCount === undefined || !instanced.getVisibleAt)
|
|
272
381
|
return '';
|
|
273
382
|
let visible = 0;
|
|
@@ -275,7 +384,7 @@ function instanceSignature(mesh) {
|
|
|
275
384
|
if (instanced.getVisibleAt(id))
|
|
276
385
|
visible = (visible * 31 + id + 1) | 0;
|
|
277
386
|
}
|
|
278
|
-
return `${instanced.instancesCount}/${visible}/${instanced.matricesTexture?.
|
|
387
|
+
return `${instanced.instancesCount}/${visible}/${instanced.matricesTexture?.revision ?? 0}`;
|
|
279
388
|
}
|
|
280
389
|
/**
|
|
281
390
|
* One corner of a box.
|
|
@@ -287,10 +396,6 @@ function instanceSignature(mesh) {
|
|
|
287
396
|
function cornerOf(box, corner, target) {
|
|
288
397
|
return target.set(corner & 1 ? box.max.x : box.min.x, corner & 2 ? box.max.y : box.min.y, corner & 4 ? box.max.z : box.min.z);
|
|
289
398
|
}
|
|
290
|
-
/** The shadow map's width and height, in texels: about 64 MB of depth. */
|
|
291
|
-
export const SHADOW_MAP_SIZE = 4096;
|
|
292
|
-
/** The shadow lookup's spread, in texels. */
|
|
293
|
-
export const SHADOW_RADIUS = 2;
|
|
294
399
|
/**
|
|
295
400
|
* Just below the floor. 2D content writes no depth, so the catcher draws over the floor, and a
|
|
296
401
|
* model's own ground, which writes depth, hides it cleanly.
|
|
@@ -298,15 +403,46 @@ export const SHADOW_RADIUS = 2;
|
|
|
298
403
|
export const CATCHER_ELEVATION = -0.5;
|
|
299
404
|
/** The catcher draws after every other transparent object, so it shades whatever floor they drew. */
|
|
300
405
|
export const CATCHER_RENDER_ORDER = 1e6;
|
|
406
|
+
/** The moving casters' shadow map's largest width and height, in texels. */
|
|
407
|
+
export const MOVING_SHADOW_MAP_SIZE = 2048;
|
|
301
408
|
/**
|
|
302
409
|
* How far the view may zoom in before the map refits to it. `2` refits at half the fitted size;
|
|
303
410
|
* `√2` refits twice as often during a zoom and keeps edges up to `√2` times sharper meanwhile.
|
|
304
411
|
*/
|
|
305
412
|
const REFIT_ZOOM_IN = Math.SQRT2;
|
|
413
|
+
/**
|
|
414
|
+
* How far a receiver's lookup moves out along its normal, in texels. Small, because solids cast
|
|
415
|
+
* from their back faces: the depth a lit face is compared with lies a whole volume behind it, so
|
|
416
|
+
* the bias has no acne to clear, and a large one opens light along the seams of roofs.
|
|
417
|
+
*/
|
|
418
|
+
const NORMAL_BIAS_TEXELS = 0.5;
|
|
419
|
+
/**
|
|
420
|
+
* The depth offset of a lookup toward the sun, in plan units: 0.02 m in the feet the event plans are
|
|
421
|
+
* drawn in.
|
|
422
|
+
*/
|
|
423
|
+
const DEPTH_BIAS = 0.066;
|
|
424
|
+
/**
|
|
425
|
+
* The spread of a shadow's edge, in plan units: the look-dev sandbox's 0.5 m, in the feet the event
|
|
426
|
+
* plans are drawn in. Held in the plan rather than in texels so an edge keeps its width when a zoom
|
|
427
|
+
* refits the map.
|
|
428
|
+
*/
|
|
429
|
+
const SOFTNESS = 1.64;
|
|
430
|
+
/**
|
|
431
|
+
* The widest a shadow's edge may spread across the map, in texels. three's lookup spreads five taps
|
|
432
|
+
* over it, so a wider edge would show the taps.
|
|
433
|
+
*/
|
|
434
|
+
const MAX_SPREAD_TEXELS = 2;
|
|
435
|
+
/** The shadow map's largest width and height, in texels: about 64 MB of depth. */
|
|
436
|
+
const SHADOW_MAP_SIZE = 4096;
|
|
306
437
|
/** How much wider than the view the fitted area is. */
|
|
307
438
|
const FIT_MARGIN = 1.25;
|
|
308
439
|
/** `groundCorner`'s index for the screen center. */
|
|
309
440
|
const CENTER = -1;
|
|
441
|
+
/**
|
|
442
|
+
* The `userData` flag on a caster routed to the moving map, and on the moving sun's shadow camera.
|
|
443
|
+
* Kept on the objects rather than in a module-level set, so a hot reload of this module keeps them.
|
|
444
|
+
*/
|
|
445
|
+
const MOVING_CASTER = 'efpMovingShadow';
|
|
310
446
|
let _lastSignature = '';
|
|
311
447
|
/** The casters found by {@link readCasters}. */
|
|
312
448
|
const _casters = [];
|
|
@@ -319,6 +455,7 @@ const _box = new Box3();
|
|
|
319
455
|
const _boundsPlan = new Box3();
|
|
320
456
|
const _to = new Vector3();
|
|
321
457
|
const _corner = new Vector3();
|
|
458
|
+
const _axis = new Vector3();
|
|
322
459
|
const _footprint = new Vector3();
|
|
323
460
|
const _near = new Vector3();
|
|
324
461
|
const _far = new Vector3();
|
|
@@ -35,7 +35,7 @@ export interface ColorMaterialProps {
|
|
|
35
35
|
*
|
|
36
36
|
* | | flat (default) | `solid` |
|
|
37
37
|
* | --- | --- | --- |
|
|
38
|
-
* | base | `MeshBasicMaterial`, unlit | `
|
|
38
|
+
* | base | `MeshBasicMaterial`, unlit | `MeshStandardMaterial`, lit |
|
|
39
39
|
* | `depthWrite` | off, from {@link sharedParams} | on |
|
|
40
40
|
* | polygon offset | on, from {@link sharedParams} | off |
|
|
41
41
|
*
|
|
@@ -44,15 +44,18 @@ export interface ColorMaterialProps {
|
|
|
44
44
|
* occluded by it. A volume carries no polygon offset, because solids are what everything else is
|
|
45
45
|
* biased against. An offset on the solids too only moves the problem.
|
|
46
46
|
*
|
|
47
|
-
* The lit branch is `
|
|
48
|
-
*
|
|
49
|
-
*
|
|
47
|
+
* The lit branch is `MeshStandardMaterial`, a rough dielectric, which is what the efp3d sandbox
|
|
48
|
+
* lights its booths with. Its diffuse term is three's `BRDF_Lambert`, `albedo / π`, the term the light
|
|
49
|
+
* rig is solved for; the specular lobe on top is what a rough painted surface adds toward grazing
|
|
50
|
+
* angles and toward the sun. Its vertex shader carries `batching_vertex` and `color_vertex`, so
|
|
50
51
|
* `BatchedMesh.setColorAt` keeps working. Its fragment shader carries the output chain, so the dim
|
|
51
|
-
* patch keeps its anchor.
|
|
52
|
+
* patch keeps its anchor. It shares its base class with every model, so it declares a program cache
|
|
53
|
+
* key of its own: three keys a program on that key, and two materials of one class that patch their
|
|
54
|
+
* shaders differently must never hand each other a compiled program.
|
|
52
55
|
*
|
|
53
|
-
* The light rig
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
+
* The light rig holds a roof to its authored color: on an up-facing surface the sun and the studio
|
|
57
|
+
* sum to `1`. A wall is darker because the side shade darkens its albedo, so that patch carries the
|
|
58
|
+
* look and the rig carries only the roof.
|
|
56
59
|
*
|
|
57
60
|
* Per-instance color arrives through `BatchedMesh.setColorAt` on either branch. three injects the
|
|
58
61
|
* batching-color chunk for whatever material the batch draws with.
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useEffect, useState } from 'react';
|
|
3
|
+
import { BackSide } from 'three';
|
|
3
4
|
import { createLogger } from '../../utils/logging';
|
|
4
5
|
import { useLayerOverlay } from '../layer/layer-context';
|
|
5
6
|
import { applyAmbientOcclusion } from './effects/ao';
|
|
6
7
|
import { useDimPatch } from './effects/color-dim';
|
|
7
8
|
import { applyContactShadow } from './effects/contact-shadow';
|
|
8
9
|
import { useSceneEffects } from './effects/effects-context';
|
|
10
|
+
import { applySideShade } from './effects/side-shade';
|
|
9
11
|
import { applySunShadow } from './effects/sun-shadow';
|
|
10
12
|
import { overlayParams, sharedParams } from './shared';
|
|
11
13
|
const logger = createLogger('color-material');
|
|
@@ -19,7 +21,7 @@ const logger = createLogger('color-material');
|
|
|
19
21
|
*
|
|
20
22
|
* | | flat (default) | `solid` |
|
|
21
23
|
* | --- | --- | --- |
|
|
22
|
-
* | base | `MeshBasicMaterial`, unlit | `
|
|
24
|
+
* | base | `MeshBasicMaterial`, unlit | `MeshStandardMaterial`, lit |
|
|
23
25
|
* | `depthWrite` | off, from {@link sharedParams} | on |
|
|
24
26
|
* | polygon offset | on, from {@link sharedParams} | off |
|
|
25
27
|
*
|
|
@@ -28,15 +30,18 @@ const logger = createLogger('color-material');
|
|
|
28
30
|
* occluded by it. A volume carries no polygon offset, because solids are what everything else is
|
|
29
31
|
* biased against. An offset on the solids too only moves the problem.
|
|
30
32
|
*
|
|
31
|
-
* The lit branch is `
|
|
32
|
-
*
|
|
33
|
-
*
|
|
33
|
+
* The lit branch is `MeshStandardMaterial`, a rough dielectric, which is what the efp3d sandbox
|
|
34
|
+
* lights its booths with. Its diffuse term is three's `BRDF_Lambert`, `albedo / π`, the term the light
|
|
35
|
+
* rig is solved for; the specular lobe on top is what a rough painted surface adds toward grazing
|
|
36
|
+
* angles and toward the sun. Its vertex shader carries `batching_vertex` and `color_vertex`, so
|
|
34
37
|
* `BatchedMesh.setColorAt` keeps working. Its fragment shader carries the output chain, so the dim
|
|
35
|
-
* patch keeps its anchor.
|
|
38
|
+
* patch keeps its anchor. It shares its base class with every model, so it declares a program cache
|
|
39
|
+
* key of its own: three keys a program on that key, and two materials of one class that patch their
|
|
40
|
+
* shaders differently must never hand each other a compiled program.
|
|
36
41
|
*
|
|
37
|
-
* The light rig
|
|
38
|
-
*
|
|
39
|
-
*
|
|
42
|
+
* The light rig holds a roof to its authored color: on an up-facing surface the sun and the studio
|
|
43
|
+
* sum to `1`. A wall is darker because the side shade darkens its albedo, so that patch carries the
|
|
44
|
+
* look and the rig carries only the roof.
|
|
40
45
|
*
|
|
41
46
|
* Per-instance color arrives through `BatchedMesh.setColorAt` on either branch. three injects the
|
|
42
47
|
* batching-color chunk for whatever material the batch draws with.
|
|
@@ -109,18 +114,35 @@ export function ColorMaterial(props) {
|
|
|
109
114
|
//
|
|
110
115
|
// Both effects land ahead of the dim patch, so the three run in the order they describe: the
|
|
111
116
|
// shadow and the occlusion shade the surface, and dimming desaturates the result. All three
|
|
112
|
-
// insert above the same anchor, so the later install runs last.
|
|
117
|
+
// insert above the same anchor, so the later install runs last. The sun's shadow is three's
|
|
118
|
+
// own; its patch only moves the shadow coordinate.
|
|
113
119
|
if (solid) {
|
|
114
120
|
applyContactShadow(parameters, effects);
|
|
121
|
+
applySideShade(parameters, effects);
|
|
115
122
|
applyAmbientOcclusion(parameters, effects);
|
|
116
|
-
applySunShadow(parameters
|
|
123
|
+
applySunShadow(parameters);
|
|
117
124
|
}
|
|
118
125
|
dimPatch(parameters);
|
|
119
126
|
};
|
|
120
127
|
});
|
|
121
128
|
if (solid) {
|
|
122
129
|
const translucent = props.translucent ?? false;
|
|
123
|
-
return (_jsx("
|
|
130
|
+
return (_jsx("meshStandardMaterial", { ...sharedParams, color: color, roughness: SOLID_ROUGHNESS, metalness: 0, depthWrite: true, polygonOffset: false, transparent: translucent, forceSinglePass: !translucent,
|
|
131
|
+
// Casting from the back faces puts the depth a lit face is compared with a whole volume
|
|
132
|
+
// behind it, so a small bias clears it and roof seams stay lit. The double-sided pass the
|
|
133
|
+
// shared side would give needed a bias that opened light along those seams.
|
|
134
|
+
shadowSide: BackSide, customProgramCacheKey: solidProgramKey, onBeforeCompile: onBeforeCompile }));
|
|
124
135
|
}
|
|
125
136
|
return (_jsx("meshBasicMaterial", { ...sharedParams, ...(overlay ? overlayParams : null), color: color, onBeforeCompile: onBeforeCompile }));
|
|
126
137
|
}
|
|
138
|
+
/**
|
|
139
|
+
* The volumes' roughness: the efp3d sandbox's booths. Its walls and columns take `0.8`, which one
|
|
140
|
+
* material for every volume does not follow.
|
|
141
|
+
*/
|
|
142
|
+
const SOLID_ROUGHNESS = 0.72;
|
|
143
|
+
/**
|
|
144
|
+
* The volume branch's program cache key. A module function, so R3F never re-applies the prop and
|
|
145
|
+
* never overwrites the key a batch wraps around it.
|
|
146
|
+
* @returns The key.
|
|
147
|
+
*/
|
|
148
|
+
const solidProgramKey = () => 'efpColorSolid';
|
|
@@ -103,10 +103,11 @@ function adjust(material, opacityUniform, dimPatch, effects, translucent) {
|
|
|
103
103
|
`.trim());
|
|
104
104
|
// The plan's shading, ahead of the dim patch. All three insert above the same anchor, so the later
|
|
105
105
|
// install runs last, and the order they run in is the order they describe: the shadow and the
|
|
106
|
-
// occlusion shade the surface, and dimming desaturates the result.
|
|
106
|
+
// occlusion shade the surface, and dimming desaturates the result. The sun's shadow is three's
|
|
107
|
+
// own; its patch only moves the shadow coordinate.
|
|
107
108
|
applyContactShadow(parameters, effects);
|
|
108
109
|
applyAmbientOcclusion(parameters, effects);
|
|
109
|
-
applySunShadow(parameters
|
|
110
|
+
applySunShadow(parameters);
|
|
110
111
|
// Dim touches rgb and opacity touches alpha, so the two patches stay independent in any order. Dim
|
|
111
112
|
// lands ahead of the colorspace conversion either way, which is where a gamma-corrected darken has
|
|
112
113
|
// to be.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The material an outline batch draws with: the efp3d sandbox's selection outline, without the
|
|
3
|
+
* edges it shows through nearer content.
|
|
4
|
+
*
|
|
5
|
+
* An inverted hull. It draws a volume's back faces alone, each vertex pushed out along its normal by
|
|
6
|
+
* {@link OUTLINE_WIDTH} screen pixels. Where the volume's own front faces stand, they are nearer, so
|
|
7
|
+
* the depth test hides the hull behind them. Only the rim pushed past the silhouette survives, and
|
|
8
|
+
* nearer content hides that too. The push is in clip space, scaled by `w`, so the rim keeps its width
|
|
9
|
+
* at every zoom and pitch.
|
|
10
|
+
*
|
|
11
|
+
* Chosen over the sandbox's screen-space edge detection because that needs a mask pass, a blur
|
|
12
|
+
* pass and a composite over the frame, which external mode cannot add: MapLibre owns the frame. This
|
|
13
|
+
* is one more draw in the ordinary stream.
|
|
14
|
+
*
|
|
15
|
+
* Unlit and opaque, and it writes no depth, so nothing it draws hides anything else.
|
|
16
|
+
* @returns The material element.
|
|
17
|
+
*/
|
|
18
|
+
export declare function OutlineMaterial(): import("react").JSX.Element;
|
|
19
|
+
/** How wide the outline is, in screen pixels: what the sandbox's 1.5 px blurred edge reads as. */
|
|
20
|
+
export declare const OUTLINE_WIDTH = 1.5;
|
|
21
|
+
//# sourceMappingURL=OutlineMaterial.d.ts.map
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
+
import { useRef, useState } from 'react';
|
|
3
|
+
import { BackSide, Vector2, Vector4, } from 'three';
|
|
4
|
+
import { declareBeforeMain, replaceAnchor } from '../../core/shader-source';
|
|
5
|
+
import { useDimPatch } from './effects/color-dim';
|
|
6
|
+
/**
|
|
7
|
+
* The material an outline batch draws with: the efp3d sandbox's selection outline, without the
|
|
8
|
+
* edges it shows through nearer content.
|
|
9
|
+
*
|
|
10
|
+
* An inverted hull. It draws a volume's back faces alone, each vertex pushed out along its normal by
|
|
11
|
+
* {@link OUTLINE_WIDTH} screen pixels. Where the volume's own front faces stand, they are nearer, so
|
|
12
|
+
* the depth test hides the hull behind them. Only the rim pushed past the silhouette survives, and
|
|
13
|
+
* nearer content hides that too. The push is in clip space, scaled by `w`, so the rim keeps its width
|
|
14
|
+
* at every zoom and pitch.
|
|
15
|
+
*
|
|
16
|
+
* Chosen over the sandbox's screen-space edge detection because that needs a mask pass, a blur
|
|
17
|
+
* pass and a composite over the frame, which external mode cannot add: MapLibre owns the frame. This
|
|
18
|
+
* is one more draw in the ordinary stream.
|
|
19
|
+
*
|
|
20
|
+
* Unlit and opaque, and it writes no depth, so nothing it draws hides anything else.
|
|
21
|
+
* @returns The material element.
|
|
22
|
+
*/
|
|
23
|
+
export function OutlineMaterial() {
|
|
24
|
+
const dimPatch = useDimPatch();
|
|
25
|
+
// A ref, as the dim patch keeps its uniform: written after render, every frame, and its identity is
|
|
26
|
+
// what the compiled program holds onto.
|
|
27
|
+
const viewportRef = useRef({ value: new Vector2(1, 1) });
|
|
28
|
+
const [onBeforeCompile] = useState(() => (parameters) => {
|
|
29
|
+
applyOutline(parameters, viewportRef.current);
|
|
30
|
+
dimPatch(parameters);
|
|
31
|
+
});
|
|
32
|
+
/**
|
|
33
|
+
* Reads the size the widths are measured against, as a line batch does: only the renderer knows it.
|
|
34
|
+
* @param renderer - The renderer about to draw the batch.
|
|
35
|
+
*/
|
|
36
|
+
function onBeforeRender(renderer) {
|
|
37
|
+
renderer.getViewport(_viewport);
|
|
38
|
+
viewportRef.current.value.set(_viewport.z, _viewport.w);
|
|
39
|
+
}
|
|
40
|
+
return (_jsx("meshBasicMaterial", { side: BackSide, depthWrite: false, customProgramCacheKey: outlineProgramKey, onBeforeCompile: onBeforeCompile, onBeforeRender: onBeforeRender }));
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Pushes each vertex out across the screen, along where its normal points there.
|
|
44
|
+
*
|
|
45
|
+
* The direction is the derivative of the vertex's screen position along its view-space normal, which
|
|
46
|
+
* is exact under perspective without probing a point that could cross the near plane.
|
|
47
|
+
* @param parameters - The shader being compiled.
|
|
48
|
+
* @param viewport - The uniform holding the viewport's size in pixels.
|
|
49
|
+
*/
|
|
50
|
+
function applyOutline(parameters, viewport) {
|
|
51
|
+
parameters.uniforms[VIEWPORT] = viewport;
|
|
52
|
+
parameters.vertexShader = replaceAnchor(declareBeforeMain(parameters.vertexShader, `uniform vec2 ${VIEWPORT};`), PROJECT_ANCHOR, `${PROJECT_ANCHOR}\n${PUSH}`);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The outline program's cache key. A module function, so R3F never re-applies the prop, and so the
|
|
56
|
+
* patched shader never shares a program with a plain `MeshBasicMaterial`.
|
|
57
|
+
* @returns The key.
|
|
58
|
+
*/
|
|
59
|
+
function outlineProgramKey() {
|
|
60
|
+
return 'efpOutline';
|
|
61
|
+
}
|
|
62
|
+
/** How wide the outline is, in screen pixels: what the sandbox's 1.5 px blurred edge reads as. */
|
|
63
|
+
export const OUTLINE_WIDTH = 1.5;
|
|
64
|
+
const VIEWPORT = 'outlineViewport';
|
|
65
|
+
const PROJECT_ANCHOR = '#include <project_vertex>';
|
|
66
|
+
const PUSH = /* glsl */ `
|
|
67
|
+
vec3 outlineNormal = normal;
|
|
68
|
+
#ifdef USE_BATCHING
|
|
69
|
+
outlineNormal = mat3( batchingMatrix ) * outlineNormal;
|
|
70
|
+
#endif
|
|
71
|
+
vec4 outlineTip = projectionMatrix * vec4( normalize( normalMatrix * outlineNormal ), 0.0 );
|
|
72
|
+
vec2 outlinePush = ( outlineTip.xy * gl_Position.w - gl_Position.xy * outlineTip.w ) * ${VIEWPORT};
|
|
73
|
+
if ( dot( outlinePush, outlinePush ) > 0.0 ) {
|
|
74
|
+
gl_Position.xy += normalize( outlinePush ) / ${VIEWPORT} * ${(2 * OUTLINE_WIDTH).toFixed(1)} * gl_Position.w;
|
|
75
|
+
}
|
|
76
|
+
`;
|
|
77
|
+
const _viewport = new Vector4();
|
|
@@ -18,6 +18,16 @@ export interface TextureMaterialProps {
|
|
|
18
18
|
* declares and writes one. Omit it to leave the fragment shader unpatched.
|
|
19
19
|
*/
|
|
20
20
|
opacityUniform?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Mip levels to shift the atlas sample by, negative for sharper. Takes effect only with
|
|
23
|
+
* {@link uvOffset}. A screen-space sprite passes `-1`: its image is often drawn at twice the
|
|
24
|
+
* pixels the screen shows, so it stays sharp between pixels and when it grows, and without the
|
|
25
|
+
* shift the GPU reads that 2:1 image from the next, box-averaged mip level, which throws the
|
|
26
|
+
* spare pixels away and blurs it more than an image drawn at screen density. Up to 2:1, a
|
|
27
|
+
* bilinear sample of the full level still covers each screen pixel, and past that the mips take
|
|
28
|
+
* over again.
|
|
29
|
+
*/
|
|
30
|
+
lodBias?: number;
|
|
21
31
|
}
|
|
22
32
|
/**
|
|
23
33
|
* The material used to render a texture, optionally through a per-instance uv-offset uniform so an
|
|
@@ -16,7 +16,7 @@ import { overlayParams, sharedParams } from './shared';
|
|
|
16
16
|
* @returns The material element.
|
|
17
17
|
*/
|
|
18
18
|
export function TextureMaterial(props) {
|
|
19
|
-
const { map, uvOffset, opacityUniform } = props;
|
|
19
|
+
const { map, uvOffset, opacityUniform, lodBias } = props;
|
|
20
20
|
const applyDim = useDimPatch();
|
|
21
21
|
const overlay = useLayerOverlay();
|
|
22
22
|
function onBeforeCompile(parameters) {
|
|
@@ -50,7 +50,10 @@ export function TextureMaterial(props) {
|
|
|
50
50
|
// rather than `sharedParams`'s straight-alpha source term; `blendDst` is unaffected. The override
|
|
51
51
|
// lives here because `ColorMaterial` and `LineMaterial` share `sharedParams` and are never
|
|
52
52
|
// premultiplied.
|
|
53
|
-
return (_jsx("meshBasicMaterial", { ...sharedParams, ...(overlay ? overlayParams : null), blendSrc: OneFactor, map: map,
|
|
53
|
+
return (_jsx("meshBasicMaterial", { ...sharedParams, ...(overlay ? overlayParams : null), blendSrc: OneFactor, map: map,
|
|
54
|
+
// A define rather than a value baked into the patch, because three keys its program cache on
|
|
55
|
+
// defines but not on what a closure in `onBeforeCompile` holds.
|
|
56
|
+
defines: lodBias ? { MAP_LOD_BIAS: lodBias.toFixed(1) } : undefined, onBeforeCompile: onBeforeCompile }));
|
|
54
57
|
}
|
|
55
58
|
/**
|
|
56
59
|
* The atlas region an instance samples, carried from the vertex shader's per-instance uniform to the
|
|
@@ -75,12 +78,20 @@ const MAP_REGION_VARYING = /* glsl */ `flat varying vec4 vMapRegion;`;
|
|
|
75
78
|
*
|
|
76
79
|
* `textureGrad` takes the gradients of the unclamped uv. Gradients of the clamped uv fall to zero
|
|
77
80
|
* where the clamp holds, and the GPU would then read that edge at full resolution.
|
|
81
|
+
*
|
|
82
|
+
* `MAP_LOD_BIAS` (see {@link TextureMaterialProps.lodBias}) scales both gradients, which moves the
|
|
83
|
+
* level the GPU reads and the level the inset is sized from by the same amount.
|
|
78
84
|
*/
|
|
79
85
|
const SAMPLE_REGION = /* glsl */ `
|
|
86
|
+
#ifndef MAP_LOD_BIAS
|
|
87
|
+
#define MAP_LOD_BIAS 0.0
|
|
88
|
+
#endif
|
|
80
89
|
vec4 sampleMapRegion( sampler2D atlas, vec2 uv, vec4 region ) {
|
|
81
90
|
vec2 size = vec2( textureSize( atlas, 0 ) );
|
|
82
91
|
vec2 gradX = dFdx( uv );
|
|
83
92
|
vec2 gradY = dFdy( uv );
|
|
93
|
+
gradX *= exp2( MAP_LOD_BIAS );
|
|
94
|
+
gradY *= exp2( MAP_LOD_BIAS );
|
|
84
95
|
vec2 texelsX = gradX * size;
|
|
85
96
|
vec2 texelsY = gradY * size;
|
|
86
97
|
float level = 0.5 * log2( max( dot( texelsX, texelsX ), dot( texelsY, texelsY ) ) );
|
|
@@ -100,11 +100,8 @@ export declare const OCCLUSION_DEFAULTS: {
|
|
|
100
100
|
/** Whether the term runs at all. */
|
|
101
101
|
enabled: boolean;
|
|
102
102
|
/**
|
|
103
|
-
* How far the term looks for an occluder, in plan units.
|
|
104
|
-
*
|
|
105
|
-
* Chosen against the story plans, whose booths run about a hundred plan units across. That makes
|
|
106
|
-
* a band under a tenth of a booth wide, where a junction's shadow reads as shading rather than as
|
|
107
|
-
* an outline.
|
|
103
|
+
* How far the term looks for an occluder, in plan units: the look-dev sandbox's 2.2 m, in the
|
|
104
|
+
* feet its event plans are drawn in.
|
|
108
105
|
*/
|
|
109
106
|
radius: number;
|
|
110
107
|
/** How far the read blends an occluded fragment toward the shadow color, from `0` to `1`. */
|
|
@@ -46,15 +46,12 @@ export const OCCLUSION_DEFAULTS = {
|
|
|
46
46
|
// three passes a frame.
|
|
47
47
|
enabled: false,
|
|
48
48
|
/**
|
|
49
|
-
* How far the term looks for an occluder, in plan units.
|
|
50
|
-
*
|
|
51
|
-
* Chosen against the story plans, whose booths run about a hundred plan units across. That makes
|
|
52
|
-
* a band under a tenth of a booth wide, where a junction's shadow reads as shading rather than as
|
|
53
|
-
* an outline.
|
|
49
|
+
* How far the term looks for an occluder, in plan units: the look-dev sandbox's 2.2 m, in the
|
|
50
|
+
* feet its event plans are drawn in.
|
|
54
51
|
*/
|
|
55
|
-
radius:
|
|
52
|
+
radius: 7.2,
|
|
56
53
|
/** How far the read blends an occluded fragment toward the shadow color, from `0` to `1`. */
|
|
57
|
-
strength: 0.
|
|
54
|
+
strength: 0.5,
|
|
58
55
|
/**
|
|
59
56
|
* The blur kernel's side length in texels. It starts at the side that covers the rotation tile,
|
|
60
57
|
* see {@link AmbientOcclusion.aoBlur}.
|