@needle-tools/engine 4.13.1 → 4.14.0-beta
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/CHANGELOG.md +9 -0
- package/components.needle.json +1 -1
- package/dist/generateMeshBVH.worker-DiCnZlf3.js +21 -0
- package/dist/gltf-progressive-Bm_6aEi4.js +1528 -0
- package/dist/gltf-progressive-BttGBXw6.umd.cjs +10 -0
- package/dist/gltf-progressive-T5WKTux5.min.js +10 -0
- package/dist/loader.worker-BqODMeeW.js +23 -0
- package/dist/{materialx-B9ddsHcF.min.js → materialx-CJyQZtjt.min.js} +1 -1
- package/dist/{materialx-fkoFuRh3.js → materialx-DMs1E08Z.js} +2 -2
- package/dist/{materialx-BF23AVE8.umd.cjs → materialx-DaKKOoVk.umd.cjs} +1 -1
- package/dist/needle-engine.bundle-BW2VusZV.min.js +1646 -0
- package/dist/{needle-engine.bundle-tjI5Fq2c.umd.cjs → needle-engine.bundle-Cb5bKEqa.umd.cjs} +152 -159
- package/dist/{needle-engine.bundle-DauZUYl7.js → needle-engine.bundle-D9VPvp5o.js} +7798 -7497
- package/dist/needle-engine.d.ts +779 -42
- package/dist/needle-engine.js +416 -415
- package/dist/needle-engine.min.js +1 -1
- package/dist/needle-engine.umd.cjs +1 -1
- package/dist/{postprocessing-BVNrgYZK.min.js → postprocessing-CctM1XIO.min.js} +1 -1
- package/dist/{postprocessing-DdM-tz1j.js → postprocessing-DGm6qJ-I.js} +2 -2
- package/dist/{postprocessing-CI2TjWpu.umd.cjs → postprocessing-Dbl2PJpd.umd.cjs} +1 -1
- package/dist/{three-BW2s1Yl-.umd.cjs → three-BjYim-vL.umd.cjs} +4 -4
- package/dist/{three-VvRoMeIN.js → three-Bvk2VKbF.js} +4210 -4210
- package/dist/{three-I__hSXzr.min.js → three-IG2qPafA.min.js} +33 -33
- package/dist/{three-examples-Bpfu6ke_.umd.cjs → three-examples-BMmNgNCN.umd.cjs} +1 -1
- package/dist/{three-examples-BhfOE7NG.js → three-examples-CMYCd5nH.js} +1 -1
- package/dist/{three-examples-D8zAE_7t.min.js → three-examples-CQl1fFZp.min.js} +1 -1
- package/dist/{three-mesh-ui-C3QbemOV.min.js → three-mesh-ui-5HVE2RV-.min.js} +1 -1
- package/dist/{three-mesh-ui-CcMp-FQm.js → three-mesh-ui-BlakAItG.js} +1 -1
- package/dist/{three-mesh-ui-BU55xDxJ.umd.cjs → three-mesh-ui-D828VbQp.umd.cjs} +1 -1
- package/dist/{vendor-DW7zqjuT.min.js → vendor-BxK0WKmT.min.js} +1 -1
- package/dist/{vendor-COVQl0b8.umd.cjs → vendor-CIDkyBaO.umd.cjs} +1 -1
- package/dist/{vendor-BiyIZ61v.js → vendor-ixwD-vv2.js} +1 -1
- package/lib/engine/api.d.ts +1 -0
- package/lib/engine/api.js +1 -0
- package/lib/engine/api.js.map +1 -1
- package/lib/engine/engine_context.js +12 -2
- package/lib/engine/engine_context.js.map +1 -1
- package/lib/engine/engine_lightdata.js +8 -6
- package/lib/engine/engine_lightdata.js.map +1 -1
- package/lib/engine/engine_materialpropertyblock.d.ts +259 -0
- package/lib/engine/engine_materialpropertyblock.js +682 -0
- package/lib/engine/engine_materialpropertyblock.js.map +1 -0
- package/lib/engine/engine_utils.d.ts +1 -0
- package/lib/engine/engine_utils.js +1 -0
- package/lib/engine/engine_utils.js.map +1 -1
- package/lib/engine/engine_utils_screenshot.d.ts +171 -14
- package/lib/engine/engine_utils_screenshot.js +65 -0
- package/lib/engine/engine_utils_screenshot.js.map +1 -1
- package/lib/engine/engine_utils_screenshot.xr.d.ts +1 -1
- package/lib/engine/engine_utils_screenshot.xr.js +1 -1
- package/lib/engine/extensions/NEEDLE_techniques_webgl.js +3 -0
- package/lib/engine/extensions/NEEDLE_techniques_webgl.js.map +1 -1
- package/lib/engine/xr/NeedleXRSession.d.ts +5 -0
- package/lib/engine/xr/NeedleXRSession.js +5 -0
- package/lib/engine/xr/NeedleXRSession.js.map +1 -1
- package/lib/engine-components/Camera.js.map +1 -1
- package/lib/engine-components/DropListener.js.map +1 -1
- package/lib/engine-components/Duplicatable.js.map +1 -1
- package/lib/engine-components/GroundProjection.js.map +1 -1
- package/lib/engine-components/NeedleMenu.js.map +1 -1
- package/lib/engine-components/NestedGltf.js.map +1 -1
- package/lib/engine-components/ReflectionProbe.d.ts +32 -4
- package/lib/engine-components/ReflectionProbe.js +58 -88
- package/lib/engine-components/ReflectionProbe.js.map +1 -1
- package/lib/engine-components/Renderer.d.ts +2 -0
- package/lib/engine-components/Renderer.js +30 -6
- package/lib/engine-components/Renderer.js.map +1 -1
- package/lib/engine-components/RendererLightmap.d.ts +13 -9
- package/lib/engine-components/RendererLightmap.js +67 -81
- package/lib/engine-components/RendererLightmap.js.map +1 -1
- package/lib/engine-components/SeeThrough.d.ts +0 -2
- package/lib/engine-components/SeeThrough.js +114 -88
- package/lib/engine-components/SeeThrough.js.map +1 -1
- package/lib/engine-components/SmoothFollow.js.map +1 -1
- package/lib/engine-components/VideoPlayer.js +6 -0
- package/lib/engine-components/VideoPlayer.js.map +1 -1
- package/lib/engine-components/ui/Button.js.map +1 -1
- package/lib/engine-components/ui/Raycaster.js.map +1 -1
- package/lib/engine-components/utils/OpenURL.d.ts +1 -0
- package/lib/engine-components/utils/OpenURL.js +1 -0
- package/lib/engine-components/utils/OpenURL.js.map +1 -1
- package/lib/engine-components/web/CursorFollow.d.ts +1 -0
- package/lib/engine-components/web/CursorFollow.js +1 -0
- package/lib/engine-components/web/CursorFollow.js.map +1 -1
- package/lib/engine-components/web/ScrollFollow.d.ts +1 -0
- package/lib/engine-components/web/ScrollFollow.js +1 -0
- package/lib/engine-components/web/ScrollFollow.js.map +1 -1
- package/lib/engine-components/web/ViewBox.d.ts +2 -2
- package/lib/engine-components/web/ViewBox.js +2 -2
- package/lib/engine-components/web/ViewBox.js.map +1 -1
- package/lib/engine-components/webxr/WebARCameraBackground.d.ts +9 -0
- package/lib/engine-components/webxr/WebARCameraBackground.js +9 -0
- package/lib/engine-components/webxr/WebARCameraBackground.js.map +1 -1
- package/lib/engine-components/webxr/WebXR.d.ts +1 -0
- package/lib/engine-components/webxr/WebXR.js +1 -0
- package/lib/engine-components/webxr/WebXR.js.map +1 -1
- package/lib/engine-components/webxr/WebXRPlaneTracking.js.map +1 -1
- package/lib/engine-components/webxr/controllers/XRControllerFollow.js.map +1 -1
- package/lib/engine-components/webxr/controllers/XRControllerMovement.js.map +1 -1
- package/package.json +5 -5
- package/plugins/vite/build-pipeline.js +16 -2
- package/src/engine/api.ts +1 -0
- package/src/engine/engine_context.ts +17 -3
- package/src/engine/engine_lightdata.ts +8 -6
- package/src/engine/engine_materialpropertyblock.ts +866 -0
- package/src/engine/engine_utils.ts +1 -0
- package/src/engine/engine_utils_screenshot.ts +241 -17
- package/src/engine/engine_utils_screenshot.xr.ts +1 -1
- package/src/engine/extensions/NEEDLE_techniques_webgl.ts +3 -0
- package/src/engine/xr/NeedleXRSession.ts +5 -0
- package/src/engine-components/Camera.ts +2 -2
- package/src/engine-components/ContactShadows.ts +1 -1
- package/src/engine-components/DropListener.ts +1 -1
- package/src/engine-components/Duplicatable.ts +1 -1
- package/src/engine-components/GroundProjection.ts +3 -0
- package/src/engine-components/NeedleMenu.ts +3 -0
- package/src/engine-components/NestedGltf.ts +1 -1
- package/src/engine-components/ReflectionProbe.ts +64 -105
- package/src/engine-components/Renderer.ts +34 -6
- package/src/engine-components/RendererLightmap.ts +75 -87
- package/src/engine-components/SeeThrough.ts +124 -109
- package/src/engine-components/SmoothFollow.ts +2 -2
- package/src/engine-components/VideoPlayer.ts +6 -0
- package/src/engine-components/ui/Button.ts +1 -1
- package/src/engine-components/ui/Raycaster.ts +1 -1
- package/src/engine-components/utils/OpenURL.ts +1 -0
- package/src/engine-components/web/CursorFollow.ts +1 -0
- package/src/engine-components/web/ScrollFollow.ts +1 -0
- package/src/engine-components/web/ViewBox.ts +9 -2
- package/src/engine-components/webxr/WebARCameraBackground.ts +12 -3
- package/src/engine-components/webxr/WebXR.ts +1 -0
- package/src/engine-components/webxr/WebXRPlaneTracking.ts +3 -3
- package/src/engine-components/webxr/controllers/XRControllerFollow.ts +1 -1
- package/src/engine-components/webxr/controllers/XRControllerMovement.ts +4 -4
- package/dist/generateMeshBVH.worker-iyfPIK6R.js +0 -21
- package/dist/gltf-progressive-BURrJW0U.umd.cjs +0 -8
- package/dist/gltf-progressive-DHLDFNvQ.min.js +0 -8
- package/dist/gltf-progressive-eiJCrjLb.js +0 -1400
- package/dist/loader.worker-C1GG9A7C.js +0 -23
- package/dist/needle-engine.bundle-BNIUpreS.min.js +0 -1653
- package/src/include/three/DragControls.js +0 -232
|
@@ -0,0 +1,682 @@
|
|
|
1
|
+
import { Texture } from "three";
|
|
2
|
+
/**
|
|
3
|
+
* Centralized registry for all property block related data.
|
|
4
|
+
* Uses WeakMaps to allow automatic garbage collection when objects are destroyed.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
class PropertyBlockRegistry {
|
|
8
|
+
// Map from object to its property block
|
|
9
|
+
objectToBlock = new WeakMap();
|
|
10
|
+
// Track which materials belong to which property block (to prevent applying to wrong materials)
|
|
11
|
+
// Use WeakSet for automatic cleanup when materials are garbage collected
|
|
12
|
+
// private objectToMaterials = new WeakMap<Object3D, WeakSet<Material>>();
|
|
13
|
+
// Track which meshes have callbacks for which property block owners
|
|
14
|
+
meshToOwners = new WeakMap();
|
|
15
|
+
// Track original callback functions for cleanup (reserved for future use)
|
|
16
|
+
meshToOriginalCallbacks = new WeakMap();
|
|
17
|
+
getBlock(object) {
|
|
18
|
+
return this.objectToBlock.get(object);
|
|
19
|
+
}
|
|
20
|
+
setBlock(object, block) {
|
|
21
|
+
this.objectToBlock.set(object, block);
|
|
22
|
+
}
|
|
23
|
+
deleteBlock(object) {
|
|
24
|
+
this.objectToBlock.delete(object);
|
|
25
|
+
// this.objectToMaterials.delete(object);
|
|
26
|
+
}
|
|
27
|
+
// addMaterial(object: Object3D, material: Material): void {
|
|
28
|
+
// let materials = this.objectToMaterials.get(object);
|
|
29
|
+
// if (!materials) {
|
|
30
|
+
// materials = new WeakSet();
|
|
31
|
+
// this.objectToMaterials.set(object, materials);
|
|
32
|
+
// }
|
|
33
|
+
// materials.add(material);
|
|
34
|
+
// }
|
|
35
|
+
// hasMaterial(object: Object3D, material: Material): boolean {
|
|
36
|
+
// return this.objectToMaterials.get(object)?.has(material) ?? false;
|
|
37
|
+
// }
|
|
38
|
+
isHooked(mesh, owner) {
|
|
39
|
+
return this.meshToOwners.get(mesh)?.has(owner) ?? false;
|
|
40
|
+
}
|
|
41
|
+
addHook(mesh, owner) {
|
|
42
|
+
let owners = this.meshToOwners.get(mesh);
|
|
43
|
+
if (!owners) {
|
|
44
|
+
owners = new Set();
|
|
45
|
+
this.meshToOwners.set(mesh, owners);
|
|
46
|
+
}
|
|
47
|
+
owners.add(owner);
|
|
48
|
+
}
|
|
49
|
+
removeHook(mesh, owner) {
|
|
50
|
+
const owners = this.meshToOwners.get(mesh);
|
|
51
|
+
if (owners) {
|
|
52
|
+
owners.delete(owner);
|
|
53
|
+
if (owners.size === 0) {
|
|
54
|
+
this.meshToOwners.delete(mesh);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
getOriginalCallbacks(mesh) {
|
|
59
|
+
return this.meshToOriginalCallbacks.get(mesh);
|
|
60
|
+
}
|
|
61
|
+
setOriginalCallbacks(mesh, callbacks) {
|
|
62
|
+
this.meshToOriginalCallbacks.set(mesh, callbacks);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const registry = new PropertyBlockRegistry();
|
|
66
|
+
/**
|
|
67
|
+
* MaterialPropertyBlock allows per-object material property overrides without creating new material instances.
|
|
68
|
+
* This is useful for rendering multiple objects with the same base material but different properties
|
|
69
|
+
* (e.g., different colors, textures, or shader parameters).
|
|
70
|
+
*
|
|
71
|
+
* The property block system works by:
|
|
72
|
+
* - Temporarily applying overrides in onBeforeRender
|
|
73
|
+
* - Restoring original values in onAfterRender
|
|
74
|
+
* - Managing shader defines and program cache keys for correct shader compilation
|
|
75
|
+
* - Supporting texture coordinate transforms per object
|
|
76
|
+
*
|
|
77
|
+
* Common use cases:
|
|
78
|
+
* - **Lightmaps**: Apply unique lightmap textures to individual objects sharing the same material
|
|
79
|
+
* - **Reflection Probes**: Apply different environment maps per object for localized reflections
|
|
80
|
+
* - **See-through effects**: Temporarily override transparency/transmission properties for X-ray effects
|
|
81
|
+
*
|
|
82
|
+
* ## Getting a MaterialPropertyBlock
|
|
83
|
+
*
|
|
84
|
+
* **Important**: Do not use the constructor directly. Instead, use the static {@link MaterialPropertyBlock.get} method:
|
|
85
|
+
*
|
|
86
|
+
* ```typescript
|
|
87
|
+
* const block = MaterialPropertyBlock.get(myMesh);
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* This method will either return an existing property block or create a new one if it doesn't exist.
|
|
91
|
+
* It automatically:
|
|
92
|
+
* - Creates the property block instance
|
|
93
|
+
* - Registers it in the internal registry
|
|
94
|
+
* - Attaches the necessary render callbacks to the object
|
|
95
|
+
* - Handles Groups by applying overrides to all child meshes
|
|
96
|
+
*
|
|
97
|
+
* @example Basic usage
|
|
98
|
+
* ```typescript
|
|
99
|
+
* // Get or create a property block for an object
|
|
100
|
+
* const block = MaterialPropertyBlock.get(myMesh);
|
|
101
|
+
*
|
|
102
|
+
* // Override the color property
|
|
103
|
+
* block.setOverride("color", new Color(1, 0, 0));
|
|
104
|
+
*
|
|
105
|
+
* // Override a texture with custom UV transform (useful for lightmaps)
|
|
106
|
+
* block.setOverride("lightMap", myLightmapTexture, {
|
|
107
|
+
* offset: new Vector2(0.5, 0.5),
|
|
108
|
+
* repeat: new Vector2(2, 2)
|
|
109
|
+
* });
|
|
110
|
+
*
|
|
111
|
+
* // Set a shader define
|
|
112
|
+
* block.setDefine("USE_CUSTOM_FEATURE", 1);
|
|
113
|
+
* ```
|
|
114
|
+
*
|
|
115
|
+
* @example Lightmap usage
|
|
116
|
+
* ```typescript
|
|
117
|
+
* const block = MaterialPropertyBlock.get(mesh);
|
|
118
|
+
* block.setOverride("lightMap", lightmapTexture);
|
|
119
|
+
* block.setOverride("lightMapIntensity", 1.5);
|
|
120
|
+
* ```
|
|
121
|
+
*
|
|
122
|
+
* @example See-through effect
|
|
123
|
+
* ```typescript
|
|
124
|
+
* const block = MaterialPropertyBlock.get(mesh);
|
|
125
|
+
* block.setOverride("transparent", true);
|
|
126
|
+
* block.setOverride("opacity", 0.3);
|
|
127
|
+
* ```
|
|
128
|
+
*
|
|
129
|
+
* @template T The material type this property block is associated with
|
|
130
|
+
*/
|
|
131
|
+
export class MaterialPropertyBlock {
|
|
132
|
+
_overrides = [];
|
|
133
|
+
_defines = {};
|
|
134
|
+
_object = null;
|
|
135
|
+
/** The object this property block is attached to */
|
|
136
|
+
get object() { return this._object; }
|
|
137
|
+
/**
|
|
138
|
+
* Creates a new MaterialPropertyBlock
|
|
139
|
+
* @param object The object this property block is for (optional)
|
|
140
|
+
*/
|
|
141
|
+
constructor(object = null) {
|
|
142
|
+
this._object = object;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Gets or creates a MaterialPropertyBlock for the given object.
|
|
146
|
+
* This is the recommended way to obtain a property block instance.
|
|
147
|
+
*
|
|
148
|
+
* @template T The material type
|
|
149
|
+
* @param object The object to get/create a property block for
|
|
150
|
+
* @returns The MaterialPropertyBlock associated with this object
|
|
151
|
+
*
|
|
152
|
+
* @example
|
|
153
|
+
* ```typescript
|
|
154
|
+
* const block = MaterialPropertyBlock.get(myMesh);
|
|
155
|
+
* block.setOverride("roughness", 0.5);
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
static get(object) {
|
|
159
|
+
let block = registry.getBlock(object);
|
|
160
|
+
if (!block) {
|
|
161
|
+
block = new MaterialPropertyBlock(object);
|
|
162
|
+
registry.setBlock(object, block);
|
|
163
|
+
attachPropertyBlockToObject(object, block);
|
|
164
|
+
}
|
|
165
|
+
return block;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Checks if an object has any property overrides
|
|
169
|
+
* @param object The object to check
|
|
170
|
+
* @returns True if the object has a property block with overrides
|
|
171
|
+
*/
|
|
172
|
+
static hasOverrides(object) {
|
|
173
|
+
const block = registry.getBlock(object);
|
|
174
|
+
return block ? block.hasOverrides() : false;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Disposes this property block and cleans up associated resources.
|
|
178
|
+
* After calling dispose, this property block should not be used.
|
|
179
|
+
*/
|
|
180
|
+
dispose() {
|
|
181
|
+
if (this._object) {
|
|
182
|
+
registry.deleteBlock(this._object);
|
|
183
|
+
// TODO: Add cleanup for hooked meshes
|
|
184
|
+
}
|
|
185
|
+
this._overrides = [];
|
|
186
|
+
this._object = null;
|
|
187
|
+
}
|
|
188
|
+
setOverride(name, value, textureTransform) {
|
|
189
|
+
const existing = this._overrides.find(o => o.name === name);
|
|
190
|
+
if (existing) {
|
|
191
|
+
existing.value = value;
|
|
192
|
+
existing.textureTransform = textureTransform;
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
this._overrides.push({ name, value, textureTransform });
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
getOverride(name) {
|
|
199
|
+
return this._overrides.find(o => o.name === name);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Removes a specific property override
|
|
203
|
+
* @param name The property name to clear
|
|
204
|
+
*/
|
|
205
|
+
removeOveride(name) {
|
|
206
|
+
const index = this._overrides.findIndex(o => o.name === name);
|
|
207
|
+
if (index >= 0) {
|
|
208
|
+
this._overrides.splice(index, 1);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Removes all property overrides from this block
|
|
213
|
+
*/
|
|
214
|
+
clearAllOverrides() {
|
|
215
|
+
this._overrides = [];
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Gets all property overrides as a readonly array
|
|
219
|
+
* @returns Array of all property overrides
|
|
220
|
+
*/
|
|
221
|
+
get overrides() {
|
|
222
|
+
return this._overrides;
|
|
223
|
+
}
|
|
224
|
+
/**
|
|
225
|
+
* Checks if this property block has any overrides
|
|
226
|
+
* @returns True if there are any overrides set
|
|
227
|
+
*/
|
|
228
|
+
hasOverrides() {
|
|
229
|
+
return this._overrides.length > 0;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Set a shader define that will be included in the program cache key.
|
|
233
|
+
* This allows different objects sharing the same material to have different shader programs.
|
|
234
|
+
*
|
|
235
|
+
* Defines affect shader compilation and are useful for enabling/disabling features per-object.
|
|
236
|
+
*
|
|
237
|
+
* @param name The define name (e.g., "USE_LIGHTMAP", "ENABLE_REFLECTIONS")
|
|
238
|
+
* @param value The define value (typically a boolean, number, or string)
|
|
239
|
+
*
|
|
240
|
+
* @example
|
|
241
|
+
* ```typescript
|
|
242
|
+
* // Enable a feature for this specific object
|
|
243
|
+
* block.setDefine("USE_CUSTOM_SHADER", true);
|
|
244
|
+
* block.setDefine("QUALITY_LEVEL", 2);
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
setDefine(name, value) {
|
|
248
|
+
this._defines[name] = value;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Remove a shader define
|
|
252
|
+
* @param name The define name to remove
|
|
253
|
+
*/
|
|
254
|
+
clearDefine(name) {
|
|
255
|
+
this._defines[name] = undefined;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Get all defines set on this property block
|
|
259
|
+
* @returns A readonly record of all defines
|
|
260
|
+
*/
|
|
261
|
+
getDefines() {
|
|
262
|
+
return this._defines;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Generates a cache key based on the current overrides and defines.
|
|
266
|
+
* This key is used internally to ensure correct shader program selection
|
|
267
|
+
* when objects share materials but have different property blocks.
|
|
268
|
+
*
|
|
269
|
+
* @returns A string representing the current state of this property block
|
|
270
|
+
* @internal
|
|
271
|
+
*/
|
|
272
|
+
getCacheKey() {
|
|
273
|
+
const parts = [];
|
|
274
|
+
// Add defines to cache key
|
|
275
|
+
const defineKeys = Object.keys(this._defines).sort();
|
|
276
|
+
for (const key of defineKeys) {
|
|
277
|
+
const value = this._defines[key];
|
|
278
|
+
if (value !== undefined) {
|
|
279
|
+
parts.push(`d:${key}=${value}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
// Add overrides to cache key
|
|
283
|
+
for (const o of this._overrides) {
|
|
284
|
+
if (o.value === null)
|
|
285
|
+
continue;
|
|
286
|
+
let val = "";
|
|
287
|
+
if (o.value instanceof Texture) {
|
|
288
|
+
val = o.value.uuid || "texture";
|
|
289
|
+
if (o.textureTransform) {
|
|
290
|
+
const t = o.textureTransform;
|
|
291
|
+
if (t.offset)
|
|
292
|
+
val += `;to:${t.offset.x},${t.offset.y}`;
|
|
293
|
+
if (t.repeat)
|
|
294
|
+
val += `;tr:${t.repeat.x},${t.repeat.y}`;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
else if (Array.isArray(o.value)) {
|
|
298
|
+
val = o.value.join(",");
|
|
299
|
+
}
|
|
300
|
+
else if (o.value && typeof o.value === "object" && "r" in o.value) {
|
|
301
|
+
const c = o.value;
|
|
302
|
+
val = `${c.r},${c.g},${c.b},${c.a !== undefined ? c.a : ""}`;
|
|
303
|
+
}
|
|
304
|
+
else if (o.value && typeof o.value === "object" && "x" in o.value) {
|
|
305
|
+
// Vector2, Vector3, Vector4
|
|
306
|
+
const v = o.value;
|
|
307
|
+
val = `${v.x},${v.y}${v.z !== undefined ? `,${v.z}` : ""}${v.w !== undefined ? `,${v.w}` : ""}`;
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
val = String(o.value);
|
|
311
|
+
}
|
|
312
|
+
parts.push(`${o.name}=${val}`);
|
|
313
|
+
}
|
|
314
|
+
return parts.join(";");
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* Symbol used to store original material values on the material object
|
|
319
|
+
* @internal
|
|
320
|
+
*/
|
|
321
|
+
const $originalValues = Symbol("originalValues");
|
|
322
|
+
/**
|
|
323
|
+
* Symbol used to store saved texture transforms on the material object
|
|
324
|
+
* @internal
|
|
325
|
+
*/
|
|
326
|
+
const $savedTextureTransforms = Symbol("savedTextureTransforms");
|
|
327
|
+
/**
|
|
328
|
+
* Collect all materials from an object and its children
|
|
329
|
+
* @internal
|
|
330
|
+
*/
|
|
331
|
+
function collectMaterials(object, materials) {
|
|
332
|
+
const obj = object;
|
|
333
|
+
if (obj.material) {
|
|
334
|
+
if (Array.isArray(obj.material)) {
|
|
335
|
+
obj.material.forEach(mat => materials.add(mat));
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
materials.add(obj.material);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
// For Groups, collect materials from children too
|
|
342
|
+
if (object.type === "Group") {
|
|
343
|
+
object.children.forEach(child => collectMaterials(child, materials));
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Find property block by checking this object and parent (if parent is a Group).
|
|
348
|
+
* Returns both the block and the owner object.
|
|
349
|
+
*
|
|
350
|
+
* @param obj The object to search from
|
|
351
|
+
* @returns The property block and its owner object, or undefined if not found
|
|
352
|
+
* @internal
|
|
353
|
+
*/
|
|
354
|
+
function findPropertyBlockAndOwner(obj) {
|
|
355
|
+
// First check if this object itself has a property block
|
|
356
|
+
let block = registry.getBlock(obj);
|
|
357
|
+
if (block)
|
|
358
|
+
return { block, owner: obj };
|
|
359
|
+
// If not, check if parent is a Group and has a property block
|
|
360
|
+
if (obj.parent && obj.parent.type === "Group") {
|
|
361
|
+
block = registry.getBlock(obj.parent);
|
|
362
|
+
if (block)
|
|
363
|
+
return { block, owner: obj.parent };
|
|
364
|
+
}
|
|
365
|
+
return undefined;
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Symbol to track which materials are currently being rendered for an object
|
|
369
|
+
* @internal
|
|
370
|
+
*/
|
|
371
|
+
const currentlyRenderingFlag = Symbol("beforeRenderingFlag");
|
|
372
|
+
/**
|
|
373
|
+
* Tracks original transparent values that were changed during render list building
|
|
374
|
+
* @internal
|
|
375
|
+
*/
|
|
376
|
+
const beforeRenderListTransparentChanged = new WeakMap();
|
|
377
|
+
/**
|
|
378
|
+
* Tracks original transmission values that were changed during render list building
|
|
379
|
+
* @internal
|
|
380
|
+
*/
|
|
381
|
+
const beforeRenderListTransparentChangedTransmission = new WeakMap();
|
|
382
|
+
/**
|
|
383
|
+
* Callback invoked before an object is added to the render list.
|
|
384
|
+
* Used to temporarily override transparency/transmission for correct render list assignment.
|
|
385
|
+
* @internal
|
|
386
|
+
*/
|
|
387
|
+
const onBeforeRenderListPush = function (_object, _geometry, material, _group) {
|
|
388
|
+
const block = registry.getBlock(_object);
|
|
389
|
+
if (!block) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
if (block.hasOverrides()) {
|
|
393
|
+
const transmission = block.getOverride("transmission")?.value;
|
|
394
|
+
const transparent = block.getOverride("transparent")?.value;
|
|
395
|
+
if (transmission !== undefined && typeof transmission === "number" && "transmission" in material && transmission !== material.transmission) {
|
|
396
|
+
beforeRenderListTransparentChangedTransmission.set(this, material.transmission);
|
|
397
|
+
material.transmission = transmission;
|
|
398
|
+
}
|
|
399
|
+
if (transparent !== undefined && typeof transparent === "boolean" && transparent !== material.transparent) {
|
|
400
|
+
beforeRenderListTransparentChanged.set(this, material.transparent);
|
|
401
|
+
material.transparent = transparent;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
};
|
|
405
|
+
/**
|
|
406
|
+
* Callback invoked after an object is added to the render list.
|
|
407
|
+
* Restores the original transparency/transmission values that were overridden in onBeforeRenderListPush.
|
|
408
|
+
* @internal
|
|
409
|
+
*/
|
|
410
|
+
const onAfterRenderListPush = function (_object, _geometry, material, _group) {
|
|
411
|
+
const prevTransparent = beforeRenderListTransparentChanged.get(_object);
|
|
412
|
+
if (prevTransparent !== undefined) {
|
|
413
|
+
beforeRenderListTransparentChanged.delete(_object);
|
|
414
|
+
material.transparent = prevTransparent;
|
|
415
|
+
}
|
|
416
|
+
const prevTransmission = beforeRenderListTransparentChangedTransmission.get(_object);
|
|
417
|
+
if (prevTransmission !== undefined) {
|
|
418
|
+
beforeRenderListTransparentChangedTransmission.delete(_object);
|
|
419
|
+
material.transmission = prevTransmission;
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
// #region OnBeforeRender
|
|
423
|
+
/**
|
|
424
|
+
* Main callback invoked before rendering an object.
|
|
425
|
+
* Applies property block overrides and defines to the material.
|
|
426
|
+
* @internal
|
|
427
|
+
*/
|
|
428
|
+
const onBeforeRender_MaterialBlock = function (_renderer, _scene, _camera, _geometry, material, _group) {
|
|
429
|
+
// Only run if the material belongs to this object and is a "regular" material (not depth or other override material)
|
|
430
|
+
const materials = this.material;
|
|
431
|
+
if (!materials)
|
|
432
|
+
return;
|
|
433
|
+
if (Array.isArray(materials)) {
|
|
434
|
+
if (!materials.includes(material))
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
else if (materials !== material) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
// Keep track of which materials rendering started for so we can check in onAfterRender if it was processed
|
|
441
|
+
// (in case of override materials like depth material where onBeforeRender runs but we don't want to apply overrides)
|
|
442
|
+
if (this[currentlyRenderingFlag] === undefined)
|
|
443
|
+
this[currentlyRenderingFlag] = new WeakSet();
|
|
444
|
+
this[currentlyRenderingFlag].add(material);
|
|
445
|
+
// Before rendering, check if this object (or its parent Group) has a property block with overrides for this material.
|
|
446
|
+
const result = findPropertyBlockAndOwner(this);
|
|
447
|
+
if (!result) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
const { block: propertyBlock, owner } = result;
|
|
451
|
+
// Only apply if this material was registered with this property block
|
|
452
|
+
// if (!registry.hasMaterial(owner, material)) {
|
|
453
|
+
// return;
|
|
454
|
+
// }
|
|
455
|
+
const overrides = propertyBlock.overrides;
|
|
456
|
+
const mat = material;
|
|
457
|
+
// Apply defines to material - this affects shader compilation
|
|
458
|
+
const defines = propertyBlock.getDefines();
|
|
459
|
+
const defineKeys = Object.keys(defines);
|
|
460
|
+
if (defineKeys.length > 0) {
|
|
461
|
+
if (!mat.defines)
|
|
462
|
+
mat.defines = {};
|
|
463
|
+
for (const key of defineKeys) {
|
|
464
|
+
const value = defines[key];
|
|
465
|
+
if (value !== undefined) {
|
|
466
|
+
mat.defines[key] = value;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
// Still set up cache key even if no overrides (defines affect it)
|
|
471
|
+
if (overrides.length === 0 && defineKeys.length === 0) {
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
// Defines always affect shader compilation → need program change
|
|
475
|
+
let needsProgramChange = defineKeys.length > 0;
|
|
476
|
+
if (!mat[$originalValues]) {
|
|
477
|
+
mat[$originalValues] = [];
|
|
478
|
+
}
|
|
479
|
+
const originalValues = mat[$originalValues];
|
|
480
|
+
for (const override of overrides) {
|
|
481
|
+
if (override.value === null)
|
|
482
|
+
continue;
|
|
483
|
+
const currentValue = mat[override.name];
|
|
484
|
+
const existingOriginal = originalValues.find((o) => o.name === override.name);
|
|
485
|
+
if (existingOriginal) {
|
|
486
|
+
// Update to current value each frame so animations/external changes are preserved
|
|
487
|
+
existingOriginal.value = currentValue;
|
|
488
|
+
}
|
|
489
|
+
else {
|
|
490
|
+
originalValues.push({ name: override.name, value: currentValue });
|
|
491
|
+
}
|
|
492
|
+
// Check if this override changes shader features (truthiness change).
|
|
493
|
+
// E.g. null → Texture enables USE_LIGHTMAP, Texture → null disables it.
|
|
494
|
+
// Pure uniform changes (red → blue, textureA → textureB) don't need program switch.
|
|
495
|
+
if (!needsProgramChange && !!currentValue !== !!override.value) {
|
|
496
|
+
needsProgramChange = true;
|
|
497
|
+
}
|
|
498
|
+
// Set all material properties including lightMap -
|
|
499
|
+
// three.js reads material.lightMap to determine shader parameters and upload uniforms
|
|
500
|
+
mat[override.name] = override.value;
|
|
501
|
+
// Apply per-object texture transform (offset/repeat) if specified
|
|
502
|
+
if (override.textureTransform && override.value instanceof Texture) {
|
|
503
|
+
const tex = override.value;
|
|
504
|
+
if (!mat[$savedTextureTransforms])
|
|
505
|
+
mat[$savedTextureTransforms] = [];
|
|
506
|
+
mat[$savedTextureTransforms].push({
|
|
507
|
+
name: override.name,
|
|
508
|
+
offsetX: tex.offset.x, offsetY: tex.offset.y,
|
|
509
|
+
repeatX: tex.repeat.x, repeatY: tex.repeat.y
|
|
510
|
+
});
|
|
511
|
+
const t = override.textureTransform;
|
|
512
|
+
if (t.offset)
|
|
513
|
+
tex.offset.copy(t.offset);
|
|
514
|
+
if (t.repeat)
|
|
515
|
+
tex.repeat.copy(t.repeat);
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
// Only set needsUpdate when overrides change shader features (truthiness changes
|
|
519
|
+
// like null↔texture, or defines added). This triggers getProgram() for program switches.
|
|
520
|
+
// Pure uniform overrides (color, roughness) skip this — no version increment needed.
|
|
521
|
+
if (needsProgramChange) {
|
|
522
|
+
mat.needsUpdate = true;
|
|
523
|
+
}
|
|
524
|
+
// _forceRefresh triggers uniform re-upload for consecutive objects sharing
|
|
525
|
+
// the same program and material (without it three.js skips the upload).
|
|
526
|
+
mat._forceRefresh = true;
|
|
527
|
+
};
|
|
528
|
+
// #region OnAfterRender
|
|
529
|
+
/**
|
|
530
|
+
* Main callback invoked after rendering an object.
|
|
531
|
+
* Restores the original material property values and defines.
|
|
532
|
+
* @internal
|
|
533
|
+
*/
|
|
534
|
+
const onAfterRender_MaterialBlock = function (_renderer, _scene, _camera, _geometry, material, _group) {
|
|
535
|
+
// We don't want to run this logic if onBeforeRender didn't run for this material (e.g. due to DepthMaterial or other override material), so we check the flag set in onBeforeRender
|
|
536
|
+
if (this[currentlyRenderingFlag] === undefined)
|
|
537
|
+
return;
|
|
538
|
+
if (!this[currentlyRenderingFlag].has(material))
|
|
539
|
+
return;
|
|
540
|
+
this[currentlyRenderingFlag].delete(material);
|
|
541
|
+
const result = findPropertyBlockAndOwner(this);
|
|
542
|
+
if (!result) {
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
const { block: propertyBlock, owner } = result;
|
|
546
|
+
// Only restore if this material was registered with this property block
|
|
547
|
+
// if (!registry.hasMaterial(owner, material)) {
|
|
548
|
+
// return;
|
|
549
|
+
// }
|
|
550
|
+
const overrides = propertyBlock.overrides;
|
|
551
|
+
const mat = material;
|
|
552
|
+
const originalValues = mat[$originalValues];
|
|
553
|
+
// Clean up defines — this affects shader compilation
|
|
554
|
+
const defines = propertyBlock.getDefines();
|
|
555
|
+
const defineKeys = Object.keys(defines);
|
|
556
|
+
let needsProgramChange = false;
|
|
557
|
+
if (defineKeys.length > 0 && mat.defines) {
|
|
558
|
+
for (const key of defineKeys) {
|
|
559
|
+
delete mat.defines[key];
|
|
560
|
+
}
|
|
561
|
+
needsProgramChange = true;
|
|
562
|
+
}
|
|
563
|
+
if (overrides.length === 0) {
|
|
564
|
+
if (needsProgramChange) {
|
|
565
|
+
mat.needsUpdate = true;
|
|
566
|
+
mat._forceRefresh = true;
|
|
567
|
+
}
|
|
568
|
+
return;
|
|
569
|
+
}
|
|
570
|
+
if (!originalValues)
|
|
571
|
+
return;
|
|
572
|
+
// Restore texture transforms before restoring material properties
|
|
573
|
+
const savedTransforms = mat[$savedTextureTransforms];
|
|
574
|
+
if (savedTransforms && savedTransforms.length > 0) {
|
|
575
|
+
for (const saved of savedTransforms) {
|
|
576
|
+
const override = overrides.find(o => o.name === saved.name);
|
|
577
|
+
if (override?.value instanceof Texture) {
|
|
578
|
+
override.value.offset.set(saved.offsetX, saved.offsetY);
|
|
579
|
+
override.value.repeat.set(saved.repeatX, saved.repeatY);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
savedTransforms.length = 0;
|
|
583
|
+
}
|
|
584
|
+
for (const override of overrides) {
|
|
585
|
+
const original = originalValues.find(o => o.name === override.name);
|
|
586
|
+
if (original) {
|
|
587
|
+
// Check if restoring changes shader features (truthiness change)
|
|
588
|
+
if (!needsProgramChange && !!override.value !== !!original.value) {
|
|
589
|
+
needsProgramChange = true;
|
|
590
|
+
}
|
|
591
|
+
mat[override.name] = original.value;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
// Only set needsUpdate when restoring affects shader features
|
|
595
|
+
if (needsProgramChange) {
|
|
596
|
+
mat.needsUpdate = true;
|
|
597
|
+
}
|
|
598
|
+
// Always force uniform refresh so the next object gets correct values
|
|
599
|
+
mat._forceRefresh = true;
|
|
600
|
+
};
|
|
601
|
+
// #region Attach Callbacks
|
|
602
|
+
/**
|
|
603
|
+
* Attaches the property block render callbacks to an object and its child meshes.
|
|
604
|
+
* @param object The object to attach callbacks to
|
|
605
|
+
* @param _propertyBlock The property block being attached (unused but kept for clarity)
|
|
606
|
+
* @internal
|
|
607
|
+
*/
|
|
608
|
+
function attachPropertyBlockToObject(object, _propertyBlock) {
|
|
609
|
+
// Collect and register all materials that belong to this property block
|
|
610
|
+
// const materials = new Set<Material>();
|
|
611
|
+
// collectMaterials(object, materials);
|
|
612
|
+
// materials.forEach(mat => registry.addMaterial(object, mat));
|
|
613
|
+
// Attach callbacks to renderable objects (Mesh, SkinnedMesh)
|
|
614
|
+
// Groups don't render themselves but we still need to handle child meshes
|
|
615
|
+
if (object.type === "Group") {
|
|
616
|
+
object.children.forEach(child => {
|
|
617
|
+
if (child.type === "Mesh" || child.type === "SkinnedMesh") {
|
|
618
|
+
attachCallbacksToMesh(child, object);
|
|
619
|
+
}
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
else if (object.type === "Mesh" || object.type === "SkinnedMesh") {
|
|
623
|
+
attachCallbacksToMesh(object, object);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
/**
|
|
627
|
+
* Attaches render callbacks to a specific mesh object.
|
|
628
|
+
* Chains with existing callbacks if they exist.
|
|
629
|
+
* @param mesh The mesh to attach callbacks to
|
|
630
|
+
* @param propertyBlockOwner The object that owns the property block (may be the mesh itself or its parent Group)
|
|
631
|
+
* @internal
|
|
632
|
+
*/
|
|
633
|
+
function attachCallbacksToMesh(mesh, propertyBlockOwner) {
|
|
634
|
+
// Check if this specific mesh already has our callbacks attached for this property block owner
|
|
635
|
+
if (registry.isHooked(mesh, propertyBlockOwner)) {
|
|
636
|
+
// Already hooked for this property block owner
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
registry.addHook(mesh, propertyBlockOwner);
|
|
640
|
+
if (!mesh.onBeforeRender) {
|
|
641
|
+
mesh.onBeforeRender = onBeforeRender_MaterialBlock;
|
|
642
|
+
}
|
|
643
|
+
else {
|
|
644
|
+
const original = mesh.onBeforeRender;
|
|
645
|
+
mesh.onBeforeRender = function (renderer, scene, camera, geometry, material, group) {
|
|
646
|
+
original.call(this, renderer, scene, camera, geometry, material, group);
|
|
647
|
+
onBeforeRender_MaterialBlock.call(this, renderer, scene, camera, geometry, material, group);
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
if (!mesh.onAfterRender) {
|
|
651
|
+
mesh.onAfterRender = onAfterRender_MaterialBlock;
|
|
652
|
+
}
|
|
653
|
+
else {
|
|
654
|
+
const original = mesh.onAfterRender;
|
|
655
|
+
mesh.onAfterRender = function (renderer, scene, camera, geometry, material, group) {
|
|
656
|
+
onAfterRender_MaterialBlock.call(this, renderer, scene, camera, geometry, material, group);
|
|
657
|
+
original.call(this, renderer, scene, camera, geometry, material, group);
|
|
658
|
+
};
|
|
659
|
+
}
|
|
660
|
+
/** @ts-ignore patched in three.js */
|
|
661
|
+
mesh.onBeforeRenderListPush = onBeforeRenderListPush;
|
|
662
|
+
/** @ts-ignore patched in three.js */
|
|
663
|
+
mesh.onAfterRenderListPush = onAfterRenderListPush;
|
|
664
|
+
}
|
|
665
|
+
//#endregion
|
|
666
|
+
/**
|
|
667
|
+
* Checks if an object has a MaterialPropertyBlock attached to it.
|
|
668
|
+
*
|
|
669
|
+
* @param object The object to check
|
|
670
|
+
* @returns True if the object has a property block registered
|
|
671
|
+
*
|
|
672
|
+
* @example
|
|
673
|
+
* ```typescript
|
|
674
|
+
* if (objectHasPropertyBlock(myMesh)) {
|
|
675
|
+
* console.log("This mesh has property overrides");
|
|
676
|
+
* }
|
|
677
|
+
* ```
|
|
678
|
+
*/
|
|
679
|
+
export function objectHasPropertyBlock(object) {
|
|
680
|
+
return registry.getBlock(object) !== undefined;
|
|
681
|
+
}
|
|
682
|
+
//# sourceMappingURL=engine_materialpropertyblock.js.map
|