mesheditor 0.0.0-stage → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/README.md +853 -2
- package/dist-lib/esm/AIAgentService.d.ts +106 -0
- package/dist-lib/esm/AIAgentService.js +1471 -0
- package/dist-lib/esm/MeshEditorApp.d.ts +412 -0
- package/dist-lib/esm/MeshEditorApp.js +9092 -0
- package/dist-lib/esm/MeshEditorService.d.ts +459 -0
- package/dist-lib/esm/MeshEditorService.js +7083 -0
- package/dist-lib/esm/Neural3DGeneratorService.d.ts +88 -0
- package/dist-lib/esm/Neural3DGeneratorService.js +1397 -0
- package/dist-lib/esm/adapters/BrowserPlatformAdapter.d.ts +16 -0
- package/dist-lib/esm/adapters/BrowserPlatformAdapter.js +25 -0
- package/dist-lib/esm/adapters/ElectronPlatformAdapter.d.ts +32 -0
- package/dist-lib/esm/adapters/ElectronPlatformAdapter.js +336 -0
- package/dist-lib/esm/adapters/HostAppAdapter.d.ts +54 -0
- package/dist-lib/esm/adapters/HostAppAdapter.js +75 -0
- package/dist-lib/esm/adapters/PlatformAdapter.d.ts +98 -0
- package/dist-lib/esm/adapters/PlatformAdapter.js +1 -0
- package/dist-lib/esm/adapters/index.d.ts +13 -0
- package/dist-lib/esm/adapters/index.js +15 -0
- package/dist-lib/esm/assets/fonts/Droid_Sans_Bold.json +1 -0
- package/dist-lib/esm/assets/fonts/Droid_Sans_Regular.json +1 -0
- package/dist-lib/esm/assets/fonts/Kenney_Future_Regular.json +1 -0
- package/dist-lib/esm/baking/TextureBaker.d.ts +84 -0
- package/dist-lib/esm/baking/TextureBaker.js +709 -0
- package/dist-lib/esm/builtInSkills.d.ts +19 -0
- package/dist-lib/esm/builtInSkills.js +186 -0
- package/dist-lib/esm/compression/ModelCompressor.d.ts +87 -0
- package/dist-lib/esm/compression/ModelCompressor.js +553 -0
- package/dist-lib/esm/decals/DecalController.d.ts +147 -0
- package/dist-lib/esm/decals/DecalController.js +1031 -0
- package/dist-lib/esm/decals/decalAssetUrls.d.ts +1 -0
- package/dist-lib/esm/decals/decalAssetUrls.js +16 -0
- package/dist-lib/esm/geometry/architecture.d.ts +114 -0
- package/dist-lib/esm/geometry/architecture.js +832 -0
- package/dist-lib/esm/geometry/bevelModifier.d.ts +59 -0
- package/dist-lib/esm/geometry/bevelModifier.js +345 -0
- package/dist-lib/esm/geometry/facetSurfaceUtils.d.ts +127 -0
- package/dist-lib/esm/geometry/facetSurfaceUtils.js +676 -0
- package/dist-lib/esm/geometry/roundedBox.d.ts +15 -0
- package/dist-lib/esm/geometry/roundedBox.js +133 -0
- package/dist-lib/esm/index.d.ts +21 -0
- package/dist-lib/esm/index.js +50 -0
- package/dist-lib/esm/pathExtrusion/PathExtrusionController.d.ts +59 -0
- package/dist-lib/esm/pathExtrusion/PathExtrusionController.js +483 -0
- package/dist-lib/esm/pathExtrusion/PathExtrusionStudio.d.ts +76 -0
- package/dist-lib/esm/pathExtrusion/PathExtrusionStudio.js +1792 -0
- package/dist-lib/esm/pathExtrusion/PathFactory.d.ts +95 -0
- package/dist-lib/esm/pathExtrusion/PathFactory.js +627 -0
- package/dist-lib/esm/pathExtrusion/index.d.ts +4 -0
- package/dist-lib/esm/pathExtrusion/index.js +4 -0
- package/dist-lib/esm/pathExtrusion/types.d.ts +50 -0
- package/dist-lib/esm/pathExtrusion/types.js +1 -0
- package/dist-lib/esm/plugin.d.ts +250 -0
- package/dist-lib/esm/plugin.js +144 -0
- package/dist-lib/esm/rigging/WeightPaintController.d.ts +162 -0
- package/dist-lib/esm/rigging/WeightPaintController.js +1421 -0
- package/dist-lib/esm/sculpting/SculptController.d.ts +146 -0
- package/dist-lib/esm/sculpting/SculptController.js +2038 -0
- package/dist-lib/esm/sdf/index.d.ts +2 -0
- package/dist-lib/esm/sdf/index.js +2 -0
- package/dist-lib/esm/sdf/sdfPrimitives.d.ts +97 -0
- package/dist-lib/esm/sdf/sdfPrimitives.js +402 -0
- package/dist-lib/esm/sdf/surfaceNets.d.ts +29 -0
- package/dist-lib/esm/sdf/surfaceNets.js +399 -0
- package/dist-lib/esm/styles.d.ts +11 -0
- package/dist-lib/esm/styles.js +32 -0
- package/dist-lib/esm/template.d.ts +12 -0
- package/dist-lib/esm/template.js +19 -0
- package/dist-lib/esm/textMesh/TextMeshController.d.ts +61 -0
- package/dist-lib/esm/textMesh/TextMeshController.js +405 -0
- package/dist-lib/esm/textures/AITextureGenerator.d.ts +119 -0
- package/dist-lib/esm/textures/AITextureGenerator.js +945 -0
- package/dist-lib/esm/textures/ExternalImageGeneratorService.d.ts +62 -0
- package/dist-lib/esm/textures/ExternalImageGeneratorService.js +879 -0
- package/dist-lib/esm/tools/AbstractTool.d.ts +68 -0
- package/dist-lib/esm/tools/AbstractTool.js +128 -0
- package/dist-lib/esm/tools/ToolExecutionContext.d.ts +40 -0
- package/dist-lib/esm/tools/ToolExecutionContext.js +1 -0
- package/dist-lib/esm/tools/ToolInterface.d.ts +104 -0
- package/dist-lib/esm/tools/ToolInterface.js +1 -0
- package/dist-lib/esm/tools/ToolRegistry.d.ts +43 -0
- package/dist-lib/esm/tools/ToolRegistry.js +115 -0
- package/dist-lib/esm/tools/builtin/ApplyMaterialTool.d.ts +34 -0
- package/dist-lib/esm/tools/builtin/ApplyMaterialTool.js +48 -0
- package/dist-lib/esm/tools/builtin/AutoUVMappingTool.d.ts +34 -0
- package/dist-lib/esm/tools/builtin/AutoUVMappingTool.js +54 -0
- package/dist-lib/esm/tools/builtin/BakeTexturesTool.d.ts +42 -0
- package/dist-lib/esm/tools/builtin/BakeTexturesTool.js +81 -0
- package/dist-lib/esm/tools/builtin/BooleanOperationTool.d.ts +51 -0
- package/dist-lib/esm/tools/builtin/BooleanOperationTool.js +86 -0
- package/dist-lib/esm/tools/builtin/BuildVoxelMeshTool.d.ts +78 -0
- package/dist-lib/esm/tools/builtin/BuildVoxelMeshTool.js +170 -0
- package/dist-lib/esm/tools/builtin/CloneArrayTool.d.ts +75 -0
- package/dist-lib/esm/tools/builtin/CloneArrayTool.js +115 -0
- package/dist-lib/esm/tools/builtin/CreateArchTool.d.ts +118 -0
- package/dist-lib/esm/tools/builtin/CreateArchTool.js +89 -0
- package/dist-lib/esm/tools/builtin/CreateColumnTool.d.ts +114 -0
- package/dist-lib/esm/tools/builtin/CreateColumnTool.js +105 -0
- package/dist-lib/esm/tools/builtin/CreateLatheTool.d.ts +87 -0
- package/dist-lib/esm/tools/builtin/CreateLatheTool.js +60 -0
- package/dist-lib/esm/tools/builtin/CreateMaterialTool.d.ts +114 -0
- package/dist-lib/esm/tools/builtin/CreateMaterialTool.js +212 -0
- package/dist-lib/esm/tools/builtin/CreateMoldingTool.d.ts +120 -0
- package/dist-lib/esm/tools/builtin/CreateMoldingTool.js +95 -0
- package/dist-lib/esm/tools/builtin/CreatePrimitiveTool.d.ts +162 -0
- package/dist-lib/esm/tools/builtin/CreatePrimitiveTool.js +302 -0
- package/dist-lib/esm/tools/builtin/CreateRoofTool.d.ts +120 -0
- package/dist-lib/esm/tools/builtin/CreateRoofTool.js +108 -0
- package/dist-lib/esm/tools/builtin/CreateRoundedBoxTool.d.ts +101 -0
- package/dist-lib/esm/tools/builtin/CreateRoundedBoxTool.js +79 -0
- package/dist-lib/esm/tools/builtin/CreateSdfMeshTool.d.ts +224 -0
- package/dist-lib/esm/tools/builtin/CreateSdfMeshTool.js +238 -0
- package/dist-lib/esm/tools/builtin/CreateStairsTool.d.ts +135 -0
- package/dist-lib/esm/tools/builtin/CreateStairsTool.js +119 -0
- package/dist-lib/esm/tools/builtin/CreateTextMeshTool.d.ts +94 -0
- package/dist-lib/esm/tools/builtin/CreateTextMeshTool.js +131 -0
- package/dist-lib/esm/tools/builtin/CreateTubeTool.d.ts +87 -0
- package/dist-lib/esm/tools/builtin/CreateTubeTool.js +59 -0
- package/dist-lib/esm/tools/builtin/CreateWallTool.d.ts +145 -0
- package/dist-lib/esm/tools/builtin/CreateWallTool.js +124 -0
- package/dist-lib/esm/tools/builtin/DecalTool.d.ts +76 -0
- package/dist-lib/esm/tools/builtin/DecalTool.js +143 -0
- package/dist-lib/esm/tools/builtin/DeleteMeshTool.d.ts +20 -0
- package/dist-lib/esm/tools/builtin/DeleteMeshTool.js +42 -0
- package/dist-lib/esm/tools/builtin/ExecuteBabylonCodeTool.d.ts +21 -0
- package/dist-lib/esm/tools/builtin/ExecuteBabylonCodeTool.js +96 -0
- package/dist-lib/esm/tools/builtin/ExtrudePolygonTool.d.ts +114 -0
- package/dist-lib/esm/tools/builtin/ExtrudePolygonTool.js +77 -0
- package/dist-lib/esm/tools/builtin/ExtrudeShapeTool.d.ts +104 -0
- package/dist-lib/esm/tools/builtin/ExtrudeShapeTool.js +68 -0
- package/dist-lib/esm/tools/builtin/FacetArcTool.d.ts +36 -0
- package/dist-lib/esm/tools/builtin/FacetArcTool.js +57 -0
- package/dist-lib/esm/tools/builtin/FacetCutThroughTool.d.ts +30 -0
- package/dist-lib/esm/tools/builtin/FacetCutThroughTool.js +49 -0
- package/dist-lib/esm/tools/builtin/FacetExtrudeTool.d.ts +30 -0
- package/dist-lib/esm/tools/builtin/FacetExtrudeTool.js +49 -0
- package/dist-lib/esm/tools/builtin/FacetInsetTool.d.ts +34 -0
- package/dist-lib/esm/tools/builtin/FacetInsetTool.js +55 -0
- package/dist-lib/esm/tools/builtin/GenerateNeuralMeshTool.d.ts +131 -0
- package/dist-lib/esm/tools/builtin/GenerateNeuralMeshTool.js +455 -0
- package/dist-lib/esm/tools/builtin/GenerateTextureTool.d.ts +87 -0
- package/dist-lib/esm/tools/builtin/GenerateTextureTool.js +216 -0
- package/dist-lib/esm/tools/builtin/GetSceneInfoTool.d.ts +11 -0
- package/dist-lib/esm/tools/builtin/GetSceneInfoTool.js +12 -0
- package/dist-lib/esm/tools/builtin/MergeMeshesTool.d.ts +28 -0
- package/dist-lib/esm/tools/builtin/MergeMeshesTool.js +56 -0
- package/dist-lib/esm/tools/builtin/PlanModelTool.d.ts +87 -0
- package/dist-lib/esm/tools/builtin/PlanModelTool.js +155 -0
- package/dist-lib/esm/tools/builtin/RetopologizeMeshTool.d.ts +50 -0
- package/dist-lib/esm/tools/builtin/RetopologizeMeshTool.js +116 -0
- package/dist-lib/esm/tools/builtin/RigWeightTool.d.ts +55 -0
- package/dist-lib/esm/tools/builtin/RigWeightTool.js +139 -0
- package/dist-lib/esm/tools/builtin/SculptMeshTool.d.ts +138 -0
- package/dist-lib/esm/tools/builtin/SculptMeshTool.js +255 -0
- package/dist-lib/esm/tools/builtin/SeparateMeshTool.d.ts +21 -0
- package/dist-lib/esm/tools/builtin/SeparateMeshTool.js +31 -0
- package/dist-lib/esm/tools/builtin/SolidifyMeshTool.d.ts +30 -0
- package/dist-lib/esm/tools/builtin/SolidifyMeshTool.js +57 -0
- package/dist-lib/esm/tools/builtin/SpawnBaseMeshTool.d.ts +66 -0
- package/dist-lib/esm/tools/builtin/SpawnBaseMeshTool.js +105 -0
- package/dist-lib/esm/tools/builtin/SubDSmoothTool.d.ts +23 -0
- package/dist-lib/esm/tools/builtin/SubDSmoothTool.js +37 -0
- package/dist-lib/esm/tools/builtin/SubdivideMeshTool.d.ts +23 -0
- package/dist-lib/esm/tools/builtin/SubdivideMeshTool.js +39 -0
- package/dist-lib/esm/tools/builtin/TransformMeshTool.d.ts +78 -0
- package/dist-lib/esm/tools/builtin/TransformMeshTool.js +69 -0
- package/dist-lib/esm/tools/builtin/UVUnwrapTool.d.ts +48 -0
- package/dist-lib/esm/tools/builtin/UVUnwrapTool.js +107 -0
- package/dist-lib/esm/tools/builtin/VoxelPainterTool.d.ts +60 -0
- package/dist-lib/esm/tools/builtin/VoxelPainterTool.js +153 -0
- package/dist-lib/esm/tools/builtin/index.d.ts +44 -0
- package/dist-lib/esm/tools/builtin/index.js +44 -0
- package/dist-lib/esm/tools/editor/AIAgentEditorTool.d.ts +18 -0
- package/dist-lib/esm/tools/editor/AIAgentEditorTool.js +43 -0
- package/dist-lib/esm/tools/editor/AITextureEditorTool.d.ts +15 -0
- package/dist-lib/esm/tools/editor/AITextureEditorTool.js +28 -0
- package/dist-lib/esm/tools/editor/AbstractEditorTool.d.ts +29 -0
- package/dist-lib/esm/tools/editor/AbstractEditorTool.js +56 -0
- package/dist-lib/esm/tools/editor/AnimationPlayerEditorTool.d.ts +17 -0
- package/dist-lib/esm/tools/editor/AnimationPlayerEditorTool.js +29 -0
- package/dist-lib/esm/tools/editor/BakingEditorTool.d.ts +21 -0
- package/dist-lib/esm/tools/editor/BakingEditorTool.js +44 -0
- package/dist-lib/esm/tools/editor/CSGEditorTool.d.ts +20 -0
- package/dist-lib/esm/tools/editor/CSGEditorTool.js +42 -0
- package/dist-lib/esm/tools/editor/CompressModelEditorTool.d.ts +20 -0
- package/dist-lib/esm/tools/editor/CompressModelEditorTool.js +48 -0
- package/dist-lib/esm/tools/editor/CreatePrimitiveEditorTool.d.ts +17 -0
- package/dist-lib/esm/tools/editor/CreatePrimitiveEditorTool.js +36 -0
- package/dist-lib/esm/tools/editor/DecalEditorTool.d.ts +17 -0
- package/dist-lib/esm/tools/editor/DecalEditorTool.js +36 -0
- package/dist-lib/esm/tools/editor/FacetEditorTool.d.ts +23 -0
- package/dist-lib/esm/tools/editor/FacetEditorTool.js +80 -0
- package/dist-lib/esm/tools/editor/MaterialEditorTool.d.ts +18 -0
- package/dist-lib/esm/tools/editor/MaterialEditorTool.js +43 -0
- package/dist-lib/esm/tools/editor/MeshPropertiesEditorTool.d.ts +17 -0
- package/dist-lib/esm/tools/editor/MeshPropertiesEditorTool.js +37 -0
- package/dist-lib/esm/tools/editor/OutlinerEditorTool.d.ts +17 -0
- package/dist-lib/esm/tools/editor/OutlinerEditorTool.js +34 -0
- package/dist-lib/esm/tools/editor/PathExtrusionEditorTool.d.ts +26 -0
- package/dist-lib/esm/tools/editor/PathExtrusionEditorTool.js +72 -0
- package/dist-lib/esm/tools/editor/RiggingEditorTool.d.ts +27 -0
- package/dist-lib/esm/tools/editor/RiggingEditorTool.js +66 -0
- package/dist-lib/esm/tools/editor/SceneEnvironmentEditorTool.d.ts +17 -0
- package/dist-lib/esm/tools/editor/SceneEnvironmentEditorTool.js +35 -0
- package/dist-lib/esm/tools/editor/SculptEditorTool.d.ts +28 -0
- package/dist-lib/esm/tools/editor/SculptEditorTool.js +70 -0
- package/dist-lib/esm/tools/editor/TextMeshEditorTool.d.ts +16 -0
- package/dist-lib/esm/tools/editor/TextMeshEditorTool.js +30 -0
- package/dist-lib/esm/tools/editor/TextureOptimizerEditorTool.d.ts +17 -0
- package/dist-lib/esm/tools/editor/TextureOptimizerEditorTool.js +28 -0
- package/dist-lib/esm/tools/editor/UVUnwrapEditorTool.d.ts +27 -0
- package/dist-lib/esm/tools/editor/UVUnwrapEditorTool.js +70 -0
- package/dist-lib/esm/tools/editor/VoxelPainterEditorTool.d.ts +26 -0
- package/dist-lib/esm/tools/editor/VoxelPainterEditorTool.js +68 -0
- package/dist-lib/esm/tools/editor/aiQuickPrompts.d.ts +30 -0
- package/dist-lib/esm/tools/editor/aiQuickPrompts.js +228 -0
- package/dist-lib/esm/tools/editor/index.d.ts +32 -0
- package/dist-lib/esm/tools/editor/index.js +54 -0
- package/dist-lib/esm/tools/editor/toolViews.d.ts +10 -0
- package/dist-lib/esm/tools/editor/toolViews.js +53 -0
- package/dist-lib/esm/tools/editorTools.d.ts +5 -0
- package/dist-lib/esm/tools/editorTools.js +5 -0
- package/dist-lib/esm/tools/index.d.ts +8 -0
- package/dist-lib/esm/tools/index.js +8 -0
- package/dist-lib/esm/tools/textureUtils.d.ts +18 -0
- package/dist-lib/esm/tools/textureUtils.js +288 -0
- package/dist-lib/esm/types.d.ts +164 -0
- package/dist-lib/esm/types.js +1 -0
- package/dist-lib/esm/uv/UVUnwrapController.d.ts +108 -0
- package/dist-lib/esm/uv/UVUnwrapController.js +1022 -0
- package/dist-lib/esm/voxel/CustomVoxelMesher.d.ts +15 -0
- package/dist-lib/esm/voxel/CustomVoxelMesher.js +169 -0
- package/dist-lib/esm/voxel/TextureAtlasManager.d.ts +54 -0
- package/dist-lib/esm/voxel/TextureAtlasManager.js +630 -0
- package/dist-lib/esm/voxel/VoxelPainterController.d.ts +100 -0
- package/dist-lib/esm/voxel/VoxelPainterController.js +671 -0
- package/dist-lib/esm/voxel/index.d.ts +4 -0
- package/dist-lib/esm/voxel/index.js +4 -0
- package/dist-lib/esm/voxel/types.d.ts +78 -0
- package/dist-lib/esm/voxel/types.js +1 -0
- package/dist-lib/esm/web-component/MeshEditorElement.d.ts +103 -0
- package/dist-lib/esm/web-component/MeshEditorElement.js +246 -0
- package/dist-lib/esm/web-component/index.d.ts +1 -0
- package/dist-lib/esm/web-component/index.js +1 -0
- package/dist-lib/index.umd.js +3 -0
- package/dist-lib/mesheditor.css +2 -0
- package/dist-lib/mesheditor.css.map +1 -0
- package/dist-lib/standalone/mesheditor-element.bundle.js +3 -0
- package/package.json +114 -4
- package/skills/README.md +48 -0
- package/skills/figure-sculptor.md +297 -0
- package/skills/image-to-mesh.md +18 -0
- package/skills/mesh-updater.md +27 -0
- package/skills/neural-3d-generator.md +53 -0
- package/skills/procedural-modeler.md +267 -0
- package/skills/voxel-painter.md +21 -0
- package/src/mesheditor.css +6477 -0
|
@@ -0,0 +1,676 @@
|
|
|
1
|
+
import { MultiMaterial, PBRMaterial, SubMesh, Vector3, VertexBuffer, } from '@babylonjs/core';
|
|
2
|
+
const KNOWN_VERTEX_KINDS = [
|
|
3
|
+
VertexBuffer.PositionKind,
|
|
4
|
+
VertexBuffer.NormalKind,
|
|
5
|
+
VertexBuffer.UVKind,
|
|
6
|
+
VertexBuffer.UV2Kind,
|
|
7
|
+
VertexBuffer.ColorKind,
|
|
8
|
+
VertexBuffer.TangentKind,
|
|
9
|
+
VertexBuffer.MatricesIndicesKind,
|
|
10
|
+
VertexBuffer.MatricesWeightsKind,
|
|
11
|
+
];
|
|
12
|
+
function getPresentVertexKinds(mesh) {
|
|
13
|
+
const kinds = new Set(mesh.getVerticesDataKinds?.() || []);
|
|
14
|
+
for (const k of KNOWN_VERTEX_KINDS) {
|
|
15
|
+
if (mesh.isVerticesDataPresent(k)) {
|
|
16
|
+
kinds.add(k);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return Array.from(kinds);
|
|
20
|
+
}
|
|
21
|
+
function getKindStride(mesh, kind) {
|
|
22
|
+
const vb = mesh.getVertexBuffer(kind);
|
|
23
|
+
if (vb) {
|
|
24
|
+
const stride = vb.getStrideSize();
|
|
25
|
+
if (stride > 0)
|
|
26
|
+
return stride;
|
|
27
|
+
}
|
|
28
|
+
switch (kind) {
|
|
29
|
+
case VertexBuffer.UVKind:
|
|
30
|
+
case VertexBuffer.UV2Kind:
|
|
31
|
+
return 2;
|
|
32
|
+
case VertexBuffer.ColorKind:
|
|
33
|
+
case VertexBuffer.TangentKind:
|
|
34
|
+
case VertexBuffer.MatricesIndicesKind:
|
|
35
|
+
case VertexBuffer.MatricesWeightsKind:
|
|
36
|
+
return 4;
|
|
37
|
+
default:
|
|
38
|
+
return 3;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
function snapshotSubMeshes(mesh) {
|
|
42
|
+
if (!mesh.subMeshes || mesh.subMeshes.length === 0)
|
|
43
|
+
return [];
|
|
44
|
+
return mesh.subMeshes.map((sm) => ({
|
|
45
|
+
materialIndex: sm.materialIndex,
|
|
46
|
+
verticesStart: sm.verticesStart,
|
|
47
|
+
verticesCount: sm.verticesCount,
|
|
48
|
+
indexStart: sm.indexStart,
|
|
49
|
+
indexCount: sm.indexCount,
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
function restoreSubMeshes(mesh, descriptors, totalVerts) {
|
|
53
|
+
mesh.subMeshes = [];
|
|
54
|
+
const vertCount = totalVerts ?? mesh.getTotalVertices();
|
|
55
|
+
for (const d of descriptors) {
|
|
56
|
+
new SubMesh(d.materialIndex, d.verticesStart, totalVerts !== undefined ? vertCount : d.verticesCount, d.indexStart, d.indexCount, mesh);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Captures a complete geometry, UV, material, and SubMesh snapshot for Undo/Redo.
|
|
61
|
+
*/
|
|
62
|
+
export function captureMeshSurfaceSnapshot(mesh) {
|
|
63
|
+
const buffers = {};
|
|
64
|
+
for (const kind of getPresentVertexKinds(mesh)) {
|
|
65
|
+
const data = mesh.getVerticesData(kind);
|
|
66
|
+
if (data) {
|
|
67
|
+
buffers[kind] = Array.from(data);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
const indices = Array.from(mesh.getIndices() || []);
|
|
71
|
+
const material = mesh.material;
|
|
72
|
+
const multiSubMaterials = material instanceof MultiMaterial ? [...material.subMaterials] : undefined;
|
|
73
|
+
return {
|
|
74
|
+
buffers,
|
|
75
|
+
indices,
|
|
76
|
+
material,
|
|
77
|
+
multiSubMaterials,
|
|
78
|
+
subMeshes: snapshotSubMeshes(mesh),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Restores a previously captured geometry, UV, material, and SubMesh snapshot.
|
|
83
|
+
*/
|
|
84
|
+
export function restoreMeshSurfaceSnapshot(mesh, snap) {
|
|
85
|
+
const existingKinds = getPresentVertexKinds(mesh);
|
|
86
|
+
for (const kind of existingKinds) {
|
|
87
|
+
if (!snap.buffers[kind]) {
|
|
88
|
+
mesh.removeVerticesData(kind);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (const [kind, data] of Object.entries(snap.buffers)) {
|
|
92
|
+
mesh.setVerticesData(kind, [...data], true);
|
|
93
|
+
}
|
|
94
|
+
mesh.setIndices([...snap.indices]);
|
|
95
|
+
mesh.material = snap.material;
|
|
96
|
+
if (snap.material instanceof MultiMaterial && snap.multiSubMaterials) {
|
|
97
|
+
snap.material.subMaterials = [...snap.multiSubMaterials];
|
|
98
|
+
}
|
|
99
|
+
if (snap.subMeshes.length > 0) {
|
|
100
|
+
restoreSubMeshes(mesh, snap.subMeshes);
|
|
101
|
+
}
|
|
102
|
+
mesh.refreshBoundingInfo();
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Returns the SubMesh materialIndex per triangle for the given mesh.
|
|
106
|
+
*/
|
|
107
|
+
export function getFaceTriangleMaterialIndices(mesh) {
|
|
108
|
+
const indices = mesh.getIndices();
|
|
109
|
+
if (!indices || indices.length < 3)
|
|
110
|
+
return [];
|
|
111
|
+
const triCount = Math.floor(indices.length / 3);
|
|
112
|
+
const result = new Array(triCount).fill(0);
|
|
113
|
+
if (!mesh.subMeshes || mesh.subMeshes.length === 0)
|
|
114
|
+
return result;
|
|
115
|
+
for (const sm of mesh.subMeshes) {
|
|
116
|
+
const startTri = Math.max(0, Math.floor(sm.indexStart / 3));
|
|
117
|
+
const endTri = Math.min(triCount, Math.ceil((sm.indexStart + sm.indexCount) / 3));
|
|
118
|
+
for (let t = startTri; t < endTri; t++) {
|
|
119
|
+
result[t] = sm.materialIndex;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Resolves the SubMesh slot and active Material assigned to a specific triangle faceId.
|
|
126
|
+
*/
|
|
127
|
+
export function getFacetMaterialInfo(mesh, faceId) {
|
|
128
|
+
const subMeshes = mesh.subMeshes || [];
|
|
129
|
+
const indexOffset = faceId * 3;
|
|
130
|
+
let subMeshIndex = 0;
|
|
131
|
+
let materialIndex = 0;
|
|
132
|
+
let matchedSubMesh = null;
|
|
133
|
+
for (let i = 0; i < subMeshes.length; i++) {
|
|
134
|
+
const sm = subMeshes[i];
|
|
135
|
+
if (indexOffset >= sm.indexStart && indexOffset < sm.indexStart + sm.indexCount) {
|
|
136
|
+
subMeshIndex = i;
|
|
137
|
+
materialIndex = sm.materialIndex;
|
|
138
|
+
matchedSubMesh = sm;
|
|
139
|
+
break;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let material = null;
|
|
143
|
+
let totalSlots = 1;
|
|
144
|
+
if (mesh.material instanceof MultiMaterial) {
|
|
145
|
+
totalSlots = Math.max(1, mesh.material.subMaterials.length);
|
|
146
|
+
material =
|
|
147
|
+
mesh.material.subMaterials[materialIndex] ??
|
|
148
|
+
matchedSubMesh?.getMaterial() ??
|
|
149
|
+
null;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
material = mesh.material;
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
materialIndex,
|
|
156
|
+
material,
|
|
157
|
+
materialName: material?.name || 'Default Material',
|
|
158
|
+
totalSlots,
|
|
159
|
+
subMeshIndex,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Resolves the active material list indexed by slot on the mesh.
|
|
164
|
+
*/
|
|
165
|
+
export function getMeshSlotMaterials(mesh) {
|
|
166
|
+
if (mesh.material instanceof MultiMaterial) {
|
|
167
|
+
return [...mesh.material.subMaterials];
|
|
168
|
+
}
|
|
169
|
+
return [mesh.material];
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Rebuilds mesh indices and SubMeshes grouped by material slot, compacting unused slots
|
|
173
|
+
* and collapsing back to a single Material when only one material remains.
|
|
174
|
+
*/
|
|
175
|
+
export function rebuildMeshSubMeshes(mesh, triMaterials, slotMaterials, preserveSingleOrder = true) {
|
|
176
|
+
const indices = Array.from(mesh.getIndices() || []);
|
|
177
|
+
const triCount = Math.floor(indices.length / 3);
|
|
178
|
+
const faceIdMap = new Int32Array(triCount);
|
|
179
|
+
if (triCount === 0) {
|
|
180
|
+
return { faceIdMap, collapsedToSingle: true };
|
|
181
|
+
}
|
|
182
|
+
const scene = mesh.getScene();
|
|
183
|
+
// Group triangles by resolved Material instance (or null fallback key) in first-seen slot order
|
|
184
|
+
const groups = [];
|
|
185
|
+
const keyToGroupIdx = new Map();
|
|
186
|
+
const matIds = new Map();
|
|
187
|
+
let nextMatUid = 0;
|
|
188
|
+
for (let t = 0; t < triCount; t++) {
|
|
189
|
+
const slotIdx = triMaterials[t] ?? 0;
|
|
190
|
+
const mat = slotMaterials[slotIdx] ?? slotMaterials[0] ?? null;
|
|
191
|
+
let key = '__null__';
|
|
192
|
+
if (mat) {
|
|
193
|
+
let mid = matIds.get(mat);
|
|
194
|
+
if (!mid) {
|
|
195
|
+
mid = `m_${nextMatUid++}`;
|
|
196
|
+
matIds.set(mat, mid);
|
|
197
|
+
}
|
|
198
|
+
key = mid;
|
|
199
|
+
}
|
|
200
|
+
let grpIdx = keyToGroupIdx.get(key);
|
|
201
|
+
if (grpIdx === undefined) {
|
|
202
|
+
grpIdx = groups.length;
|
|
203
|
+
keyToGroupIdx.set(key, grpIdx);
|
|
204
|
+
groups.push({ key, material: mat, triangles: [] });
|
|
205
|
+
}
|
|
206
|
+
groups[grpIdx].triangles.push(t);
|
|
207
|
+
}
|
|
208
|
+
if (groups.length <= 1) {
|
|
209
|
+
const singleMat = groups[0]?.material ?? null;
|
|
210
|
+
if (!preserveSingleOrder) {
|
|
211
|
+
mesh.setIndices(indices);
|
|
212
|
+
}
|
|
213
|
+
for (let t = 0; t < triCount; t++)
|
|
214
|
+
faceIdMap[t] = t;
|
|
215
|
+
mesh.material = singleMat;
|
|
216
|
+
mesh.subMeshes = [];
|
|
217
|
+
new SubMesh(0, 0, mesh.getTotalVertices(), 0, indices.length, mesh);
|
|
218
|
+
return { faceIdMap, collapsedToSingle: true };
|
|
219
|
+
}
|
|
220
|
+
const newIndices = [];
|
|
221
|
+
const subMeshRanges = [];
|
|
222
|
+
const compactedMaterials = [];
|
|
223
|
+
let nextTri = 0;
|
|
224
|
+
for (let gIdx = 0; gIdx < groups.length; gIdx++) {
|
|
225
|
+
const grp = groups[gIdx];
|
|
226
|
+
compactedMaterials.push(grp.material);
|
|
227
|
+
const rangeStart = newIndices.length;
|
|
228
|
+
for (const oldTri of grp.triangles) {
|
|
229
|
+
faceIdMap[oldTri] = nextTri++;
|
|
230
|
+
newIndices.push(indices[oldTri * 3], indices[oldTri * 3 + 1], indices[oldTri * 3 + 2]);
|
|
231
|
+
}
|
|
232
|
+
const rangeCount = newIndices.length - rangeStart;
|
|
233
|
+
subMeshRanges.push({
|
|
234
|
+
materialIndex: gIdx,
|
|
235
|
+
indexStart: rangeStart,
|
|
236
|
+
indexCount: rangeCount,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
const multiMat = new MultiMaterial(`${mesh.name}_MultiMat`, scene);
|
|
240
|
+
multiMat.subMaterials = compactedMaterials;
|
|
241
|
+
mesh.setIndices(newIndices);
|
|
242
|
+
mesh.material = multiMat;
|
|
243
|
+
mesh.subMeshes = [];
|
|
244
|
+
const totalVerts = mesh.getTotalVertices();
|
|
245
|
+
for (const r of subMeshRanges) {
|
|
246
|
+
new SubMesh(r.materialIndex, 0, totalVerts, r.indexStart, r.indexCount, mesh);
|
|
247
|
+
}
|
|
248
|
+
return { faceIdMap, collapsedToSingle: false };
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Ensures any vertices shared between targetFaceIds and other faces are duplicated
|
|
252
|
+
* so facet-level UV modifications never distort neighboring faces.
|
|
253
|
+
* Also initializes UVKind if missing on the mesh.
|
|
254
|
+
*/
|
|
255
|
+
export function isolateFacetVertices(mesh, targetFaceIds) {
|
|
256
|
+
const indices = Array.from(mesh.getIndices() || []);
|
|
257
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
258
|
+
if (!positions || indices.length < 3 || targetFaceIds.length === 0)
|
|
259
|
+
return;
|
|
260
|
+
const triCount = Math.floor(indices.length / 3);
|
|
261
|
+
const targetFaceSet = new Set(targetFaceIds);
|
|
262
|
+
const targetVertSet = new Set();
|
|
263
|
+
const outsideVertSet = new Set();
|
|
264
|
+
for (let t = 0; t < triCount; t++) {
|
|
265
|
+
const i0 = indices[t * 3];
|
|
266
|
+
const i1 = indices[t * 3 + 1];
|
|
267
|
+
const i2 = indices[t * 3 + 2];
|
|
268
|
+
if (targetFaceSet.has(t)) {
|
|
269
|
+
targetVertSet.add(i0);
|
|
270
|
+
targetVertSet.add(i1);
|
|
271
|
+
targetVertSet.add(i2);
|
|
272
|
+
}
|
|
273
|
+
else {
|
|
274
|
+
outsideVertSet.add(i0);
|
|
275
|
+
outsideVertSet.add(i1);
|
|
276
|
+
outsideVertSet.add(i2);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
let vertCount = positions.length / 3;
|
|
280
|
+
const presentKinds = getPresentVertexKinds(mesh);
|
|
281
|
+
const buffers = new Map();
|
|
282
|
+
for (const kind of presentKinds) {
|
|
283
|
+
const raw = mesh.getVerticesData(kind);
|
|
284
|
+
if (raw) {
|
|
285
|
+
buffers.set(kind, {
|
|
286
|
+
stride: getKindStride(mesh, kind),
|
|
287
|
+
data: Array.from(raw),
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
// Ensure UVKind exists
|
|
292
|
+
if (!buffers.has(VertexBuffer.UVKind)) {
|
|
293
|
+
buffers.set(VertexBuffer.UVKind, {
|
|
294
|
+
stride: 2,
|
|
295
|
+
data: new Array(vertCount * 2).fill(0),
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
const remap = new Map();
|
|
299
|
+
for (const v of targetVertSet) {
|
|
300
|
+
if (outsideVertSet.has(v)) {
|
|
301
|
+
const newV = vertCount++;
|
|
302
|
+
for (const [, buf] of buffers) {
|
|
303
|
+
const s = buf.stride;
|
|
304
|
+
for (let k = 0; k < s; k++) {
|
|
305
|
+
buf.data.push(buf.data[v * s + k] ?? 0);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
remap.set(v, newV);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
if (remap.size === 0) {
|
|
312
|
+
// Still ensure UVKind buffer was attached if previously missing
|
|
313
|
+
if (!mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
|
|
314
|
+
const uvBuf = buffers.get(VertexBuffer.UVKind);
|
|
315
|
+
mesh.setVerticesData(VertexBuffer.UVKind, uvBuf.data, true);
|
|
316
|
+
}
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
for (const f of targetFaceIds) {
|
|
320
|
+
if (f * 3 + 2 >= indices.length)
|
|
321
|
+
continue;
|
|
322
|
+
for (let k = 0; k < 3; k++) {
|
|
323
|
+
const oldV = indices[f * 3 + k];
|
|
324
|
+
const mapped = remap.get(oldV);
|
|
325
|
+
if (mapped !== undefined) {
|
|
326
|
+
indices[f * 3 + k] = mapped;
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
const prevSubMeshes = snapshotSubMeshes(mesh);
|
|
331
|
+
for (const [kind, buf] of buffers) {
|
|
332
|
+
mesh.setVerticesData(kind, buf.data, true);
|
|
333
|
+
}
|
|
334
|
+
mesh.setIndices(indices);
|
|
335
|
+
if (prevSubMeshes.length > 0) {
|
|
336
|
+
restoreSubMeshes(mesh, prevSubMeshes, vertCount);
|
|
337
|
+
}
|
|
338
|
+
mesh.refreshBoundingInfo();
|
|
339
|
+
}
|
|
340
|
+
/**
|
|
341
|
+
* Computes an orthonormal tangent/bitangent basis on a facet plane from its outward normal.
|
|
342
|
+
*/
|
|
343
|
+
export function computeFacetLocalBasis(normal) {
|
|
344
|
+
const n = normal.lengthSquared() > 1e-8 ? normal.clone().normalize() : new Vector3(0, 1, 0);
|
|
345
|
+
const refUp = Math.abs(n.y) < 0.99 ? new Vector3(0, 1, 0) : new Vector3(0, 0, 1);
|
|
346
|
+
let tangentU = Vector3.Cross(n, refUp);
|
|
347
|
+
if (tangentU.lengthSquared() < 1e-8) {
|
|
348
|
+
tangentU = Vector3.Cross(n, new Vector3(1, 0, 0));
|
|
349
|
+
}
|
|
350
|
+
tangentU.normalize();
|
|
351
|
+
const bitangentV = Vector3.Cross(tangentU, n).normalize();
|
|
352
|
+
return { tangentU, bitangentV };
|
|
353
|
+
}
|
|
354
|
+
function collectFacetUniqueVertices(indices, faceIds) {
|
|
355
|
+
const set = new Set();
|
|
356
|
+
for (const f of faceIds) {
|
|
357
|
+
if (f * 3 + 2 >= indices.length)
|
|
358
|
+
continue;
|
|
359
|
+
set.add(indices[f * 3]);
|
|
360
|
+
set.add(indices[f * 3 + 1]);
|
|
361
|
+
set.add(indices[f * 3 + 2]);
|
|
362
|
+
}
|
|
363
|
+
return Array.from(set);
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Reads corner UVs and overall UV bounds for a target triangle or quad facet.
|
|
367
|
+
*/
|
|
368
|
+
export function getFacetUVInfo(mesh, faceIds, orderedCornerPositions, isQuad) {
|
|
369
|
+
const indices = mesh.getIndices();
|
|
370
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
371
|
+
if (!indices || !positions || faceIds.length === 0)
|
|
372
|
+
return null;
|
|
373
|
+
const uvs = mesh.getVerticesData(VertexBuffer.UVKind);
|
|
374
|
+
const hasUVs = !!(uvs && uvs.length >= (positions.length / 3) * 2);
|
|
375
|
+
const uniqueVerts = collectFacetUniqueVertices(indices, faceIds);
|
|
376
|
+
if (uniqueVerts.length === 0)
|
|
377
|
+
return null;
|
|
378
|
+
const corners = [];
|
|
379
|
+
let uMin = Infinity;
|
|
380
|
+
let uMax = -Infinity;
|
|
381
|
+
let vMin = Infinity;
|
|
382
|
+
let vMax = -Infinity;
|
|
383
|
+
for (const cornerPos of orderedCornerPositions) {
|
|
384
|
+
let bestV = uniqueVerts[0];
|
|
385
|
+
let bestDistSq = Infinity;
|
|
386
|
+
for (const v of uniqueVerts) {
|
|
387
|
+
const dx = positions[v * 3] - cornerPos.x;
|
|
388
|
+
const dy = positions[v * 3 + 1] - cornerPos.y;
|
|
389
|
+
const dz = positions[v * 3 + 2] - cornerPos.z;
|
|
390
|
+
const dSq = dx * dx + dy * dy + dz * dz;
|
|
391
|
+
if (dSq < bestDistSq) {
|
|
392
|
+
bestDistSq = dSq;
|
|
393
|
+
bestV = v;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
const u = hasUVs ? uvs[bestV * 2] ?? 0 : 0;
|
|
397
|
+
const vCoord = hasUVs ? uvs[bestV * 2 + 1] ?? 0 : 0;
|
|
398
|
+
corners.push({ vertexIndex: bestV, u, v: vCoord });
|
|
399
|
+
if (u < uMin)
|
|
400
|
+
uMin = u;
|
|
401
|
+
if (u > uMax)
|
|
402
|
+
uMax = u;
|
|
403
|
+
if (vCoord < vMin)
|
|
404
|
+
vMin = vCoord;
|
|
405
|
+
if (vCoord > vMax)
|
|
406
|
+
vMax = vCoord;
|
|
407
|
+
}
|
|
408
|
+
return {
|
|
409
|
+
faceIds: [...faceIds],
|
|
410
|
+
isQuad,
|
|
411
|
+
corners,
|
|
412
|
+
uMin: Number.isFinite(uMin) ? uMin : 0,
|
|
413
|
+
uMax: Number.isFinite(uMax) ? uMax : 1,
|
|
414
|
+
vMin: Number.isFinite(vMin) ? vMin : 0,
|
|
415
|
+
vMax: Number.isFinite(vMax) ? vMax : 1,
|
|
416
|
+
hasUVs,
|
|
417
|
+
};
|
|
418
|
+
}
|
|
419
|
+
/**
|
|
420
|
+
* Projects UV coordinates onto a facet using either normalized '[0..1]' Fit or world-scale Planar mapping.
|
|
421
|
+
*/
|
|
422
|
+
export function applyFacetUVProjection(mesh, faceIds, normal, mode, options = {}) {
|
|
423
|
+
isolateFacetVertices(mesh, faceIds);
|
|
424
|
+
const indices = mesh.getIndices();
|
|
425
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
426
|
+
if (!indices || !positions)
|
|
427
|
+
return;
|
|
428
|
+
const vertCount = positions.length / 3;
|
|
429
|
+
const rawUVs = mesh.getVerticesData(VertexBuffer.UVKind);
|
|
430
|
+
const uvs = rawUVs ? Array.from(rawUVs) : new Array(vertCount * 2).fill(0);
|
|
431
|
+
const targetVerts = collectFacetUniqueVertices(indices, faceIds);
|
|
432
|
+
if (targetVerts.length === 0)
|
|
433
|
+
return;
|
|
434
|
+
if (mode === 'planar') {
|
|
435
|
+
mesh.computeWorldMatrix(true);
|
|
436
|
+
const wm = mesh.getWorldMatrix();
|
|
437
|
+
const worldNormal = Vector3.TransformNormal(normal, wm);
|
|
438
|
+
const { tangentU, bitangentV } = computeFacetLocalBasis(worldNormal);
|
|
439
|
+
const scaleU = options.scaleU ?? 1;
|
|
440
|
+
const scaleV = options.scaleV ?? 1;
|
|
441
|
+
const offsetU = options.offsetU ?? 0;
|
|
442
|
+
const offsetV = options.offsetV ?? 0;
|
|
443
|
+
for (const v of targetVerts) {
|
|
444
|
+
const localPos = new Vector3(positions[v * 3], positions[v * 3 + 1], positions[v * 3 + 2]);
|
|
445
|
+
const worldPos = Vector3.TransformCoordinates(localPos, wm);
|
|
446
|
+
let u = Vector3.Dot(worldPos, tangentU) * scaleU + offsetU;
|
|
447
|
+
let vCoord = Vector3.Dot(worldPos, bitangentV) * scaleV + offsetV;
|
|
448
|
+
if (options.flipU)
|
|
449
|
+
u = -u;
|
|
450
|
+
if (options.flipV)
|
|
451
|
+
vCoord = -vCoord;
|
|
452
|
+
uvs[v * 2] = u;
|
|
453
|
+
uvs[v * 2 + 1] = vCoord;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
else {
|
|
457
|
+
const { tangentU, bitangentV } = computeFacetLocalBasis(normal);
|
|
458
|
+
const projected = new Map();
|
|
459
|
+
let minRawU = Infinity;
|
|
460
|
+
let maxRawU = -Infinity;
|
|
461
|
+
let minRawV = Infinity;
|
|
462
|
+
let maxRawV = -Infinity;
|
|
463
|
+
for (const v of targetVerts) {
|
|
464
|
+
const p = new Vector3(positions[v * 3], positions[v * 3 + 1], positions[v * 3 + 2]);
|
|
465
|
+
const ru = Vector3.Dot(p, tangentU);
|
|
466
|
+
const rv = Vector3.Dot(p, bitangentV);
|
|
467
|
+
projected.set(v, { u: ru, v: rv });
|
|
468
|
+
if (ru < minRawU)
|
|
469
|
+
minRawU = ru;
|
|
470
|
+
if (ru > maxRawU)
|
|
471
|
+
maxRawU = ru;
|
|
472
|
+
if (rv < minRawV)
|
|
473
|
+
minRawV = rv;
|
|
474
|
+
if (rv > maxRawV)
|
|
475
|
+
maxRawV = rv;
|
|
476
|
+
}
|
|
477
|
+
const spanU = Math.max(1e-6, maxRawU - minRawU);
|
|
478
|
+
const spanV = Math.max(1e-6, maxRawV - minRawV);
|
|
479
|
+
const uMin = options.uMin ?? 0;
|
|
480
|
+
const uMax = options.uMax ?? 1;
|
|
481
|
+
const vMin = options.vMin ?? 0;
|
|
482
|
+
const vMax = options.vMax ?? 1;
|
|
483
|
+
const rotRad = ((options.rotationDeg ?? 0) * Math.PI) / 180;
|
|
484
|
+
const cosR = Math.cos(rotRad);
|
|
485
|
+
const sinR = Math.sin(rotRad);
|
|
486
|
+
for (const v of targetVerts) {
|
|
487
|
+
const raw = projected.get(v);
|
|
488
|
+
let nu = (raw.u - minRawU) / spanU;
|
|
489
|
+
let nv = (raw.v - minRawV) / spanV;
|
|
490
|
+
if (rotRad !== 0) {
|
|
491
|
+
const du = nu - 0.5;
|
|
492
|
+
const dv = nv - 0.5;
|
|
493
|
+
nu = 0.5 + du * cosR - dv * sinR;
|
|
494
|
+
nv = 0.5 + du * sinR + dv * cosR;
|
|
495
|
+
}
|
|
496
|
+
if (options.flipU)
|
|
497
|
+
nu = 1 - nu;
|
|
498
|
+
if (options.flipV)
|
|
499
|
+
nv = 1 - nv;
|
|
500
|
+
uvs[v * 2] = uMin + nu * (uMax - uMin);
|
|
501
|
+
uvs[v * 2 + 1] = vMin + nv * (vMax - vMin);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
mesh.setVerticesData(VertexBuffer.UVKind, uvs, true);
|
|
505
|
+
}
|
|
506
|
+
/**
|
|
507
|
+
* Remaps the facet's existing UV bounding box (or planar fit if degenerate) into [targetUMin..targetUMax, targetVMin..targetVMax].
|
|
508
|
+
*/
|
|
509
|
+
export function remapFacetUVRect(mesh, faceIds, normal, targetUMin, targetUMax, targetVMin, targetVMax) {
|
|
510
|
+
isolateFacetVertices(mesh, faceIds);
|
|
511
|
+
const indices = mesh.getIndices();
|
|
512
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
513
|
+
if (!indices || !positions)
|
|
514
|
+
return;
|
|
515
|
+
const targetVerts = collectFacetUniqueVertices(indices, faceIds);
|
|
516
|
+
if (targetVerts.length === 0)
|
|
517
|
+
return;
|
|
518
|
+
const rawUVs = mesh.getVerticesData(VertexBuffer.UVKind);
|
|
519
|
+
if (!rawUVs) {
|
|
520
|
+
applyFacetUVProjection(mesh, faceIds, normal, 'fit', {
|
|
521
|
+
uMin: targetUMin,
|
|
522
|
+
uMax: targetUMax,
|
|
523
|
+
vMin: targetVMin,
|
|
524
|
+
vMax: targetVMax,
|
|
525
|
+
});
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
const uvs = Array.from(rawUVs);
|
|
529
|
+
let curMinU = Infinity;
|
|
530
|
+
let curMaxU = -Infinity;
|
|
531
|
+
let curMinV = Infinity;
|
|
532
|
+
let curMaxV = -Infinity;
|
|
533
|
+
for (const v of targetVerts) {
|
|
534
|
+
const u = uvs[v * 2] ?? 0;
|
|
535
|
+
const vc = uvs[v * 2 + 1] ?? 0;
|
|
536
|
+
if (u < curMinU)
|
|
537
|
+
curMinU = u;
|
|
538
|
+
if (u > curMaxU)
|
|
539
|
+
curMaxU = u;
|
|
540
|
+
if (vc < curMinV)
|
|
541
|
+
curMinV = vc;
|
|
542
|
+
if (vc > curMaxV)
|
|
543
|
+
curMaxV = vc;
|
|
544
|
+
}
|
|
545
|
+
const spanU = curMaxU - curMinU;
|
|
546
|
+
const spanV = curMaxV - curMinV;
|
|
547
|
+
if (spanU < 1e-5 || spanV < 1e-5) {
|
|
548
|
+
applyFacetUVProjection(mesh, faceIds, normal, 'fit', {
|
|
549
|
+
uMin: targetUMin,
|
|
550
|
+
uMax: targetUMax,
|
|
551
|
+
vMin: targetVMin,
|
|
552
|
+
vMax: targetVMax,
|
|
553
|
+
});
|
|
554
|
+
return;
|
|
555
|
+
}
|
|
556
|
+
for (const v of targetVerts) {
|
|
557
|
+
const nu = ((uvs[v * 2] ?? 0) - curMinU) / spanU;
|
|
558
|
+
const nv = ((uvs[v * 2 + 1] ?? 0) - curMinV) / spanV;
|
|
559
|
+
uvs[v * 2] = targetUMin + nu * (targetUMax - targetUMin);
|
|
560
|
+
uvs[v * 2 + 1] = targetVMin + nv * (targetVMax - targetVMin);
|
|
561
|
+
}
|
|
562
|
+
mesh.setVerticesData(VertexBuffer.UVKind, uvs, true);
|
|
563
|
+
}
|
|
564
|
+
/**
|
|
565
|
+
* Applies rotation, flip, scale, or offset transforms to a facet's isolated UVs around its UV center.
|
|
566
|
+
*/
|
|
567
|
+
export function transformFacetUVs(mesh, faceIds, options) {
|
|
568
|
+
isolateFacetVertices(mesh, faceIds);
|
|
569
|
+
const indices = mesh.getIndices();
|
|
570
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
571
|
+
if (!indices || !positions)
|
|
572
|
+
return;
|
|
573
|
+
const targetVerts = collectFacetUniqueVertices(indices, faceIds);
|
|
574
|
+
if (targetVerts.length === 0)
|
|
575
|
+
return;
|
|
576
|
+
const vertCount = positions.length / 3;
|
|
577
|
+
const rawUVs = mesh.getVerticesData(VertexBuffer.UVKind);
|
|
578
|
+
const uvs = rawUVs ? Array.from(rawUVs) : new Array(vertCount * 2).fill(0);
|
|
579
|
+
let minU = Infinity;
|
|
580
|
+
let maxU = -Infinity;
|
|
581
|
+
let minV = Infinity;
|
|
582
|
+
let maxV = -Infinity;
|
|
583
|
+
for (const v of targetVerts) {
|
|
584
|
+
const u = uvs[v * 2] ?? 0;
|
|
585
|
+
const vc = uvs[v * 2 + 1] ?? 0;
|
|
586
|
+
if (u < minU)
|
|
587
|
+
minU = u;
|
|
588
|
+
if (u > maxU)
|
|
589
|
+
maxU = u;
|
|
590
|
+
if (vc < minV)
|
|
591
|
+
minV = vc;
|
|
592
|
+
if (vc > maxV)
|
|
593
|
+
maxV = vc;
|
|
594
|
+
}
|
|
595
|
+
const cu = (minU + maxU) * 0.5;
|
|
596
|
+
const cv = (minV + maxV) * 0.5;
|
|
597
|
+
const rotRad = ((options.rotateDeg ?? 0) * Math.PI) / 180;
|
|
598
|
+
const cosR = Math.cos(rotRad);
|
|
599
|
+
const sinR = Math.sin(rotRad);
|
|
600
|
+
const scaleU = options.scaleU ?? 1;
|
|
601
|
+
const scaleV = options.scaleV ?? 1;
|
|
602
|
+
const offsetU = options.offsetU ?? 0;
|
|
603
|
+
const offsetV = options.offsetV ?? 0;
|
|
604
|
+
for (const v of targetVerts) {
|
|
605
|
+
let du = (uvs[v * 2] ?? 0) - cu;
|
|
606
|
+
let dv = (uvs[v * 2 + 1] ?? 0) - cv;
|
|
607
|
+
if (options.flipU)
|
|
608
|
+
du = -du;
|
|
609
|
+
if (options.flipV)
|
|
610
|
+
dv = -dv;
|
|
611
|
+
if (rotRad !== 0) {
|
|
612
|
+
const ru = du * cosR - dv * sinR;
|
|
613
|
+
const rv = du * sinR + dv * cosR;
|
|
614
|
+
du = ru;
|
|
615
|
+
dv = rv;
|
|
616
|
+
}
|
|
617
|
+
du *= scaleU;
|
|
618
|
+
dv *= scaleV;
|
|
619
|
+
uvs[v * 2] = cu + du + offsetU;
|
|
620
|
+
uvs[v * 2 + 1] = cv + dv + offsetV;
|
|
621
|
+
}
|
|
622
|
+
mesh.setVerticesData(VertexBuffer.UVKind, uvs, true);
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Sets explicit UV coordinates for each ordered corner of the target facet.
|
|
626
|
+
*/
|
|
627
|
+
export function setFacetCornerUVs(mesh, faceIds, cornerPositions, cornerUVs) {
|
|
628
|
+
if (cornerPositions.length === 0 || cornerUVs.length === 0)
|
|
629
|
+
return;
|
|
630
|
+
isolateFacetVertices(mesh, faceIds);
|
|
631
|
+
const indices = mesh.getIndices();
|
|
632
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
633
|
+
if (!indices || !positions)
|
|
634
|
+
return;
|
|
635
|
+
const vertCount = positions.length / 3;
|
|
636
|
+
const rawUVs = mesh.getVerticesData(VertexBuffer.UVKind);
|
|
637
|
+
const uvs = rawUVs ? Array.from(rawUVs) : new Array(vertCount * 2).fill(0);
|
|
638
|
+
const targetVerts = collectFacetUniqueVertices(indices, faceIds);
|
|
639
|
+
for (const v of targetVerts) {
|
|
640
|
+
const vx = positions[v * 3];
|
|
641
|
+
const vy = positions[v * 3 + 1];
|
|
642
|
+
const vz = positions[v * 3 + 2];
|
|
643
|
+
let bestCorner = 0;
|
|
644
|
+
let bestDistSq = Infinity;
|
|
645
|
+
for (let c = 0; c < cornerPositions.length && c < cornerUVs.length; c++) {
|
|
646
|
+
const cp = cornerPositions[c];
|
|
647
|
+
const dx = vx - cp.x;
|
|
648
|
+
const dy = vy - cp.y;
|
|
649
|
+
const dz = vz - cp.z;
|
|
650
|
+
const dSq = dx * dx + dy * dy + dz * dz;
|
|
651
|
+
if (dSq < bestDistSq) {
|
|
652
|
+
bestDistSq = dSq;
|
|
653
|
+
bestCorner = c;
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
uvs[v * 2] = cornerUVs[bestCorner].u;
|
|
657
|
+
uvs[v * 2 + 1] = cornerUVs[bestCorner].v;
|
|
658
|
+
}
|
|
659
|
+
mesh.setVerticesData(VertexBuffer.UVKind, uvs, true);
|
|
660
|
+
}
|
|
661
|
+
/**
|
|
662
|
+
* Ensures a mesh has a valid base material before multi-material splitting.
|
|
663
|
+
*/
|
|
664
|
+
export function ensureBaseMaterial(mesh) {
|
|
665
|
+
if (mesh.material && !(mesh.material instanceof MultiMaterial)) {
|
|
666
|
+
return mesh.material;
|
|
667
|
+
}
|
|
668
|
+
if (mesh.material instanceof MultiMaterial && mesh.material.subMaterials[0]) {
|
|
669
|
+
return mesh.material.subMaterials[0];
|
|
670
|
+
}
|
|
671
|
+
const mat = new PBRMaterial(`${mesh.name}_BaseMat`, mesh.getScene());
|
|
672
|
+
mat.roughness = 0.5;
|
|
673
|
+
mat.metallic = 0.0;
|
|
674
|
+
mesh.material = mat;
|
|
675
|
+
return mat;
|
|
676
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Mesh, Scene, VertexData } from '@babylonjs/core';
|
|
2
|
+
export interface RoundedBoxOptions {
|
|
3
|
+
width?: number;
|
|
4
|
+
height?: number;
|
|
5
|
+
depth?: number;
|
|
6
|
+
size?: number;
|
|
7
|
+
radius?: number;
|
|
8
|
+
segments?: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Generates an indexed VertexData for a beveled / rounded box where flat faces remain minimal quads
|
|
12
|
+
* and only edges and corners are subdivided with smooth bevel rings.
|
|
13
|
+
*/
|
|
14
|
+
export declare function createRoundedBoxVertexData(options?: RoundedBoxOptions): VertexData;
|
|
15
|
+
export declare function createRoundedBoxMesh(name: string, options: RoundedBoxOptions, scene: Scene): Mesh;
|