@combeenation/3d-viewer 14.0.0 → 14.0.1-rc1
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/README.md +9 -9
- package/dist/lib-cjs/buildinfo.json +3 -3
- package/dist/lib-cjs/commonjs.tsconfig.tsbuildinfo +1 -1
- package/dist/lib-cjs/index.d.ts +62 -62
- package/dist/lib-cjs/index.js +94 -94
- package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.d.ts +10 -10
- package/dist/lib-cjs/internal/cbn-custom-babylon-loader-plugin.js +131 -131
- package/dist/lib-cjs/internal/cloning-helper.d.ts +19 -19
- package/dist/lib-cjs/internal/cloning-helper.js +163 -163
- package/dist/lib-cjs/internal/device-helper.d.ts +9 -9
- package/dist/lib-cjs/internal/device-helper.js +24 -24
- package/dist/lib-cjs/internal/geometry-helper.d.ts +21 -21
- package/dist/lib-cjs/internal/geometry-helper.js +145 -145
- package/dist/lib-cjs/internal/metadata-helper.d.ts +26 -26
- package/dist/lib-cjs/internal/metadata-helper.js +50 -50
- package/dist/lib-cjs/internal/paintable-helper.d.ts +40 -40
- package/dist/lib-cjs/internal/paintable-helper.js +286 -286
- package/dist/lib-cjs/internal/tags-helper.d.ts +12 -12
- package/dist/lib-cjs/internal/tags-helper.js +37 -37
- package/dist/lib-cjs/manager/camera-manager.d.ts +110 -110
- package/dist/lib-cjs/manager/camera-manager.js +206 -206
- package/dist/lib-cjs/manager/debug-manager.d.ts +60 -60
- package/dist/lib-cjs/manager/debug-manager.js +217 -217
- package/dist/lib-cjs/manager/event-manager.d.ts +52 -52
- package/dist/lib-cjs/manager/event-manager.js +71 -71
- package/dist/lib-cjs/manager/gltf-export-manager.d.ts +84 -75
- package/dist/lib-cjs/manager/gltf-export-manager.js +290 -278
- package/dist/lib-cjs/manager/gltf-export-manager.js.map +1 -1
- package/dist/lib-cjs/manager/material-manager.d.ts +35 -35
- package/dist/lib-cjs/manager/material-manager.js +125 -125
- package/dist/lib-cjs/manager/model-manager.d.ts +145 -145
- package/dist/lib-cjs/manager/model-manager.js +382 -382
- package/dist/lib-cjs/manager/parameter-manager.d.ts +210 -210
- package/dist/lib-cjs/manager/parameter-manager.js +514 -514
- package/dist/lib-cjs/manager/scene-manager.d.ts +45 -45
- package/dist/lib-cjs/manager/scene-manager.js +64 -64
- package/dist/lib-cjs/manager/texture-manager.d.ts +12 -12
- package/dist/lib-cjs/manager/texture-manager.js +43 -43
- package/dist/lib-cjs/viewer-error.d.ts +48 -48
- package/dist/lib-cjs/viewer-error.js +60 -60
- package/dist/lib-cjs/viewer.d.ts +115 -115
- package/dist/lib-cjs/viewer.js +217 -217
- package/package.json +91 -91
- package/src/buildinfo.json +3 -3
- package/src/dev.ts +47 -47
- package/src/global-types.d.ts +39 -39
- package/src/index.ts +81 -81
- package/src/internal/cbn-custom-babylon-loader-plugin.ts +159 -159
- package/src/internal/cloning-helper.ts +225 -225
- package/src/internal/device-helper.ts +25 -25
- package/src/internal/geometry-helper.ts +181 -181
- package/src/internal/metadata-helper.ts +63 -63
- package/src/internal/paintable-helper.ts +310 -310
- package/src/internal/tags-helper.ts +41 -41
- package/src/manager/camera-manager.ts +365 -365
- package/src/manager/debug-manager.ts +245 -245
- package/src/manager/event-manager.ts +72 -72
- package/src/manager/gltf-export-manager.ts +357 -341
- package/src/manager/material-manager.ts +135 -135
- package/src/manager/model-manager.ts +458 -458
- package/src/manager/parameter-manager.ts +652 -652
- package/src/manager/scene-manager.ts +101 -101
- package/src/manager/texture-manager.ts +32 -32
- package/src/viewer-error.ts +68 -68
- package/src/viewer.ts +290 -290
|
@@ -1,225 +1,225 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AbstractMesh,
|
|
3
|
-
AssetContainer,
|
|
4
|
-
InstancedMesh,
|
|
5
|
-
Mesh,
|
|
6
|
-
MorphTargetManager,
|
|
7
|
-
NodeNamingStrategy,
|
|
8
|
-
Scene,
|
|
9
|
-
TagNamingStrategy,
|
|
10
|
-
TransformNode,
|
|
11
|
-
} from '../index';
|
|
12
|
-
import {
|
|
13
|
-
clearInternalMetadataValue,
|
|
14
|
-
cloneInternalMetadata,
|
|
15
|
-
cloneMetadata,
|
|
16
|
-
getInternalMetadataValue,
|
|
17
|
-
setInternalMetadataValue,
|
|
18
|
-
} from './metadata-helper';
|
|
19
|
-
import { cloneTags, getTags, setTags } from './tags-helper';
|
|
20
|
-
|
|
21
|
-
const _defaultNodeNamingStrategy: NodeNamingStrategy = (node: TransformNode, newModelName: string): string => {
|
|
22
|
-
return `${node.name}.${newModelName}`;
|
|
23
|
-
};
|
|
24
|
-
const _defaultTagNamingStrategy: TagNamingStrategy = (tag: string, newModelName: string): string => {
|
|
25
|
-
// don't rename tags by default
|
|
26
|
-
return tag;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Clones the node hierarchy of the asset container with some special treatment that differs from Babylon.js default
|
|
31
|
-
* cloning behaviour, such as:
|
|
32
|
-
* - create new instances of morph target manager
|
|
33
|
-
* - deep cloning internal metadata
|
|
34
|
-
* - deep cloning metadata and tags for instanced meshes
|
|
35
|
-
* - reassigns source meshes in cloned asset container
|
|
36
|
-
* Furthermore there is the possibility to adjust naming of nodes and tags in the cloned asset container
|
|
37
|
-
*/
|
|
38
|
-
export function cloneModelAssetContainer(
|
|
39
|
-
sourceAssetContainer: AssetContainer,
|
|
40
|
-
newModelName: string,
|
|
41
|
-
renaming: {
|
|
42
|
-
nodeNamingStrategy?: NodeNamingStrategy;
|
|
43
|
-
tagNamingStrategy?: TagNamingStrategy;
|
|
44
|
-
},
|
|
45
|
-
scene: Scene
|
|
46
|
-
): AssetContainer {
|
|
47
|
-
const targetAssetContainer = new AssetContainer(scene);
|
|
48
|
-
|
|
49
|
-
sourceAssetContainer.rootNodes.forEach(node => {
|
|
50
|
-
if (node instanceof TransformNode) {
|
|
51
|
-
// this is the actual node tree cloning procedure
|
|
52
|
-
_cloneNode(node, newModelName, null, targetAssetContainer, {
|
|
53
|
-
nodeNamingStrategy: renaming?.nodeNamingStrategy,
|
|
54
|
-
tagNamingStrategy: renaming?.tagNamingStrategy,
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
targetAssetContainer.populateRootNodes();
|
|
60
|
-
targetAssetContainer.rootNodes.forEach(node => {
|
|
61
|
-
if (node instanceof TransformNode) {
|
|
62
|
-
// reassigning instanced meshes can only be done after the model has been fully cloned, as we can't be sure if
|
|
63
|
-
// the source or instanced mesh is handled prior
|
|
64
|
-
_reAssignSourceMesh(node, sourceAssetContainer.meshes, targetAssetContainer.meshes);
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
// morph target manager is not cloned per default, therefore morph targets would affect original and cloned model at
|
|
69
|
-
// the same time, which we don't want
|
|
70
|
-
sourceAssetContainer.morphTargetManagers.forEach(sourceMTM => {
|
|
71
|
-
cloneMorphTargetManager(sourceMTM, targetAssetContainer);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
_clearCloningMetadata(sourceAssetContainer);
|
|
75
|
-
_clearCloningMetadata(targetAssetContainer);
|
|
76
|
-
|
|
77
|
-
// hide immediately, so that show model code can do it's thing (scene preparation) before finally showing the model
|
|
78
|
-
targetAssetContainer.removeAllFromScene();
|
|
79
|
-
|
|
80
|
-
return targetAssetContainer;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Helper function for cloning a morph target manager and updating all the mesh assignments.
|
|
85
|
-
* Meshes need to be already available in the target asset container.
|
|
86
|
-
*/
|
|
87
|
-
export function cloneMorphTargetManager(
|
|
88
|
-
sourceMTM: MorphTargetManager,
|
|
89
|
-
targetAssetContainer: AssetContainer
|
|
90
|
-
): MorphTargetManager {
|
|
91
|
-
const clonedMTM = sourceMTM.clone();
|
|
92
|
-
|
|
93
|
-
clonedMTM._parentContainer = targetAssetContainer;
|
|
94
|
-
targetAssetContainer.morphTargetManagers.push(clonedMTM);
|
|
95
|
-
|
|
96
|
-
targetAssetContainer.meshes.forEach(mesh => {
|
|
97
|
-
if (mesh.morphTargetManager === sourceMTM) {
|
|
98
|
-
mesh.morphTargetManager = clonedMTM;
|
|
99
|
-
}
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
return clonedMTM;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* The actual geometry cloning function.
|
|
107
|
-
* Calls "clone" function of dedicated Node type and recourses into children to clone the whole node tree.
|
|
108
|
-
* Adds temporary metadata for special treatment of certain node types (e.g. InstancedMesh).
|
|
109
|
-
*/
|
|
110
|
-
function _cloneNode(
|
|
111
|
-
node: TransformNode,
|
|
112
|
-
newModelName: string,
|
|
113
|
-
newParent: TransformNode | null,
|
|
114
|
-
assetContainer: AssetContainer,
|
|
115
|
-
renaming: {
|
|
116
|
-
nodeNamingStrategy?: NodeNamingStrategy;
|
|
117
|
-
tagNamingStrategy?: TagNamingStrategy;
|
|
118
|
-
}
|
|
119
|
-
): void {
|
|
120
|
-
const nodeNamingStrategy = renaming.nodeNamingStrategy ?? _defaultNodeNamingStrategy;
|
|
121
|
-
const tagNamingStrategy = renaming.tagNamingStrategy ?? _defaultTagNamingStrategy;
|
|
122
|
-
|
|
123
|
-
const cloneName = nodeNamingStrategy(node, newModelName);
|
|
124
|
-
const clone = node.clone(cloneName, newParent, true);
|
|
125
|
-
if (!clone) {
|
|
126
|
-
throw new Error(`Cloning node "${node.name}" failed`);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
clone.id = cloneName;
|
|
130
|
-
// create a deep clone for internal metadata, as the default clone function uses the same assignment of the sources
|
|
131
|
-
// internal metadata
|
|
132
|
-
cloneInternalMetadata(node, clone);
|
|
133
|
-
// ATM the assignment from clone to source is needed for reassigning instanced meshes after cloning
|
|
134
|
-
// may be usefull for other node types in the future as well
|
|
135
|
-
setInternalMetadataValue(clone, 'cloneSource', node.uniqueId);
|
|
136
|
-
setInternalMetadataValue(node, 'cloneTarget', clone.uniqueId);
|
|
137
|
-
|
|
138
|
-
if (node instanceof InstancedMesh) {
|
|
139
|
-
_cloneInstancedMeshData(node, clone as InstancedMesh);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const sourceTags = getTags(node);
|
|
143
|
-
const mappedTags = sourceTags.map(tag => tagNamingStrategy(tag, newModelName));
|
|
144
|
-
setTags(clone, mappedTags);
|
|
145
|
-
|
|
146
|
-
clone._parentContainer = assetContainer;
|
|
147
|
-
// TODO WTT: create global helper function for distinguishing between transform nodes and meshes
|
|
148
|
-
if (clone instanceof AbstractMesh) {
|
|
149
|
-
assetContainer.meshes.push(clone);
|
|
150
|
-
} else {
|
|
151
|
-
assetContainer.transformNodes.push(clone);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
const children = node.getChildTransformNodes(true);
|
|
155
|
-
children.forEach(child => _cloneNode(child, newModelName, clone, assetContainer, renaming));
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
/**
|
|
159
|
-
* Cloned instanced meshes are having the same source mesh as their cloning source.
|
|
160
|
-
* However we want the cloned source mesh to be the actual source.
|
|
161
|
-
* Exchanging and reassigning is done within this function.
|
|
162
|
-
* Operates on the given `node` and all its children recursively.
|
|
163
|
-
*/
|
|
164
|
-
function _reAssignSourceMesh(node: TransformNode, sourceMeshes: AbstractMesh[], clonedMeshes: AbstractMesh[]): void {
|
|
165
|
-
if (node instanceof InstancedMesh) {
|
|
166
|
-
// find the cloned instance and source mesh
|
|
167
|
-
const clonedInstance = node;
|
|
168
|
-
const originalInstance = _findByUniqueId(
|
|
169
|
-
sourceMeshes,
|
|
170
|
-
getInternalMetadataValue(clonedInstance, 'cloneSource') as number
|
|
171
|
-
);
|
|
172
|
-
if (originalInstance instanceof InstancedMesh) {
|
|
173
|
-
const originalSource = originalInstance.sourceMesh;
|
|
174
|
-
const clonedSource = _findByUniqueId(
|
|
175
|
-
clonedMeshes,
|
|
176
|
-
getInternalMetadataValue(originalSource, 'cloneTarget') as number
|
|
177
|
-
);
|
|
178
|
-
if (clonedSource instanceof Mesh) {
|
|
179
|
-
// it's not possible to just exchange the source mesh => readonly property
|
|
180
|
-
// we have to create a new clone with the correct source mesh
|
|
181
|
-
const exchangedInstance = clonedInstance.clone(clonedInstance.name, clonedInstance.parent, true, clonedSource);
|
|
182
|
-
clonedInstance._parentContainer!.meshes.push(exchangedInstance);
|
|
183
|
-
|
|
184
|
-
_cloneInstancedMeshData(clonedInstance, exchangedInstance);
|
|
185
|
-
|
|
186
|
-
// re-reference the childs of the cloned instance as well
|
|
187
|
-
const childs = clonedInstance.getChildTransformNodes(true);
|
|
188
|
-
childs.forEach(child => (child.parent = exchangedInstance));
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
|
|
192
|
-
// old instance mesh is not needed anymore and can be disposed
|
|
193
|
-
clonedInstance.dispose();
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// recurse into childs
|
|
197
|
-
const children = node.getChildTransformNodes(true);
|
|
198
|
-
children.forEach(child => _reAssignSourceMesh(child, sourceMeshes, clonedMeshes));
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
/**
|
|
202
|
-
* Removes metadata entries "cloneSource" and "cloneTarget" from all meshes and transform nodes inside the asset
|
|
203
|
-
* container
|
|
204
|
-
*/
|
|
205
|
-
function _clearCloningMetadata(assetContainer: AssetContainer): void {
|
|
206
|
-
[...assetContainer.meshes, ...assetContainer.transformNodes].forEach(node => {
|
|
207
|
-
clearInternalMetadataValue(node, 'cloneSource');
|
|
208
|
-
clearInternalMetadataValue(node, 'cloneTarget');
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
/**
|
|
213
|
-
* "InstancedMesh.clone" function doesn't clone tags and metadata, also the enabled state doesn't seem to be cloned
|
|
214
|
-
* correctly.
|
|
215
|
-
* This function adjusts the behaviour to fulfill our expectations.
|
|
216
|
-
*/
|
|
217
|
-
function _cloneInstancedMeshData(sourceInstancedMesh: InstancedMesh, targetInstancedMesh: InstancedMesh): void {
|
|
218
|
-
targetInstancedMesh.setEnabled(sourceInstancedMesh.isEnabled(false));
|
|
219
|
-
cloneMetadata(sourceInstancedMesh, targetInstancedMesh);
|
|
220
|
-
cloneTags(sourceInstancedMesh, targetInstancedMesh);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
function _findByUniqueId(nodes: TransformNode[], uniqueId: number): TransformNode | undefined {
|
|
224
|
-
return nodes.find(node => node.uniqueId === uniqueId);
|
|
225
|
-
}
|
|
1
|
+
import {
|
|
2
|
+
AbstractMesh,
|
|
3
|
+
AssetContainer,
|
|
4
|
+
InstancedMesh,
|
|
5
|
+
Mesh,
|
|
6
|
+
MorphTargetManager,
|
|
7
|
+
NodeNamingStrategy,
|
|
8
|
+
Scene,
|
|
9
|
+
TagNamingStrategy,
|
|
10
|
+
TransformNode,
|
|
11
|
+
} from '../index';
|
|
12
|
+
import {
|
|
13
|
+
clearInternalMetadataValue,
|
|
14
|
+
cloneInternalMetadata,
|
|
15
|
+
cloneMetadata,
|
|
16
|
+
getInternalMetadataValue,
|
|
17
|
+
setInternalMetadataValue,
|
|
18
|
+
} from './metadata-helper';
|
|
19
|
+
import { cloneTags, getTags, setTags } from './tags-helper';
|
|
20
|
+
|
|
21
|
+
const _defaultNodeNamingStrategy: NodeNamingStrategy = (node: TransformNode, newModelName: string): string => {
|
|
22
|
+
return `${node.name}.${newModelName}`;
|
|
23
|
+
};
|
|
24
|
+
const _defaultTagNamingStrategy: TagNamingStrategy = (tag: string, newModelName: string): string => {
|
|
25
|
+
// don't rename tags by default
|
|
26
|
+
return tag;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Clones the node hierarchy of the asset container with some special treatment that differs from Babylon.js default
|
|
31
|
+
* cloning behaviour, such as:
|
|
32
|
+
* - create new instances of morph target manager
|
|
33
|
+
* - deep cloning internal metadata
|
|
34
|
+
* - deep cloning metadata and tags for instanced meshes
|
|
35
|
+
* - reassigns source meshes in cloned asset container
|
|
36
|
+
* Furthermore there is the possibility to adjust naming of nodes and tags in the cloned asset container
|
|
37
|
+
*/
|
|
38
|
+
export function cloneModelAssetContainer(
|
|
39
|
+
sourceAssetContainer: AssetContainer,
|
|
40
|
+
newModelName: string,
|
|
41
|
+
renaming: {
|
|
42
|
+
nodeNamingStrategy?: NodeNamingStrategy;
|
|
43
|
+
tagNamingStrategy?: TagNamingStrategy;
|
|
44
|
+
},
|
|
45
|
+
scene: Scene
|
|
46
|
+
): AssetContainer {
|
|
47
|
+
const targetAssetContainer = new AssetContainer(scene);
|
|
48
|
+
|
|
49
|
+
sourceAssetContainer.rootNodes.forEach(node => {
|
|
50
|
+
if (node instanceof TransformNode) {
|
|
51
|
+
// this is the actual node tree cloning procedure
|
|
52
|
+
_cloneNode(node, newModelName, null, targetAssetContainer, {
|
|
53
|
+
nodeNamingStrategy: renaming?.nodeNamingStrategy,
|
|
54
|
+
tagNamingStrategy: renaming?.tagNamingStrategy,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
targetAssetContainer.populateRootNodes();
|
|
60
|
+
targetAssetContainer.rootNodes.forEach(node => {
|
|
61
|
+
if (node instanceof TransformNode) {
|
|
62
|
+
// reassigning instanced meshes can only be done after the model has been fully cloned, as we can't be sure if
|
|
63
|
+
// the source or instanced mesh is handled prior
|
|
64
|
+
_reAssignSourceMesh(node, sourceAssetContainer.meshes, targetAssetContainer.meshes);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
// morph target manager is not cloned per default, therefore morph targets would affect original and cloned model at
|
|
69
|
+
// the same time, which we don't want
|
|
70
|
+
sourceAssetContainer.morphTargetManagers.forEach(sourceMTM => {
|
|
71
|
+
cloneMorphTargetManager(sourceMTM, targetAssetContainer);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
_clearCloningMetadata(sourceAssetContainer);
|
|
75
|
+
_clearCloningMetadata(targetAssetContainer);
|
|
76
|
+
|
|
77
|
+
// hide immediately, so that show model code can do it's thing (scene preparation) before finally showing the model
|
|
78
|
+
targetAssetContainer.removeAllFromScene();
|
|
79
|
+
|
|
80
|
+
return targetAssetContainer;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Helper function for cloning a morph target manager and updating all the mesh assignments.
|
|
85
|
+
* Meshes need to be already available in the target asset container.
|
|
86
|
+
*/
|
|
87
|
+
export function cloneMorphTargetManager(
|
|
88
|
+
sourceMTM: MorphTargetManager,
|
|
89
|
+
targetAssetContainer: AssetContainer
|
|
90
|
+
): MorphTargetManager {
|
|
91
|
+
const clonedMTM = sourceMTM.clone();
|
|
92
|
+
|
|
93
|
+
clonedMTM._parentContainer = targetAssetContainer;
|
|
94
|
+
targetAssetContainer.morphTargetManagers.push(clonedMTM);
|
|
95
|
+
|
|
96
|
+
targetAssetContainer.meshes.forEach(mesh => {
|
|
97
|
+
if (mesh.morphTargetManager === sourceMTM) {
|
|
98
|
+
mesh.morphTargetManager = clonedMTM;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return clonedMTM;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* The actual geometry cloning function.
|
|
107
|
+
* Calls "clone" function of dedicated Node type and recourses into children to clone the whole node tree.
|
|
108
|
+
* Adds temporary metadata for special treatment of certain node types (e.g. InstancedMesh).
|
|
109
|
+
*/
|
|
110
|
+
function _cloneNode(
|
|
111
|
+
node: TransformNode,
|
|
112
|
+
newModelName: string,
|
|
113
|
+
newParent: TransformNode | null,
|
|
114
|
+
assetContainer: AssetContainer,
|
|
115
|
+
renaming: {
|
|
116
|
+
nodeNamingStrategy?: NodeNamingStrategy;
|
|
117
|
+
tagNamingStrategy?: TagNamingStrategy;
|
|
118
|
+
}
|
|
119
|
+
): void {
|
|
120
|
+
const nodeNamingStrategy = renaming.nodeNamingStrategy ?? _defaultNodeNamingStrategy;
|
|
121
|
+
const tagNamingStrategy = renaming.tagNamingStrategy ?? _defaultTagNamingStrategy;
|
|
122
|
+
|
|
123
|
+
const cloneName = nodeNamingStrategy(node, newModelName);
|
|
124
|
+
const clone = node.clone(cloneName, newParent, true);
|
|
125
|
+
if (!clone) {
|
|
126
|
+
throw new Error(`Cloning node "${node.name}" failed`);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
clone.id = cloneName;
|
|
130
|
+
// create a deep clone for internal metadata, as the default clone function uses the same assignment of the sources
|
|
131
|
+
// internal metadata
|
|
132
|
+
cloneInternalMetadata(node, clone);
|
|
133
|
+
// ATM the assignment from clone to source is needed for reassigning instanced meshes after cloning
|
|
134
|
+
// may be usefull for other node types in the future as well
|
|
135
|
+
setInternalMetadataValue(clone, 'cloneSource', node.uniqueId);
|
|
136
|
+
setInternalMetadataValue(node, 'cloneTarget', clone.uniqueId);
|
|
137
|
+
|
|
138
|
+
if (node instanceof InstancedMesh) {
|
|
139
|
+
_cloneInstancedMeshData(node, clone as InstancedMesh);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const sourceTags = getTags(node);
|
|
143
|
+
const mappedTags = sourceTags.map(tag => tagNamingStrategy(tag, newModelName));
|
|
144
|
+
setTags(clone, mappedTags);
|
|
145
|
+
|
|
146
|
+
clone._parentContainer = assetContainer;
|
|
147
|
+
// TODO WTT: create global helper function for distinguishing between transform nodes and meshes
|
|
148
|
+
if (clone instanceof AbstractMesh) {
|
|
149
|
+
assetContainer.meshes.push(clone);
|
|
150
|
+
} else {
|
|
151
|
+
assetContainer.transformNodes.push(clone);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const children = node.getChildTransformNodes(true);
|
|
155
|
+
children.forEach(child => _cloneNode(child, newModelName, clone, assetContainer, renaming));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Cloned instanced meshes are having the same source mesh as their cloning source.
|
|
160
|
+
* However we want the cloned source mesh to be the actual source.
|
|
161
|
+
* Exchanging and reassigning is done within this function.
|
|
162
|
+
* Operates on the given `node` and all its children recursively.
|
|
163
|
+
*/
|
|
164
|
+
function _reAssignSourceMesh(node: TransformNode, sourceMeshes: AbstractMesh[], clonedMeshes: AbstractMesh[]): void {
|
|
165
|
+
if (node instanceof InstancedMesh) {
|
|
166
|
+
// find the cloned instance and source mesh
|
|
167
|
+
const clonedInstance = node;
|
|
168
|
+
const originalInstance = _findByUniqueId(
|
|
169
|
+
sourceMeshes,
|
|
170
|
+
getInternalMetadataValue(clonedInstance, 'cloneSource') as number
|
|
171
|
+
);
|
|
172
|
+
if (originalInstance instanceof InstancedMesh) {
|
|
173
|
+
const originalSource = originalInstance.sourceMesh;
|
|
174
|
+
const clonedSource = _findByUniqueId(
|
|
175
|
+
clonedMeshes,
|
|
176
|
+
getInternalMetadataValue(originalSource, 'cloneTarget') as number
|
|
177
|
+
);
|
|
178
|
+
if (clonedSource instanceof Mesh) {
|
|
179
|
+
// it's not possible to just exchange the source mesh => readonly property
|
|
180
|
+
// we have to create a new clone with the correct source mesh
|
|
181
|
+
const exchangedInstance = clonedInstance.clone(clonedInstance.name, clonedInstance.parent, true, clonedSource);
|
|
182
|
+
clonedInstance._parentContainer!.meshes.push(exchangedInstance);
|
|
183
|
+
|
|
184
|
+
_cloneInstancedMeshData(clonedInstance, exchangedInstance);
|
|
185
|
+
|
|
186
|
+
// re-reference the childs of the cloned instance as well
|
|
187
|
+
const childs = clonedInstance.getChildTransformNodes(true);
|
|
188
|
+
childs.forEach(child => (child.parent = exchangedInstance));
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// old instance mesh is not needed anymore and can be disposed
|
|
193
|
+
clonedInstance.dispose();
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// recurse into childs
|
|
197
|
+
const children = node.getChildTransformNodes(true);
|
|
198
|
+
children.forEach(child => _reAssignSourceMesh(child, sourceMeshes, clonedMeshes));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Removes metadata entries "cloneSource" and "cloneTarget" from all meshes and transform nodes inside the asset
|
|
203
|
+
* container
|
|
204
|
+
*/
|
|
205
|
+
function _clearCloningMetadata(assetContainer: AssetContainer): void {
|
|
206
|
+
[...assetContainer.meshes, ...assetContainer.transformNodes].forEach(node => {
|
|
207
|
+
clearInternalMetadataValue(node, 'cloneSource');
|
|
208
|
+
clearInternalMetadataValue(node, 'cloneTarget');
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* "InstancedMesh.clone" function doesn't clone tags and metadata, also the enabled state doesn't seem to be cloned
|
|
214
|
+
* correctly.
|
|
215
|
+
* This function adjusts the behaviour to fulfill our expectations.
|
|
216
|
+
*/
|
|
217
|
+
function _cloneInstancedMeshData(sourceInstancedMesh: InstancedMesh, targetInstancedMesh: InstancedMesh): void {
|
|
218
|
+
targetInstancedMesh.setEnabled(sourceInstancedMesh.isEnabled(false));
|
|
219
|
+
cloneMetadata(sourceInstancedMesh, targetInstancedMesh);
|
|
220
|
+
cloneTags(sourceInstancedMesh, targetInstancedMesh);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
function _findByUniqueId(nodes: TransformNode[], uniqueId: number): TransformNode | undefined {
|
|
224
|
+
return nodes.find(node => node.uniqueId === uniqueId);
|
|
225
|
+
}
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { LimitTextureSizeConfig } from '../index';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Checks if the current device is an iPhone
|
|
5
|
-
*/
|
|
6
|
-
export function getIsIPhone(): boolean {
|
|
7
|
-
const isIPhone = /iPhone/.test(navigator.userAgent);
|
|
8
|
-
|
|
9
|
-
return isIPhone;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Checks if the current device is "scaled down", as defined in the Spec
|
|
14
|
-
*/
|
|
15
|
-
export function getIsScaledDownDevice(limitTextureSize: LimitTextureSizeConfig | false): boolean {
|
|
16
|
-
if (!limitTextureSize) {
|
|
17
|
-
// no down scaling defined
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// check device
|
|
22
|
-
const deviceFits = limitTextureSize.devices === 'all' || (limitTextureSize.devices === 'iPhone' && getIsIPhone());
|
|
23
|
-
|
|
24
|
-
return deviceFits;
|
|
25
|
-
}
|
|
1
|
+
import { LimitTextureSizeConfig } from '../index';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if the current device is an iPhone
|
|
5
|
+
*/
|
|
6
|
+
export function getIsIPhone(): boolean {
|
|
7
|
+
const isIPhone = /iPhone/.test(navigator.userAgent);
|
|
8
|
+
|
|
9
|
+
return isIPhone;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the current device is "scaled down", as defined in the Spec
|
|
14
|
+
*/
|
|
15
|
+
export function getIsScaledDownDevice(limitTextureSize: LimitTextureSizeConfig | false): boolean {
|
|
16
|
+
if (!limitTextureSize) {
|
|
17
|
+
// no down scaling defined
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// check device
|
|
22
|
+
const deviceFits = limitTextureSize.devices === 'all' || (limitTextureSize.devices === 'iPhone' && getIsIPhone());
|
|
23
|
+
|
|
24
|
+
return deviceFits;
|
|
25
|
+
}
|