@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.
Files changed (66) hide show
  1. package/dist/Renderer.d.ts +2 -2
  2. package/dist/core/SquareDataTexture.d.ts +5 -0
  3. package/dist/core/SquareDataTexture.js +6 -0
  4. package/dist/core/batch/BatchedMesh.d.ts +12 -0
  5. package/dist/core/batch/BatchedMesh.js +29 -0
  6. package/dist/core/instanced/InstancedMesh.d.ts +5 -0
  7. package/dist/core/instanced/InstancedMesh.js +12 -0
  8. package/dist/core/sprites/BatchedSprites.js +2 -2
  9. package/dist/core/sprites/RaycastingSprites.d.ts +9 -1
  10. package/dist/core/sprites/RaycastingSprites.js +23 -3
  11. package/dist/core/sprites/sprite-material.js +5 -6
  12. package/dist/core/text/MemberTexture.js +2 -4
  13. package/dist/events/LayoutDriver.js +17 -1
  14. package/dist/events/layout-targets.js +30 -5
  15. package/dist/events/placement/box.d.ts +14 -0
  16. package/dist/events/placement/box.js +19 -2
  17. package/dist/events/placement/index.d.ts +1 -0
  18. package/dist/events/placement/index.js +1 -0
  19. package/dist/events/placement/pass.js +9 -8
  20. package/dist/events/pointer.d.ts +27 -6
  21. package/dist/events/pointer.js +50 -9
  22. package/dist/index.d.ts +1 -1
  23. package/dist/index.js +2 -2
  24. package/dist/objects/containers/SlotContainer.d.ts +5 -0
  25. package/dist/objects/containers/SlotContainer.js +7 -0
  26. package/dist/objects/images/Images.js +1 -1
  27. package/dist/objects/lighting/Lighting.d.ts +56 -38
  28. package/dist/objects/lighting/Lighting.js +136 -344
  29. package/dist/objects/lighting/lighting-gui.js +32 -18
  30. package/dist/objects/lighting/studio-environment.d.ts +15 -43
  31. package/dist/objects/lighting/studio-environment.js +18 -126
  32. package/dist/objects/lighting/sun-shadow-map.d.ts +39 -15
  33. package/dist/objects/lighting/sun-shadow-map.js +163 -26
  34. package/dist/objects/materials/ColorMaterial.d.ts +11 -8
  35. package/dist/objects/materials/ColorMaterial.js +33 -11
  36. package/dist/objects/materials/ModelMaterial.js +3 -2
  37. package/dist/objects/materials/OutlineMaterial.d.ts +21 -0
  38. package/dist/objects/materials/OutlineMaterial.js +77 -0
  39. package/dist/objects/materials/TextureMaterial.d.ts +10 -0
  40. package/dist/objects/materials/TextureMaterial.js +13 -2
  41. package/dist/objects/materials/effects/ao/index.d.ts +2 -5
  42. package/dist/objects/materials/effects/ao/index.js +4 -7
  43. package/dist/objects/materials/effects/contact-shadow.d.ts +21 -10
  44. package/dist/objects/materials/effects/contact-shadow.js +60 -16
  45. package/dist/objects/materials/effects/effects-context.d.ts +2 -1
  46. package/dist/objects/materials/effects/effects-context.js +2 -1
  47. package/dist/objects/materials/effects/side-shade.d.ts +40 -0
  48. package/dist/objects/materials/effects/side-shade.js +52 -0
  49. package/dist/objects/materials/effects/sun-shadow.d.ts +5 -21
  50. package/dist/objects/materials/effects/sun-shadow.js +56 -89
  51. package/dist/objects/models/Models.d.ts +3 -2
  52. package/dist/objects/models/Models.js +46 -8
  53. package/dist/objects/models/gltf-cache.js +1 -1
  54. package/dist/objects/models/model-asset.d.ts +84 -20
  55. package/dist/objects/models/model-asset.js +171 -40
  56. package/dist/objects/models/model-container.d.ts +46 -10
  57. package/dist/objects/models/model-container.js +92 -19
  58. package/dist/objects/shapes/Shapes.js +11 -6
  59. package/dist/objects/shapes/shape-container.d.ts +3 -2
  60. package/dist/objects/shapes/shape-container.js +6 -4
  61. package/dist/objects/shapes/shape-geometry.d.ts +16 -4
  62. package/dist/objects/shapes/shape-geometry.js +17 -13
  63. package/dist/types/defs.d.ts +30 -1
  64. package/dist/utils/math.d.ts +14 -0
  65. package/dist/utils/math.js +20 -0
  66. package/package.json +2 -2
@@ -13,7 +13,7 @@ import { Color, type IUniform, Matrix4, type WebGLProgramParametersWithUniforms
13
13
  * shadow: it is the plan's own matrix, and the shadow is only its one reader today.
14
14
  */
15
15
  export interface ContactShadow {
16
- /** The shadow's reach above a volume's base, in plan units. */
16
+ /** The shadow's reach above the floor, in plan units. */
17
17
  shadowDistance: IUniform<number>;
18
18
  /**
19
19
  * The depth the shader blends to, from `0` to `1`: {@link ContactShadow.contactStrength} while the
@@ -54,14 +54,14 @@ export interface ContactShadow {
54
54
  /**
55
55
  * Installs the contact shadow into a lit shader about to be compiled.
56
56
  *
57
- * A fake, not a measurement. It reads the vertex's height above the plan and darkens toward the base
58
- * of every volume, which separates a wall side from a wall top and stands in for the shadow a wall
59
- * casts onto the floor it meets. Ambient occlusion is a separate term, applied at the same anchor by
60
- * `ao/ambient-occlusion-patch.ts`.
57
+ * A fake, not a measurement. It reads the vertex's height above the plan and darkens the sides of
58
+ * every volume toward the floor, which stands in for the light a wall loses where it meets the floor.
59
+ * Ambient occlusion is a separate term, applied at the same anchor by `ao/ambient-occlusion-patch.ts`.
61
60
  *
62
61
  * The reach is a fixed world distance rather than each volume's own height. It needs no new data and
63
- * stays consistent across a plan. A roof above the reach keeps its authored color exactly; a volume
64
- * shorter than the reach blends its roof as it blends the rest of its height.
62
+ * stays consistent across a plan. The darkening is weighed by how far the face turns from the plan's
63
+ * up axis, so a face that looks up (a roof, a carpet, the water of a pool) keeps its color however
64
+ * low it sits, and a wall takes the whole of it.
65
65
  *
66
66
  * The blend goes toward an authored color rather than multiplying down toward black, so the shade is
67
67
  * a choice and not only its depth. MapLibre multiplies toward black and clamps.
@@ -102,11 +102,22 @@ export declare function createContactShadow(): ContactShadow;
102
102
  * tuned per plan through the GUI.
103
103
  */
104
104
  export declare const CONTACT_SHADOW_DEFAULTS: {
105
- /** How far above a volume's base the shadow reaches, in plan units. */
105
+ /**
106
+ * How far above the floor the shadow reaches, in plan units. The efp3d sandbox darkens a booth up
107
+ * to 51% of the booth's own height, and the median booth of imexamerica26-3d is 8 units tall.
108
+ */
106
109
  distance: number;
107
- /** How far the base is blended toward the shadow color, from `0` to `1`. */
110
+ /**
111
+ * How far a wall at the floor is blended toward the shadow color, from `0` to `1`: the sandbox's
112
+ * base darkening of 0.24.
113
+ */
108
114
  strength: number;
109
- /** The shade the shadow blends toward. */
115
+ /**
116
+ * The shade the shadow blends toward. Black, so the blend is the plain multiply the sandbox
117
+ * applies, and the shadow darkens a wall without tinting it.
118
+ */
110
119
  color: string;
111
120
  };
121
+ /** The varying carrying how far a vertex's normal turns from the plan's up axis, `0` to `1`. */
122
+ export declare const SIDE_VARYING = "vContactSide";
112
123
  //# sourceMappingURL=contact-shadow.d.ts.map
@@ -3,14 +3,14 @@ import { declareBeforeMain, replaceAnchor, sceneColorAnchor } from '../../../cor
3
3
  /**
4
4
  * Installs the contact shadow into a lit shader about to be compiled.
5
5
  *
6
- * A fake, not a measurement. It reads the vertex's height above the plan and darkens toward the base
7
- * of every volume, which separates a wall side from a wall top and stands in for the shadow a wall
8
- * casts onto the floor it meets. Ambient occlusion is a separate term, applied at the same anchor by
9
- * `ao/ambient-occlusion-patch.ts`.
6
+ * A fake, not a measurement. It reads the vertex's height above the plan and darkens the sides of
7
+ * every volume toward the floor, which stands in for the light a wall loses where it meets the floor.
8
+ * Ambient occlusion is a separate term, applied at the same anchor by `ao/ambient-occlusion-patch.ts`.
10
9
  *
11
10
  * The reach is a fixed world distance rather than each volume's own height. It needs no new data and
12
- * stays consistent across a plan. A roof above the reach keeps its authored color exactly; a volume
13
- * shorter than the reach blends its roof as it blends the rest of its height.
11
+ * stays consistent across a plan. The darkening is weighed by how far the face turns from the plan's
12
+ * up axis, so a face that looks up (a roof, a carpet, the water of a pool) keeps its color however
13
+ * low it sits, and a wall takes the whole of it.
14
14
  *
15
15
  * The blend goes toward an authored color rather than multiplying down toward black, so the shade is
16
16
  * a choice and not only its depth. MapLibre multiplies toward black and clamps.
@@ -38,7 +38,7 @@ export function applyContactShadow(parameters, shadow) {
38
38
  // exactly as it found them. A shader whose vertex stage places its vertices some other way throws
39
39
  // from `replaceAnchor` below, and a caller that means to carry on — a material whose asset is not
40
40
  // one three built — must not be left holding four bindings for a program nothing patched.
41
- const vertexShader = replaceAnchor(declareBeforeMain(parameters.vertexShader, VERTEX_DEFS), PLACEMENT_ANCHOR, `${PLACEMENT_ANCHOR}\n${VERTEX_APPLY}`);
41
+ const vertexShader = replaceAnchor(declareBeforeMain(parameters.vertexShader, VERTEX_DEFS), PLACEMENT_ANCHOR, `${PLACEMENT_ANCHOR}\n${VERTEX_APPLY}\n${buildsNormal(parameters.vertexShader) ? SIDE_APPLY : ALL_SIDE}`);
42
42
  const fragmentShader = declareBeforeMain(parameters.fragmentShader, FRAGMENT_DEFS).replace(anchor, `${SHADOW_APPLY}\n${anchor}`);
43
43
  parameters.uniforms[SHADOW_DISTANCE] = shadow.shadowDistance;
44
44
  parameters.uniforms[SHADOW_STRENGTH] = shadow.shadowStrength;
@@ -74,6 +74,19 @@ export function createContactShadow() {
74
74
  planFromWorld: { value: new Matrix4() },
75
75
  };
76
76
  }
77
+ /**
78
+ * Whether a vertex shader builds its view-space normal on every path: three's unlit material builds
79
+ * it only for an environment map or skinning, so its `transformedNormal` cannot be read.
80
+ * @param vertexShader - The shader's source, its chunks not yet expanded.
81
+ * @returns Whether the normal chunk sits outside every preprocessor branch in `main`.
82
+ */
83
+ function buildsNormal(vertexShader) {
84
+ const at = vertexShader.indexOf(NORMAL_CHUNK);
85
+ if (at < 0)
86
+ return false;
87
+ const body = vertexShader.slice(vertexShader.indexOf('void main'), at);
88
+ return (body.match(/^\s*#if/gm) ?? []).length === (body.match(/^\s*#endif/gm) ?? []).length;
89
+ }
77
90
  /**
78
91
  * The contact shadow values this design agreed.
79
92
  *
@@ -83,12 +96,21 @@ export function createContactShadow() {
83
96
  * tuned per plan through the GUI.
84
97
  */
85
98
  export const CONTACT_SHADOW_DEFAULTS = {
86
- /** How far above a volume's base the shadow reaches, in plan units. */
87
- distance: 1.5,
88
- /** How far the base is blended toward the shadow color, from `0` to `1`. */
89
- strength: 0.2,
90
- /** The shade the shadow blends toward. */
91
- color: '#4a5260',
99
+ /**
100
+ * How far above the floor the shadow reaches, in plan units. The efp3d sandbox darkens a booth up
101
+ * to 51% of the booth's own height, and the median booth of imexamerica26-3d is 8 units tall.
102
+ */
103
+ distance: 4.08,
104
+ /**
105
+ * How far a wall at the floor is blended toward the shadow color, from `0` to `1`: the sandbox's
106
+ * base darkening of 0.24.
107
+ */
108
+ strength: 0.24,
109
+ /**
110
+ * The shade the shadow blends toward. Black, so the blend is the plain multiply the sandbox
111
+ * applies, and the shadow darkens a wall without tinting it.
112
+ */
113
+ color: '#000000',
92
114
  };
93
115
  /** The uniform carrying the shadow's reach. */
94
116
  const SHADOW_DISTANCE = 'uShadowDistance';
@@ -102,6 +124,10 @@ const PLAN_FROM_WORLD = 'uPlanFromWorld';
102
124
  const SHADOW_FUNCTION = 'vec3 contactShadow(';
103
125
  /** The varying carrying a vertex's height above the plan. */
104
126
  const HEIGHT_VARYING = 'vHeight';
127
+ /** The varying carrying how far a vertex's normal turns from the plan's up axis, `0` to `1`. */
128
+ export const SIDE_VARYING = 'vContactSide';
129
+ /** three's chunk that leaves the view-space normal in `transformedNormal`. */
130
+ const NORMAL_CHUNK = '#include <defaultnormal_vertex>';
105
131
  /** The local three's own placement leaves the vertex's world position in. */
106
132
  const WORLD_POSITION = 'worldPosition';
107
133
  /**
@@ -136,6 +162,7 @@ const VERTEX_DEFS = /* glsl */ `
136
162
  #define DISTANCE
137
163
  uniform mat4 ${PLAN_FROM_WORLD};
138
164
  varying float ${HEIGHT_VARYING};
165
+ varying float ${SIDE_VARYING};
139
166
  `;
140
167
  /**
141
168
  * The vertex-stage read: the vertex's height in the plan's own space.
@@ -153,16 +180,33 @@ const VERTEX_DEFS = /* glsl */ `
153
180
  const VERTEX_APPLY = /* glsl */ `
154
181
  ${HEIGHT_VARYING} = (${PLAN_FROM_WORLD} * ${WORLD_POSITION}).z;
155
182
  `;
183
+ /**
184
+ * How far the vertex's normal turns from the plan's up axis. The plan's up axis as a world-space
185
+ * normal is the gradient of the plan height, which is the matrix's third row; a normal along it,
186
+ * either way, faces up or down.
187
+ */
188
+ const SIDE_APPLY = /* glsl */ `
189
+ {
190
+ vec3 planUp = normalize( vec3( ${PLAN_FROM_WORLD}[ 0 ][ 2 ], ${PLAN_FROM_WORLD}[ 1 ][ 2 ], ${PLAN_FROM_WORLD}[ 2 ][ 2 ] ) );
191
+ vec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );
192
+ ${SIDE_VARYING} = 1. - abs( dot( worldNormal, planUp ) );
193
+ }
194
+ `;
195
+ /** The side share of a shader with no normal to read, three's unlit one: all of it, as before. */
196
+ const ALL_SIDE = /* glsl */ `
197
+ ${SIDE_VARYING} = 1.;
198
+ `;
156
199
  /** The fragment-stage declarations. */
157
200
  const FRAGMENT_DEFS = /* glsl */ `
158
201
  uniform float ${SHADOW_DISTANCE};
159
202
  uniform float ${SHADOW_STRENGTH};
160
203
  uniform vec3 ${SHADOW_COLOR};
161
204
  varying float ${HEIGHT_VARYING};
205
+ varying float ${SIDE_VARYING};
162
206
 
163
- ${SHADOW_FUNCTION}vec3 rgb, float height) {
207
+ ${SHADOW_FUNCTION}vec3 rgb, float height, float side) {
164
208
  float lit = clamp(height / max(${SHADOW_DISTANCE}, 1e-4), 0., 1.);
165
- return mix(rgb, ${SHADOW_COLOR}, ${SHADOW_STRENGTH} * (1. - lit));
209
+ return mix(rgb, ${SHADOW_COLOR}, ${SHADOW_STRENGTH} * (1. - lit) * clamp(side, 0., 1.));
166
210
  }
167
211
  `;
168
212
  /**
@@ -172,5 +216,5 @@ const FRAGMENT_DEFS = /* glsl */ `
172
216
  * already-patched detector and nothing else.
173
217
  */
174
218
  const SHADOW_APPLY = /* glsl */ `
175
- gl_FragColor.rgb = contactShadow(gl_FragColor.rgb, ${HEIGHT_VARYING});
219
+ gl_FragColor.rgb = contactShadow(gl_FragColor.rgb, ${HEIGHT_VARYING}, ${SIDE_VARYING});
176
220
  `;
@@ -1,5 +1,6 @@
1
1
  import { type AmbientOcclusion } from './ao';
2
2
  import { type ContactShadow } from './contact-shadow';
3
+ import { type SideShade } from './side-shade';
3
4
  /**
4
5
  * Everything one mounted scene's shared effects hold: the tuned values, the registry of what asks
5
6
  * for ambient occlusion, and the uniforms the lit shaders read.
@@ -32,7 +33,7 @@ import { type ContactShadow } from './contact-shadow';
32
33
  * Merging either one in would either re-provide this object at every layer boundary or flatten
33
34
  * that nesting.
34
35
  */
35
- export type SceneEffects = AmbientOcclusion & ContactShadow;
36
+ export type SceneEffects = AmbientOcclusion & ContactShadow & SideShade;
36
37
  /**
37
38
  * Builds one scene's effect state, from each term's own factory.
38
39
  * @returns A fresh object, whose identity is meant to outlive every write into it.
@@ -1,12 +1,13 @@
1
1
  import { createContext, use } from 'react';
2
2
  import { createAmbientOcclusion } from './ao';
3
3
  import { createContactShadow } from './contact-shadow';
4
+ import { createSideShade } from './side-shade';
4
5
  /**
5
6
  * Builds one scene's effect state, from each term's own factory.
6
7
  * @returns A fresh object, whose identity is meant to outlive every write into it.
7
8
  */
8
9
  export function createSceneEffects() {
9
- return { ...createAmbientOcclusion(), ...createContactShadow() };
10
+ return { ...createAmbientOcclusion(), ...createContactShadow(), ...createSideShade() };
10
11
  }
11
12
  /**
12
13
  * The effect state of the enclosing scene.
@@ -0,0 +1,40 @@
1
+ import { Color, type IUniform, type WebGLProgramParametersWithUniforms } from 'three';
2
+ /**
3
+ * The scene-wide state of the side shade: the uniforms the solid shapes read, and the hex string a
4
+ * lil-gui color controller writes.
5
+ */
6
+ export interface SideShade {
7
+ /** How much darker a wall is than a roof, from `0` (the same) to `1` (black). */
8
+ sideShade: IUniform<number>;
9
+ /** The tint a wall's color is multiplied by, in the working space. */
10
+ sideTint: IUniform<Color>;
11
+ /** The tint as a hex string, which is what a lil-gui color controller reads and writes. */
12
+ sideTintHex: string;
13
+ }
14
+ /**
15
+ * Installs the side shade into a solid shape's shader: the efp3d sandbox's own wall darkening.
16
+ *
17
+ * It multiplies the albedo of a face by `tint · (1 − shade)`, weighed by how far the face turns from
18
+ * the plan's up axis. A roof keeps its albedo, and a wall takes all of it. It lands on the albedo
19
+ * rather than on the lit color, so the light rig still decides how bright a roof is and the specular
20
+ * lobe stays untinted, as in the sandbox.
21
+ *
22
+ * It reads the side weight the contact shadow computes, so the contact shadow must be installed
23
+ * first.
24
+ * @param parameters - The shader being compiled.
25
+ * @param shade - The mount's own side shade state, whose uniform objects the compiled program
26
+ * captures.
27
+ * @throws {Error} If the contact shadow is not installed, or the shader has no albedo to darken.
28
+ */
29
+ export declare function applySideShade(parameters: WebGLProgramParametersWithUniforms, shade: SideShade): void;
30
+ /**
31
+ * Builds this term's scene-wide state, at the sandbox's defaults.
32
+ * @returns A fresh object, whose identity is meant to outlive every write into it.
33
+ */
34
+ export declare function createSideShade(): SideShade;
35
+ /** The efp3d sandbox's booth sides: `boothSideShade` and `boothSideTint`. */
36
+ export declare const SIDE_SHADE_DEFAULTS: {
37
+ shade: number;
38
+ tint: string;
39
+ };
40
+ //# sourceMappingURL=side-shade.d.ts.map
@@ -0,0 +1,52 @@
1
+ import { Color } from 'three';
2
+ import { declareBeforeMain, replaceAnchor } from '../../../core/shader-source';
3
+ import { SIDE_VARYING } from './contact-shadow';
4
+ /**
5
+ * Installs the side shade into a solid shape's shader: the efp3d sandbox's own wall darkening.
6
+ *
7
+ * It multiplies the albedo of a face by `tint · (1 − shade)`, weighed by how far the face turns from
8
+ * the plan's up axis. A roof keeps its albedo, and a wall takes all of it. It lands on the albedo
9
+ * rather than on the lit color, so the light rig still decides how bright a roof is and the specular
10
+ * lobe stays untinted, as in the sandbox.
11
+ *
12
+ * It reads the side weight the contact shadow computes, so the contact shadow must be installed
13
+ * first.
14
+ * @param parameters - The shader being compiled.
15
+ * @param shade - The mount's own side shade state, whose uniform objects the compiled program
16
+ * captures.
17
+ * @throws {Error} If the contact shadow is not installed, or the shader has no albedo to darken.
18
+ */
19
+ export function applySideShade(parameters, shade) {
20
+ if (parameters.fragmentShader.includes(SIDE_SHADE))
21
+ return;
22
+ if (!parameters.fragmentShader.includes(`varying float ${SIDE_VARYING};`)) {
23
+ throw new Error('applySideShade: install the contact shadow first, for its side weight.');
24
+ }
25
+ parameters.fragmentShader = replaceAnchor(declareBeforeMain(parameters.fragmentShader, FRAGMENT_DEFS), COLOR_ANCHOR, `${COLOR_ANCHOR}\n${SIDE_APPLY}`);
26
+ parameters.uniforms[SIDE_SHADE] = shade.sideShade;
27
+ parameters.uniforms[SIDE_TINT] = shade.sideTint;
28
+ }
29
+ /**
30
+ * Builds this term's scene-wide state, at the sandbox's defaults.
31
+ * @returns A fresh object, whose identity is meant to outlive every write into it.
32
+ */
33
+ export function createSideShade() {
34
+ return {
35
+ sideShade: { value: SIDE_SHADE_DEFAULTS.shade },
36
+ sideTint: { value: new Color(SIDE_SHADE_DEFAULTS.tint) },
37
+ sideTintHex: SIDE_SHADE_DEFAULTS.tint,
38
+ };
39
+ }
40
+ /** The efp3d sandbox's booth sides: `boothSideShade` and `boothSideTint`. */
41
+ export const SIDE_SHADE_DEFAULTS = { shade: 0.24, tint: '#dde3ee' };
42
+ const SIDE_SHADE = 'uSideShade';
43
+ const SIDE_TINT = 'uSideTint';
44
+ /** three's chunk that leaves the albedo in `diffuseColor`. */
45
+ const COLOR_ANCHOR = '#include <color_fragment>';
46
+ const FRAGMENT_DEFS = /* glsl */ `
47
+ uniform float ${SIDE_SHADE};
48
+ uniform vec3 ${SIDE_TINT};
49
+ `;
50
+ const SIDE_APPLY = /* glsl */ `
51
+ diffuseColor.rgb *= mix( vec3( 1. ), ${SIDE_TINT} * ( 1. - ${SIDE_SHADE} ), clamp( ${SIDE_VARYING}, 0., 1. ) );
52
+ `;
@@ -1,32 +1,16 @@
1
- import { type Color, type IUniform, Matrix4, type WebGLProgramParametersWithUniforms } from 'three';
2
- /** The darkening in full shadow, from `0` to `1`. `Lighting` writes it every frame. */
3
- export declare const SUN_SHADOW_STRENGTH: IUniform<number>;
1
+ import { type IUniform, Matrix4, type WebGLProgramParametersWithUniforms } from 'three';
4
2
  /**
5
3
  * View space → the sun's shadow-map coordinates, `shadow.matrix · camera.matrixWorld`, composed on
6
- * the CPU in float64. In MapLibre mode the world unit is the whole Earth, so three's own lookup
4
+ * the CPU in float64. In MapLibre mode the world unit is the whole Earth, so three's own coordinate
7
5
  * (`shadowMatrix · worldPosition` in float32) loses more depth than a building is tall. View-space
8
6
  * positions are camera-relative and stay precise. `Lighting` writes it as the scene starts to draw,
9
7
  * once the camera has moved for the frame.
10
8
  */
11
9
  export declare const SHADOW_FROM_VIEW: IUniform<Matrix4>;
12
10
  /**
13
- * Replaces the sun's shadow coordinate with the camera-relative one, and declares the lookup.
11
+ * Replaces the sun's shadow coordinate with the camera-relative one, and makes the sun's shadow the
12
+ * darker of its two maps.
14
13
  * @param parameters - The compiling program.
15
- * @returns Whether the shader has a shadow coordinate to replace.
16
14
  */
17
- export declare function applySunShadowVertex(parameters: WebGLProgramParametersWithUniforms): boolean;
18
- /**
19
- * Installs the sun-shadow darkening ahead of the output chain, on a lit material.
20
- * @param parameters - The compiling program.
21
- * @param effects - The scene's effect state, for the shade every darkening term blends toward.
22
- * @param effects.shadowColor - The shade.
23
- */
24
- export declare function applySunShadow(parameters: WebGLProgramParametersWithUniforms, effects: {
25
- shadowColor: IUniform<Color>;
26
- }): void;
27
- /**
28
- * Points the floor's shadow catcher at the same lookup.
29
- * @param parameters - The compiling program.
30
- */
31
- export declare function applySunShadowCatcher(parameters: WebGLProgramParametersWithUniforms): void;
15
+ export declare function applySunShadow(parameters: WebGLProgramParametersWithUniforms): void;
32
16
  //# sourceMappingURL=sun-shadow.d.ts.map
@@ -1,108 +1,75 @@
1
1
  import { Matrix4, ShaderChunk, } from 'three';
2
- import { declareBeforeMain, replaceAnchor, sceneColorAnchor } from '../../../core/shader-source';
3
- // Darkens a lit surface where the fixed, shadow-only sun cannot reach it.
4
- //
5
- // The sun casts the scene's only shadow map, so three sorts it to directional shadow index `0`. It
6
- // has intensity `0` and only casts; the shadow it measures stands in for the share of light the
7
- // camera-relative directional light gives, which is what `SUN_SHADOW_STRENGTH` holds.
8
- //
9
- // One lookup per pixel, ours, replaces three's two: three's light loop samples the map for the sun
10
- // although its light is zero, and three's PCF rotates five taps by screen-space noise, which crawls
11
- // across a still surface whenever the camera moves. Ours takes four fixed hardware-compare taps.
12
- /** The darkening in full shadow, from `0` to `1`. `Lighting` writes it every frame. */
13
- export const SUN_SHADOW_STRENGTH = { value: 0 };
2
+ import { declareBeforeMain, replaceAnchor } from '../../../core/shader-source';
3
+ // The sun is the scene's directional light, so three's own light loop and `ShadowMaterial` apply
4
+ // its shadow. This patch keeps only what three lacks: a shadow coordinate precise enough for
5
+ // MapLibre mode, and a second map for the casters that move every frame, read by the same lookup.
14
6
  /**
15
7
  * View space → the sun's shadow-map coordinates, `shadow.matrix · camera.matrixWorld`, composed on
16
- * the CPU in float64. In MapLibre mode the world unit is the whole Earth, so three's own lookup
8
+ * the CPU in float64. In MapLibre mode the world unit is the whole Earth, so three's own coordinate
17
9
  * (`shadowMatrix · worldPosition` in float32) loses more depth than a building is tall. View-space
18
10
  * positions are camera-relative and stay precise. `Lighting` writes it as the scene starts to draw,
19
11
  * once the camera has moved for the frame.
20
12
  */
21
13
  export const SHADOW_FROM_VIEW = { value: new Matrix4() };
22
14
  /**
23
- * Replaces the sun's shadow coordinate with the camera-relative one, and declares the lookup.
15
+ * Replaces the sun's shadow coordinate with the camera-relative one, and makes the sun's shadow the
16
+ * darker of its two maps.
24
17
  * @param parameters - The compiling program.
25
- * @returns Whether the shader has a shadow coordinate to replace.
26
18
  */
27
- export function applySunShadowVertex(parameters) {
28
- if (parameters.vertexShader.includes(VIEW_UNIFORM))
29
- return true;
19
+ export function applySunShadow(parameters) {
30
20
  // The patch needs three's shadow chunk, the place the shadow coordinate is written.
31
- if (!parameters.vertexShader.includes(VERTEX_ANCHOR))
32
- return false;
33
- parameters.uniforms[VIEW_UNIFORM] = SHADOW_FROM_VIEW;
34
- parameters.vertexShader = replaceAnchor(declareBeforeMain(parameters.vertexShader, `uniform mat4 ${VIEW_UNIFORM};`), VERTEX_ANCHOR, `${VERTEX_ANCHOR}\n${VERTEX_APPLY}`);
35
- parameters.fragmentShader = declareBeforeMain(parameters.fragmentShader, LOOKUP);
36
- return true;
37
- }
38
- /**
39
- * Installs the sun-shadow darkening ahead of the output chain, on a lit material.
40
- * @param parameters - The compiling program.
41
- * @param effects - The scene's effect state, for the shade every darkening term blends toward.
42
- * @param effects.shadowColor - The shade.
43
- */
44
- export function applySunShadow(parameters, effects) {
45
- if (parameters.fragmentShader.includes(UNIFORM))
46
- return;
47
- const anchor = sceneColorAnchor(parameters.fragmentShader);
48
- if (!anchor)
49
- return;
50
- if (!applySunShadowVertex(parameters))
51
- return;
52
- parameters.uniforms[UNIFORM] = SUN_SHADOW_STRENGTH;
53
- parameters.uniforms[COLOR_UNIFORM] = effects.shadowColor;
54
- parameters.fragmentShader = declareBeforeMain(parameters.fragmentShader, `uniform float ${UNIFORM};\nuniform vec3 ${COLOR_UNIFORM};`)
55
- // three's own sample for the sun, whose light is zero anyway.
56
- .replace('#include <lights_fragment_begin>', LIGHTS_WITHOUT_DIRECTIONAL_SHADOWS)
57
- .replace(anchor, `${APPLY}\n${anchor}`);
58
- }
59
- /**
60
- * Points the floor's shadow catcher at the same lookup.
61
- * @param parameters - The compiling program.
62
- */
63
- export function applySunShadowCatcher(parameters) {
64
- if (!applySunShadowVertex(parameters))
21
+ if (parameters.vertexShader.includes(UNIFORM) || !parameters.vertexShader.includes(ANCHOR))
65
22
  return;
66
- parameters.fragmentShader = parameters.fragmentShader.replace('getShadowMask()', 'sunShadowLit()');
23
+ parameters.uniforms[UNIFORM] = SHADOW_FROM_VIEW;
24
+ parameters.vertexShader = replaceAnchor(declareBeforeMain(parameters.vertexShader, `uniform mat4 ${UNIFORM};`), ANCHOR, `${ANCHOR}\n${APPLY}`);
25
+ for (const [chunk, lookup] of LOOKUPS) {
26
+ const include = `#include <${chunk}>`;
27
+ if (!parameters.fragmentShader.includes(include))
28
+ continue;
29
+ const source = replaceAnchor(ShaderChunk[chunk], lookup, `SUN_SHADOW( UNROLLED_LOOP_INDEX )`);
30
+ parameters.fragmentShader = replaceAnchor(parameters.fragmentShader, include, `${SUN_SHADOW}\n${source}`);
31
+ }
67
32
  }
68
- const UNIFORM = 'uSunShadowStrength';
69
- /** The shade the contact shadow and ambient occlusion blend toward too. */
70
- const COLOR_UNIFORM = 'uSunShadowColor';
71
- const VIEW_UNIFORM = 'uShadowFromView';
72
- const VERTEX_ANCHOR = /* glsl */ `#include <shadowmap_vertex>`;
73
- const VERTEX_APPLY = /* glsl */ `
33
+ const UNIFORM = 'uShadowFromView';
34
+ const ANCHOR = /* glsl */ `#include <shadowmap_vertex>`;
35
+ // Shadow-casting lights sort first, in mount order, so the sun is directional shadow 0 and the
36
+ // moving casters' sun is 1. The two share the fit, so only the moving map's texel-sized normal bias
37
+ // differs.
38
+ const APPLY = /* glsl */ `
74
39
  #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
75
- vDirectionalShadowCoord[ 0 ] = ${VIEW_UNIFORM} * vec4( mvPosition.xyz + normalize( transformedNormal ) * directionalLightShadows[ 0 ].shadowNormalBias, 1.0 );
40
+ vDirectionalShadowCoord[ 0 ] = ${UNIFORM} * vec4( mvPosition.xyz + normalize( transformedNormal ) * directionalLightShadows[ 0 ].shadowNormalBias, 1.0 );
41
+ #if NUM_DIR_LIGHT_SHADOWS > 1
42
+ vDirectionalShadowCoord[ 1 ] = ${UNIFORM} * vec4( mvPosition.xyz + normalize( transformedNormal ) * directionalLightShadows[ 1 ].shadowNormalBias, 1.0 );
43
+ #endif
76
44
  #endif
77
45
  `;
78
- const LOOKUP = /* glsl */ `
79
- #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0 && defined( SHADOWMAP_TYPE_PCF )
80
- float sunShadowLit() {
81
- vec4 coord = vDirectionalShadowCoord[ 0 ];
82
- coord.z += directionalLightShadows[ 0 ].shadowBias;
83
- if ( coord.x < 0.0 || coord.x > 1.0 || coord.y < 0.0 || coord.y > 1.0 || coord.z > 1.0 ) return 1.0;
84
- // Four taps half a radius apart, each a hardware 2x2 compare: a fixed 4x4 footprint.
85
- vec2 offset = directionalLightShadows[ 0 ].shadowRadius * 0.5 / directionalLightShadows[ 0 ].shadowMapSize;
86
- return 0.25 * (
87
- texture( directionalShadowMap[ 0 ], vec3( coord.xy + vec2( -offset.x, -offset.y ), coord.z ) ) +
88
- texture( directionalShadowMap[ 0 ], vec3( coord.xy + vec2( offset.x, -offset.y ), coord.z ) ) +
89
- texture( directionalShadowMap[ 0 ], vec3( coord.xy + vec2( -offset.x, offset.y ), coord.z ) ) +
90
- texture( directionalShadowMap[ 0 ], vec3( coord.xy + vec2( offset.x, offset.y ), coord.z ) )
91
- );
92
- }
93
- #else
94
- float sunShadowLit() { return 1.0; }
95
- #endif
96
- `;
97
- const APPLY = /* glsl */ `
98
- #if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0
99
- if ( receiveShadow ) {
100
- // Shadow-casting lights sort first, so the sun is directional light 0 as well.
101
- float sunFacing = max( dot( normalize( normal ), directionalLights[ 0 ].direction ), 0.0 );
102
- // A face turned from the sun keeps its color, so it skips the lookup.
103
- float sunLit = sunFacing > 0.0 ? sunShadowLit() : 1.0;
104
- gl_FragColor.rgb = mix( gl_FragColor.rgb, ${COLOR_UNIFORM}, min( ${UNIFORM} * sunFacing, 1.0 ) * ( 1.0 - sunLit ) );
105
- }
46
+ /**
47
+ * Directional shadow `i` as three's chunks look it up, and the sun's shadow that replaces it. Shadow
48
+ * 0 takes the darker of the two maps: a surface behind a still caster and a moving one is as dark as
49
+ * behind either, where a product would darken it twice. The moving sun gives no light, so shadow 1
50
+ * has nothing left to darken.
51
+ */
52
+ const SUN_SHADOW = /* glsl */ `
53
+ #ifndef SUN_SHADOW
54
+ #define SUN_LOOKUP( i ) getShadow( directionalShadowMap[ i ], directionalLightShadows[ i ].shadowMapSize, directionalLightShadows[ i ].shadowIntensity, directionalLightShadows[ i ].shadowBias, directionalLightShadows[ i ].shadowRadius, vDirectionalShadowCoord[ i ] )
55
+ #if NUM_DIR_LIGHT_SHADOWS > 1
56
+ #define SUN_SHADOW( i ) ( i == 0 ? min( SUN_LOOKUP( 0 ), SUN_LOOKUP( 1 ) ) : 1.0 )
57
+ #else
58
+ #define SUN_SHADOW( i ) SUN_LOOKUP( i )
59
+ #endif
106
60
  #endif
107
61
  `;
108
- const LIGHTS_WITHOUT_DIRECTIONAL_SHADOWS = ShaderChunk.lights_fragment_begin.replace('( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )', '( UNROLLED_LOOP_INDEX < 0 )');
62
+ /**
63
+ * The chunks that look up a directional shadow, and the lookup each one spells: the light loop of
64
+ * the lit materials, and `ShadowMaterial`'s mask.
65
+ */
66
+ const LOOKUPS = [
67
+ [
68
+ 'lights_fragment_begin',
69
+ 'getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowIntensity, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] )',
70
+ ],
71
+ [
72
+ 'shadowmask_pars_fragment',
73
+ 'getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowIntensity, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] )',
74
+ ],
75
+ ];
@@ -4,8 +4,9 @@ import { type DefsProps } from '../containers';
4
4
  export type ModelsProps = DefsProps<ModelDef>;
5
5
  /**
6
6
  * Draws a layer's models. Every def sharing a URL draws from the same loaded asset, and every part of
7
- * that asset — one per source material, merged at load — becomes a single `InstancedMesh`. A layer
8
- * holding twenty copies of a two-material chair is therefore **two draw calls**, not forty.
7
+ * that asset — one per source material, merged at load, plus one per mesh the GLB reuses — becomes a
8
+ * single `InstancedMesh`. A layer holding twenty copies of a two-material chair is therefore **two
9
+ * draw calls**, not forty.
9
10
  *
10
11
  * `InstancedMesh` rather than `BatchedMesh` on purpose. Nothing here ever addresses a sub-mesh
11
12
  * individually — the addressable unit is the def — so merging at load is strictly better than
@@ -1,5 +1,5 @@
1
1
  import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
- import { useThree } from '@react-three/fiber';
2
+ import { useFrame, useThree } from '@react-three/fiber';
3
3
  import { Fragment, useEffect, useState } from 'react';
4
4
  import { MeshDepthMaterial } from 'three';
5
5
  import { InstancedMesh } from '../../core/instanced/InstancedMesh';
@@ -7,16 +7,19 @@ import { sameSequence } from '../../utils/arrays';
7
7
  import { createLogger } from '../../utils/logging';
8
8
  import { defKey, useContainer } from '../containers';
9
9
  import { useOccluder } from '../lighting/occluder';
10
+ import { routeShadow } from '../lighting/sun-shadow-map';
10
11
  import { ModelMaterial } from '../materials/ModelMaterial';
11
12
  import { loadModel } from './gltf-cache';
13
+ import { poseAnimation } from './model-asset';
12
14
  import { MODEL_KINDS, ModelContainer, modelKindOf, OPACITY_UNIFORM, } from './model-container';
13
15
  import { useDefaultModelOpacity } from './model-opacity';
14
16
  import { ModelInstance } from './ModelInstance';
15
17
  const logger = createLogger('models');
16
18
  /**
17
19
  * Draws a layer's models. Every def sharing a URL draws from the same loaded asset, and every part of
18
- * that asset — one per source material, merged at load — becomes a single `InstancedMesh`. A layer
19
- * holding twenty copies of a two-material chair is therefore **two draw calls**, not forty.
20
+ * that asset — one per source material, merged at load, plus one per mesh the GLB reuses — becomes a
21
+ * single `InstancedMesh`. A layer holding twenty copies of a two-material chair is therefore **two
22
+ * draw calls**, not forty.
20
23
  *
21
24
  * `InstancedMesh` rather than `BatchedMesh` on purpose. Nothing here ever addresses a sub-mesh
22
25
  * individually — the addressable unit is the def — so merging at load is strictly better than
@@ -143,10 +146,10 @@ function ModelPartMesh(props) {
143
146
  // Only the instances are staged; the container is sized and ordered from every member.
144
147
  const mounted = props.mounted ?? defs.length;
145
148
  // Ordering is a no-op for models — their container declares it so, and the mesh depth-sorts itself
146
- // instead — but the hit resolution is not: `InstancedMeshImpl.raycast` walks the live slots,
147
- // skipping deleted and hidden ones, and raycasts the real merged geometry, so a hit names the
148
- // model's actual triangles and the slot that drew them.
149
- const container = useContainer(() => new ModelContainer(asset.extents, defs.length, kind), defs, onKindMismatch);
149
+ // instead — but the hit resolution is not: `InstancedMeshImpl.raycast` walks the live instances,
150
+ // skipping deleted and hidden ones and any whose bounds the ray misses, and raycasts the part's
151
+ // real geometry, so a hit names the model's actual triangles and the copy that drew them.
152
+ const container = useContainer(() => new ModelContainer(asset.extents, defs.length, kind, part), defs, onKindMismatch);
150
153
  const occluder = useOccluder();
151
154
  // The geometry belongs to the module-level cache and may be drawing in another layer, so it is
152
155
  // mounted as a `primitive` with `dispose={null}` — R3F must not free it on unmount. Sharing it is
@@ -170,9 +173,44 @@ function ModelPartMesh(props) {
170
173
  // instancing patch binds the first mesh's textures into it. Each mesh needs its own.
171
174
  if (mesh && !mesh.customDepthMaterial)
172
175
  mesh.customDepthMaterial = new MeshDepthMaterial();
176
+ if (mesh)
177
+ routeShadow(mesh, part.motion !== undefined);
173
178
  }, matrixAutoUpdate: false, matrix: container.meshMatrix,
174
179
  // Models cast the sun's shadow and receive it.
175
- castShadow: true, receiveShadow: true, sortObjects: true, renderOrder: renderOrder, children: [_jsx("primitive", { object: part.geometry, attach: "geometry", dispose: null }), _jsx(ModelMaterial, { source: part.material, opacityUniform: OPACITY_UNIFORM, translucent: kind === 'translucent' })] }), defs.slice(0, mounted).map((def) => (_jsx(ModelInstance, { def: def, container: container }, defKey(def))))] }));
180
+ castShadow: true, receiveShadow: true, sortObjects: true, renderOrder: renderOrder, children: [_jsx("primitive", { object: part.geometry, attach: "geometry", dispose: null }), _jsx(ModelMaterial, { source: part.material, opacityUniform: OPACITY_UNIFORM, translucent: kind === 'translucent' })] }), defs.slice(0, mounted).map((def) => (_jsx(ModelInstance, { def: def, container: container }, defKey(def)))), asset.animation && part.motion && (_jsx(PartAnimation, { animation: asset.animation, container: container }))] }));
181
+ }
182
+ /**
183
+ * Plays an animated part: each frame, poses the asset at the page's time and moves every instance
184
+ * with the part's node.
185
+ *
186
+ * The frame loop runs on demand, so this asks for the next frame itself, and only while the mesh is
187
+ * visible. A hidden layer therefore stops the loop instead of keeping the map at full frame rate.
188
+ * @param props - The animation and the part's container.
189
+ * @returns `null`; the instances live in the container's mesh.
190
+ */
191
+ function PartAnimation(props) {
192
+ const { animation, container } = props;
193
+ useFrame((state) => {
194
+ if (!isShown(container.mesh))
195
+ return;
196
+ // Not `state.clock`: under an external host's frame loop R3F sets it to milliseconds.
197
+ poseAnimation(animation, performance.now() / 1000);
198
+ container.follow();
199
+ state.invalidate();
200
+ });
201
+ return null;
202
+ }
203
+ /**
204
+ * Whether an object and every ancestor draw.
205
+ * @param object - The object to test.
206
+ * @returns `true` when nothing above it is hidden.
207
+ */
208
+ function isShown(object) {
209
+ for (let current = object; current; current = current.parent) {
210
+ if (!current.visible)
211
+ return false;
212
+ }
213
+ return true;
176
214
  }
177
215
  /**
178
216
  * Loads one model, reporting a failure rather than rejecting — one missing model should not cost a