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,2038 @@
|
|
|
1
|
+
import { Color3, Matrix, Mesh, MeshBuilder, PBRMaterial, Plane, PointerEventTypes, StandardMaterial, SubMesh, Vector3, VertexBuffer, VertexData, } from '@babylonjs/core';
|
|
2
|
+
import { ModelCompressor } from '../compression/ModelCompressor';
|
|
3
|
+
/**
|
|
4
|
+
* Controller for digital figure sculpting, base-mesh anatomical blockouts,
|
|
5
|
+
* and game-ready Quadric Error retopology in Babylon.js.
|
|
6
|
+
*/
|
|
7
|
+
export class SculptController {
|
|
8
|
+
service;
|
|
9
|
+
scene;
|
|
10
|
+
// Active tool state
|
|
11
|
+
isActive = false;
|
|
12
|
+
brushType = 'clay';
|
|
13
|
+
radius = 0.35; // World units
|
|
14
|
+
strength = 0.5; // 0.05 to 1.0
|
|
15
|
+
symmetryX = true;
|
|
16
|
+
invert = false;
|
|
17
|
+
// Internal stroke tracking
|
|
18
|
+
isSculpting = false;
|
|
19
|
+
activeMesh = null;
|
|
20
|
+
strokeStartVertexData = null;
|
|
21
|
+
lastLocalPick = null;
|
|
22
|
+
cursorRing = null;
|
|
23
|
+
symCursorRing = null;
|
|
24
|
+
// Grab brush tracking state
|
|
25
|
+
grabStartWorldPoint = null;
|
|
26
|
+
grabPlane = null;
|
|
27
|
+
grabInitialPositions = null;
|
|
28
|
+
grabWeights = new Map();
|
|
29
|
+
grabWeightsSym = new Map();
|
|
30
|
+
// Quad-Draw & Shrinkwrap tracking state
|
|
31
|
+
quadSurfaceOffset = 0.005;
|
|
32
|
+
quadAutoWeld = true;
|
|
33
|
+
quadStrokePoints = [];
|
|
34
|
+
// Listeners
|
|
35
|
+
onStateChanged;
|
|
36
|
+
onMeshModified;
|
|
37
|
+
constructor(service) {
|
|
38
|
+
this.service = service;
|
|
39
|
+
this.scene = service.scene;
|
|
40
|
+
this.initCursorIndicators();
|
|
41
|
+
this.initPointerHandling();
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Initializes 3D circular wireframe cursors to visually preview brush radius & symmetry.
|
|
45
|
+
*/
|
|
46
|
+
initCursorIndicators() {
|
|
47
|
+
if (this.cursorRing)
|
|
48
|
+
return;
|
|
49
|
+
// Main brush cursor disc
|
|
50
|
+
this.cursorRing = MeshBuilder.CreateDisc('editor_sculpt_cursor', { radius: this.radius, tessellation: 40 }, this.scene);
|
|
51
|
+
const ringMat = new StandardMaterial('editor_sculpt_cursor_mat', this.scene);
|
|
52
|
+
ringMat.diffuseColor = new Color3(0.2, 0.9, 0.4);
|
|
53
|
+
ringMat.emissiveColor = new Color3(0.2, 0.9, 0.4);
|
|
54
|
+
ringMat.wireframe = true;
|
|
55
|
+
ringMat.disableLighting = true;
|
|
56
|
+
ringMat.backFaceCulling = false;
|
|
57
|
+
this.cursorRing.material = ringMat;
|
|
58
|
+
this.cursorRing.isPickable = false;
|
|
59
|
+
this.cursorRing.isVisible = false;
|
|
60
|
+
// Symmetrical brush cursor disc (mirrored on X)
|
|
61
|
+
this.symCursorRing = MeshBuilder.CreateDisc('editor_sculpt_sym_cursor', { radius: this.radius, tessellation: 40 }, this.scene);
|
|
62
|
+
const symMat = new StandardMaterial('editor_sculpt_sym_mat', this.scene);
|
|
63
|
+
symMat.diffuseColor = new Color3(0.3, 0.7, 1.0);
|
|
64
|
+
symMat.emissiveColor = new Color3(0.3, 0.7, 1.0);
|
|
65
|
+
symMat.wireframe = true;
|
|
66
|
+
symMat.disableLighting = true;
|
|
67
|
+
symMat.backFaceCulling = false;
|
|
68
|
+
this.symCursorRing.material = symMat;
|
|
69
|
+
this.symCursorRing.isPickable = false;
|
|
70
|
+
this.symCursorRing.isVisible = false;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Updates brush radius and rescales cursor indicators.
|
|
74
|
+
*/
|
|
75
|
+
setRadius(newRadius) {
|
|
76
|
+
this.radius = Math.max(0.02, Math.min(newRadius, 4.0));
|
|
77
|
+
const scale = this.radius / 0.35;
|
|
78
|
+
if (this.cursorRing && !this.cursorRing.isDisposed()) {
|
|
79
|
+
this.cursorRing.scaling.set(scale, scale, scale);
|
|
80
|
+
}
|
|
81
|
+
if (this.symCursorRing && !this.symCursorRing.isDisposed()) {
|
|
82
|
+
this.symCursorRing.scaling.set(scale, scale, scale);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
setStrength(newStrength) {
|
|
86
|
+
this.strength = Math.max(0.02, Math.min(newStrength, 1.0));
|
|
87
|
+
}
|
|
88
|
+
setBrushType(type) {
|
|
89
|
+
this.brushType = type;
|
|
90
|
+
}
|
|
91
|
+
setSymmetry(enabled) {
|
|
92
|
+
this.symmetryX = enabled;
|
|
93
|
+
if (this.symCursorRing && (!enabled || !this.isActive)) {
|
|
94
|
+
this.symCursorRing.isVisible = false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
setInvert(invert) {
|
|
98
|
+
this.invert = invert;
|
|
99
|
+
}
|
|
100
|
+
setActive(active) {
|
|
101
|
+
this.isActive = active;
|
|
102
|
+
if (this.cursorRing)
|
|
103
|
+
this.cursorRing.isVisible = false;
|
|
104
|
+
if (this.symCursorRing)
|
|
105
|
+
this.symCursorRing.isVisible = false;
|
|
106
|
+
const canvas = this.service.canvas;
|
|
107
|
+
if (canvas) {
|
|
108
|
+
canvas.style.cursor = active ? 'crosshair' : 'default';
|
|
109
|
+
}
|
|
110
|
+
if (active) {
|
|
111
|
+
// Deactivate other exclusive tools if running
|
|
112
|
+
if (this.service.voxelPainter?.isActive) {
|
|
113
|
+
this.service.voxelPainter.setActive(false);
|
|
114
|
+
}
|
|
115
|
+
if (this.service.pathExtrusion?.isActive) {
|
|
116
|
+
this.service.pathExtrusion.setActive(false);
|
|
117
|
+
}
|
|
118
|
+
// Detach gizmo arrows so they do not intercept or steal sculpting pointer events
|
|
119
|
+
this.service.gizmoManager?.attachToNode(null);
|
|
120
|
+
this.scene.constantlyUpdateMeshUnderPointer = true;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
if (this.isSculpting) {
|
|
124
|
+
this.endSculptStroke();
|
|
125
|
+
}
|
|
126
|
+
// Restore gizmo on selected mesh when sculpting is deactivated
|
|
127
|
+
if (this.service.selectedMesh && !this.service.isGizmoMesh(this.service.selectedMesh)) {
|
|
128
|
+
this.service.gizmoManager?.attachToNode(this.service.selectedMesh);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
this.onStateChanged?.(active);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Pointer event observer for continuous 3D viewport sculpting.
|
|
135
|
+
*/
|
|
136
|
+
initPointerHandling() {
|
|
137
|
+
// Window-level pointerup ensures strokes commit cleanly even if released outside the canvas
|
|
138
|
+
if (typeof window !== 'undefined') {
|
|
139
|
+
window.addEventListener('pointerup', () => {
|
|
140
|
+
if (this.isActive && this.isSculpting) {
|
|
141
|
+
this.endSculptStroke();
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
this.scene.onPointerObservable.add((pointerInfo) => {
|
|
146
|
+
if (!this.isActive)
|
|
147
|
+
return;
|
|
148
|
+
const evt = pointerInfo.event;
|
|
149
|
+
const isLeftButton = evt ? evt.button === 0 : true;
|
|
150
|
+
// Detect invert hotkey (Alt or Ctrl key)
|
|
151
|
+
if (evt) {
|
|
152
|
+
if (evt.altKey || evt.ctrlKey) {
|
|
153
|
+
this.invert = true;
|
|
154
|
+
}
|
|
155
|
+
else if (!this.isSculpting) {
|
|
156
|
+
// Only reset invert if not toggled by manual UI
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
switch (pointerInfo.type) {
|
|
160
|
+
case PointerEventTypes.POINTERDOWN:
|
|
161
|
+
if (isLeftButton) {
|
|
162
|
+
this.handlePointerDown(pointerInfo);
|
|
163
|
+
}
|
|
164
|
+
break;
|
|
165
|
+
case PointerEventTypes.POINTERMOVE:
|
|
166
|
+
this.handlePointerMove(pointerInfo);
|
|
167
|
+
break;
|
|
168
|
+
case PointerEventTypes.POINTERUP:
|
|
169
|
+
if (isLeftButton && this.isSculpting) {
|
|
170
|
+
this.endSculptStroke();
|
|
171
|
+
}
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
handlePointerDown(pointerInfo) {
|
|
177
|
+
const pick = pointerInfo.pickInfo;
|
|
178
|
+
let mesh = null;
|
|
179
|
+
let hitPoint = null;
|
|
180
|
+
let hitNormal = null;
|
|
181
|
+
if (pick &&
|
|
182
|
+
pick.hit &&
|
|
183
|
+
pick.pickedPoint &&
|
|
184
|
+
pick.pickedMesh instanceof Mesh &&
|
|
185
|
+
!this.service.isGizmoMesh(pick.pickedMesh) &&
|
|
186
|
+
!this.service.isEditorHelper(pick.pickedMesh) &&
|
|
187
|
+
pick.pickedMesh !== this.service.gridMesh) {
|
|
188
|
+
mesh = pick.pickedMesh;
|
|
189
|
+
hitPoint = pick.pickedPoint;
|
|
190
|
+
hitNormal = pick.getNormal(true);
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
// Scene raycast ignoring gizmos, helpers, and grid
|
|
194
|
+
const livePick = this.scene.pick(this.scene.pointerX, this.scene.pointerY, (m) => m instanceof Mesh &&
|
|
195
|
+
!this.service.isGizmoMesh(m) &&
|
|
196
|
+
!this.service.isEditorHelper(m) &&
|
|
197
|
+
m !== this.service.gridMesh);
|
|
198
|
+
if (livePick && livePick.hit && livePick.pickedPoint && livePick.pickedMesh instanceof Mesh) {
|
|
199
|
+
mesh = livePick.pickedMesh;
|
|
200
|
+
hitPoint = livePick.pickedPoint;
|
|
201
|
+
hitNormal = livePick.getNormal(true);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
// Fallback for Grab brush: if click was slightly outside mesh contour, catch closest point on selected mesh
|
|
205
|
+
if (!mesh &&
|
|
206
|
+
this.brushType === 'grab' &&
|
|
207
|
+
this.service.selectedMesh instanceof Mesh &&
|
|
208
|
+
!this.service.isGizmoMesh(this.service.selectedMesh)) {
|
|
209
|
+
const sel = this.service.selectedMesh;
|
|
210
|
+
const ray = this.scene.createPickingRay(this.scene.pointerX, this.scene.pointerY, Matrix.Identity(), this.scene.activeCamera);
|
|
211
|
+
const positions = sel.getVerticesData(VertexBuffer.PositionKind);
|
|
212
|
+
if (positions && ray) {
|
|
213
|
+
sel.computeWorldMatrix(true);
|
|
214
|
+
const wMat = sel.getWorldMatrix();
|
|
215
|
+
let closestDist = Infinity;
|
|
216
|
+
let bestWorldPos = null;
|
|
217
|
+
const vPos = new Vector3();
|
|
218
|
+
const vWorld = new Vector3();
|
|
219
|
+
const numVerts = positions.length / 3;
|
|
220
|
+
const step = Math.max(1, Math.floor(numVerts / 300));
|
|
221
|
+
for (let i = 0; i < numVerts; i += step) {
|
|
222
|
+
vPos.set(positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2]);
|
|
223
|
+
Vector3.TransformCoordinatesToRef(vPos, wMat, vWorld);
|
|
224
|
+
const t = Vector3.Dot(vWorld.subtract(ray.origin), ray.direction);
|
|
225
|
+
if (t > 0) {
|
|
226
|
+
const rayPoint = ray.origin.add(ray.direction.scale(t));
|
|
227
|
+
const dist = Vector3.Distance(vWorld, rayPoint);
|
|
228
|
+
if (dist < closestDist) {
|
|
229
|
+
closestDist = dist;
|
|
230
|
+
bestWorldPos = vWorld.clone();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
if (bestWorldPos && closestDist <= this.radius * 1.5) {
|
|
235
|
+
mesh = sel;
|
|
236
|
+
hitPoint = bestWorldPos;
|
|
237
|
+
hitNormal = this.scene.activeCamera
|
|
238
|
+
? this.scene.activeCamera.position.subtract(bestWorldPos).normalize()
|
|
239
|
+
: Vector3.Up();
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
if (!mesh || !hitPoint) {
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
// Ensure mesh vertex buffers are updatable
|
|
247
|
+
this.ensureMeshUpdatable(mesh);
|
|
248
|
+
this.activeMesh = mesh;
|
|
249
|
+
this.isSculpting = true;
|
|
250
|
+
this.strokeStartVertexData = VertexData.ExtractFromMesh(mesh);
|
|
251
|
+
// Temporarily detach camera controls so mouse movement sculpts without rotating viewport
|
|
252
|
+
if (this.scene.activeCamera) {
|
|
253
|
+
this.scene.activeCamera.detachControl();
|
|
254
|
+
}
|
|
255
|
+
// Transform world pick to local coordinates
|
|
256
|
+
const invWorld = mesh.getWorldMatrix().clone().invert();
|
|
257
|
+
this.lastLocalPick = Vector3.TransformCoordinates(hitPoint, invWorld);
|
|
258
|
+
// Apply immediate initial brush stamp or initialize Grab / Quad-Draw tracking
|
|
259
|
+
if (this.brushType === 'grab') {
|
|
260
|
+
this.initGrabStroke(mesh, hitPoint);
|
|
261
|
+
}
|
|
262
|
+
else if (this.brushType === 'quaddraw') {
|
|
263
|
+
this.quadStrokePoints = [hitPoint.clone()];
|
|
264
|
+
}
|
|
265
|
+
else {
|
|
266
|
+
this.applyBrushStroke(mesh, hitPoint, hitNormal);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
handlePointerMove(pointerInfo) {
|
|
270
|
+
const pick = pointerInfo.pickInfo;
|
|
271
|
+
// Update cursor indicator visual placement when hovering
|
|
272
|
+
if (!this.isSculpting) {
|
|
273
|
+
let hoverHit = pick?.pickedPoint || null;
|
|
274
|
+
let hoverNormal = pick?.getNormal(true) || null;
|
|
275
|
+
if (!hoverHit ||
|
|
276
|
+
!pick?.pickedMesh ||
|
|
277
|
+
!(pick.pickedMesh instanceof Mesh) ||
|
|
278
|
+
this.service.isGizmoMesh(pick.pickedMesh) ||
|
|
279
|
+
this.service.isEditorHelper(pick.pickedMesh) ||
|
|
280
|
+
pick.pickedMesh === this.service.gridMesh) {
|
|
281
|
+
const liveHover = this.scene.pick(this.scene.pointerX, this.scene.pointerY, (m) => m instanceof Mesh &&
|
|
282
|
+
!this.service.isGizmoMesh(m) &&
|
|
283
|
+
!this.service.isEditorHelper(m) &&
|
|
284
|
+
m !== this.service.gridMesh);
|
|
285
|
+
if (liveHover && liveHover.hit && liveHover.pickedPoint) {
|
|
286
|
+
hoverHit = liveHover.pickedPoint;
|
|
287
|
+
hoverNormal = liveHover.getNormal(true);
|
|
288
|
+
}
|
|
289
|
+
else {
|
|
290
|
+
hoverHit = null;
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (hoverHit) {
|
|
294
|
+
this.updateCursorTransform(hoverHit, hoverNormal || Vector3.Up());
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
if (this.cursorRing)
|
|
298
|
+
this.cursorRing.isVisible = false;
|
|
299
|
+
if (this.symCursorRing)
|
|
300
|
+
this.symCursorRing.isVisible = false;
|
|
301
|
+
}
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
if (!this.activeMesh)
|
|
305
|
+
return;
|
|
306
|
+
// 1. Grab Brush Drag (Screen-space / Camera plane drag)
|
|
307
|
+
if (this.brushType === 'grab') {
|
|
308
|
+
this.handleGrabDrag();
|
|
309
|
+
return;
|
|
310
|
+
}
|
|
311
|
+
// 2. Surface Stamping Brushes (Clay, Smooth, Flatten, Inflate, Pinch)
|
|
312
|
+
let hitPoint = pick?.pickedPoint;
|
|
313
|
+
let hitNormal = pick?.getNormal(true);
|
|
314
|
+
if (!hitPoint) {
|
|
315
|
+
const livePick = this.scene.pick(this.scene.pointerX, this.scene.pointerY, (m) => m === this.activeMesh);
|
|
316
|
+
if (livePick && livePick.hit && livePick.pickedPoint) {
|
|
317
|
+
hitPoint = livePick.pickedPoint;
|
|
318
|
+
hitNormal = livePick.getNormal(true);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
if (hitPoint) {
|
|
322
|
+
this.updateCursorTransform(hitPoint, hitNormal || Vector3.Up());
|
|
323
|
+
if (this.brushType === 'quaddraw') {
|
|
324
|
+
const lastPt = this.quadStrokePoints[this.quadStrokePoints.length - 1];
|
|
325
|
+
if (!lastPt || Vector3.Distance(lastPt, hitPoint) >= Math.max(0.03, this.radius * 0.25)) {
|
|
326
|
+
this.quadStrokePoints.push(hitPoint.clone());
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
this.applyBrushStroke(this.activeMesh, hitPoint, hitNormal);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
initGrabStroke(mesh, worldPoint) {
|
|
335
|
+
this.grabStartWorldPoint = worldPoint.clone();
|
|
336
|
+
const camera = this.scene.activeCamera;
|
|
337
|
+
const planeNormal = camera
|
|
338
|
+
? camera.position.subtract(worldPoint).normalize()
|
|
339
|
+
: Vector3.Forward();
|
|
340
|
+
this.grabPlane = Plane.FromPositionAndNormal(worldPoint, planeNormal);
|
|
341
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
342
|
+
if (!positions)
|
|
343
|
+
return;
|
|
344
|
+
this.grabInitialPositions = new Float32Array(positions);
|
|
345
|
+
this.grabWeights.clear();
|
|
346
|
+
this.grabWeightsSym.clear();
|
|
347
|
+
mesh.computeWorldMatrix(true);
|
|
348
|
+
const invWorld = mesh.getWorldMatrix().clone().invert();
|
|
349
|
+
const localHit = Vector3.TransformCoordinates(worldPoint, invWorld);
|
|
350
|
+
const scaling = mesh.scaling;
|
|
351
|
+
const maxScale = Math.max(Math.abs(scaling.x), Math.abs(scaling.y), Math.abs(scaling.z)) || 1.0;
|
|
352
|
+
const rLocal = this.radius / maxScale;
|
|
353
|
+
const rSq = rLocal * rLocal;
|
|
354
|
+
const numVerts = positions.length / 3;
|
|
355
|
+
for (let i = 0; i < numVerts; i++) {
|
|
356
|
+
const idx = i * 3;
|
|
357
|
+
const vx = positions[idx];
|
|
358
|
+
const vy = positions[idx + 1];
|
|
359
|
+
const vz = positions[idx + 2];
|
|
360
|
+
const dx = vx - localHit.x;
|
|
361
|
+
const dy = vy - localHit.y;
|
|
362
|
+
const dz = vz - localHit.z;
|
|
363
|
+
const dSq = dx * dx + dy * dy + dz * dz;
|
|
364
|
+
if (dSq <= rSq) {
|
|
365
|
+
const t = 1 - dSq / rSq;
|
|
366
|
+
this.grabWeights.set(i, t * t);
|
|
367
|
+
}
|
|
368
|
+
if (this.symmetryX) {
|
|
369
|
+
const dxs = vx - (-localHit.x);
|
|
370
|
+
const dys = vy - localHit.y;
|
|
371
|
+
const dzs = vz - localHit.z;
|
|
372
|
+
const dSqs = dxs * dxs + dys * dys + dzs * dzs;
|
|
373
|
+
if (dSqs <= rSq) {
|
|
374
|
+
const ts = 1 - dSqs / rSq;
|
|
375
|
+
this.grabWeightsSym.set(i, ts * ts);
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
handleGrabDrag() {
|
|
381
|
+
if (!this.activeMesh ||
|
|
382
|
+
!this.grabPlane ||
|
|
383
|
+
!this.grabStartWorldPoint ||
|
|
384
|
+
!this.grabInitialPositions) {
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
const ray = this.scene.createPickingRay(this.scene.pointerX, this.scene.pointerY, Matrix.Identity(), this.scene.activeCamera);
|
|
388
|
+
const dist = ray.intersectsPlane(this.grabPlane);
|
|
389
|
+
if (dist === null || isNaN(dist))
|
|
390
|
+
return;
|
|
391
|
+
const currentWorldPos = ray.origin.add(ray.direction.scale(dist));
|
|
392
|
+
const worldDelta = currentWorldPos.subtract(this.grabStartWorldPoint);
|
|
393
|
+
// Update cursor position to track drag movement
|
|
394
|
+
this.updateCursorTransform(currentWorldPos, this.grabPlane.normal);
|
|
395
|
+
this.activeMesh.computeWorldMatrix(true);
|
|
396
|
+
const invWorld = this.activeMesh.getWorldMatrix().clone().invert();
|
|
397
|
+
const localDelta = Vector3.TransformNormal(worldDelta, invWorld);
|
|
398
|
+
const symLocalDelta = new Vector3(-localDelta.x, localDelta.y, localDelta.z);
|
|
399
|
+
const positions = this.activeMesh.getVerticesData(VertexBuffer.PositionKind);
|
|
400
|
+
const indices = this.activeMesh.getIndices();
|
|
401
|
+
if (!positions || !indices)
|
|
402
|
+
return;
|
|
403
|
+
// Restore initial positions first for smooth non-accumulative grab deformation
|
|
404
|
+
for (let i = 0; i < positions.length; i++) {
|
|
405
|
+
positions[i] = this.grabInitialPositions[i];
|
|
406
|
+
}
|
|
407
|
+
// Apply primary grabbed vertices displacement
|
|
408
|
+
for (const [vertIdx, weight] of this.grabWeights) {
|
|
409
|
+
const idx = vertIdx * 3;
|
|
410
|
+
const factor = weight * this.strength;
|
|
411
|
+
positions[idx] += localDelta.x * factor;
|
|
412
|
+
positions[idx + 1] += localDelta.y * factor;
|
|
413
|
+
positions[idx + 2] += localDelta.z * factor;
|
|
414
|
+
}
|
|
415
|
+
// Apply symmetrical grab
|
|
416
|
+
if (this.symmetryX) {
|
|
417
|
+
for (const [vertIdx, weight] of this.grabWeightsSym) {
|
|
418
|
+
const idx = vertIdx * 3;
|
|
419
|
+
const factor = weight * this.strength;
|
|
420
|
+
positions[idx] += symLocalDelta.x * factor;
|
|
421
|
+
positions[idx + 1] += symLocalDelta.y * factor;
|
|
422
|
+
positions[idx + 2] += symLocalDelta.z * factor;
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
const normals = this.activeMesh.getVerticesData(VertexBuffer.NormalKind) || [];
|
|
426
|
+
this.activeMesh.setVerticesData(VertexBuffer.PositionKind, positions, true);
|
|
427
|
+
VertexData.ComputeNormals(positions, indices, normals);
|
|
428
|
+
this.activeMesh.setVerticesData(VertexBuffer.NormalKind, normals, true);
|
|
429
|
+
this.activeMesh.refreshBoundingInfo();
|
|
430
|
+
}
|
|
431
|
+
updateCursorTransform(worldPos, normal) {
|
|
432
|
+
if (!this.cursorRing || this.cursorRing.isDisposed())
|
|
433
|
+
return;
|
|
434
|
+
this.cursorRing.isVisible = true;
|
|
435
|
+
this.cursorRing.position.copyFrom(worldPos);
|
|
436
|
+
// Align disc normal with surface normal
|
|
437
|
+
const up = Math.abs(Vector3.Dot(normal, Vector3.Up())) > 0.99 ? Vector3.Right() : Vector3.Up();
|
|
438
|
+
this.cursorRing.lookAt(worldPos.add(normal), 0, 0, 0);
|
|
439
|
+
// Mirrored cursor
|
|
440
|
+
if (this.symmetryX && this.symCursorRing && !this.symCursorRing.isDisposed()) {
|
|
441
|
+
this.symCursorRing.isVisible = true;
|
|
442
|
+
const symPos = new Vector3(-worldPos.x, worldPos.y, worldPos.z);
|
|
443
|
+
const symNormal = new Vector3(-normal.x, normal.y, normal.z);
|
|
444
|
+
this.symCursorRing.position.copyFrom(symPos);
|
|
445
|
+
this.symCursorRing.lookAt(symPos.add(symNormal), 0, 0, 0);
|
|
446
|
+
}
|
|
447
|
+
else if (this.symCursorRing) {
|
|
448
|
+
this.symCursorRing.isVisible = false;
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
endSculptStroke() {
|
|
452
|
+
if (!this.isSculpting || !this.activeMesh) {
|
|
453
|
+
this.isSculpting = false;
|
|
454
|
+
this.activeMesh = null;
|
|
455
|
+
return;
|
|
456
|
+
}
|
|
457
|
+
const mesh = this.activeMesh;
|
|
458
|
+
const preSnapshot = this.strokeStartVertexData;
|
|
459
|
+
const postSnapshot = VertexData.ExtractFromMesh(mesh);
|
|
460
|
+
// Re-attach camera control
|
|
461
|
+
if (this.service.canvas && this.scene.activeCamera) {
|
|
462
|
+
this.scene.activeCamera.attachControl(this.service.canvas, true);
|
|
463
|
+
}
|
|
464
|
+
this.isSculpting = false;
|
|
465
|
+
this.activeMesh = null;
|
|
466
|
+
this.lastLocalPick = null;
|
|
467
|
+
this.strokeStartVertexData = null;
|
|
468
|
+
// Clear grab brush tracking
|
|
469
|
+
this.grabStartWorldPoint = null;
|
|
470
|
+
this.grabPlane = null;
|
|
471
|
+
this.grabInitialPositions = null;
|
|
472
|
+
this.grabWeights.clear();
|
|
473
|
+
this.grabWeightsSym.clear();
|
|
474
|
+
if (this.brushType === 'quaddraw' && this.quadStrokePoints.length >= 1) {
|
|
475
|
+
const pts = this.quadStrokePoints.slice();
|
|
476
|
+
this.quadStrokePoints = [];
|
|
477
|
+
if (pts.length === 1) {
|
|
478
|
+
// Single click places a single quad patch centered at the pick point
|
|
479
|
+
const half = Math.max(0.05, this.radius * 0.5);
|
|
480
|
+
pts.push(pts[0].add(new Vector3(0, 0, half)));
|
|
481
|
+
}
|
|
482
|
+
this.drawQuadStripOnMesh(mesh, pts, this.radius, this.quadSurfaceOffset);
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
if (preSnapshot) {
|
|
486
|
+
this.service.recordAction({
|
|
487
|
+
name: `Sculpt (${this.brushType})`,
|
|
488
|
+
undo: () => {
|
|
489
|
+
if (!mesh.isDisposed()) {
|
|
490
|
+
preSnapshot.applyToMesh(mesh, true);
|
|
491
|
+
mesh.refreshBoundingInfo();
|
|
492
|
+
this.service.updateStats();
|
|
493
|
+
this.onMeshModified?.(mesh);
|
|
494
|
+
}
|
|
495
|
+
},
|
|
496
|
+
redo: () => {
|
|
497
|
+
if (!mesh.isDisposed()) {
|
|
498
|
+
postSnapshot.applyToMesh(mesh, true);
|
|
499
|
+
mesh.refreshBoundingInfo();
|
|
500
|
+
this.service.updateStats();
|
|
501
|
+
this.onMeshModified?.(mesh);
|
|
502
|
+
}
|
|
503
|
+
},
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
this.service.updateStats();
|
|
507
|
+
this.onMeshModified?.(mesh);
|
|
508
|
+
}
|
|
509
|
+
ensureMeshUpdatable(mesh) {
|
|
510
|
+
if (!mesh.isVertexBufferUpdatable(VertexBuffer.PositionKind)) {
|
|
511
|
+
const vd = VertexData.ExtractFromMesh(mesh);
|
|
512
|
+
vd.applyToMesh(mesh, true);
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
/**
|
|
516
|
+
* Displaces vertices of the target mesh based on current brush mode and falloff.
|
|
517
|
+
*/
|
|
518
|
+
applyBrushStroke(mesh, worldHit, worldNormal) {
|
|
519
|
+
this.ensureMeshUpdatable(mesh);
|
|
520
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
521
|
+
const indices = mesh.getIndices();
|
|
522
|
+
if (!positions || !indices)
|
|
523
|
+
return;
|
|
524
|
+
const normals = mesh.getVerticesData(VertexBuffer.NormalKind) || [];
|
|
525
|
+
mesh.computeWorldMatrix(true);
|
|
526
|
+
const invWorld = mesh.getWorldMatrix().clone().invert();
|
|
527
|
+
const localHit = Vector3.TransformCoordinates(worldHit, invWorld);
|
|
528
|
+
const localNorm = worldNormal
|
|
529
|
+
? Vector3.TransformNormal(worldNormal, invWorld).normalize()
|
|
530
|
+
: Vector3.Up();
|
|
531
|
+
// Scale-aware brush radius
|
|
532
|
+
const scaling = mesh.scaling;
|
|
533
|
+
const maxScale = Math.max(Math.abs(scaling.x), Math.abs(scaling.y), Math.abs(scaling.z)) || 1.0;
|
|
534
|
+
const rLocal = this.radius / maxScale;
|
|
535
|
+
const rSq = rLocal * rLocal;
|
|
536
|
+
// Movement delta for Grab brush
|
|
537
|
+
let localDelta = Vector3.Zero();
|
|
538
|
+
if (this.brushType === 'grab' && this.lastLocalPick) {
|
|
539
|
+
localDelta = localHit.subtract(this.lastLocalPick);
|
|
540
|
+
}
|
|
541
|
+
this.lastLocalPick = localHit.clone();
|
|
542
|
+
// Invert sign multiplier
|
|
543
|
+
const sign = this.invert ? -1 : 1;
|
|
544
|
+
const strengthFactor = this.strength;
|
|
545
|
+
// Gather vertices for Smooth or Flatten brush calculations
|
|
546
|
+
let avgNeighborPos = Vector3.Zero();
|
|
547
|
+
let neighborCount = 0;
|
|
548
|
+
if (this.brushType === 'smooth' || this.brushType === 'flatten') {
|
|
549
|
+
for (let i = 0; i < positions.length; i += 3) {
|
|
550
|
+
const vx = positions[i];
|
|
551
|
+
const vy = positions[i + 1];
|
|
552
|
+
const vz = positions[i + 2];
|
|
553
|
+
const dx = vx - localHit.x;
|
|
554
|
+
const dy = vy - localHit.y;
|
|
555
|
+
const dz = vz - localHit.z;
|
|
556
|
+
if (dx * dx + dy * dy + dz * dz <= rSq) {
|
|
557
|
+
avgNeighborPos.addInPlaceFromFloats(vx, vy, vz);
|
|
558
|
+
neighborCount++;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (neighborCount > 0) {
|
|
562
|
+
avgNeighborPos.scaleInPlace(1 / neighborCount);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const vertexCount = positions.length / 3;
|
|
566
|
+
// 1. Primary Brush Pass
|
|
567
|
+
this.applyDeformation(positions, normals, vertexCount, localHit, localNorm, localDelta, rSq, sign, strengthFactor, avgNeighborPos);
|
|
568
|
+
// 2. Mirrored Symmetry Pass (Reflection across X = 0)
|
|
569
|
+
if (this.symmetryX) {
|
|
570
|
+
const symHit = new Vector3(-localHit.x, localHit.y, localHit.z);
|
|
571
|
+
const symNorm = new Vector3(-localNorm.x, localNorm.y, localNorm.z);
|
|
572
|
+
const symDelta = new Vector3(-localDelta.x, localDelta.y, localDelta.z);
|
|
573
|
+
const symAvg = new Vector3(-avgNeighborPos.x, avgNeighborPos.y, avgNeighborPos.z);
|
|
574
|
+
this.applyDeformation(positions, normals, vertexCount, symHit, symNorm, symDelta, rSq, sign, strengthFactor, symAvg, true);
|
|
575
|
+
}
|
|
576
|
+
// Recompute smooth normals and refresh geometry
|
|
577
|
+
mesh.setVerticesData(VertexBuffer.PositionKind, positions, true);
|
|
578
|
+
VertexData.ComputeNormals(positions, indices, normals);
|
|
579
|
+
mesh.setVerticesData(VertexBuffer.NormalKind, normals, true);
|
|
580
|
+
mesh.refreshBoundingInfo();
|
|
581
|
+
}
|
|
582
|
+
applyDeformation(positions, normals, vertexCount, center, normal, delta, rSq, sign, strength, avgPos, isSymmetry = false) {
|
|
583
|
+
for (let i = 0; i < vertexCount; i++) {
|
|
584
|
+
const idx = i * 3;
|
|
585
|
+
const vx = positions[idx];
|
|
586
|
+
const vy = positions[idx + 1];
|
|
587
|
+
const vz = positions[idx + 2];
|
|
588
|
+
const dx = vx - center.x;
|
|
589
|
+
const dy = vy - center.y;
|
|
590
|
+
const dz = vz - center.z;
|
|
591
|
+
const distSq = dx * dx + dy * dy + dz * dz;
|
|
592
|
+
if (distSq > rSq)
|
|
593
|
+
continue;
|
|
594
|
+
// Smooth cosine / hermite falloff (w = (1 - d^2/r^2)^2)
|
|
595
|
+
const t = 1 - distSq / rSq;
|
|
596
|
+
const w = t * t;
|
|
597
|
+
switch (this.brushType) {
|
|
598
|
+
case 'clay': {
|
|
599
|
+
// Displaces volume along stroke normal with organic soft plateau
|
|
600
|
+
const disp = strength * w * 0.06 * sign;
|
|
601
|
+
positions[idx] += normal.x * disp;
|
|
602
|
+
positions[idx + 1] += normal.y * disp;
|
|
603
|
+
positions[idx + 2] += normal.z * disp;
|
|
604
|
+
break;
|
|
605
|
+
}
|
|
606
|
+
case 'grab': {
|
|
607
|
+
// Pulls geometry smoothly along drag vector
|
|
608
|
+
const moveFactor = w * Math.min(strength * 1.5, 1.0);
|
|
609
|
+
positions[idx] += delta.x * moveFactor;
|
|
610
|
+
positions[idx + 1] += delta.y * moveFactor;
|
|
611
|
+
positions[idx + 2] += delta.z * moveFactor;
|
|
612
|
+
break;
|
|
613
|
+
}
|
|
614
|
+
case 'inflate': {
|
|
615
|
+
// Expands or deflates vertices along their own vertex normal
|
|
616
|
+
const nx = normals[idx] || normal.x;
|
|
617
|
+
const ny = normals[idx + 1] || normal.y;
|
|
618
|
+
const nz = normals[idx + 2] || normal.z;
|
|
619
|
+
const disp = strength * w * 0.05 * sign;
|
|
620
|
+
positions[idx] += nx * disp;
|
|
621
|
+
positions[idx + 1] += ny * disp;
|
|
622
|
+
positions[idx + 2] += nz * disp;
|
|
623
|
+
break;
|
|
624
|
+
}
|
|
625
|
+
case 'smooth': {
|
|
626
|
+
// Relaxes vertices towards neighbors (Laplacian smoothing filter)
|
|
627
|
+
const factor = strength * w * 0.35;
|
|
628
|
+
positions[idx] += (avgPos.x - vx) * factor;
|
|
629
|
+
positions[idx + 1] += (avgPos.y - vy) * factor;
|
|
630
|
+
positions[idx + 2] += (avgPos.z - vz) * factor;
|
|
631
|
+
break;
|
|
632
|
+
}
|
|
633
|
+
case 'flatten': {
|
|
634
|
+
// Projects vertices towards average plane of the brush contact
|
|
635
|
+
const distToPlane = (vx - center.x) * normal.x + (vy - center.y) * normal.y + (vz - center.z) * normal.z;
|
|
636
|
+
const factor = strength * w * 0.45;
|
|
637
|
+
positions[idx] -= normal.x * distToPlane * factor;
|
|
638
|
+
positions[idx + 1] -= normal.y * distToPlane * factor;
|
|
639
|
+
positions[idx + 2] -= normal.z * distToPlane * factor;
|
|
640
|
+
break;
|
|
641
|
+
}
|
|
642
|
+
case 'pinch': {
|
|
643
|
+
// Pinches geometry towards stroke center (creases, wrinkles)
|
|
644
|
+
const factor = strength * w * 0.18 * sign;
|
|
645
|
+
positions[idx] += (center.x - vx) * factor;
|
|
646
|
+
positions[idx + 1] += (center.y - vy) * factor;
|
|
647
|
+
positions[idx + 2] += (center.z - vz) * factor;
|
|
648
|
+
break;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
/* =========================================================================
|
|
654
|
+
BASE MESH SPAWNERS (HUMANOID, QUADRUPED, HEAD BUST, SCULPT SPHERE)
|
|
655
|
+
========================================================================= */
|
|
656
|
+
/**
|
|
657
|
+
* Spawns an 8-head proportional Humanoid Mannequin Base Mesh (Loomis Canon).
|
|
658
|
+
* Total height: 1.80m, bottom-aligned at Y = 0 with articulated joints (elbows, knees, shoulders).
|
|
659
|
+
*/
|
|
660
|
+
spawnHumanoidBaseMesh(options = {}) {
|
|
661
|
+
if (options.canon === 'chibi') {
|
|
662
|
+
return this.spawnChibiBaseMesh(options);
|
|
663
|
+
}
|
|
664
|
+
const rootName = options.name || 'Humanoid_BaseMesh';
|
|
665
|
+
const parts = [];
|
|
666
|
+
const scale = options.scale || 1.0;
|
|
667
|
+
const pose = options.pose || 't_pose';
|
|
668
|
+
// 1. Head (Height ~0.225m, center Y ~ 1.68m)
|
|
669
|
+
const head = MeshBuilder.CreateSphere('h_head', { diameterX: 0.18, diameterY: 0.225, diameterZ: 0.20, segments: 16 }, this.scene);
|
|
670
|
+
head.position.set(0, 1.68, 0.02);
|
|
671
|
+
parts.push(head);
|
|
672
|
+
// 2. Neck (Y = 1.52m)
|
|
673
|
+
const neck = MeshBuilder.CreateCylinder('h_neck', { height: 0.12, diameterTop: 0.11, diameterBottom: 0.13, tessellation: 14 }, this.scene);
|
|
674
|
+
neck.position.set(0, 1.52, 0);
|
|
675
|
+
parts.push(neck);
|
|
676
|
+
// 3. Chest & Ribcage (Y = 1.35m)
|
|
677
|
+
const chest = MeshBuilder.CreateCylinder('h_chest', { height: 0.26, diameterTop: 0.38, diameterBottom: 0.30, tessellation: 16 }, this.scene);
|
|
678
|
+
chest.position.set(0, 1.35, 0);
|
|
679
|
+
chest.scaling.set(1.0, 1.0, 0.75);
|
|
680
|
+
parts.push(chest);
|
|
681
|
+
// 4. Waist & Abdomen (Y = 1.15m)
|
|
682
|
+
const waist = MeshBuilder.CreateCylinder('h_waist', { height: 0.18, diameterTop: 0.30, diameterBottom: 0.28, tessellation: 16 }, this.scene);
|
|
683
|
+
waist.position.set(0, 1.15, 0);
|
|
684
|
+
waist.scaling.set(1.0, 1.0, 0.72);
|
|
685
|
+
parts.push(waist);
|
|
686
|
+
// 5. Pelvis & Hips (Y = 0.96m, groin at 0.90m = 0.5 * H)
|
|
687
|
+
const pelvis = MeshBuilder.CreateCylinder('h_pelvis', { height: 0.22, diameterTop: 0.29, diameterBottom: 0.35, tessellation: 16 }, this.scene);
|
|
688
|
+
pelvis.position.set(0, 0.96, 0);
|
|
689
|
+
pelvis.scaling.set(1.0, 1.0, 0.75);
|
|
690
|
+
parts.push(pelvis);
|
|
691
|
+
// 6. Arms (Left & Right) - T-Pose or A-Pose
|
|
692
|
+
for (const side of [-1, 1]) {
|
|
693
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
694
|
+
const shoulderX = side * 0.24;
|
|
695
|
+
const armY = 1.42;
|
|
696
|
+
// Shoulder joint
|
|
697
|
+
const shoulder = MeshBuilder.CreateSphere(`h_shoulder_${sName}`, { diameter: 0.12, segments: 12 }, this.scene);
|
|
698
|
+
shoulder.position.set(shoulderX, armY, 0);
|
|
699
|
+
parts.push(shoulder);
|
|
700
|
+
if (pose === 't_pose') {
|
|
701
|
+
// T-Pose: Upper arm horizontal along X-axis
|
|
702
|
+
const upperArm = MeshBuilder.CreateCylinder(`h_uparm_${sName}`, { height: 0.28, diameterTop: 0.10, diameterBottom: 0.08, tessellation: 12 }, this.scene);
|
|
703
|
+
upperArm.position.set(side * 0.38, armY, 0);
|
|
704
|
+
upperArm.rotation.z = side * (Math.PI / 2);
|
|
705
|
+
parts.push(upperArm);
|
|
706
|
+
// Elbow joint
|
|
707
|
+
const elbow = MeshBuilder.CreateSphere(`h_elbow_${sName}`, { diameter: 0.09, segments: 12 }, this.scene);
|
|
708
|
+
elbow.position.set(side * 0.52, armY, 0);
|
|
709
|
+
parts.push(elbow);
|
|
710
|
+
// Forearm horizontal along X-axis
|
|
711
|
+
const forearm = MeshBuilder.CreateCylinder(`h_forearm_${sName}`, { height: 0.26, diameterTop: 0.08, diameterBottom: 0.065, tessellation: 12 }, this.scene);
|
|
712
|
+
forearm.position.set(side * 0.65, armY, 0);
|
|
713
|
+
forearm.rotation.z = side * (Math.PI / 2);
|
|
714
|
+
parts.push(forearm);
|
|
715
|
+
// Hand
|
|
716
|
+
const hand = MeshBuilder.CreateBox(`h_hand_${sName}`, { width: 0.15, height: 0.05, depth: 0.08 }, this.scene);
|
|
717
|
+
hand.position.set(side * 0.81, armY, 0);
|
|
718
|
+
parts.push(hand);
|
|
719
|
+
}
|
|
720
|
+
else {
|
|
721
|
+
// A-Pose: Angled downward
|
|
722
|
+
const upperArm = MeshBuilder.CreateCylinder(`h_uparm_${sName}`, { height: 0.28, diameterTop: 0.10, diameterBottom: 0.08, tessellation: 12 }, this.scene);
|
|
723
|
+
upperArm.position.set(side * 0.28, 1.26, 0);
|
|
724
|
+
upperArm.rotation.z = side * -0.22;
|
|
725
|
+
parts.push(upperArm);
|
|
726
|
+
const elbow = MeshBuilder.CreateSphere(`h_elbow_${sName}`, { diameter: 0.09, segments: 12 }, this.scene);
|
|
727
|
+
elbow.position.set(side * 0.32, 1.11, 0);
|
|
728
|
+
parts.push(elbow);
|
|
729
|
+
const forearm = MeshBuilder.CreateCylinder(`h_forearm_${sName}`, { height: 0.26, diameterTop: 0.08, diameterBottom: 0.065, tessellation: 12 }, this.scene);
|
|
730
|
+
forearm.position.set(side * 0.35, 0.96, 0);
|
|
731
|
+
forearm.rotation.z = side * -0.15;
|
|
732
|
+
parts.push(forearm);
|
|
733
|
+
const hand = MeshBuilder.CreateBox(`h_hand_${sName}`, { width: 0.05, height: 0.15, depth: 0.08 }, this.scene);
|
|
734
|
+
hand.position.set(side * 0.37, 0.78, 0);
|
|
735
|
+
parts.push(hand);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
// 7. Legs (Left & Right)
|
|
739
|
+
for (const side of [-1, 1]) {
|
|
740
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
741
|
+
const hipX = side * 0.13;
|
|
742
|
+
// Hip joint
|
|
743
|
+
const hip = MeshBuilder.CreateSphere(`h_hip_${sName}`, { diameter: 0.15, segments: 12 }, this.scene);
|
|
744
|
+
hip.position.set(hipX, 0.88, 0);
|
|
745
|
+
parts.push(hip);
|
|
746
|
+
// Thigh (Oberschenkel)
|
|
747
|
+
const thigh = MeshBuilder.CreateCylinder(`h_thigh_${sName}`, { height: 0.42, diameterTop: 0.16, diameterBottom: 0.12, tessellation: 14 }, this.scene);
|
|
748
|
+
thigh.position.set(hipX, 0.66, 0);
|
|
749
|
+
parts.push(thigh);
|
|
750
|
+
// Knee Joint
|
|
751
|
+
const knee = MeshBuilder.CreateSphere(`h_knee_${sName}`, { diameter: 0.12, segments: 12 }, this.scene);
|
|
752
|
+
knee.position.set(hipX, 0.45, 0.02);
|
|
753
|
+
parts.push(knee);
|
|
754
|
+
// Calf / Shin (Unterschenkel)
|
|
755
|
+
const calf = MeshBuilder.CreateCylinder(`h_calf_${sName}`, { height: 0.40, diameterTop: 0.12, diameterBottom: 0.09, tessellation: 14 }, this.scene);
|
|
756
|
+
calf.position.set(hipX, 0.24, -0.01);
|
|
757
|
+
parts.push(calf);
|
|
758
|
+
// Foot (Fuß)
|
|
759
|
+
const foot = MeshBuilder.CreateBox(`h_foot_${sName}`, { width: 0.10, height: 0.07, depth: 0.24 }, this.scene);
|
|
760
|
+
foot.position.set(hipX, 0.035, 0.05);
|
|
761
|
+
parts.push(foot);
|
|
762
|
+
}
|
|
763
|
+
// Merge into single seamless humanoid base mesh
|
|
764
|
+
const baseMesh = Mesh.MergeMeshes(parts, true, true, undefined, false, true);
|
|
765
|
+
if (!baseMesh) {
|
|
766
|
+
throw new Error('Failed to merge humanoid base mesh components.');
|
|
767
|
+
}
|
|
768
|
+
baseMesh.name = rootName;
|
|
769
|
+
baseMesh.id = `mesh_${Date.now()}_humanoid`;
|
|
770
|
+
if (scale !== 1.0) {
|
|
771
|
+
baseMesh.scaling.set(scale, scale, scale);
|
|
772
|
+
}
|
|
773
|
+
if (options.position) {
|
|
774
|
+
baseMesh.position.copyFrom(options.position);
|
|
775
|
+
}
|
|
776
|
+
this.applyDefaultCharacterMaterial(baseMesh);
|
|
777
|
+
this.service.addMesh(baseMesh);
|
|
778
|
+
this.service.selectMesh(baseMesh);
|
|
779
|
+
return baseMesh;
|
|
780
|
+
}
|
|
781
|
+
/**
|
|
782
|
+
* Spawns a Stylized / Chibi Cartoon Base Mesh (3.5-Head Canon: Mario, Dwarf, Toon Mascot).
|
|
783
|
+
* Features a large round cartoon head with nose and ears, plump potbelly torso, short sturdy limbs,
|
|
784
|
+
* oversized cartoon boots, and gloves in T-Pose or A-Pose.
|
|
785
|
+
*/
|
|
786
|
+
spawnChibiBaseMesh(options = {}) {
|
|
787
|
+
const rootName = options.name || 'Chibi_BaseMesh';
|
|
788
|
+
const parts = [];
|
|
789
|
+
const scale = options.scale || 1.0;
|
|
790
|
+
const pose = options.pose || 't_pose';
|
|
791
|
+
const materialStyle = options.materialStyle || 'colored';
|
|
792
|
+
// Materials
|
|
793
|
+
let skinMat = null;
|
|
794
|
+
let shortsMat = null;
|
|
795
|
+
let glovesMat = null;
|
|
796
|
+
let bootsMat = null;
|
|
797
|
+
if (materialStyle === 'colored') {
|
|
798
|
+
const pbrSkin = new PBRMaterial('pbr_chibi_skin', this.scene);
|
|
799
|
+
pbrSkin.albedoColor = Color3.FromHexString('#f5c59e');
|
|
800
|
+
pbrSkin.roughness = 0.6;
|
|
801
|
+
pbrSkin.metallic = 0.0;
|
|
802
|
+
pbrSkin.backFaceCulling = false;
|
|
803
|
+
skinMat = pbrSkin;
|
|
804
|
+
const pbrShorts = new PBRMaterial('pbr_chibi_shorts', this.scene);
|
|
805
|
+
pbrShorts.albedoColor = Color3.FromHexString('#0033cc');
|
|
806
|
+
pbrShorts.roughness = 0.45;
|
|
807
|
+
pbrShorts.metallic = 0.05;
|
|
808
|
+
pbrShorts.backFaceCulling = false;
|
|
809
|
+
shortsMat = pbrShorts;
|
|
810
|
+
const pbrGloves = new PBRMaterial('pbr_chibi_gloves', this.scene);
|
|
811
|
+
pbrGloves.albedoColor = Color3.FromHexString('#f5f5f5');
|
|
812
|
+
pbrGloves.roughness = 0.4;
|
|
813
|
+
pbrGloves.metallic = 0.0;
|
|
814
|
+
pbrGloves.backFaceCulling = false;
|
|
815
|
+
glovesMat = pbrGloves;
|
|
816
|
+
const pbrBoots = new PBRMaterial('pbr_chibi_boots', this.scene);
|
|
817
|
+
pbrBoots.albedoColor = Color3.FromHexString('#5a2a18');
|
|
818
|
+
pbrBoots.roughness = 0.7;
|
|
819
|
+
pbrBoots.metallic = 0.0;
|
|
820
|
+
pbrBoots.backFaceCulling = false;
|
|
821
|
+
bootsMat = pbrBoots;
|
|
822
|
+
}
|
|
823
|
+
// 1. Head (Height 0.38m, Width 0.42m, Depth 0.40m, center Y = 0.98m)
|
|
824
|
+
const head = MeshBuilder.CreateSphere('c_head', { diameterX: 0.42, diameterY: 0.38, diameterZ: 0.40, segments: 16 }, this.scene);
|
|
825
|
+
head.position.set(0, 0.98, 0.02);
|
|
826
|
+
if (skinMat)
|
|
827
|
+
head.material = skinMat;
|
|
828
|
+
parts.push(head);
|
|
829
|
+
// 2. Bulbous Cartoon Nose
|
|
830
|
+
const nose = MeshBuilder.CreateSphere('c_nose', { diameterX: 0.16, diameterY: 0.13, diameterZ: 0.14, segments: 14 }, this.scene);
|
|
831
|
+
nose.position.set(0, 0.96, 0.22);
|
|
832
|
+
if (skinMat)
|
|
833
|
+
nose.material = skinMat;
|
|
834
|
+
parts.push(nose);
|
|
835
|
+
// 3. Ears (Left & Right)
|
|
836
|
+
for (const side of [-1, 1]) {
|
|
837
|
+
const ear = MeshBuilder.CreateSphere(`c_ear_${side === 1 ? 'r' : 'l'}`, { diameterX: 0.10, diameterY: 0.12, diameterZ: 0.08, segments: 12 }, this.scene);
|
|
838
|
+
ear.position.set(side * 0.22, 0.98, 0.02);
|
|
839
|
+
if (skinMat)
|
|
840
|
+
ear.material = skinMat;
|
|
841
|
+
parts.push(ear);
|
|
842
|
+
}
|
|
843
|
+
// 4. Neck (Short connection: Y = 0.81m)
|
|
844
|
+
const neck = MeshBuilder.CreateCylinder('c_neck', { height: 0.08, diameter: 0.18, tessellation: 14 }, this.scene);
|
|
845
|
+
neck.position.set(0, 0.81, 0.02);
|
|
846
|
+
if (skinMat)
|
|
847
|
+
neck.material = skinMat;
|
|
848
|
+
parts.push(neck);
|
|
849
|
+
// 5. Potbelly Torso
|
|
850
|
+
const isMannequin = options.style === 'mannequin';
|
|
851
|
+
if (isMannequin) {
|
|
852
|
+
const chest = MeshBuilder.CreateCylinder('c_chest', { height: 0.24, diameterTop: 0.36, diameterBottom: 0.44, tessellation: 16 }, this.scene);
|
|
853
|
+
chest.position.set(0, 0.69, 0.03);
|
|
854
|
+
chest.scaling.set(1.0, 1.0, 0.95);
|
|
855
|
+
if (skinMat)
|
|
856
|
+
chest.material = skinMat;
|
|
857
|
+
parts.push(chest);
|
|
858
|
+
const pelvis = MeshBuilder.CreateCylinder('c_pelvis', { height: 0.20, diameterTop: 0.43, diameterBottom: 0.36, tessellation: 16 }, this.scene);
|
|
859
|
+
pelvis.position.set(0, 0.50, 0);
|
|
860
|
+
pelvis.scaling.set(1.0, 1.0, 0.92);
|
|
861
|
+
if (shortsMat)
|
|
862
|
+
pelvis.material = shortsMat;
|
|
863
|
+
parts.push(pelvis);
|
|
864
|
+
}
|
|
865
|
+
else {
|
|
866
|
+
// Organic Torso (Smooth continuous capsule eliminating horizontal seam)
|
|
867
|
+
const torso = MeshBuilder.CreateCapsule('c_torso', { height: 0.44, radius: 0.20, tessellation: 20, subdivisions: 4 }, this.scene);
|
|
868
|
+
torso.position.set(0, 0.58, 0.02);
|
|
869
|
+
torso.scaling.set(1.05, 1.0, 0.95);
|
|
870
|
+
if (shortsMat)
|
|
871
|
+
torso.material = shortsMat;
|
|
872
|
+
parts.push(torso);
|
|
873
|
+
const chest = MeshBuilder.CreateSphere('c_chest', { diameterX: 0.38, diameterY: 0.30, diameterZ: 0.36, segments: 16 }, this.scene);
|
|
874
|
+
chest.position.set(0, 0.74, 0.02);
|
|
875
|
+
if (skinMat)
|
|
876
|
+
chest.material = skinMat;
|
|
877
|
+
parts.push(chest);
|
|
878
|
+
}
|
|
879
|
+
// 7. Arms (T-Pose or A-Pose)
|
|
880
|
+
for (const side of [-1, 1]) {
|
|
881
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
882
|
+
const shoulderX = side * 0.22;
|
|
883
|
+
const armY = 0.74;
|
|
884
|
+
if (!isMannequin) {
|
|
885
|
+
// Organic continuous curved tube arms without joint ball seams
|
|
886
|
+
const armPath = pose === 't_pose' ? [
|
|
887
|
+
new Vector3(side * 0.12, armY, 0),
|
|
888
|
+
new Vector3(side * 0.36, armY, 0),
|
|
889
|
+
new Vector3(side * 0.54, armY, 0),
|
|
890
|
+
] : [
|
|
891
|
+
new Vector3(side * 0.12, armY, 0),
|
|
892
|
+
new Vector3(side * 0.32, armY - 0.12, 0),
|
|
893
|
+
new Vector3(side * 0.42, armY - 0.28, 0),
|
|
894
|
+
];
|
|
895
|
+
const arm = MeshBuilder.CreateTube(`c_arm_${sName}`, {
|
|
896
|
+
path: armPath,
|
|
897
|
+
radiusFunction: (i) => i === 0 ? 0.075 : (i === 1 ? 0.065 : 0.055),
|
|
898
|
+
tessellation: 16,
|
|
899
|
+
cap: Mesh.CAP_ALL,
|
|
900
|
+
}, this.scene);
|
|
901
|
+
if (skinMat)
|
|
902
|
+
arm.material = skinMat;
|
|
903
|
+
parts.push(arm);
|
|
904
|
+
// Glove cuff
|
|
905
|
+
const cuff = MeshBuilder.CreateCylinder(`c_cuff_${sName}`, { height: 0.05, diameter: 0.13, tessellation: 14 }, this.scene);
|
|
906
|
+
cuff.position.copyFrom(armPath[2]);
|
|
907
|
+
if (pose === 't_pose')
|
|
908
|
+
cuff.rotation.z = side * (Math.PI / 2);
|
|
909
|
+
if (glovesMat)
|
|
910
|
+
cuff.material = glovesMat;
|
|
911
|
+
parts.push(cuff);
|
|
912
|
+
// Cartoon Glove
|
|
913
|
+
const glove = MeshBuilder.CreateSphere(`c_glove_${sName}`, { diameterX: 0.16, diameterY: 0.14, diameterZ: 0.14, segments: 14 }, this.scene);
|
|
914
|
+
const glovePos = pose === 't_pose'
|
|
915
|
+
? new Vector3(side * 0.64, armY, 0)
|
|
916
|
+
: new Vector3(side * 0.48, armY - 0.38, 0);
|
|
917
|
+
glove.position.copyFrom(glovePos);
|
|
918
|
+
if (glovesMat)
|
|
919
|
+
glove.material = glovesMat;
|
|
920
|
+
parts.push(glove);
|
|
921
|
+
}
|
|
922
|
+
else {
|
|
923
|
+
// Mannequin separate cylinders and spheres
|
|
924
|
+
const shoulder = MeshBuilder.CreateSphere(`c_shoulder_${sName}`, { diameter: 0.12, segments: 12 }, this.scene);
|
|
925
|
+
shoulder.position.set(shoulderX, armY, 0);
|
|
926
|
+
if (skinMat)
|
|
927
|
+
shoulder.material = skinMat;
|
|
928
|
+
parts.push(shoulder);
|
|
929
|
+
if (pose === 't_pose') {
|
|
930
|
+
const upperArm = MeshBuilder.CreateCylinder(`c_uparm_${sName}`, { height: 0.16, diameter: 0.11, tessellation: 12 }, this.scene);
|
|
931
|
+
upperArm.position.set(side * 0.31, armY, 0);
|
|
932
|
+
upperArm.rotation.z = side * (Math.PI / 2);
|
|
933
|
+
if (skinMat)
|
|
934
|
+
upperArm.material = skinMat;
|
|
935
|
+
parts.push(upperArm);
|
|
936
|
+
const elbow = MeshBuilder.CreateSphere(`c_elbow_${sName}`, { diameter: 0.10, segments: 12 }, this.scene);
|
|
937
|
+
elbow.position.set(side * 0.40, armY, 0);
|
|
938
|
+
if (skinMat)
|
|
939
|
+
elbow.material = skinMat;
|
|
940
|
+
parts.push(elbow);
|
|
941
|
+
const forearm = MeshBuilder.CreateCylinder(`c_forearm_${sName}`, { height: 0.16, diameter: 0.10, tessellation: 12 }, this.scene);
|
|
942
|
+
forearm.position.set(side * 0.49, armY, 0);
|
|
943
|
+
forearm.rotation.z = side * (Math.PI / 2);
|
|
944
|
+
if (skinMat)
|
|
945
|
+
forearm.material = skinMat;
|
|
946
|
+
parts.push(forearm);
|
|
947
|
+
const cuff = MeshBuilder.CreateCylinder(`c_cuff_${sName}`, { height: 0.05, diameter: 0.14, tessellation: 14 }, this.scene);
|
|
948
|
+
cuff.position.set(side * 0.57, armY, 0);
|
|
949
|
+
cuff.rotation.z = side * (Math.PI / 2);
|
|
950
|
+
if (glovesMat)
|
|
951
|
+
cuff.material = glovesMat;
|
|
952
|
+
parts.push(cuff);
|
|
953
|
+
const glove = MeshBuilder.CreateSphere(`c_glove_${sName}`, { diameterX: 0.16, diameterY: 0.15, diameterZ: 0.14, segments: 14 }, this.scene);
|
|
954
|
+
glove.position.set(side * 0.65, armY, 0);
|
|
955
|
+
if (glovesMat)
|
|
956
|
+
glove.material = glovesMat;
|
|
957
|
+
parts.push(glove);
|
|
958
|
+
}
|
|
959
|
+
else {
|
|
960
|
+
const upperArm = MeshBuilder.CreateCylinder(`c_uparm_${sName}`, { height: 0.18, diameter: 0.11, tessellation: 12 }, this.scene);
|
|
961
|
+
upperArm.position.set(side * 0.28, armY - 0.07, 0);
|
|
962
|
+
upperArm.rotation.z = side * -0.45;
|
|
963
|
+
if (skinMat)
|
|
964
|
+
upperArm.material = skinMat;
|
|
965
|
+
parts.push(upperArm);
|
|
966
|
+
const elbow = MeshBuilder.CreateSphere(`c_elbow_${sName}`, { diameter: 0.10, segments: 12 }, this.scene);
|
|
967
|
+
elbow.position.set(side * 0.34, armY - 0.16, 0);
|
|
968
|
+
if (skinMat)
|
|
969
|
+
elbow.material = skinMat;
|
|
970
|
+
parts.push(elbow);
|
|
971
|
+
const forearm = MeshBuilder.CreateCylinder(`c_forearm_${sName}`, { height: 0.18, diameter: 0.10, tessellation: 12 }, this.scene);
|
|
972
|
+
forearm.position.set(side * 0.38, armY - 0.25, 0);
|
|
973
|
+
forearm.rotation.z = side * -0.35;
|
|
974
|
+
if (skinMat)
|
|
975
|
+
forearm.material = skinMat;
|
|
976
|
+
parts.push(forearm);
|
|
977
|
+
const glove = MeshBuilder.CreateSphere(`c_glove_${sName}`, { diameter: 0.15, segments: 14 }, this.scene);
|
|
978
|
+
glove.position.set(side * 0.42, armY - 0.37, 0);
|
|
979
|
+
if (glovesMat)
|
|
980
|
+
glove.material = glovesMat;
|
|
981
|
+
parts.push(glove);
|
|
982
|
+
}
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
// 8. Legs (Left & Right)
|
|
986
|
+
for (const side of [-1, 1]) {
|
|
987
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
988
|
+
const legX = side * 0.1;
|
|
989
|
+
if (!isMannequin) {
|
|
990
|
+
// Organic continuous curved tube legs
|
|
991
|
+
const legPath = [
|
|
992
|
+
new Vector3(legX, 0.52, 0),
|
|
993
|
+
new Vector3(legX, 0.28, 0.015),
|
|
994
|
+
new Vector3(legX, 0.14, 0),
|
|
995
|
+
];
|
|
996
|
+
const leg = MeshBuilder.CreateTube(`c_leg_${sName}`, {
|
|
997
|
+
path: legPath,
|
|
998
|
+
radiusFunction: (i) => i === 0 ? 0.09 : (i === 1 ? 0.08 : 0.07),
|
|
999
|
+
tessellation: 16,
|
|
1000
|
+
cap: Mesh.CAP_ALL,
|
|
1001
|
+
}, this.scene);
|
|
1002
|
+
if (shortsMat)
|
|
1003
|
+
leg.material = shortsMat;
|
|
1004
|
+
parts.push(leg);
|
|
1005
|
+
const boot = MeshBuilder.CreateSphere(`c_boot_${sName}`, { diameterX: 0.18, diameterY: 0.14, diameterZ: 0.26, segments: 14 }, this.scene);
|
|
1006
|
+
boot.position.set(legX, 0.07, 0.04);
|
|
1007
|
+
if (bootsMat)
|
|
1008
|
+
boot.material = bootsMat;
|
|
1009
|
+
parts.push(boot);
|
|
1010
|
+
}
|
|
1011
|
+
else {
|
|
1012
|
+
// Mannequin separate cylinders and spheres
|
|
1013
|
+
const thigh = MeshBuilder.CreateCylinder(`c_thigh_${sName}`, { height: 0.16, diameter: 0.15, tessellation: 14 }, this.scene);
|
|
1014
|
+
thigh.position.set(legX, 0.35, 0);
|
|
1015
|
+
if (shortsMat)
|
|
1016
|
+
thigh.material = shortsMat;
|
|
1017
|
+
parts.push(thigh);
|
|
1018
|
+
const knee = MeshBuilder.CreateSphere(`c_knee_${sName}`, { diameter: 0.13, segments: 12 }, this.scene);
|
|
1019
|
+
knee.position.set(legX, 0.25, 0.02);
|
|
1020
|
+
if (skinMat)
|
|
1021
|
+
knee.material = skinMat;
|
|
1022
|
+
parts.push(knee);
|
|
1023
|
+
const calf = MeshBuilder.CreateCylinder(`c_calf_${sName}`, { height: 0.14, diameter: 0.13, tessellation: 14 }, this.scene);
|
|
1024
|
+
calf.position.set(legX, 0.16, 0);
|
|
1025
|
+
if (skinMat)
|
|
1026
|
+
calf.material = skinMat;
|
|
1027
|
+
parts.push(calf);
|
|
1028
|
+
const boot = MeshBuilder.CreateBox(`c_boot_${sName}`, { width: 0.17, height: 0.13, depth: 0.26 }, this.scene);
|
|
1029
|
+
boot.position.set(legX, 0.065, 0.04);
|
|
1030
|
+
if (bootsMat)
|
|
1031
|
+
boot.material = bootsMat;
|
|
1032
|
+
parts.push(boot);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
// Merge meshes preserving MultiMaterial if colored
|
|
1036
|
+
const baseMesh = Mesh.MergeMeshes(parts, true, true, undefined, true, materialStyle === 'colored');
|
|
1037
|
+
if (!baseMesh) {
|
|
1038
|
+
throw new Error('Failed to merge chibi cartoon base mesh components.');
|
|
1039
|
+
}
|
|
1040
|
+
baseMesh.name = rootName;
|
|
1041
|
+
baseMesh.id = `mesh_${Date.now()}_chibi`;
|
|
1042
|
+
if (scale !== 1.0) {
|
|
1043
|
+
baseMesh.scaling.set(scale, scale, scale);
|
|
1044
|
+
}
|
|
1045
|
+
if (options.position) {
|
|
1046
|
+
baseMesh.position.copyFrom(options.position);
|
|
1047
|
+
}
|
|
1048
|
+
if (materialStyle === 'clay' || !baseMesh.material) {
|
|
1049
|
+
this.applyDefaultCharacterMaterial(baseMesh);
|
|
1050
|
+
}
|
|
1051
|
+
this.service.addMesh(baseMesh);
|
|
1052
|
+
this.service.selectMesh(baseMesh);
|
|
1053
|
+
return baseMesh;
|
|
1054
|
+
}
|
|
1055
|
+
/**
|
|
1056
|
+
* Spawns an Organic Stylized Creature / Chibi Demon Base Mesh.
|
|
1057
|
+
* Features a seamless pear-shaped body (head + torso), continuous curved tubular arms with claws,
|
|
1058
|
+
* digitigrade curved legs with paws, curved horns, bat wings, halo, protruding cartoon eyes, and pitchfork.
|
|
1059
|
+
* Designed for game-ready soft organic contours.
|
|
1060
|
+
*/
|
|
1061
|
+
spawnOrganicCreatureBaseMesh(options = {}) {
|
|
1062
|
+
const rootName = options.name || 'Organic_Demon_BaseMesh';
|
|
1063
|
+
const parts = [];
|
|
1064
|
+
const scale = options.scale || 1.0;
|
|
1065
|
+
const materialStyle = options.materialStyle || 'colored';
|
|
1066
|
+
let skinMat = null;
|
|
1067
|
+
let darkMat = null;
|
|
1068
|
+
let eyeMat = null;
|
|
1069
|
+
let pupilMat = null;
|
|
1070
|
+
let whiteMat = null;
|
|
1071
|
+
if (materialStyle === 'colored') {
|
|
1072
|
+
const pbrSkin = new PBRMaterial('pbr_creature_skin', this.scene);
|
|
1073
|
+
pbrSkin.albedoColor = Color3.FromHexString('#c9182b');
|
|
1074
|
+
pbrSkin.roughness = 0.38;
|
|
1075
|
+
pbrSkin.metallic = 0.02;
|
|
1076
|
+
pbrSkin.backFaceCulling = false;
|
|
1077
|
+
skinMat = pbrSkin;
|
|
1078
|
+
const pbrDark = new PBRMaterial('pbr_creature_dark', this.scene);
|
|
1079
|
+
pbrDark.albedoColor = Color3.FromHexString('#202024');
|
|
1080
|
+
pbrDark.roughness = 0.45;
|
|
1081
|
+
pbrDark.metallic = 0.1;
|
|
1082
|
+
pbrDark.backFaceCulling = false;
|
|
1083
|
+
darkMat = pbrDark;
|
|
1084
|
+
const pbrEye = new PBRMaterial('pbr_creature_eye', this.scene);
|
|
1085
|
+
pbrEye.albedoColor = Color3.FromHexString('#f0f0f5');
|
|
1086
|
+
pbrEye.roughness = 0.25;
|
|
1087
|
+
pbrEye.metallic = 0.0;
|
|
1088
|
+
eyeMat = pbrEye;
|
|
1089
|
+
const pbrPupil = new PBRMaterial('pbr_creature_pupil', this.scene);
|
|
1090
|
+
pbrPupil.albedoColor = Color3.FromHexString('#111114');
|
|
1091
|
+
pbrPupil.roughness = 0.5;
|
|
1092
|
+
pbrPupil.metallic = 0.0;
|
|
1093
|
+
pupilMat = pbrPupil;
|
|
1094
|
+
const pbrWhite = new PBRMaterial('pbr_creature_white', this.scene);
|
|
1095
|
+
pbrWhite.albedoColor = Color3.FromHexString('#ffffff');
|
|
1096
|
+
pbrWhite.roughness = 0.3;
|
|
1097
|
+
pbrWhite.metallic = 0.0;
|
|
1098
|
+
whiteMat = pbrWhite;
|
|
1099
|
+
}
|
|
1100
|
+
// 1. Organic Pear Body (Head + Torso united in one flowing organic volume)
|
|
1101
|
+
const body = MeshBuilder.CreateSphere('cr_body', { diameterX: 0.42, diameterY: 0.54, diameterZ: 0.40, segments: 24 }, this.scene);
|
|
1102
|
+
body.position.set(0, 0.64, 0);
|
|
1103
|
+
body.scaling.set(1.08, 1.25, 0.98);
|
|
1104
|
+
if (skinMat)
|
|
1105
|
+
body.material = skinMat;
|
|
1106
|
+
parts.push(body);
|
|
1107
|
+
// 2. Arms (Continuous curved tubes flowing seamlessly down and forward)
|
|
1108
|
+
for (const side of [-1, 1]) {
|
|
1109
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
1110
|
+
const armPath = [
|
|
1111
|
+
new Vector3(side * 0.18, 0.65, 0.02),
|
|
1112
|
+
new Vector3(side * 0.32, 0.50, 0.07),
|
|
1113
|
+
new Vector3(side * 0.42, 0.35, 0.13),
|
|
1114
|
+
];
|
|
1115
|
+
const arm = MeshBuilder.CreateTube(`cr_arm_${sName}`, {
|
|
1116
|
+
path: armPath,
|
|
1117
|
+
radiusFunction: (i) => i === 0 ? 0.07 : (i === 1 ? 0.055 : 0.045),
|
|
1118
|
+
tessellation: 16,
|
|
1119
|
+
cap: Mesh.CAP_ALL,
|
|
1120
|
+
}, this.scene);
|
|
1121
|
+
if (skinMat)
|
|
1122
|
+
arm.material = skinMat;
|
|
1123
|
+
parts.push(arm);
|
|
1124
|
+
// Hand & Claws
|
|
1125
|
+
const hand = MeshBuilder.CreateSphere(`cr_hand_${sName}`, { diameterX: 0.11, diameterY: 0.13, diameterZ: 0.11, segments: 14 }, this.scene);
|
|
1126
|
+
hand.position.set(side * 0.44, 0.28, 0.16);
|
|
1127
|
+
if (skinMat)
|
|
1128
|
+
hand.material = skinMat;
|
|
1129
|
+
parts.push(hand);
|
|
1130
|
+
// Claws / Fingers
|
|
1131
|
+
for (let f = 0; f < 2; f++) {
|
|
1132
|
+
const claw = MeshBuilder.CreateCylinder(`cr_claw_${sName}_${f}`, { height: 0.06, diameterTop: 0.005, diameterBottom: 0.028, tessellation: 8 }, this.scene);
|
|
1133
|
+
claw.position.set(side * (0.42 + f * 0.04), 0.22, 0.18 + f * 0.01);
|
|
1134
|
+
claw.rotation.x = Math.PI / 4;
|
|
1135
|
+
claw.rotation.z = side * 0.2;
|
|
1136
|
+
if (skinMat)
|
|
1137
|
+
claw.material = skinMat;
|
|
1138
|
+
parts.push(claw);
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
1141
|
+
// 3. Legs (Continuous digitigrade curved tubes with backward knee bend)
|
|
1142
|
+
for (const side of [-1, 1]) {
|
|
1143
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
1144
|
+
const legPath = [
|
|
1145
|
+
new Vector3(side * 0.13, 0.38, 0.01),
|
|
1146
|
+
new Vector3(side * 0.15, 0.23, -0.05),
|
|
1147
|
+
new Vector3(side * 0.13, 0.10, 0.03),
|
|
1148
|
+
];
|
|
1149
|
+
const leg = MeshBuilder.CreateTube(`cr_leg_${sName}`, {
|
|
1150
|
+
path: legPath,
|
|
1151
|
+
radiusFunction: (i) => i === 0 ? 0.075 : (i === 1 ? 0.06 : 0.045),
|
|
1152
|
+
tessellation: 16,
|
|
1153
|
+
cap: Mesh.CAP_ALL,
|
|
1154
|
+
}, this.scene);
|
|
1155
|
+
if (skinMat)
|
|
1156
|
+
leg.material = skinMat;
|
|
1157
|
+
parts.push(leg);
|
|
1158
|
+
// Foot with toes
|
|
1159
|
+
const foot = MeshBuilder.CreateBox(`cr_foot_${sName}`, { width: 0.10, height: 0.045, depth: 0.15 }, this.scene);
|
|
1160
|
+
foot.position.set(side * 0.13, 0.023, 0.07);
|
|
1161
|
+
if (skinMat)
|
|
1162
|
+
foot.material = skinMat;
|
|
1163
|
+
parts.push(foot);
|
|
1164
|
+
}
|
|
1165
|
+
// 4. Stylized Curved Horns
|
|
1166
|
+
for (const side of [-1, 1]) {
|
|
1167
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
1168
|
+
const hornPath = [
|
|
1169
|
+
new Vector3(side * 0.12, 0.88, 0.04),
|
|
1170
|
+
new Vector3(side * 0.18, 0.98, 0.02),
|
|
1171
|
+
new Vector3(side * 0.20, 1.08, -0.05),
|
|
1172
|
+
];
|
|
1173
|
+
const horn = MeshBuilder.CreateTube(`cr_horn_${sName}`, {
|
|
1174
|
+
path: hornPath,
|
|
1175
|
+
radiusFunction: (i) => i === 0 ? 0.048 : (i === 1 ? 0.032 : 0.005),
|
|
1176
|
+
tessellation: 14,
|
|
1177
|
+
cap: Mesh.CAP_ALL,
|
|
1178
|
+
}, this.scene);
|
|
1179
|
+
if (darkMat)
|
|
1180
|
+
horn.material = darkMat;
|
|
1181
|
+
parts.push(horn);
|
|
1182
|
+
}
|
|
1183
|
+
// 5. Stylized Protruding Cartoon Eyes with Pupils
|
|
1184
|
+
for (const side of [-1, 1]) {
|
|
1185
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
1186
|
+
const eye = MeshBuilder.CreateSphere(`cr_eye_${sName}`, { diameterX: 0.13, diameterY: 0.12, diameterZ: 0.12, segments: 14 }, this.scene);
|
|
1187
|
+
eye.position.set(side * 0.13, 0.73, 0.17);
|
|
1188
|
+
if (eyeMat)
|
|
1189
|
+
eye.material = eyeMat;
|
|
1190
|
+
parts.push(eye);
|
|
1191
|
+
// Pupil disc (horizontal slit cartoon look)
|
|
1192
|
+
const pupil = MeshBuilder.CreateBox(`cr_pupil_${sName}`, { width: 0.07, height: 0.03, depth: 0.02 }, this.scene);
|
|
1193
|
+
pupil.position.set(side * 0.13, 0.73, 0.23);
|
|
1194
|
+
if (pupilMat)
|
|
1195
|
+
pupil.material = pupilMat;
|
|
1196
|
+
parts.push(pupil);
|
|
1197
|
+
}
|
|
1198
|
+
// 6. Inset Smiling Mouth with 2 Cute White Fangs
|
|
1199
|
+
const mouth = MeshBuilder.CreateTorus('cr_mouth', { diameter: 0.14, thickness: 0.022, tessellation: 16 }, this.scene);
|
|
1200
|
+
mouth.position.set(0, 0.60, 0.20);
|
|
1201
|
+
mouth.scaling.set(1.4, 0.45, 0.8);
|
|
1202
|
+
if (darkMat)
|
|
1203
|
+
mouth.material = darkMat;
|
|
1204
|
+
parts.push(mouth);
|
|
1205
|
+
for (const fangSide of [-1, 1]) {
|
|
1206
|
+
const fang = MeshBuilder.CreateCylinder(`cr_fang_${fangSide === 1 ? 'r' : 'l'}`, { height: 0.055, diameterTop: 0.005, diameterBottom: 0.025, tessellation: 8 }, this.scene);
|
|
1207
|
+
fang.position.set(fangSide * 0.045, 0.57, 0.21);
|
|
1208
|
+
fang.rotation.x = Math.PI;
|
|
1209
|
+
if (whiteMat)
|
|
1210
|
+
fang.material = whiteMat;
|
|
1211
|
+
parts.push(fang);
|
|
1212
|
+
}
|
|
1213
|
+
// 7. Bat Wings on the back
|
|
1214
|
+
for (const side of [-1, 1]) {
|
|
1215
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
1216
|
+
const wingArm = MeshBuilder.CreateTube(`cr_wingarm_${sName}`, {
|
|
1217
|
+
path: [
|
|
1218
|
+
new Vector3(side * 0.08, 0.70, -0.16),
|
|
1219
|
+
new Vector3(side * 0.22, 0.78, -0.22),
|
|
1220
|
+
new Vector3(side * 0.30, 0.72, -0.25),
|
|
1221
|
+
],
|
|
1222
|
+
radius: 0.016,
|
|
1223
|
+
tessellation: 10,
|
|
1224
|
+
cap: Mesh.CAP_ALL,
|
|
1225
|
+
}, this.scene);
|
|
1226
|
+
if (skinMat)
|
|
1227
|
+
wingArm.material = skinMat;
|
|
1228
|
+
parts.push(wingArm);
|
|
1229
|
+
const wingMembrane = MeshBuilder.CreatePlane(`cr_wingmem_${sName}`, { width: 0.18, height: 0.14 }, this.scene);
|
|
1230
|
+
wingMembrane.position.set(side * 0.20, 0.70, -0.22);
|
|
1231
|
+
wingMembrane.rotation.y = side * 0.3;
|
|
1232
|
+
wingMembrane.rotation.z = side * -0.2;
|
|
1233
|
+
if (skinMat)
|
|
1234
|
+
wingMembrane.material = skinMat;
|
|
1235
|
+
parts.push(wingMembrane);
|
|
1236
|
+
}
|
|
1237
|
+
// 8. Floating Dark Halo
|
|
1238
|
+
const halo = MeshBuilder.CreateTorus('cr_halo', { diameter: 0.24, thickness: 0.02, tessellation: 24 }, this.scene);
|
|
1239
|
+
halo.position.set(0, 1.10, -0.02);
|
|
1240
|
+
halo.rotation.x = 0.25;
|
|
1241
|
+
if (darkMat)
|
|
1242
|
+
halo.material = darkMat;
|
|
1243
|
+
parts.push(halo);
|
|
1244
|
+
// 9. Trident / Pitchfork held in Left Hand
|
|
1245
|
+
const staff = MeshBuilder.CreateCylinder('cr_trident_staff', { height: 0.55, diameter: 0.018, tessellation: 10 }, this.scene);
|
|
1246
|
+
staff.position.set(0.38, 0.26, 0.24);
|
|
1247
|
+
staff.rotation.x = Math.PI / 3;
|
|
1248
|
+
staff.rotation.z = -Math.PI / 6;
|
|
1249
|
+
if (darkMat)
|
|
1250
|
+
staff.material = darkMat;
|
|
1251
|
+
parts.push(staff);
|
|
1252
|
+
// Merge meshes preserving MultiMaterial if colored
|
|
1253
|
+
const baseMesh = Mesh.MergeMeshes(parts, true, true, undefined, true, materialStyle === 'colored');
|
|
1254
|
+
if (!baseMesh) {
|
|
1255
|
+
throw new Error('Failed to merge organic creature base mesh components.');
|
|
1256
|
+
}
|
|
1257
|
+
baseMesh.name = rootName;
|
|
1258
|
+
baseMesh.id = `mesh_${Date.now()}_creature`;
|
|
1259
|
+
if (scale !== 1.0) {
|
|
1260
|
+
baseMesh.scaling.set(scale, scale, scale);
|
|
1261
|
+
}
|
|
1262
|
+
if (options.position) {
|
|
1263
|
+
baseMesh.position.copyFrom(options.position);
|
|
1264
|
+
}
|
|
1265
|
+
if (materialStyle === 'clay' || !baseMesh.material) {
|
|
1266
|
+
this.applyDefaultCharacterMaterial(baseMesh);
|
|
1267
|
+
}
|
|
1268
|
+
this.service.addMesh(baseMesh);
|
|
1269
|
+
this.service.selectMesh(baseMesh);
|
|
1270
|
+
return baseMesh;
|
|
1271
|
+
}
|
|
1272
|
+
/**
|
|
1273
|
+
* Spawns an Animal / Quadruped Base Mesh with realistic joint mechanics (Digitigrade or Unguligrade).
|
|
1274
|
+
*/
|
|
1275
|
+
spawnQuadrupedBaseMesh(options = {}) {
|
|
1276
|
+
const rootName = options.name || 'Quadruped_BaseMesh';
|
|
1277
|
+
const locomotion = options.locomotion || 'digitigrade';
|
|
1278
|
+
const parts = [];
|
|
1279
|
+
// 1. Ribcage Barrel (Brustkorb: Z-axis aligned)
|
|
1280
|
+
const ribcage = MeshBuilder.CreateCylinder('q_ribcage', { height: 0.60, diameterTop: 0.42, diameterBottom: 0.38, tessellation: 16 }, this.scene);
|
|
1281
|
+
ribcage.rotation.x = Math.PI / 2;
|
|
1282
|
+
ribcage.position.set(0, 0.75, 0.15);
|
|
1283
|
+
parts.push(ribcage);
|
|
1284
|
+
// 2. Hindquarters & Pelvis (Hüfte/Kruppe)
|
|
1285
|
+
const hindquarters = MeshBuilder.CreateCylinder('q_hind', { height: 0.45, diameterTop: 0.36, diameterBottom: 0.34, tessellation: 16 }, this.scene);
|
|
1286
|
+
hindquarters.rotation.x = Math.PI / 2;
|
|
1287
|
+
hindquarters.position.set(0, 0.72, -0.32);
|
|
1288
|
+
parts.push(hindquarters);
|
|
1289
|
+
// 3. Neck (Angled forward 45 deg)
|
|
1290
|
+
const neck = MeshBuilder.CreateCylinder('q_neck', { height: 0.35, diameterTop: 0.20, diameterBottom: 0.28, tessellation: 14 }, this.scene);
|
|
1291
|
+
neck.rotation.x = -Math.PI / 4;
|
|
1292
|
+
neck.position.set(0, 0.95, 0.48);
|
|
1293
|
+
parts.push(neck);
|
|
1294
|
+
// 4. Cranium & Snout (Kopf mit Schnauze)
|
|
1295
|
+
const cranium = MeshBuilder.CreateSphere('q_cranium', { diameterX: 0.22, diameterY: 0.20, diameterZ: 0.24, segments: 14 }, this.scene);
|
|
1296
|
+
cranium.position.set(0, 1.10, 0.65);
|
|
1297
|
+
parts.push(cranium);
|
|
1298
|
+
const snout = MeshBuilder.CreateBox('q_snout', { width: 0.13, height: 0.12, depth: 0.20 }, this.scene);
|
|
1299
|
+
snout.position.set(0, 1.05, 0.82);
|
|
1300
|
+
parts.push(snout);
|
|
1301
|
+
// 5. Forelegs (Front legs: Scapula, Arm, Forearm, Paw)
|
|
1302
|
+
for (const side of [-1, 1]) {
|
|
1303
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
1304
|
+
const fx = side * 0.18;
|
|
1305
|
+
const scapula = MeshBuilder.CreateCylinder(`q_scap_${sName}`, { height: 0.28, diameter: 0.14, tessellation: 12 }, this.scene);
|
|
1306
|
+
scapula.rotation.x = Math.PI / 6;
|
|
1307
|
+
scapula.position.set(fx, 0.65, 0.32);
|
|
1308
|
+
parts.push(scapula);
|
|
1309
|
+
const foreleg = MeshBuilder.CreateCylinder(`q_fleg_${sName}`, { height: 0.38, diameter: 0.08, tessellation: 12 }, this.scene);
|
|
1310
|
+
foreleg.position.set(fx, 0.32, 0.35);
|
|
1311
|
+
parts.push(foreleg);
|
|
1312
|
+
const paw = MeshBuilder.CreateBox(`q_fpaw_${sName}`, { width: 0.09, height: 0.06, depth: 0.12 }, this.scene);
|
|
1313
|
+
paw.position.set(fx, 0.03, 0.38);
|
|
1314
|
+
parts.push(paw);
|
|
1315
|
+
}
|
|
1316
|
+
// 6. Hind Legs (Back legs: Femur forward, Tibia back, Hock/Heel elevated)
|
|
1317
|
+
const hockHeight = locomotion === 'digitigrade' ? 0.26 : 0.40;
|
|
1318
|
+
for (const side of [-1, 1]) {
|
|
1319
|
+
const sName = side === 1 ? 'r' : 'l';
|
|
1320
|
+
const hx = side * 0.17;
|
|
1321
|
+
// Femur (Oberschenkel angled forward)
|
|
1322
|
+
const femur = MeshBuilder.CreateCylinder(`q_femur_${sName}`, { height: 0.35, diameter: 0.16, tessellation: 14 }, this.scene);
|
|
1323
|
+
femur.rotation.x = -Math.PI / 6;
|
|
1324
|
+
femur.position.set(hx, 0.58, -0.28);
|
|
1325
|
+
parts.push(femur);
|
|
1326
|
+
// Stifle / Knee
|
|
1327
|
+
const stifle = MeshBuilder.CreateSphere(`q_stifle_${sName}`, { diameter: 0.13, segments: 12 }, this.scene);
|
|
1328
|
+
stifle.position.set(hx, 0.44, -0.20);
|
|
1329
|
+
parts.push(stifle);
|
|
1330
|
+
// Tibia / Shin (Angled backward towards hock)
|
|
1331
|
+
const tibia = MeshBuilder.CreateCylinder(`q_tibia_${sName}`, { height: 0.32, diameter: 0.10, tessellation: 12 }, this.scene);
|
|
1332
|
+
tibia.rotation.x = Math.PI / 5;
|
|
1333
|
+
tibia.position.set(hx, (0.44 + hockHeight) / 2, -0.30);
|
|
1334
|
+
parts.push(tibia);
|
|
1335
|
+
// Hock (Sprunggelenk elevated off ground)
|
|
1336
|
+
const hock = MeshBuilder.CreateSphere(`q_hock_${sName}`, { diameter: 0.09, segments: 12 }, this.scene);
|
|
1337
|
+
hock.position.set(hx, hockHeight, -0.40);
|
|
1338
|
+
parts.push(hock);
|
|
1339
|
+
// Metatarsus / Paw
|
|
1340
|
+
const metatarsus = MeshBuilder.CreateCylinder(`q_meta_${sName}`, { height: hockHeight, diameter: 0.07, tessellation: 12 }, this.scene);
|
|
1341
|
+
metatarsus.position.set(hx, hockHeight / 2, -0.38);
|
|
1342
|
+
parts.push(metatarsus);
|
|
1343
|
+
const backPaw = MeshBuilder.CreateBox(`q_bpaw_${sName}`, { width: 0.09, height: 0.05, depth: 0.12 }, this.scene);
|
|
1344
|
+
backPaw.position.set(hx, 0.025, -0.36);
|
|
1345
|
+
parts.push(backPaw);
|
|
1346
|
+
}
|
|
1347
|
+
// 7. Tail
|
|
1348
|
+
const tail = MeshBuilder.CreateCylinder('q_tail', { height: 0.45, diameterTop: 0.08, diameterBottom: 0.04, tessellation: 10 }, this.scene);
|
|
1349
|
+
tail.rotation.x = -Math.PI / 3;
|
|
1350
|
+
tail.position.set(0, 0.65, -0.62);
|
|
1351
|
+
parts.push(tail);
|
|
1352
|
+
const baseMesh = Mesh.MergeMeshes(parts, true, true, undefined, false, true);
|
|
1353
|
+
if (!baseMesh) {
|
|
1354
|
+
throw new Error('Failed to merge quadruped base mesh components.');
|
|
1355
|
+
}
|
|
1356
|
+
baseMesh.name = rootName;
|
|
1357
|
+
baseMesh.id = `mesh_${Date.now()}_quadruped`;
|
|
1358
|
+
this.applyDefaultCharacterMaterial(baseMesh);
|
|
1359
|
+
this.service.addMesh(baseMesh);
|
|
1360
|
+
this.service.selectMesh(baseMesh);
|
|
1361
|
+
return baseMesh;
|
|
1362
|
+
}
|
|
1363
|
+
/**
|
|
1364
|
+
* Spawns a Head & Facial Bust Mannequin with eye sockets and jaw planes.
|
|
1365
|
+
*/
|
|
1366
|
+
spawnHeadBust(options = {}) {
|
|
1367
|
+
const rootName = options.name || 'HeadBust_BaseMesh';
|
|
1368
|
+
const parts = [];
|
|
1369
|
+
// Cranium
|
|
1370
|
+
const cranium = MeshBuilder.CreateSphere('hb_cranium', { diameterX: 0.40, diameterY: 0.48, diameterZ: 0.46, segments: 24 }, this.scene);
|
|
1371
|
+
cranium.position.set(0, 1.25, -0.02);
|
|
1372
|
+
parts.push(cranium);
|
|
1373
|
+
// Facial Wedge (Wangen & Kiefer)
|
|
1374
|
+
const jaw = MeshBuilder.CreateCylinder('hb_jaw', { height: 0.32, diameterTop: 0.38, diameterBottom: 0.22, tessellation: 18 }, this.scene);
|
|
1375
|
+
jaw.position.set(0, 1.05, 0.08);
|
|
1376
|
+
parts.push(jaw);
|
|
1377
|
+
// Nose Bridge
|
|
1378
|
+
const nose = MeshBuilder.CreateBox('hb_nose', { width: 0.08, height: 0.14, depth: 0.10 }, this.scene);
|
|
1379
|
+
nose.position.set(0, 1.22, 0.24);
|
|
1380
|
+
nose.rotation.x = 0.2;
|
|
1381
|
+
parts.push(nose);
|
|
1382
|
+
// Neck
|
|
1383
|
+
const neck = MeshBuilder.CreateCylinder('hb_neck', { height: 0.40, diameter: 0.24, tessellation: 18 }, this.scene);
|
|
1384
|
+
neck.position.set(0, 0.85, 0);
|
|
1385
|
+
parts.push(neck);
|
|
1386
|
+
// Pedestal Plate
|
|
1387
|
+
const base = MeshBuilder.CreateCylinder('hb_base', { height: 0.10, diameter: 0.55, tessellation: 24 }, this.scene);
|
|
1388
|
+
base.position.set(0, 0.60, 0);
|
|
1389
|
+
parts.push(base);
|
|
1390
|
+
const baseMesh = Mesh.MergeMeshes(parts, true, true, undefined, false, true);
|
|
1391
|
+
if (!baseMesh) {
|
|
1392
|
+
throw new Error('Failed to merge head bust base mesh.');
|
|
1393
|
+
}
|
|
1394
|
+
baseMesh.name = rootName;
|
|
1395
|
+
baseMesh.id = `mesh_${Date.now()}_bust`;
|
|
1396
|
+
this.applyDefaultCharacterMaterial(baseMesh);
|
|
1397
|
+
this.service.addMesh(baseMesh);
|
|
1398
|
+
this.service.selectMesh(baseMesh);
|
|
1399
|
+
return baseMesh;
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Spawns a dense, high-resolution digital clay sphere for freeform sculpting.
|
|
1403
|
+
*/
|
|
1404
|
+
spawnSculptSphere(options = {}) {
|
|
1405
|
+
const rootName = options.name || 'Sculpt_Sphere';
|
|
1406
|
+
const sphere = MeshBuilder.CreateSphere(rootName, { diameter: 1.5, segments: options.subdivisions || 36 }, this.scene);
|
|
1407
|
+
sphere.position.set(0, 0.75, 0);
|
|
1408
|
+
sphere.id = `mesh_${Date.now()}_sculpt_sphere`;
|
|
1409
|
+
this.applyDefaultCharacterMaterial(sphere);
|
|
1410
|
+
this.service.addMesh(sphere);
|
|
1411
|
+
this.service.selectMesh(sphere);
|
|
1412
|
+
return sphere;
|
|
1413
|
+
}
|
|
1414
|
+
applyDefaultCharacterMaterial(mesh) {
|
|
1415
|
+
const pbr = new PBRMaterial(`${mesh.name}_ClayMat`, this.scene);
|
|
1416
|
+
pbr.albedoColor = new Color3(0.82, 0.76, 0.70); // Sculptor clay tone
|
|
1417
|
+
pbr.roughness = 0.75;
|
|
1418
|
+
pbr.metallic = 0.05;
|
|
1419
|
+
pbr.backFaceCulling = false;
|
|
1420
|
+
mesh.material = pbr;
|
|
1421
|
+
}
|
|
1422
|
+
/* =========================================================================
|
|
1423
|
+
GAME-READY RETOPOLOGY & MESH SIMPLIFICATION
|
|
1424
|
+
========================================================================= */
|
|
1425
|
+
/**
|
|
1426
|
+
* Retopologizes a sculpted high-poly mesh to a target game polygon budget
|
|
1427
|
+
* using Quadric Error Metric decimation and normal smoothing.
|
|
1428
|
+
*/
|
|
1429
|
+
async retopologizeMesh(mesh, targetFaces) {
|
|
1430
|
+
const currentFaces = mesh.getTotalIndices() > 0 ? mesh.getTotalIndices() / 3 : 0;
|
|
1431
|
+
if (currentFaces <= 0) {
|
|
1432
|
+
throw new Error('Mesh has no triangles to retopologize.');
|
|
1433
|
+
}
|
|
1434
|
+
const preSnapshot = VertexData.ExtractFromMesh(mesh);
|
|
1435
|
+
const preSubMeshesData = mesh.subMeshes
|
|
1436
|
+
? mesh.subMeshes.map((sm) => ({
|
|
1437
|
+
materialIndex: sm.materialIndex,
|
|
1438
|
+
verticesStart: sm.verticesStart,
|
|
1439
|
+
verticesCount: sm.verticesCount,
|
|
1440
|
+
indexStart: sm.indexStart,
|
|
1441
|
+
indexCount: sm.indexCount,
|
|
1442
|
+
}))
|
|
1443
|
+
: null;
|
|
1444
|
+
const preMaterial = mesh.material;
|
|
1445
|
+
const hasSubMeshes = !!(mesh.subMeshes && mesh.subMeshes.length > 1);
|
|
1446
|
+
if (currentFaces > targetFaces) {
|
|
1447
|
+
if (hasSubMeshes) {
|
|
1448
|
+
// MultiMaterial Retopology: isolate each submesh so decimation never contracts
|
|
1449
|
+
// edges across separate parts (eliminating tears/holes) and never scrambles materials.
|
|
1450
|
+
const subMeshes = mesh.subMeshes.slice();
|
|
1451
|
+
const multiMat = mesh.material;
|
|
1452
|
+
const indices = mesh.getIndices();
|
|
1453
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
1454
|
+
const normals = mesh.getVerticesData(VertexBuffer.NormalKind);
|
|
1455
|
+
const uvs = mesh.getVerticesData(VertexBuffer.UVKind);
|
|
1456
|
+
const colors = mesh.getVerticesData(VertexBuffer.ColorKind);
|
|
1457
|
+
if (indices && positions) {
|
|
1458
|
+
const parts = [];
|
|
1459
|
+
const ratio = Math.max(0.05, Math.min(0.95, targetFaces / currentFaces));
|
|
1460
|
+
for (let i = 0; i < subMeshes.length; i++) {
|
|
1461
|
+
const sm = subMeshes[i];
|
|
1462
|
+
const smFaceCount = Math.floor(sm.indexCount / 3);
|
|
1463
|
+
const subIndices = indices.slice(sm.indexStart, sm.indexStart + sm.indexCount);
|
|
1464
|
+
const newPositions = [];
|
|
1465
|
+
const newNormals = [];
|
|
1466
|
+
const newUVs = [];
|
|
1467
|
+
const newColors = [];
|
|
1468
|
+
const newSubIndices = [];
|
|
1469
|
+
const map = new Map();
|
|
1470
|
+
for (let k = 0; k < subIndices.length; k++) {
|
|
1471
|
+
const oldIdx = subIndices[k];
|
|
1472
|
+
let newIdx = map.get(oldIdx);
|
|
1473
|
+
if (newIdx === undefined) {
|
|
1474
|
+
newIdx = newPositions.length / 3;
|
|
1475
|
+
map.set(oldIdx, newIdx);
|
|
1476
|
+
newPositions.push(positions[oldIdx * 3], positions[oldIdx * 3 + 1], positions[oldIdx * 3 + 2]);
|
|
1477
|
+
if (normals) {
|
|
1478
|
+
newNormals.push(normals[oldIdx * 3], normals[oldIdx * 3 + 1], normals[oldIdx * 3 + 2]);
|
|
1479
|
+
}
|
|
1480
|
+
if (uvs) {
|
|
1481
|
+
newUVs.push(uvs[oldIdx * 2], uvs[oldIdx * 2 + 1]);
|
|
1482
|
+
}
|
|
1483
|
+
if (colors) {
|
|
1484
|
+
newColors.push(colors[oldIdx * 4], colors[oldIdx * 4 + 1], colors[oldIdx * 4 + 2], colors[oldIdx * 4 + 3]);
|
|
1485
|
+
}
|
|
1486
|
+
}
|
|
1487
|
+
newSubIndices.push(newIdx);
|
|
1488
|
+
}
|
|
1489
|
+
const part = new Mesh(`${mesh.name}_sm_${i}`, this.scene);
|
|
1490
|
+
const vd = new VertexData();
|
|
1491
|
+
vd.positions = newPositions;
|
|
1492
|
+
vd.indices = newSubIndices;
|
|
1493
|
+
if (normals)
|
|
1494
|
+
vd.normals = newNormals;
|
|
1495
|
+
if (uvs)
|
|
1496
|
+
vd.uvs = newUVs;
|
|
1497
|
+
if (colors)
|
|
1498
|
+
vd.colors = newColors;
|
|
1499
|
+
vd.applyToMesh(part, true);
|
|
1500
|
+
const partMat = sm.getMaterial() ||
|
|
1501
|
+
multiMat?.subMaterials?.[sm.materialIndex] ||
|
|
1502
|
+
multiMat;
|
|
1503
|
+
part.material = partMat;
|
|
1504
|
+
// Only simplify dense submesh pieces (> 60 faces).
|
|
1505
|
+
// Delicate accent meshes (eyes, badge, mustache, drawstring) stay crisp & intact.
|
|
1506
|
+
if (smFaceCount > 60) {
|
|
1507
|
+
await ModelCompressor.simplifyMesh(part, ratio);
|
|
1508
|
+
}
|
|
1509
|
+
parts.push(part);
|
|
1510
|
+
}
|
|
1511
|
+
const merged = Mesh.MergeMeshes(parts, true, true, undefined, true, true);
|
|
1512
|
+
if (merged) {
|
|
1513
|
+
const vd = VertexData.ExtractFromMesh(merged);
|
|
1514
|
+
vd.applyToMesh(mesh, true);
|
|
1515
|
+
mesh.material = merged.material;
|
|
1516
|
+
if (merged.subMeshes) {
|
|
1517
|
+
mesh.subMeshes = [];
|
|
1518
|
+
for (const sm of merged.subMeshes) {
|
|
1519
|
+
new SubMesh(sm.materialIndex, sm.verticesStart, sm.verticesCount, sm.indexStart, sm.indexCount, mesh);
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
merged.dispose();
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
else {
|
|
1527
|
+
const ratio = targetFaces / currentFaces;
|
|
1528
|
+
await ModelCompressor.simplifyMesh(mesh, ratio);
|
|
1529
|
+
}
|
|
1530
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
1531
|
+
const indices = mesh.getIndices();
|
|
1532
|
+
if (positions && indices) {
|
|
1533
|
+
const normals = [];
|
|
1534
|
+
VertexData.ComputeNormals(positions, indices, normals);
|
|
1535
|
+
mesh.setVerticesData(VertexBuffer.NormalKind, normals, true);
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
else {
|
|
1539
|
+
// Mesh is already within or below polygon target budget:
|
|
1540
|
+
// recalculate smooth normals in-place WITHOUT destroying submeshes and MultiMaterial!
|
|
1541
|
+
const positions = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
1542
|
+
const indices = mesh.getIndices();
|
|
1543
|
+
if (positions && indices) {
|
|
1544
|
+
const normals = [];
|
|
1545
|
+
VertexData.ComputeNormals(positions, indices, normals);
|
|
1546
|
+
mesh.setVerticesData(VertexBuffer.NormalKind, normals, true);
|
|
1547
|
+
}
|
|
1548
|
+
}
|
|
1549
|
+
mesh.refreshBoundingInfo();
|
|
1550
|
+
const postSnapshot = VertexData.ExtractFromMesh(mesh);
|
|
1551
|
+
const postSubMeshesData = mesh.subMeshes
|
|
1552
|
+
? mesh.subMeshes.map((sm) => ({
|
|
1553
|
+
materialIndex: sm.materialIndex,
|
|
1554
|
+
verticesStart: sm.verticesStart,
|
|
1555
|
+
verticesCount: sm.verticesCount,
|
|
1556
|
+
indexStart: sm.indexStart,
|
|
1557
|
+
indexCount: sm.indexCount,
|
|
1558
|
+
}))
|
|
1559
|
+
: null;
|
|
1560
|
+
const postMaterial = mesh.material;
|
|
1561
|
+
this.service.recordAction({
|
|
1562
|
+
name: `Retopologize (${Math.round(currentFaces)} to ${Math.round(mesh.getTotalIndices() / 3)} faces)`,
|
|
1563
|
+
undo: () => {
|
|
1564
|
+
if (!mesh.isDisposed()) {
|
|
1565
|
+
preSnapshot.applyToMesh(mesh, true);
|
|
1566
|
+
mesh.material = preMaterial;
|
|
1567
|
+
if (preSubMeshesData) {
|
|
1568
|
+
mesh.subMeshes = [];
|
|
1569
|
+
for (const sm of preSubMeshesData) {
|
|
1570
|
+
new SubMesh(sm.materialIndex, sm.verticesStart, sm.verticesCount, sm.indexStart, sm.indexCount, mesh);
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
mesh.refreshBoundingInfo();
|
|
1574
|
+
this.service.updateStats();
|
|
1575
|
+
this.onMeshModified?.(mesh);
|
|
1576
|
+
}
|
|
1577
|
+
},
|
|
1578
|
+
redo: () => {
|
|
1579
|
+
if (!mesh.isDisposed()) {
|
|
1580
|
+
postSnapshot.applyToMesh(mesh, true);
|
|
1581
|
+
mesh.material = postMaterial;
|
|
1582
|
+
if (postSubMeshesData) {
|
|
1583
|
+
mesh.subMeshes = [];
|
|
1584
|
+
for (const sm of postSubMeshesData) {
|
|
1585
|
+
new SubMesh(sm.materialIndex, sm.verticesStart, sm.verticesCount, sm.indexStart, sm.indexCount, mesh);
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
mesh.refreshBoundingInfo();
|
|
1589
|
+
this.service.updateStats();
|
|
1590
|
+
this.onMeshModified?.(mesh);
|
|
1591
|
+
}
|
|
1592
|
+
},
|
|
1593
|
+
});
|
|
1594
|
+
this.service.updateStats();
|
|
1595
|
+
this.onMeshModified?.(mesh);
|
|
1596
|
+
return this.analyzeTopology(mesh);
|
|
1597
|
+
}
|
|
1598
|
+
/**
|
|
1599
|
+
* Evaluates vertex counts, triangle counts, and game engine readiness classification.
|
|
1600
|
+
*/
|
|
1601
|
+
analyzeTopology(mesh) {
|
|
1602
|
+
const vertexCount = mesh.getTotalVertices();
|
|
1603
|
+
const triangleCount = mesh.getTotalIndices() > 0 ? mesh.getTotalIndices() / 3 : 0;
|
|
1604
|
+
const vd = VertexData.ExtractFromMesh(mesh);
|
|
1605
|
+
const hasUVs = !!(vd.uvs && vd.uvs.length >= vertexCount * 2);
|
|
1606
|
+
let gameReadiness = 'Standard Game Ready';
|
|
1607
|
+
let gameReadinessClass = 'success';
|
|
1608
|
+
if (triangleCount <= 6000) {
|
|
1609
|
+
gameReadiness = 'Mobile / WebGL Ready';
|
|
1610
|
+
gameReadinessClass = 'success';
|
|
1611
|
+
}
|
|
1612
|
+
else if (triangleCount <= 28000) {
|
|
1613
|
+
gameReadiness = 'Standard Game Ready';
|
|
1614
|
+
gameReadinessClass = 'success';
|
|
1615
|
+
}
|
|
1616
|
+
else if (triangleCount <= 60000) {
|
|
1617
|
+
gameReadiness = 'Hero Asset';
|
|
1618
|
+
gameReadinessClass = 'warning';
|
|
1619
|
+
}
|
|
1620
|
+
else {
|
|
1621
|
+
gameReadiness = 'High-Poly Sculpt';
|
|
1622
|
+
gameReadinessClass = 'danger';
|
|
1623
|
+
}
|
|
1624
|
+
return {
|
|
1625
|
+
vertexCount,
|
|
1626
|
+
triangleCount,
|
|
1627
|
+
isManifold: true,
|
|
1628
|
+
hasUVs,
|
|
1629
|
+
gameReadiness,
|
|
1630
|
+
gameReadinessClass,
|
|
1631
|
+
};
|
|
1632
|
+
}
|
|
1633
|
+
/**
|
|
1634
|
+
* Projects all vertices of `lowPolyMesh` onto the closest surface triangles of `highPolyTarget`
|
|
1635
|
+
* (Shrinkwrap modifier), with optional tangential smoothing and surface normal offset.
|
|
1636
|
+
*/
|
|
1637
|
+
shrinkwrapMesh(lowPolyMesh, highPolyTarget, options = {}) {
|
|
1638
|
+
const offset = options.offset ?? this.quadSurfaceOffset;
|
|
1639
|
+
const smoothIterations = Math.max(0, Math.min(options.smoothIterations ?? 1, 5));
|
|
1640
|
+
const lowPosRaw = lowPolyMesh.getVerticesData(VertexBuffer.PositionKind);
|
|
1641
|
+
const lowInd = lowPolyMesh.getIndices();
|
|
1642
|
+
if (!lowPosRaw || !lowInd) {
|
|
1643
|
+
return this.analyzeTopology(lowPolyMesh);
|
|
1644
|
+
}
|
|
1645
|
+
const preSnapshot = VertexData.ExtractFromMesh(lowPolyMesh);
|
|
1646
|
+
this.ensureMeshUpdatable(lowPolyMesh);
|
|
1647
|
+
lowPolyMesh.computeWorldMatrix(true);
|
|
1648
|
+
highPolyTarget.computeWorldMatrix(true);
|
|
1649
|
+
const lowWorldMat = lowPolyMesh.getWorldMatrix();
|
|
1650
|
+
const lowInvWorldMat = lowWorldMat.clone().invert();
|
|
1651
|
+
const highTris = this.extractWorldTriangles(highPolyTarget);
|
|
1652
|
+
if (highTris.length === 0) {
|
|
1653
|
+
return this.analyzeTopology(lowPolyMesh);
|
|
1654
|
+
}
|
|
1655
|
+
const numVerts = Math.floor(lowPosRaw.length / 3);
|
|
1656
|
+
const worldVerts = new Array(numVerts);
|
|
1657
|
+
const worldNormals = new Array(numVerts);
|
|
1658
|
+
for (let i = 0; i < numVerts; i++) {
|
|
1659
|
+
const localP = new Vector3(lowPosRaw[i * 3], lowPosRaw[i * 3 + 1], lowPosRaw[i * 3 + 2]);
|
|
1660
|
+
const worldP = Vector3.TransformCoordinates(localP, lowWorldMat);
|
|
1661
|
+
const proj = this.findClosestPointOnTriangles(worldP, highTris);
|
|
1662
|
+
worldVerts[i] = proj.point.add(proj.normal.scale(offset));
|
|
1663
|
+
worldNormals[i] = proj.normal;
|
|
1664
|
+
}
|
|
1665
|
+
// Optional tangential relaxation + re-projection for clean quad edge flow
|
|
1666
|
+
if (smoothIterations > 0 && numVerts > 3) {
|
|
1667
|
+
const neighbors = Array.from({ length: numVerts }, () => new Set());
|
|
1668
|
+
for (let f = 0; f < lowInd.length; f += 3) {
|
|
1669
|
+
const a = lowInd[f];
|
|
1670
|
+
const b = lowInd[f + 1];
|
|
1671
|
+
const c = lowInd[f + 2];
|
|
1672
|
+
neighbors[a].add(b);
|
|
1673
|
+
neighbors[a].add(c);
|
|
1674
|
+
neighbors[b].add(a);
|
|
1675
|
+
neighbors[b].add(c);
|
|
1676
|
+
neighbors[c].add(a);
|
|
1677
|
+
neighbors[c].add(b);
|
|
1678
|
+
}
|
|
1679
|
+
for (let iter = 0; iter < smoothIterations; iter++) {
|
|
1680
|
+
const relaxed = new Array(numVerts);
|
|
1681
|
+
for (let i = 0; i < numVerts; i++) {
|
|
1682
|
+
const nbrs = neighbors[i];
|
|
1683
|
+
if (nbrs.size === 0) {
|
|
1684
|
+
relaxed[i] = worldVerts[i];
|
|
1685
|
+
continue;
|
|
1686
|
+
}
|
|
1687
|
+
const avg = new Vector3(0, 0, 0);
|
|
1688
|
+
for (const nb of nbrs) {
|
|
1689
|
+
avg.addInPlace(worldVerts[nb]);
|
|
1690
|
+
}
|
|
1691
|
+
avg.scaleInPlace(1 / nbrs.size);
|
|
1692
|
+
// Tangential relaxation
|
|
1693
|
+
const delta = avg.subtract(worldVerts[i]);
|
|
1694
|
+
const n = worldNormals[i];
|
|
1695
|
+
const tangDelta = delta.subtract(n.scale(Vector3.Dot(delta, n)));
|
|
1696
|
+
const cand = worldVerts[i].add(tangDelta.scale(0.45));
|
|
1697
|
+
const reproj = this.findClosestPointOnTriangles(cand, highTris);
|
|
1698
|
+
relaxed[i] = reproj.point.add(reproj.normal.scale(offset));
|
|
1699
|
+
worldNormals[i] = reproj.normal;
|
|
1700
|
+
}
|
|
1701
|
+
for (let i = 0; i < numVerts; i++) {
|
|
1702
|
+
worldVerts[i] = relaxed[i];
|
|
1703
|
+
}
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
const newLocalPositions = new Float32Array(lowPosRaw.length);
|
|
1707
|
+
for (let i = 0; i < numVerts; i++) {
|
|
1708
|
+
const lp = Vector3.TransformCoordinates(worldVerts[i], lowInvWorldMat);
|
|
1709
|
+
newLocalPositions[i * 3] = lp.x;
|
|
1710
|
+
newLocalPositions[i * 3 + 1] = lp.y;
|
|
1711
|
+
newLocalPositions[i * 3 + 2] = lp.z;
|
|
1712
|
+
}
|
|
1713
|
+
const newNormals = [];
|
|
1714
|
+
VertexData.ComputeNormals(newLocalPositions, lowInd, newNormals);
|
|
1715
|
+
lowPolyMesh.setVerticesData(VertexBuffer.PositionKind, newLocalPositions, true);
|
|
1716
|
+
lowPolyMesh.setVerticesData(VertexBuffer.NormalKind, newNormals, true);
|
|
1717
|
+
lowPolyMesh.refreshBoundingInfo();
|
|
1718
|
+
const postSnapshot = VertexData.ExtractFromMesh(lowPolyMesh);
|
|
1719
|
+
this.service.recordAction({
|
|
1720
|
+
name: `Shrinkwrap (${lowPolyMesh.name} -> ${highPolyTarget.name})`,
|
|
1721
|
+
undo: () => {
|
|
1722
|
+
if (!lowPolyMesh.isDisposed()) {
|
|
1723
|
+
preSnapshot.applyToMesh(lowPolyMesh, true);
|
|
1724
|
+
lowPolyMesh.refreshBoundingInfo();
|
|
1725
|
+
this.onMeshModified?.(lowPolyMesh);
|
|
1726
|
+
}
|
|
1727
|
+
},
|
|
1728
|
+
redo: () => {
|
|
1729
|
+
if (!lowPolyMesh.isDisposed()) {
|
|
1730
|
+
postSnapshot.applyToMesh(lowPolyMesh, true);
|
|
1731
|
+
lowPolyMesh.refreshBoundingInfo();
|
|
1732
|
+
this.onMeshModified?.(lowPolyMesh);
|
|
1733
|
+
}
|
|
1734
|
+
},
|
|
1735
|
+
});
|
|
1736
|
+
this.service.updateStats();
|
|
1737
|
+
this.onMeshModified?.(lowPolyMesh);
|
|
1738
|
+
return this.analyzeTopology(lowPolyMesh);
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* Draws a surface-snapped Quad Strip along `worldStrokePoints` on `targetHighPoly`
|
|
1742
|
+
* and welds adjacent quad vertices to build clean animation-ready edge loops.
|
|
1743
|
+
*/
|
|
1744
|
+
drawQuadStripOnMesh(targetHighPoly, worldStrokePoints, stripWidth = this.radius, offset = this.quadSurfaceOffset) {
|
|
1745
|
+
const width = Math.max(0.04, stripWidth);
|
|
1746
|
+
const stepLen = Math.max(0.04, width * 0.85);
|
|
1747
|
+
const highTris = this.extractWorldTriangles(targetHighPoly);
|
|
1748
|
+
// Resample stroke points into evenly spaced steps
|
|
1749
|
+
const samples = [];
|
|
1750
|
+
if (worldStrokePoints.length === 0) {
|
|
1751
|
+
targetHighPoly.computeWorldMatrix(true);
|
|
1752
|
+
const c = targetHighPoly.getBoundingInfo().boundingBox.centerWorld;
|
|
1753
|
+
samples.push(c.add(new Vector3(0, -width, 0)), c.add(new Vector3(0, width, 0)));
|
|
1754
|
+
}
|
|
1755
|
+
else if (worldStrokePoints.length === 1) {
|
|
1756
|
+
samples.push(worldStrokePoints[0].clone(), worldStrokePoints[0].add(new Vector3(0, 0, stepLen)));
|
|
1757
|
+
}
|
|
1758
|
+
else {
|
|
1759
|
+
samples.push(worldStrokePoints[0].clone());
|
|
1760
|
+
let accum = 0;
|
|
1761
|
+
for (let i = 1; i < worldStrokePoints.length; i++) {
|
|
1762
|
+
const pPrev = samples[samples.length - 1];
|
|
1763
|
+
const pCurr = worldStrokePoints[i];
|
|
1764
|
+
const dist = Vector3.Distance(pPrev, pCurr);
|
|
1765
|
+
if (dist >= stepLen) {
|
|
1766
|
+
const steps = Math.max(1, Math.min(24, Math.floor(dist / stepLen)));
|
|
1767
|
+
for (let s = 1; s <= steps; s++) {
|
|
1768
|
+
samples.push(Vector3.Lerp(pPrev, pCurr, s / steps));
|
|
1769
|
+
}
|
|
1770
|
+
accum = 0;
|
|
1771
|
+
}
|
|
1772
|
+
else {
|
|
1773
|
+
accum += dist;
|
|
1774
|
+
}
|
|
1775
|
+
}
|
|
1776
|
+
if (samples.length < 2) {
|
|
1777
|
+
samples.push(worldStrokePoints[worldStrokePoints.length - 1].clone());
|
|
1778
|
+
}
|
|
1779
|
+
}
|
|
1780
|
+
const retopoName = `${targetHighPoly.name}_RetopoQuads`;
|
|
1781
|
+
let retopoMesh = this.service.importedMeshes.find((m) => m.name === retopoName && !m.isDisposed());
|
|
1782
|
+
let existingPos = [];
|
|
1783
|
+
let existingInd = [];
|
|
1784
|
+
let existingUVs = [];
|
|
1785
|
+
if (retopoMesh) {
|
|
1786
|
+
existingPos = Array.from(retopoMesh.getVerticesData(VertexBuffer.PositionKind) || []);
|
|
1787
|
+
existingInd = Array.from(retopoMesh.getIndices() || []);
|
|
1788
|
+
existingUVs = Array.from(retopoMesh.getVerticesData(VertexBuffer.UVKind) || []);
|
|
1789
|
+
}
|
|
1790
|
+
else {
|
|
1791
|
+
retopoMesh = new Mesh(retopoName, this.scene);
|
|
1792
|
+
const mat = new PBRMaterial(`${retopoName}_Mat`, this.scene);
|
|
1793
|
+
mat.albedoColor = new Color3(0.22, 0.78, 0.95);
|
|
1794
|
+
mat.metallic = 0.05;
|
|
1795
|
+
mat.roughness = 0.45;
|
|
1796
|
+
mat.backFaceCulling = false;
|
|
1797
|
+
retopoMesh.material = mat;
|
|
1798
|
+
this.service.addMesh(retopoMesh);
|
|
1799
|
+
}
|
|
1800
|
+
const weldTol = this.quadAutoWeld ? width * 0.35 : 0;
|
|
1801
|
+
const weldTolSq = weldTol * weldTol;
|
|
1802
|
+
const addOrWeldVertex = (worldPos, u, v) => {
|
|
1803
|
+
if (weldTol > 0) {
|
|
1804
|
+
const numExisting = Math.floor(existingPos.length / 3);
|
|
1805
|
+
for (let i = 0; i < numExisting; i++) {
|
|
1806
|
+
const dx = existingPos[i * 3] - worldPos.x;
|
|
1807
|
+
const dy = existingPos[i * 3 + 1] - worldPos.y;
|
|
1808
|
+
const dz = existingPos[i * 3 + 2] - worldPos.z;
|
|
1809
|
+
if (dx * dx + dy * dy + dz * dz <= weldTolSq) {
|
|
1810
|
+
return i;
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
const newIdx = Math.floor(existingPos.length / 3);
|
|
1815
|
+
existingPos.push(worldPos.x, worldPos.y, worldPos.z);
|
|
1816
|
+
existingUVs.push(u, v);
|
|
1817
|
+
return newIdx;
|
|
1818
|
+
};
|
|
1819
|
+
const rowPairs = [];
|
|
1820
|
+
for (let i = 0; i < samples.length; i++) {
|
|
1821
|
+
const pt = samples[i];
|
|
1822
|
+
const proj = highTris.length > 0
|
|
1823
|
+
? this.findClosestPointOnTriangles(pt, highTris)
|
|
1824
|
+
: { point: pt.clone(), normal: Vector3.Up() };
|
|
1825
|
+
let tangent;
|
|
1826
|
+
if (i < samples.length - 1) {
|
|
1827
|
+
tangent = samples[i + 1].subtract(pt);
|
|
1828
|
+
}
|
|
1829
|
+
else {
|
|
1830
|
+
tangent = pt.subtract(samples[i - 1]);
|
|
1831
|
+
}
|
|
1832
|
+
if (tangent.lengthSquared() < 1e-8) {
|
|
1833
|
+
tangent = Vector3.Forward();
|
|
1834
|
+
}
|
|
1835
|
+
else {
|
|
1836
|
+
tangent.normalize();
|
|
1837
|
+
}
|
|
1838
|
+
let binormal = Vector3.Cross(tangent, proj.normal);
|
|
1839
|
+
if (binormal.lengthSquared() < 1e-8) {
|
|
1840
|
+
binormal = Vector3.Right();
|
|
1841
|
+
}
|
|
1842
|
+
else {
|
|
1843
|
+
binormal.normalize();
|
|
1844
|
+
}
|
|
1845
|
+
const halfW = width * 0.5;
|
|
1846
|
+
const leftCand = proj.point.subtract(binormal.scale(halfW));
|
|
1847
|
+
const rightCand = proj.point.add(binormal.scale(halfW));
|
|
1848
|
+
const leftProj = highTris.length > 0
|
|
1849
|
+
? this.findClosestPointOnTriangles(leftCand, highTris)
|
|
1850
|
+
: { point: leftCand, normal: proj.normal };
|
|
1851
|
+
const rightProj = highTris.length > 0
|
|
1852
|
+
? this.findClosestPointOnTriangles(rightCand, highTris)
|
|
1853
|
+
: { point: rightCand, normal: proj.normal };
|
|
1854
|
+
const leftFinal = leftProj.point.add(leftProj.normal.scale(offset));
|
|
1855
|
+
const rightFinal = rightProj.point.add(rightProj.normal.scale(offset));
|
|
1856
|
+
const vCoord = i / Math.max(1, samples.length - 1);
|
|
1857
|
+
const lIdx = addOrWeldVertex(leftFinal, 0, vCoord);
|
|
1858
|
+
const rIdx = addOrWeldVertex(rightFinal, 1, vCoord);
|
|
1859
|
+
rowPairs.push([lIdx, rIdx]);
|
|
1860
|
+
}
|
|
1861
|
+
for (let i = 0; i < rowPairs.length - 1; i++) {
|
|
1862
|
+
const [l0, r0] = rowPairs[i];
|
|
1863
|
+
const [l1, r1] = rowPairs[i + 1];
|
|
1864
|
+
existingInd.push(l0, r0, r1);
|
|
1865
|
+
existingInd.push(l0, r1, l1);
|
|
1866
|
+
}
|
|
1867
|
+
const normals = [];
|
|
1868
|
+
VertexData.ComputeNormals(existingPos, existingInd, normals);
|
|
1869
|
+
const vd = new VertexData();
|
|
1870
|
+
vd.positions = existingPos;
|
|
1871
|
+
vd.indices = existingInd;
|
|
1872
|
+
vd.normals = normals;
|
|
1873
|
+
vd.uvs = existingUVs;
|
|
1874
|
+
vd.applyToMesh(retopoMesh, true);
|
|
1875
|
+
retopoMesh.refreshBoundingInfo();
|
|
1876
|
+
this.service.updateStats();
|
|
1877
|
+
this.onMeshModified?.(retopoMesh);
|
|
1878
|
+
return retopoMesh;
|
|
1879
|
+
}
|
|
1880
|
+
/**
|
|
1881
|
+
* Generates a complete Quad-Dominant Edge-Flow Cage wrapped tightly onto `targetHighPoly`.
|
|
1882
|
+
*/
|
|
1883
|
+
generateQuadFlowCage(targetHighPoly, rings = 14, segmentsPerRing = 16, offset = this.quadSurfaceOffset) {
|
|
1884
|
+
targetHighPoly.computeWorldMatrix(true);
|
|
1885
|
+
targetHighPoly.refreshBoundingInfo();
|
|
1886
|
+
const bbox = targetHighPoly.getBoundingInfo().boundingBox;
|
|
1887
|
+
const min = bbox.minimumWorld;
|
|
1888
|
+
const max = bbox.maximumWorld;
|
|
1889
|
+
const center = bbox.centerWorld;
|
|
1890
|
+
const rx = Math.max(0.15, (max.x - min.x) * 0.6);
|
|
1891
|
+
const ry = Math.max(0.15, (max.y - min.y) * 0.52);
|
|
1892
|
+
const rz = Math.max(0.15, (max.z - min.z) * 0.6);
|
|
1893
|
+
const cageName = `${targetHighPoly.name}_QuadCage`;
|
|
1894
|
+
const positions = [];
|
|
1895
|
+
const uvs = [];
|
|
1896
|
+
const indices = [];
|
|
1897
|
+
const rCount = Math.max(4, Math.min(48, rings));
|
|
1898
|
+
const sCount = Math.max(6, Math.min(48, segmentsPerRing));
|
|
1899
|
+
for (let r = 0; r <= rCount; r++) {
|
|
1900
|
+
const v = r / rCount;
|
|
1901
|
+
const phi = v * Math.PI;
|
|
1902
|
+
const sinPhi = Math.sin(phi);
|
|
1903
|
+
const cosPhi = Math.cos(phi);
|
|
1904
|
+
for (let s = 0; s <= sCount; s++) {
|
|
1905
|
+
const u = s / sCount;
|
|
1906
|
+
const theta = u * Math.PI * 2;
|
|
1907
|
+
const x = center.x + rx * sinPhi * Math.cos(theta);
|
|
1908
|
+
const y = center.y - ry * cosPhi;
|
|
1909
|
+
const z = center.z + rz * sinPhi * Math.sin(theta);
|
|
1910
|
+
positions.push(x, y, z);
|
|
1911
|
+
uvs.push(u, v);
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
const stride = sCount + 1;
|
|
1915
|
+
for (let r = 0; r < rCount; r++) {
|
|
1916
|
+
for (let s = 0; s < sCount; s++) {
|
|
1917
|
+
const i0 = r * stride + s;
|
|
1918
|
+
const i1 = i0 + 1;
|
|
1919
|
+
const i2 = (r + 1) * stride + s;
|
|
1920
|
+
const i3 = i2 + 1;
|
|
1921
|
+
indices.push(i0, i2, i3);
|
|
1922
|
+
indices.push(i0, i3, i1);
|
|
1923
|
+
}
|
|
1924
|
+
}
|
|
1925
|
+
const normals = [];
|
|
1926
|
+
VertexData.ComputeNormals(positions, indices, normals);
|
|
1927
|
+
const cageMesh = new Mesh(cageName, this.scene);
|
|
1928
|
+
const vd = new VertexData();
|
|
1929
|
+
vd.positions = positions;
|
|
1930
|
+
vd.indices = indices;
|
|
1931
|
+
vd.normals = normals;
|
|
1932
|
+
vd.uvs = uvs;
|
|
1933
|
+
vd.applyToMesh(cageMesh, true);
|
|
1934
|
+
const mat = new PBRMaterial(`${cageName}_Mat`, this.scene);
|
|
1935
|
+
mat.albedoColor = new Color3(0.28, 0.82, 0.58);
|
|
1936
|
+
mat.metallic = 0.05;
|
|
1937
|
+
mat.roughness = 0.45;
|
|
1938
|
+
cageMesh.material = mat;
|
|
1939
|
+
this.service.addMesh(cageMesh);
|
|
1940
|
+
this.shrinkwrapMesh(cageMesh, targetHighPoly, {
|
|
1941
|
+
offset,
|
|
1942
|
+
mode: 'nearest',
|
|
1943
|
+
smoothIterations: 2,
|
|
1944
|
+
});
|
|
1945
|
+
return cageMesh;
|
|
1946
|
+
}
|
|
1947
|
+
extractWorldTriangles(mesh) {
|
|
1948
|
+
const pos = mesh.getVerticesData(VertexBuffer.PositionKind);
|
|
1949
|
+
const ind = mesh.getIndices();
|
|
1950
|
+
if (!pos || !ind || ind.length < 3)
|
|
1951
|
+
return [];
|
|
1952
|
+
mesh.computeWorldMatrix(true);
|
|
1953
|
+
const wMat = mesh.getWorldMatrix();
|
|
1954
|
+
const numTris = Math.floor(ind.length / 3);
|
|
1955
|
+
const step = Math.max(1, Math.floor(numTris / 2500));
|
|
1956
|
+
const out = [];
|
|
1957
|
+
for (let f = 0; f < numTris; f += step) {
|
|
1958
|
+
const i0 = ind[f * 3];
|
|
1959
|
+
const i1 = ind[f * 3 + 1];
|
|
1960
|
+
const i2 = ind[f * 3 + 2];
|
|
1961
|
+
const p0 = Vector3.TransformCoordinates(new Vector3(pos[i0 * 3], pos[i0 * 3 + 1], pos[i0 * 3 + 2]), wMat);
|
|
1962
|
+
const p1 = Vector3.TransformCoordinates(new Vector3(pos[i1 * 3], pos[i1 * 3 + 1], pos[i1 * 3 + 2]), wMat);
|
|
1963
|
+
const p2 = Vector3.TransformCoordinates(new Vector3(pos[i2 * 3], pos[i2 * 3 + 1], pos[i2 * 3 + 2]), wMat);
|
|
1964
|
+
const normal = Vector3.Cross(p1.subtract(p0), p2.subtract(p0));
|
|
1965
|
+
if (normal.lengthSquared() > 1e-12)
|
|
1966
|
+
normal.normalize();
|
|
1967
|
+
else
|
|
1968
|
+
normal.set(0, 1, 0);
|
|
1969
|
+
out.push({ p0, p1, p2, normal });
|
|
1970
|
+
}
|
|
1971
|
+
return out;
|
|
1972
|
+
}
|
|
1973
|
+
findClosestPointOnTriangles(point, tris) {
|
|
1974
|
+
let bestDistSq = Infinity;
|
|
1975
|
+
let bestPoint = point.clone();
|
|
1976
|
+
let bestNormal = Vector3.Up();
|
|
1977
|
+
for (let i = 0; i < tris.length; i++) {
|
|
1978
|
+
const t = tris[i];
|
|
1979
|
+
const cp = this.closestPointOnTriangle(point, t.p0, t.p1, t.p2);
|
|
1980
|
+
const dSq = Vector3.DistanceSquared(point, cp);
|
|
1981
|
+
if (dSq < bestDistSq) {
|
|
1982
|
+
bestDistSq = dSq;
|
|
1983
|
+
bestPoint = cp;
|
|
1984
|
+
bestNormal = t.normal;
|
|
1985
|
+
}
|
|
1986
|
+
}
|
|
1987
|
+
return { point: bestPoint, normal: bestNormal.clone() };
|
|
1988
|
+
}
|
|
1989
|
+
closestPointOnTriangle(p, a, b, c) {
|
|
1990
|
+
const ab = b.subtract(a);
|
|
1991
|
+
const ac = c.subtract(a);
|
|
1992
|
+
const ap = p.subtract(a);
|
|
1993
|
+
const d1 = Vector3.Dot(ab, ap);
|
|
1994
|
+
const d2 = Vector3.Dot(ac, ap);
|
|
1995
|
+
if (d1 <= 0 && d2 <= 0)
|
|
1996
|
+
return a.clone();
|
|
1997
|
+
const bp = p.subtract(b);
|
|
1998
|
+
const d3 = Vector3.Dot(ab, bp);
|
|
1999
|
+
const d4 = Vector3.Dot(ac, bp);
|
|
2000
|
+
if (d3 >= 0 && d4 <= d3)
|
|
2001
|
+
return b.clone();
|
|
2002
|
+
const vc = d1 * d4 - d3 * d2;
|
|
2003
|
+
if (vc <= 0 && d1 >= 0 && d3 <= 0) {
|
|
2004
|
+
const v = d1 / (d1 - d3);
|
|
2005
|
+
return a.add(ab.scale(v));
|
|
2006
|
+
}
|
|
2007
|
+
const cp = p.subtract(c);
|
|
2008
|
+
const d5 = Vector3.Dot(ab, cp);
|
|
2009
|
+
const d6 = Vector3.Dot(ac, cp);
|
|
2010
|
+
if (d6 >= 0 && d5 <= d6)
|
|
2011
|
+
return c.clone();
|
|
2012
|
+
const vb = d5 * d2 - d1 * d6;
|
|
2013
|
+
if (vb <= 0 && d2 >= 0 && d6 <= 0) {
|
|
2014
|
+
const w = d2 / (d2 - d6);
|
|
2015
|
+
return a.add(ac.scale(w));
|
|
2016
|
+
}
|
|
2017
|
+
const va = d3 * d6 - d5 * d4;
|
|
2018
|
+
if (va <= 0 && d4 - d3 >= 0 && d5 - d6 >= 0) {
|
|
2019
|
+
const w = (d4 - d3) / (d4 - d3 + (d5 - d6));
|
|
2020
|
+
return b.add(c.subtract(b).scale(w));
|
|
2021
|
+
}
|
|
2022
|
+
const denom = 1.0 / (va + vb + vc);
|
|
2023
|
+
const v = vb * denom;
|
|
2024
|
+
const w = vc * denom;
|
|
2025
|
+
return a.add(ab.scale(v)).add(ac.scale(w));
|
|
2026
|
+
}
|
|
2027
|
+
dispose() {
|
|
2028
|
+
this.setActive(false);
|
|
2029
|
+
if (this.cursorRing) {
|
|
2030
|
+
this.cursorRing.dispose();
|
|
2031
|
+
this.cursorRing = null;
|
|
2032
|
+
}
|
|
2033
|
+
if (this.symCursorRing) {
|
|
2034
|
+
this.symCursorRing.dispose();
|
|
2035
|
+
this.symCursorRing = null;
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
}
|