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.
Files changed (258) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +853 -2
  3. package/dist-lib/esm/AIAgentService.d.ts +106 -0
  4. package/dist-lib/esm/AIAgentService.js +1471 -0
  5. package/dist-lib/esm/MeshEditorApp.d.ts +412 -0
  6. package/dist-lib/esm/MeshEditorApp.js +9092 -0
  7. package/dist-lib/esm/MeshEditorService.d.ts +459 -0
  8. package/dist-lib/esm/MeshEditorService.js +7083 -0
  9. package/dist-lib/esm/Neural3DGeneratorService.d.ts +88 -0
  10. package/dist-lib/esm/Neural3DGeneratorService.js +1397 -0
  11. package/dist-lib/esm/adapters/BrowserPlatformAdapter.d.ts +16 -0
  12. package/dist-lib/esm/adapters/BrowserPlatformAdapter.js +25 -0
  13. package/dist-lib/esm/adapters/ElectronPlatformAdapter.d.ts +32 -0
  14. package/dist-lib/esm/adapters/ElectronPlatformAdapter.js +336 -0
  15. package/dist-lib/esm/adapters/HostAppAdapter.d.ts +54 -0
  16. package/dist-lib/esm/adapters/HostAppAdapter.js +75 -0
  17. package/dist-lib/esm/adapters/PlatformAdapter.d.ts +98 -0
  18. package/dist-lib/esm/adapters/PlatformAdapter.js +1 -0
  19. package/dist-lib/esm/adapters/index.d.ts +13 -0
  20. package/dist-lib/esm/adapters/index.js +15 -0
  21. package/dist-lib/esm/assets/fonts/Droid_Sans_Bold.json +1 -0
  22. package/dist-lib/esm/assets/fonts/Droid_Sans_Regular.json +1 -0
  23. package/dist-lib/esm/assets/fonts/Kenney_Future_Regular.json +1 -0
  24. package/dist-lib/esm/baking/TextureBaker.d.ts +84 -0
  25. package/dist-lib/esm/baking/TextureBaker.js +709 -0
  26. package/dist-lib/esm/builtInSkills.d.ts +19 -0
  27. package/dist-lib/esm/builtInSkills.js +186 -0
  28. package/dist-lib/esm/compression/ModelCompressor.d.ts +87 -0
  29. package/dist-lib/esm/compression/ModelCompressor.js +553 -0
  30. package/dist-lib/esm/decals/DecalController.d.ts +147 -0
  31. package/dist-lib/esm/decals/DecalController.js +1031 -0
  32. package/dist-lib/esm/decals/decalAssetUrls.d.ts +1 -0
  33. package/dist-lib/esm/decals/decalAssetUrls.js +16 -0
  34. package/dist-lib/esm/geometry/architecture.d.ts +114 -0
  35. package/dist-lib/esm/geometry/architecture.js +832 -0
  36. package/dist-lib/esm/geometry/bevelModifier.d.ts +59 -0
  37. package/dist-lib/esm/geometry/bevelModifier.js +345 -0
  38. package/dist-lib/esm/geometry/facetSurfaceUtils.d.ts +127 -0
  39. package/dist-lib/esm/geometry/facetSurfaceUtils.js +676 -0
  40. package/dist-lib/esm/geometry/roundedBox.d.ts +15 -0
  41. package/dist-lib/esm/geometry/roundedBox.js +133 -0
  42. package/dist-lib/esm/index.d.ts +21 -0
  43. package/dist-lib/esm/index.js +50 -0
  44. package/dist-lib/esm/pathExtrusion/PathExtrusionController.d.ts +59 -0
  45. package/dist-lib/esm/pathExtrusion/PathExtrusionController.js +483 -0
  46. package/dist-lib/esm/pathExtrusion/PathExtrusionStudio.d.ts +76 -0
  47. package/dist-lib/esm/pathExtrusion/PathExtrusionStudio.js +1792 -0
  48. package/dist-lib/esm/pathExtrusion/PathFactory.d.ts +95 -0
  49. package/dist-lib/esm/pathExtrusion/PathFactory.js +627 -0
  50. package/dist-lib/esm/pathExtrusion/index.d.ts +4 -0
  51. package/dist-lib/esm/pathExtrusion/index.js +4 -0
  52. package/dist-lib/esm/pathExtrusion/types.d.ts +50 -0
  53. package/dist-lib/esm/pathExtrusion/types.js +1 -0
  54. package/dist-lib/esm/plugin.d.ts +250 -0
  55. package/dist-lib/esm/plugin.js +144 -0
  56. package/dist-lib/esm/rigging/WeightPaintController.d.ts +162 -0
  57. package/dist-lib/esm/rigging/WeightPaintController.js +1421 -0
  58. package/dist-lib/esm/sculpting/SculptController.d.ts +146 -0
  59. package/dist-lib/esm/sculpting/SculptController.js +2038 -0
  60. package/dist-lib/esm/sdf/index.d.ts +2 -0
  61. package/dist-lib/esm/sdf/index.js +2 -0
  62. package/dist-lib/esm/sdf/sdfPrimitives.d.ts +97 -0
  63. package/dist-lib/esm/sdf/sdfPrimitives.js +402 -0
  64. package/dist-lib/esm/sdf/surfaceNets.d.ts +29 -0
  65. package/dist-lib/esm/sdf/surfaceNets.js +399 -0
  66. package/dist-lib/esm/styles.d.ts +11 -0
  67. package/dist-lib/esm/styles.js +32 -0
  68. package/dist-lib/esm/template.d.ts +12 -0
  69. package/dist-lib/esm/template.js +19 -0
  70. package/dist-lib/esm/textMesh/TextMeshController.d.ts +61 -0
  71. package/dist-lib/esm/textMesh/TextMeshController.js +405 -0
  72. package/dist-lib/esm/textures/AITextureGenerator.d.ts +119 -0
  73. package/dist-lib/esm/textures/AITextureGenerator.js +945 -0
  74. package/dist-lib/esm/textures/ExternalImageGeneratorService.d.ts +62 -0
  75. package/dist-lib/esm/textures/ExternalImageGeneratorService.js +879 -0
  76. package/dist-lib/esm/tools/AbstractTool.d.ts +68 -0
  77. package/dist-lib/esm/tools/AbstractTool.js +128 -0
  78. package/dist-lib/esm/tools/ToolExecutionContext.d.ts +40 -0
  79. package/dist-lib/esm/tools/ToolExecutionContext.js +1 -0
  80. package/dist-lib/esm/tools/ToolInterface.d.ts +104 -0
  81. package/dist-lib/esm/tools/ToolInterface.js +1 -0
  82. package/dist-lib/esm/tools/ToolRegistry.d.ts +43 -0
  83. package/dist-lib/esm/tools/ToolRegistry.js +115 -0
  84. package/dist-lib/esm/tools/builtin/ApplyMaterialTool.d.ts +34 -0
  85. package/dist-lib/esm/tools/builtin/ApplyMaterialTool.js +48 -0
  86. package/dist-lib/esm/tools/builtin/AutoUVMappingTool.d.ts +34 -0
  87. package/dist-lib/esm/tools/builtin/AutoUVMappingTool.js +54 -0
  88. package/dist-lib/esm/tools/builtin/BakeTexturesTool.d.ts +42 -0
  89. package/dist-lib/esm/tools/builtin/BakeTexturesTool.js +81 -0
  90. package/dist-lib/esm/tools/builtin/BooleanOperationTool.d.ts +51 -0
  91. package/dist-lib/esm/tools/builtin/BooleanOperationTool.js +86 -0
  92. package/dist-lib/esm/tools/builtin/BuildVoxelMeshTool.d.ts +78 -0
  93. package/dist-lib/esm/tools/builtin/BuildVoxelMeshTool.js +170 -0
  94. package/dist-lib/esm/tools/builtin/CloneArrayTool.d.ts +75 -0
  95. package/dist-lib/esm/tools/builtin/CloneArrayTool.js +115 -0
  96. package/dist-lib/esm/tools/builtin/CreateArchTool.d.ts +118 -0
  97. package/dist-lib/esm/tools/builtin/CreateArchTool.js +89 -0
  98. package/dist-lib/esm/tools/builtin/CreateColumnTool.d.ts +114 -0
  99. package/dist-lib/esm/tools/builtin/CreateColumnTool.js +105 -0
  100. package/dist-lib/esm/tools/builtin/CreateLatheTool.d.ts +87 -0
  101. package/dist-lib/esm/tools/builtin/CreateLatheTool.js +60 -0
  102. package/dist-lib/esm/tools/builtin/CreateMaterialTool.d.ts +114 -0
  103. package/dist-lib/esm/tools/builtin/CreateMaterialTool.js +212 -0
  104. package/dist-lib/esm/tools/builtin/CreateMoldingTool.d.ts +120 -0
  105. package/dist-lib/esm/tools/builtin/CreateMoldingTool.js +95 -0
  106. package/dist-lib/esm/tools/builtin/CreatePrimitiveTool.d.ts +162 -0
  107. package/dist-lib/esm/tools/builtin/CreatePrimitiveTool.js +302 -0
  108. package/dist-lib/esm/tools/builtin/CreateRoofTool.d.ts +120 -0
  109. package/dist-lib/esm/tools/builtin/CreateRoofTool.js +108 -0
  110. package/dist-lib/esm/tools/builtin/CreateRoundedBoxTool.d.ts +101 -0
  111. package/dist-lib/esm/tools/builtin/CreateRoundedBoxTool.js +79 -0
  112. package/dist-lib/esm/tools/builtin/CreateSdfMeshTool.d.ts +224 -0
  113. package/dist-lib/esm/tools/builtin/CreateSdfMeshTool.js +238 -0
  114. package/dist-lib/esm/tools/builtin/CreateStairsTool.d.ts +135 -0
  115. package/dist-lib/esm/tools/builtin/CreateStairsTool.js +119 -0
  116. package/dist-lib/esm/tools/builtin/CreateTextMeshTool.d.ts +94 -0
  117. package/dist-lib/esm/tools/builtin/CreateTextMeshTool.js +131 -0
  118. package/dist-lib/esm/tools/builtin/CreateTubeTool.d.ts +87 -0
  119. package/dist-lib/esm/tools/builtin/CreateTubeTool.js +59 -0
  120. package/dist-lib/esm/tools/builtin/CreateWallTool.d.ts +145 -0
  121. package/dist-lib/esm/tools/builtin/CreateWallTool.js +124 -0
  122. package/dist-lib/esm/tools/builtin/DecalTool.d.ts +76 -0
  123. package/dist-lib/esm/tools/builtin/DecalTool.js +143 -0
  124. package/dist-lib/esm/tools/builtin/DeleteMeshTool.d.ts +20 -0
  125. package/dist-lib/esm/tools/builtin/DeleteMeshTool.js +42 -0
  126. package/dist-lib/esm/tools/builtin/ExecuteBabylonCodeTool.d.ts +21 -0
  127. package/dist-lib/esm/tools/builtin/ExecuteBabylonCodeTool.js +96 -0
  128. package/dist-lib/esm/tools/builtin/ExtrudePolygonTool.d.ts +114 -0
  129. package/dist-lib/esm/tools/builtin/ExtrudePolygonTool.js +77 -0
  130. package/dist-lib/esm/tools/builtin/ExtrudeShapeTool.d.ts +104 -0
  131. package/dist-lib/esm/tools/builtin/ExtrudeShapeTool.js +68 -0
  132. package/dist-lib/esm/tools/builtin/FacetArcTool.d.ts +36 -0
  133. package/dist-lib/esm/tools/builtin/FacetArcTool.js +57 -0
  134. package/dist-lib/esm/tools/builtin/FacetCutThroughTool.d.ts +30 -0
  135. package/dist-lib/esm/tools/builtin/FacetCutThroughTool.js +49 -0
  136. package/dist-lib/esm/tools/builtin/FacetExtrudeTool.d.ts +30 -0
  137. package/dist-lib/esm/tools/builtin/FacetExtrudeTool.js +49 -0
  138. package/dist-lib/esm/tools/builtin/FacetInsetTool.d.ts +34 -0
  139. package/dist-lib/esm/tools/builtin/FacetInsetTool.js +55 -0
  140. package/dist-lib/esm/tools/builtin/GenerateNeuralMeshTool.d.ts +131 -0
  141. package/dist-lib/esm/tools/builtin/GenerateNeuralMeshTool.js +455 -0
  142. package/dist-lib/esm/tools/builtin/GenerateTextureTool.d.ts +87 -0
  143. package/dist-lib/esm/tools/builtin/GenerateTextureTool.js +216 -0
  144. package/dist-lib/esm/tools/builtin/GetSceneInfoTool.d.ts +11 -0
  145. package/dist-lib/esm/tools/builtin/GetSceneInfoTool.js +12 -0
  146. package/dist-lib/esm/tools/builtin/MergeMeshesTool.d.ts +28 -0
  147. package/dist-lib/esm/tools/builtin/MergeMeshesTool.js +56 -0
  148. package/dist-lib/esm/tools/builtin/PlanModelTool.d.ts +87 -0
  149. package/dist-lib/esm/tools/builtin/PlanModelTool.js +155 -0
  150. package/dist-lib/esm/tools/builtin/RetopologizeMeshTool.d.ts +50 -0
  151. package/dist-lib/esm/tools/builtin/RetopologizeMeshTool.js +116 -0
  152. package/dist-lib/esm/tools/builtin/RigWeightTool.d.ts +55 -0
  153. package/dist-lib/esm/tools/builtin/RigWeightTool.js +139 -0
  154. package/dist-lib/esm/tools/builtin/SculptMeshTool.d.ts +138 -0
  155. package/dist-lib/esm/tools/builtin/SculptMeshTool.js +255 -0
  156. package/dist-lib/esm/tools/builtin/SeparateMeshTool.d.ts +21 -0
  157. package/dist-lib/esm/tools/builtin/SeparateMeshTool.js +31 -0
  158. package/dist-lib/esm/tools/builtin/SolidifyMeshTool.d.ts +30 -0
  159. package/dist-lib/esm/tools/builtin/SolidifyMeshTool.js +57 -0
  160. package/dist-lib/esm/tools/builtin/SpawnBaseMeshTool.d.ts +66 -0
  161. package/dist-lib/esm/tools/builtin/SpawnBaseMeshTool.js +105 -0
  162. package/dist-lib/esm/tools/builtin/SubDSmoothTool.d.ts +23 -0
  163. package/dist-lib/esm/tools/builtin/SubDSmoothTool.js +37 -0
  164. package/dist-lib/esm/tools/builtin/SubdivideMeshTool.d.ts +23 -0
  165. package/dist-lib/esm/tools/builtin/SubdivideMeshTool.js +39 -0
  166. package/dist-lib/esm/tools/builtin/TransformMeshTool.d.ts +78 -0
  167. package/dist-lib/esm/tools/builtin/TransformMeshTool.js +69 -0
  168. package/dist-lib/esm/tools/builtin/UVUnwrapTool.d.ts +48 -0
  169. package/dist-lib/esm/tools/builtin/UVUnwrapTool.js +107 -0
  170. package/dist-lib/esm/tools/builtin/VoxelPainterTool.d.ts +60 -0
  171. package/dist-lib/esm/tools/builtin/VoxelPainterTool.js +153 -0
  172. package/dist-lib/esm/tools/builtin/index.d.ts +44 -0
  173. package/dist-lib/esm/tools/builtin/index.js +44 -0
  174. package/dist-lib/esm/tools/editor/AIAgentEditorTool.d.ts +18 -0
  175. package/dist-lib/esm/tools/editor/AIAgentEditorTool.js +43 -0
  176. package/dist-lib/esm/tools/editor/AITextureEditorTool.d.ts +15 -0
  177. package/dist-lib/esm/tools/editor/AITextureEditorTool.js +28 -0
  178. package/dist-lib/esm/tools/editor/AbstractEditorTool.d.ts +29 -0
  179. package/dist-lib/esm/tools/editor/AbstractEditorTool.js +56 -0
  180. package/dist-lib/esm/tools/editor/AnimationPlayerEditorTool.d.ts +17 -0
  181. package/dist-lib/esm/tools/editor/AnimationPlayerEditorTool.js +29 -0
  182. package/dist-lib/esm/tools/editor/BakingEditorTool.d.ts +21 -0
  183. package/dist-lib/esm/tools/editor/BakingEditorTool.js +44 -0
  184. package/dist-lib/esm/tools/editor/CSGEditorTool.d.ts +20 -0
  185. package/dist-lib/esm/tools/editor/CSGEditorTool.js +42 -0
  186. package/dist-lib/esm/tools/editor/CompressModelEditorTool.d.ts +20 -0
  187. package/dist-lib/esm/tools/editor/CompressModelEditorTool.js +48 -0
  188. package/dist-lib/esm/tools/editor/CreatePrimitiveEditorTool.d.ts +17 -0
  189. package/dist-lib/esm/tools/editor/CreatePrimitiveEditorTool.js +36 -0
  190. package/dist-lib/esm/tools/editor/DecalEditorTool.d.ts +17 -0
  191. package/dist-lib/esm/tools/editor/DecalEditorTool.js +36 -0
  192. package/dist-lib/esm/tools/editor/FacetEditorTool.d.ts +23 -0
  193. package/dist-lib/esm/tools/editor/FacetEditorTool.js +80 -0
  194. package/dist-lib/esm/tools/editor/MaterialEditorTool.d.ts +18 -0
  195. package/dist-lib/esm/tools/editor/MaterialEditorTool.js +43 -0
  196. package/dist-lib/esm/tools/editor/MeshPropertiesEditorTool.d.ts +17 -0
  197. package/dist-lib/esm/tools/editor/MeshPropertiesEditorTool.js +37 -0
  198. package/dist-lib/esm/tools/editor/OutlinerEditorTool.d.ts +17 -0
  199. package/dist-lib/esm/tools/editor/OutlinerEditorTool.js +34 -0
  200. package/dist-lib/esm/tools/editor/PathExtrusionEditorTool.d.ts +26 -0
  201. package/dist-lib/esm/tools/editor/PathExtrusionEditorTool.js +72 -0
  202. package/dist-lib/esm/tools/editor/RiggingEditorTool.d.ts +27 -0
  203. package/dist-lib/esm/tools/editor/RiggingEditorTool.js +66 -0
  204. package/dist-lib/esm/tools/editor/SceneEnvironmentEditorTool.d.ts +17 -0
  205. package/dist-lib/esm/tools/editor/SceneEnvironmentEditorTool.js +35 -0
  206. package/dist-lib/esm/tools/editor/SculptEditorTool.d.ts +28 -0
  207. package/dist-lib/esm/tools/editor/SculptEditorTool.js +70 -0
  208. package/dist-lib/esm/tools/editor/TextMeshEditorTool.d.ts +16 -0
  209. package/dist-lib/esm/tools/editor/TextMeshEditorTool.js +30 -0
  210. package/dist-lib/esm/tools/editor/TextureOptimizerEditorTool.d.ts +17 -0
  211. package/dist-lib/esm/tools/editor/TextureOptimizerEditorTool.js +28 -0
  212. package/dist-lib/esm/tools/editor/UVUnwrapEditorTool.d.ts +27 -0
  213. package/dist-lib/esm/tools/editor/UVUnwrapEditorTool.js +70 -0
  214. package/dist-lib/esm/tools/editor/VoxelPainterEditorTool.d.ts +26 -0
  215. package/dist-lib/esm/tools/editor/VoxelPainterEditorTool.js +68 -0
  216. package/dist-lib/esm/tools/editor/aiQuickPrompts.d.ts +30 -0
  217. package/dist-lib/esm/tools/editor/aiQuickPrompts.js +228 -0
  218. package/dist-lib/esm/tools/editor/index.d.ts +32 -0
  219. package/dist-lib/esm/tools/editor/index.js +54 -0
  220. package/dist-lib/esm/tools/editor/toolViews.d.ts +10 -0
  221. package/dist-lib/esm/tools/editor/toolViews.js +53 -0
  222. package/dist-lib/esm/tools/editorTools.d.ts +5 -0
  223. package/dist-lib/esm/tools/editorTools.js +5 -0
  224. package/dist-lib/esm/tools/index.d.ts +8 -0
  225. package/dist-lib/esm/tools/index.js +8 -0
  226. package/dist-lib/esm/tools/textureUtils.d.ts +18 -0
  227. package/dist-lib/esm/tools/textureUtils.js +288 -0
  228. package/dist-lib/esm/types.d.ts +164 -0
  229. package/dist-lib/esm/types.js +1 -0
  230. package/dist-lib/esm/uv/UVUnwrapController.d.ts +108 -0
  231. package/dist-lib/esm/uv/UVUnwrapController.js +1022 -0
  232. package/dist-lib/esm/voxel/CustomVoxelMesher.d.ts +15 -0
  233. package/dist-lib/esm/voxel/CustomVoxelMesher.js +169 -0
  234. package/dist-lib/esm/voxel/TextureAtlasManager.d.ts +54 -0
  235. package/dist-lib/esm/voxel/TextureAtlasManager.js +630 -0
  236. package/dist-lib/esm/voxel/VoxelPainterController.d.ts +100 -0
  237. package/dist-lib/esm/voxel/VoxelPainterController.js +671 -0
  238. package/dist-lib/esm/voxel/index.d.ts +4 -0
  239. package/dist-lib/esm/voxel/index.js +4 -0
  240. package/dist-lib/esm/voxel/types.d.ts +78 -0
  241. package/dist-lib/esm/voxel/types.js +1 -0
  242. package/dist-lib/esm/web-component/MeshEditorElement.d.ts +103 -0
  243. package/dist-lib/esm/web-component/MeshEditorElement.js +246 -0
  244. package/dist-lib/esm/web-component/index.d.ts +1 -0
  245. package/dist-lib/esm/web-component/index.js +1 -0
  246. package/dist-lib/index.umd.js +3 -0
  247. package/dist-lib/mesheditor.css +2 -0
  248. package/dist-lib/mesheditor.css.map +1 -0
  249. package/dist-lib/standalone/mesheditor-element.bundle.js +3 -0
  250. package/package.json +114 -4
  251. package/skills/README.md +48 -0
  252. package/skills/figure-sculptor.md +297 -0
  253. package/skills/image-to-mesh.md +18 -0
  254. package/skills/mesh-updater.md +27 -0
  255. package/skills/neural-3d-generator.md +53 -0
  256. package/skills/procedural-modeler.md +267 -0
  257. package/skills/voxel-painter.md +21 -0
  258. package/src/mesheditor.css +6477 -0
@@ -0,0 +1,832 @@
1
+ import { Mesh, MeshBuilder, Vector3, VertexData, } from '@babylonjs/core';
2
+ import * as earcutModule from 'earcut';
3
+ const earcut = earcutModule.default || earcutModule;
4
+ import { createRoundedBoxMesh } from './roundedBox';
5
+ function makeUpdatableMerged(parts, name, scene) {
6
+ const valid = parts.filter((p) => p && !p.isDisposed() && p.getTotalVertices() > 0);
7
+ if (valid.length === 0) {
8
+ return MeshBuilder.CreateBox(name, { size: 0.1, updatable: true }, scene);
9
+ }
10
+ for (const p of valid) {
11
+ p.computeWorldMatrix(true);
12
+ }
13
+ const merged = valid.length === 1
14
+ ? valid[0]
15
+ : Mesh.MergeMeshes(valid, true, true, undefined, false, false);
16
+ if (!merged) {
17
+ return valid[0];
18
+ }
19
+ merged.name = name;
20
+ const vd = VertexData.ExtractFromMesh(merged);
21
+ vd.applyToMesh(merged, true);
22
+ return merged;
23
+ }
24
+ /**
25
+ * Generates 2D contour points (x, z in ExtrudePolygon's XZ plane, mapping to wall X and wall Y)
26
+ * for a window/door opening.
27
+ */
28
+ function buildOpeningContour2D(op, wallW, wallH) {
29
+ const type = (op.type || 'rect').toLowerCase();
30
+ const w = Math.max(0.1, Math.min(wallW - 0.1, Number(op.width ?? 0.9)));
31
+ const h = Math.max(0.1, Math.min(wallH - 0.05, Number(op.height ?? 1.4)));
32
+ const cx = Math.max(-wallW * 0.5 + w * 0.5 + 0.03, Math.min(wallW * 0.5 - w * 0.5 - 0.03, Number(op.x ?? 0)));
33
+ const y0 = Math.max(0.005, Math.min(wallH - h - 0.03, Number(op.y ?? 0)));
34
+ const hw = w * 0.5;
35
+ const pts = [];
36
+ if (type === 'round') {
37
+ const r = Math.min(hw, h * 0.5);
38
+ const cy = y0 + h * 0.5;
39
+ const segs = 16;
40
+ for (let i = 0; i < segs; i++) {
41
+ const a = (i / segs) * Math.PI * 2;
42
+ pts.push(new Vector3(cx + Math.cos(a) * r, 0, cy + Math.sin(a) * r));
43
+ }
44
+ return pts;
45
+ }
46
+ if (type === 'arch') {
47
+ const archRise = Math.min(h * 0.6, Number(op.archRise ?? hw));
48
+ const springY = y0 + Math.max(0.05, h - archRise);
49
+ pts.push(new Vector3(cx - hw, 0, y0));
50
+ pts.push(new Vector3(cx + hw, 0, y0));
51
+ pts.push(new Vector3(cx + hw, 0, springY));
52
+ const segs = 12;
53
+ for (let i = 1; i < segs; i++) {
54
+ const t = (i / segs) * Math.PI;
55
+ pts.push(new Vector3(cx + Math.cos(t) * hw, 0, springY + Math.sin(t) * archRise));
56
+ }
57
+ pts.push(new Vector3(cx - hw, 0, springY));
58
+ return pts;
59
+ }
60
+ // Rectangular opening
61
+ pts.push(new Vector3(cx - hw, 0, y0));
62
+ pts.push(new Vector3(cx + hw, 0, y0));
63
+ pts.push(new Vector3(cx + hw, 0, y0 + h));
64
+ pts.push(new Vector3(cx - hw, 0, y0 + h));
65
+ return pts;
66
+ }
67
+ /**
68
+ * Builds a parametric architectural wall standing on Y = 0, centered on X = 0, Z = 0,
69
+ * with cleanly cut openings, optional window/door frames, plinth, and cornice.
70
+ */
71
+ export function buildWallMesh(scene, name, opts = {}) {
72
+ const width = Math.max(0.2, Number(opts.width ?? 4.0));
73
+ const height = Math.max(0.2, Number(opts.height ?? 3.0));
74
+ const thickness = Math.max(0.05, Number(opts.thickness ?? 0.25));
75
+ const openings = Array.isArray(opts.openings) ? opts.openings : [];
76
+ const parts = [];
77
+ if (openings.length === 0) {
78
+ const bevel = opts.bevelRadius !== undefined ? Number(opts.bevelRadius) : Math.min(thickness * 0.08, 0.02);
79
+ const box = createRoundedBoxMesh(`${name}_body`, { width, height, depth: thickness, radius: bevel, segments: 2 }, scene);
80
+ box.position.set(0, height * 0.5, 0);
81
+ box.bakeCurrentTransformIntoVertices();
82
+ parts.push(box);
83
+ }
84
+ else {
85
+ const hw = width * 0.5;
86
+ const outer = [
87
+ new Vector3(-hw, 0, 0),
88
+ new Vector3(hw, 0, 0),
89
+ new Vector3(hw, 0, height),
90
+ new Vector3(-hw, 0, height),
91
+ ];
92
+ const holes = openings.map((op) => buildOpeningContour2D(op, width, height));
93
+ // ExtrudePolygon extrudes downward along -Y in local space from Y=0 to Y=-depth
94
+ const poly = MeshBuilder.ExtrudePolygon(`${name}_poly`, {
95
+ shape: outer,
96
+ holes,
97
+ depth: thickness,
98
+ sideOrientation: Mesh.DOUBLESIDE,
99
+ updatable: true,
100
+ }, scene, earcut);
101
+ // Rotate -90 deg around X so local Z (0..height) maps to world +Y (0..height),
102
+ // and local Y (-thickness..0) maps to world Z (-thickness/2..+thickness/2)
103
+ poly.rotation.x = -Math.PI / 2;
104
+ poly.position.set(0, 0, -thickness * 0.5);
105
+ poly.bakeCurrentTransformIntoVertices();
106
+ parts.push(poly);
107
+ // Optional frames around openings
108
+ for (let i = 0; i < openings.length; i++) {
109
+ const op = openings[i];
110
+ if (op.frame === false)
111
+ continue;
112
+ const contour = holes[i];
113
+ if (!contour || contour.length < 3)
114
+ continue;
115
+ const fw = Math.max(0.02, Number(op.frameWidth ?? 0.06));
116
+ const fd = Math.max(thickness + 0.02, Number(op.frameDepth ?? thickness + 0.05));
117
+ for (let j = 0; j < contour.length; j++) {
118
+ const p0 = contour[j];
119
+ const p1 = contour[(j + 1) % contour.length];
120
+ // Skip bottom sill for doors touching ground (y <= 0.02)
121
+ if (Math.abs(p0.z) < 0.02 && Math.abs(p1.z) < 0.02)
122
+ continue;
123
+ const yStart = p0.z < 0.02 ? fw * 0.25 : p0.z;
124
+ const yEnd = p1.z < 0.02 ? fw * 0.25 : p1.z;
125
+ const dx = p1.x - p0.x;
126
+ const dy = yEnd - yStart;
127
+ const segLen = Math.hypot(dx, dy);
128
+ if (segLen < 1e-3)
129
+ continue;
130
+ const bar = MeshBuilder.CreateBox(`${name}_frame_${i}_${j}`, { width: segLen + fw * 0.4, height: fw, depth: fd }, scene);
131
+ bar.rotation.z = Math.atan2(dy, dx);
132
+ bar.position.set((p0.x + p1.x) * 0.5, (yStart + yEnd) * 0.5, 0);
133
+ bar.bakeCurrentTransformIntoVertices();
134
+ parts.push(bar);
135
+ }
136
+ }
137
+ }
138
+ // Optional plinth band at base
139
+ const plinthH = Number(opts.plinthHeight ?? 0);
140
+ if (plinthH > 0.01 && openings.every((o) => (o.y ?? 1) > plinthH)) {
141
+ const plinth = createRoundedBoxMesh(`${name}_plinth`, { width: width + 0.06, height: plinthH, depth: thickness + 0.06, radius: 0.015, segments: 2 }, scene);
142
+ plinth.position.set(0, plinthH * 0.5, 0);
143
+ plinth.bakeCurrentTransformIntoVertices();
144
+ parts.push(plinth);
145
+ }
146
+ // Optional cornice band at top
147
+ const corniceH = Number(opts.corniceHeight ?? 0);
148
+ if (corniceH > 0.01) {
149
+ const cornice = createRoundedBoxMesh(`${name}_cornice`, { width: width + 0.1, height: corniceH, depth: thickness + 0.1, radius: 0.02, segments: 2 }, scene);
150
+ cornice.position.set(0, height - corniceH * 0.5, 0);
151
+ cornice.bakeCurrentTransformIntoVertices();
152
+ parts.push(cornice);
153
+ }
154
+ return makeUpdatableMerged(parts, name, scene);
155
+ }
156
+ /**
157
+ * Builds a freestanding architectural arch / portal standing on Y = 0.
158
+ */
159
+ export function buildArchMesh(scene, name, opts = {}) {
160
+ const style = (opts.style || 'round').toLowerCase();
161
+ const span = Math.max(0.3, Number(opts.span ?? 2.0));
162
+ const halfSpan = span * 0.5;
163
+ const pierHeight = Math.max(0, Number(opts.pierHeight ?? 1.8));
164
+ const pierWidth = Math.max(0.1, Number(opts.pierWidth ?? 0.35));
165
+ const thickness = Math.max(0.08, Number(opts.thickness ?? pierWidth));
166
+ const depth = Math.max(0.1, Number(opts.depth ?? 0.4));
167
+ const rise = Math.max(0.1, Number(opts.rise ?? (style === 'pointed' ? span * 0.65 : style === 'segmental' ? span * 0.28 : halfSpan)));
168
+ const segs = Math.max(6, Math.min(48, Math.round(opts.segments ?? 16)));
169
+ const parts = [];
170
+ // 1. Left and Right Piers + Plinth Bases + Impost Caps
171
+ if (pierHeight > 0.02) {
172
+ for (const side of [-1, 1]) {
173
+ const px = side * (halfSpan + pierWidth * 0.5);
174
+ const pier = createRoundedBoxMesh(`${name}_pier_${side}`, { width: pierWidth, height: pierHeight, depth, radius: Math.min(0.02, pierWidth * 0.08), segments: 2 }, scene);
175
+ pier.position.set(px, pierHeight * 0.5, 0);
176
+ pier.bakeCurrentTransformIntoVertices();
177
+ parts.push(pier);
178
+ // Base plinth
179
+ const baseH = Math.min(0.16, pierHeight * 0.12);
180
+ const base = createRoundedBoxMesh(`${name}_pier_base_${side}`, { width: pierWidth + 0.08, height: baseH, depth: depth + 0.08, radius: 0.018, segments: 2 }, scene);
181
+ base.position.set(px, baseH * 0.5, 0);
182
+ base.bakeCurrentTransformIntoVertices();
183
+ parts.push(base);
184
+ // Impost cap at spring line
185
+ if (opts.impostCap !== false) {
186
+ const capH = Math.min(0.12, pierHeight * 0.08 + 0.04);
187
+ const cap = createRoundedBoxMesh(`${name}_impost_${side}`, { width: pierWidth + 0.08, height: capH, depth: depth + 0.08, radius: 0.018, segments: 2 }, scene);
188
+ cap.position.set(px, pierHeight - capH * 0.5, 0);
189
+ cap.bakeCurrentTransformIntoVertices();
190
+ parts.push(cap);
191
+ }
192
+ }
193
+ }
194
+ // 2. Arch Ring (voussoir band above pierHeight)
195
+ const evalInnerArch = (t) => {
196
+ if (style === 'flat') {
197
+ const x = halfSpan * (1 - 2 * t);
198
+ return { x, y: pierHeight, nx: 0, ny: 1 };
199
+ }
200
+ if (style === 'pointed') {
201
+ // Gothic lancet arch: two intersecting arcs meeting at apex (t = 0.5)
202
+ if (t <= 0.5) {
203
+ const u = t * 2; // 0 (right spring) to 1 (apex)
204
+ const angle = (u * Math.PI) / 2;
205
+ const x = halfSpan * Math.cos(angle);
206
+ const y = pierHeight + rise * Math.sin(angle);
207
+ const nx = Math.cos(angle);
208
+ const ny = Math.sin(angle);
209
+ return { x, y, nx, ny };
210
+ }
211
+ else {
212
+ const u = (1 - t) * 2; // 1 (apex) to 0 (left spring)
213
+ const angle = (u * Math.PI) / 2;
214
+ const x = -halfSpan * Math.cos(angle);
215
+ const y = pierHeight + rise * Math.sin(angle);
216
+ const nx = -Math.cos(angle);
217
+ const ny = Math.sin(angle);
218
+ return { x, y, nx, ny };
219
+ }
220
+ }
221
+ if (style === 'horseshoe') {
222
+ const startAngle = -0.22 * Math.PI;
223
+ const endAngle = 1.22 * Math.PI;
224
+ const angle = startAngle + t * (endAngle - startAngle);
225
+ const x = Math.cos(angle) * halfSpan;
226
+ const y = pierHeight + rise * 0.35 + Math.sin(angle) * rise;
227
+ return { x, y, nx: Math.cos(angle), ny: Math.sin(angle) };
228
+ }
229
+ // Round or segmental arch
230
+ const angle = t * Math.PI;
231
+ const x = Math.cos(angle) * halfSpan;
232
+ const y = pierHeight + Math.sin(angle) * rise;
233
+ const len = Math.hypot(Math.cos(angle) / halfSpan, Math.sin(angle) / rise) || 1;
234
+ const nx = Math.cos(angle) / halfSpan / len;
235
+ const ny = Math.sin(angle) / rise / len;
236
+ return { x, y, nx, ny };
237
+ };
238
+ const innerPts = [];
239
+ const outerPts = [];
240
+ for (let i = 0; i <= segs; i++) {
241
+ const t = i / segs;
242
+ const p = evalInnerArch(t);
243
+ innerPts.push(new Vector3(p.x, 0, p.y));
244
+ outerPts.push(new Vector3(p.x + p.nx * thickness, 0, p.y + p.ny * thickness));
245
+ }
246
+ const ringContour = [...innerPts, ...outerPts.reverse()];
247
+ const ringMesh = MeshBuilder.ExtrudePolygon(`${name}_ring`, {
248
+ shape: ringContour,
249
+ depth,
250
+ sideOrientation: Mesh.DOUBLESIDE,
251
+ updatable: true,
252
+ }, scene, earcut);
253
+ ringMesh.rotation.x = -Math.PI / 2;
254
+ ringMesh.position.set(0, 0, -depth * 0.5);
255
+ ringMesh.bakeCurrentTransformIntoVertices();
256
+ parts.push(ringMesh);
257
+ // 3. Optional Keystone at apex
258
+ if (opts.keystone !== false) {
259
+ const apex = evalInnerArch(0.5);
260
+ const keyH = thickness * 1.22;
261
+ const keyW = Math.max(0.1, span * 0.11);
262
+ const keystone = createRoundedBoxMesh(`${name}_keystone`, { width: keyW, height: keyH, depth: depth + 0.06, radius: 0.015, segments: 2 }, scene);
263
+ keystone.position.set(0, apex.y + thickness * 0.52, 0);
264
+ keystone.bakeCurrentTransformIntoVertices();
265
+ parts.push(keystone);
266
+ }
267
+ return makeUpdatableMerged(parts, name, scene);
268
+ }
269
+ /**
270
+ * Builds a classical or modern column standing on Y = 0 up to Y = height,
271
+ * with plinth, torus base, fluted + entasis shaft, necking ring, echinus capital, and abacus.
272
+ */
273
+ export function buildColumnMesh(scene, name, opts = {}) {
274
+ const order = (opts.order || 'doric').toLowerCase();
275
+ const totalHeight = Math.max(0.4, Number(opts.height ?? 3.2));
276
+ const baseRadius = Math.max(0.05, Number(opts.radius ?? 0.24));
277
+ const taper = Math.max(0.5, Math.min(1.0, Number(opts.taper ?? (order === 'modern' ? 1.0 : 0.85))));
278
+ const entasis = Math.max(0, Math.min(0.15, Number(opts.entasis ?? (order === 'modern' ? 0 : 0.025))));
279
+ const defaultFlutes = order === 'tuscan' || order === 'modern' ? 0 : order === 'doric' ? 20 : 24;
280
+ const flutes = Math.max(0, Math.min(40, Math.round(opts.flutes ?? defaultFlutes)));
281
+ const fluteDepth = Math.max(0, Number(opts.fluteDepth ?? baseRadius * 0.065));
282
+ const radialSegs = Math.max(16, Math.min(96, flutes > 0 ? flutes * 4 : Math.round(opts.radialSegments ?? 28)));
283
+ const parts = [];
284
+ const hasPlinth = order !== 'modern';
285
+ const baseHeight = hasPlinth ? totalHeight * 0.09 : 0;
286
+ const capHeight = order !== 'modern' ? totalHeight * 0.1 : 0;
287
+ const shaftY0 = baseHeight;
288
+ const shaftY1 = totalHeight - capHeight;
289
+ const shaftHeight = Math.max(0.1, shaftY1 - shaftY0);
290
+ // 1. Plinth & Molded Base
291
+ if (hasPlinth) {
292
+ const plinthH = baseHeight * 0.45;
293
+ const plinthW = baseRadius * 2.65;
294
+ const plinth = createRoundedBoxMesh(`${name}_plinth`, { width: plinthW, height: plinthH, depth: plinthW, radius: 0.015, segments: 2 }, scene);
295
+ plinth.position.set(0, plinthH * 0.5, 0);
296
+ plinth.bakeCurrentTransformIntoVertices();
297
+ parts.push(plinth);
298
+ const torusBaseProfile = [
299
+ new Vector3(baseRadius * 1.25, plinthH, 0),
300
+ new Vector3(baseRadius * 1.32, plinthH + baseHeight * 0.2, 0),
301
+ new Vector3(baseRadius * 1.15, plinthH + baseHeight * 0.4, 0),
302
+ new Vector3(baseRadius * 1.20, plinthH + baseHeight * 0.65, 0),
303
+ new Vector3(baseRadius, shaftY0, 0),
304
+ ];
305
+ const baseLathe = MeshBuilder.CreateLathe(`${name}_base_torus`, { shape: torusBaseProfile, tessellation: Math.min(36, radialSegs), sideOrientation: Mesh.FRONTSIDE }, scene);
306
+ parts.push(baseLathe);
307
+ }
308
+ // 2. Parametric Fluted & Entasis Shaft
309
+ const vSegs = 24;
310
+ const shaftPos = [];
311
+ const shaftNorm = [];
312
+ const shaftUV = [];
313
+ const shaftIdx = [];
314
+ for (let iv = 0; iv <= vSegs; iv++) {
315
+ const v = iv / vSegs;
316
+ const y = shaftY0 + v * shaftHeight;
317
+ // Classical entasis peaks around 1/3 up the shaft
318
+ const entasisBulge = Math.sin(v * Math.PI) * entasis * baseRadius;
319
+ const ringR = baseRadius * (1 - (1 - taper) * v) + entasisBulge;
320
+ // Fade flutes out smoothly at bottom and top 8% with smoothstep
321
+ const fade = 0.08;
322
+ let fluteEnvelope = 1.0;
323
+ if (v < fade) {
324
+ const t = v / fade;
325
+ fluteEnvelope = t * t * (3 - 2 * t);
326
+ }
327
+ else if (v > 1 - fade) {
328
+ const t = (1 - v) / fade;
329
+ fluteEnvelope = t * t * (3 - 2 * t);
330
+ }
331
+ if (flutes === 0)
332
+ fluteEnvelope = 0;
333
+ for (let iu = 0; iu <= radialSegs; iu++) {
334
+ const u = iu / radialSegs;
335
+ const theta = u * Math.PI * 2;
336
+ let r = ringR;
337
+ if (flutes > 0) {
338
+ const fluteWave = Math.pow(Math.max(0, Math.sin(theta * flutes)), 0.65);
339
+ r -= fluteDepth * fluteEnvelope * fluteWave;
340
+ }
341
+ const x = Math.cos(theta) * r;
342
+ const z = Math.sin(theta) * r;
343
+ shaftPos.push(x, y, z);
344
+ shaftUV.push(u, v);
345
+ }
346
+ }
347
+ const stride = radialSegs + 1;
348
+ for (let iv = 0; iv < vSegs; iv++) {
349
+ for (let iu = 0; iu < radialSegs; iu++) {
350
+ const i00 = iv * stride + iu;
351
+ const i10 = iv * stride + (iu + 1);
352
+ const i01 = (iv + 1) * stride + iu;
353
+ const i11 = (iv + 1) * stride + (iu + 1);
354
+ // Correct outward normal winding:
355
+ shaftIdx.push(i00, i10, i11, i00, i11, i01);
356
+ }
357
+ }
358
+ const shaftMesh = new Mesh(`${name}_shaft`, scene);
359
+ const shaftVD = new VertexData();
360
+ const shaftNormalsComputed = new Float32Array(shaftPos.length);
361
+ VertexData.ComputeNormals(shaftPos, shaftIdx, shaftNormalsComputed);
362
+ shaftVD.positions = shaftPos;
363
+ shaftVD.indices = shaftIdx;
364
+ shaftVD.normals = shaftNormalsComputed;
365
+ shaftVD.uvs = shaftUV;
366
+ shaftVD.applyToMesh(shaftMesh, true);
367
+ parts.push(shaftMesh);
368
+ // 3. Capital (Astragal necking + Echinus flare + Abacus slab + Order ornaments)
369
+ if (capHeight > 0) {
370
+ const topR = baseRadius * taper;
371
+ const abacusH = capHeight * 0.35;
372
+ const echinusTopY = totalHeight - abacusH;
373
+ const capProfile = [
374
+ new Vector3(topR * 1.05, shaftY1, 0),
375
+ new Vector3(topR * 1.02, shaftY1 + capHeight * 0.15, 0),
376
+ new Vector3(topR * 1.25, shaftY1 + capHeight * 0.45, 0),
377
+ new Vector3(topR * (order === 'corinthian' ? 1.45 : 1.35), echinusTopY, 0),
378
+ ];
379
+ const echinus = MeshBuilder.CreateLathe(`${name}_echinus`, { shape: capProfile, tessellation: Math.min(36, radialSegs), sideOrientation: Mesh.FRONTSIDE }, scene);
380
+ parts.push(echinus);
381
+ const abacusW = topR * 2.85;
382
+ const abacus = createRoundedBoxMesh(`${name}_abacus`, { width: abacusW, height: abacusH, depth: abacusW, radius: 0.015, segments: 2 }, scene);
383
+ abacus.position.set(0, totalHeight - abacusH * 0.5, 0);
384
+ abacus.bakeCurrentTransformIntoVertices();
385
+ parts.push(abacus);
386
+ if (order === 'ionic') {
387
+ // Proportional Ionic volute scroll discs under the abacus
388
+ const voluteR = capHeight * 0.32;
389
+ const voluteThick = topR * 0.45;
390
+ for (const sx of [-1, 1]) {
391
+ const volute = MeshBuilder.CreateCylinder(`${name}_volute_${sx}`, {
392
+ height: voluteThick,
393
+ diameter: voluteR * 2,
394
+ tessellation: 24,
395
+ }, scene);
396
+ volute.rotation.x = Math.PI / 2;
397
+ volute.position.set(sx * (topR * 1.05), echinusTopY - voluteR * 0.65, 0);
398
+ volute.bakeCurrentTransformIntoVertices();
399
+ parts.push(volute);
400
+ }
401
+ }
402
+ }
403
+ return makeUpdatableMerged(parts, name, scene);
404
+ }
405
+ /**
406
+ * Generates 2D cross-section profile points (x = outward projection, y = vertical height) for moldings.
407
+ */
408
+ function getMoldingProfilePoints(preset, width, height, custom) {
409
+ if (custom && custom.length >= 3) {
410
+ return custom.map((p) => Array.isArray(p) ? [Number(p[0]) || 0, Number(p[1]) || 0] : [Number(p.x) || 0, Number(p.y) || 0]);
411
+ }
412
+ const w = width;
413
+ const h = height;
414
+ switch (preset) {
415
+ case 'cove':
416
+ return [
417
+ [0, 0],
418
+ [w * 0.3, 0],
419
+ [w * 0.45, h * 0.35],
420
+ [w * 0.72, h * 0.68],
421
+ [w, h * 0.85],
422
+ [w, h],
423
+ [0, h],
424
+ ];
425
+ case 'torus':
426
+ return [
427
+ [0, 0],
428
+ [w * 0.65, 0],
429
+ [w * 0.92, h * 0.25],
430
+ [w, h * 0.5],
431
+ [w * 0.92, h * 0.75],
432
+ [w * 0.65, h],
433
+ [0, h],
434
+ ];
435
+ case 'baseboard':
436
+ return [
437
+ [0, 0],
438
+ [w, 0],
439
+ [w, h * 0.7],
440
+ [w * 0.8, h * 0.85],
441
+ [w * 0.5, h * 0.94],
442
+ [0, h],
443
+ ];
444
+ case 'stepped':
445
+ return [
446
+ [0, 0],
447
+ [w * 0.4, 0],
448
+ [w * 0.4, h * 0.35],
449
+ [w * 0.7, h * 0.35],
450
+ [w * 0.7, h * 0.7],
451
+ [w, h * 0.7],
452
+ [w, h],
453
+ [0, h],
454
+ ];
455
+ case 'ogee':
456
+ case 'cyma_recta':
457
+ case 'cornice':
458
+ default:
459
+ return [
460
+ [0, 0],
461
+ [w * 0.35, 0],
462
+ [w * 0.35, h * 0.2],
463
+ [w * 0.62, h * 0.38],
464
+ [w * 0.82, h * 0.65],
465
+ [w, h * 0.82],
466
+ [w, h],
467
+ [0, h],
468
+ ];
469
+ }
470
+ }
471
+ /**
472
+ * Sweeps a 2D molding profile along a 3D polyline with exact tangent-bisector mitered corners
473
+ * so rectangular frames, room baseboards, and roof cornices have sharp, distortion-free corners.
474
+ */
475
+ export function buildMoldingMesh(scene, name, opts) {
476
+ const rawPath = Array.isArray(opts.path) ? opts.path : [];
477
+ const pathPts = rawPath.map((p) => Array.isArray(p)
478
+ ? new Vector3(Number(p[0]) || 0, Number(p[1]) || 0, Number(p[2]) || 0)
479
+ : new Vector3(Number(p?.x) || 0, Number(p?.y) || 0, Number(p?.z) || 0));
480
+ if (pathPts.length < 2) {
481
+ throw new Error('modeler_create_molding requires at least 2 path points.');
482
+ }
483
+ const closed = !!opts.closed;
484
+ const width = Math.max(0.01, Number(opts.width ?? 0.15));
485
+ const height = Math.max(0.01, Number(opts.height ?? 0.18));
486
+ const profile = getMoldingProfilePoints(opts.profile || 'cornice', width, height, opts.profilePoints);
487
+ const profCount = profile.length;
488
+ const nPts = pathPts.length;
489
+ // Compute segment tangents and outward horizontal normals
490
+ const segTangents = [];
491
+ const segCount = closed ? nPts : nPts - 1;
492
+ for (let i = 0; i < segCount; i++) {
493
+ const p0 = pathPts[i];
494
+ const p1 = pathPts[(i + 1) % nPts];
495
+ const dir = p1.subtract(p0);
496
+ if (dir.lengthSquared() < 1e-8) {
497
+ segTangents.push(new Vector3(1, 0, 0));
498
+ }
499
+ else {
500
+ segTangents.push(dir.normalize());
501
+ }
502
+ }
503
+ const up = Vector3.Up();
504
+ const positions = [];
505
+ const uvs = [];
506
+ const indices = [];
507
+ let accumLen = 0;
508
+ for (let i = 0; i < nPts; i++) {
509
+ if (i > 0) {
510
+ accumLen += Vector3.Distance(pathPts[i - 1], pathPts[i]);
511
+ }
512
+ const tPrev = closed ? segTangents[(i - 1 + segCount) % segCount] : segTangents[Math.max(0, i - 1)];
513
+ const tNext = closed ? segTangents[i % segCount] : segTangents[Math.min(segCount - 1, i)];
514
+ // Outward right-hand normal in XZ plane: (t.z, 0, -t.x) or up x tangent
515
+ let nPrev = Vector3.Cross(up, tPrev);
516
+ if (nPrev.lengthSquared() < 1e-6)
517
+ nPrev = new Vector3(1, 0, 0);
518
+ else
519
+ nPrev.normalize();
520
+ let nNext = Vector3.Cross(up, tNext);
521
+ if (nNext.lengthSquared() < 1e-6)
522
+ nNext = new Vector3(1, 0, 0);
523
+ else
524
+ nNext.normalize();
525
+ let miter = nPrev.add(nNext);
526
+ if (miter.lengthSquared() < 1e-6) {
527
+ miter = nNext.clone();
528
+ }
529
+ else {
530
+ miter.normalize();
531
+ }
532
+ const dot = Math.max(0.25, Vector3.Dot(miter, nNext));
533
+ const miterScale = Math.min(2.5, 1.0 / dot);
534
+ const outVec = miter.scale(miterScale);
535
+ const pt = pathPts[i];
536
+ for (let j = 0; j < profCount; j++) {
537
+ const [px, py] = profile[j];
538
+ const vx = pt.x + outVec.x * px;
539
+ const vy = pt.y + py;
540
+ const vz = pt.z + outVec.z * px;
541
+ positions.push(vx, vy, vz);
542
+ uvs.push(accumLen, j / profCount);
543
+ }
544
+ }
545
+ const ringCount = closed ? nPts : nPts - 1;
546
+ for (let i = 0; i < ringCount; i++) {
547
+ const r0 = i * profCount;
548
+ const r1 = ((i + 1) % nPts) * profCount;
549
+ for (let j = 0; j < profCount; j++) {
550
+ const jNext = (j + 1) % profCount;
551
+ const a = r0 + j;
552
+ const b = r1 + j;
553
+ const c = r1 + jNext;
554
+ const d = r0 + jNext;
555
+ indices.push(a, c, b, a, d, c);
556
+ }
557
+ }
558
+ // Cap open ends
559
+ if (!closed && profCount >= 3) {
560
+ const startBase = 0;
561
+ const endBase = (nPts - 1) * profCount;
562
+ for (let j = 1; j < profCount - 1; j++) {
563
+ indices.push(startBase, startBase + j + 1, startBase + j);
564
+ indices.push(endBase, endBase + j, endBase + j + 1);
565
+ }
566
+ }
567
+ const mesh = new Mesh(name, scene);
568
+ const vd = new VertexData();
569
+ const normals = new Float32Array(positions.length);
570
+ VertexData.ComputeNormals(positions, indices, normals);
571
+ vd.positions = positions;
572
+ vd.indices = indices;
573
+ vd.normals = normals;
574
+ vd.uvs = uvs;
575
+ vd.applyToMesh(mesh, true);
576
+ return mesh;
577
+ }
578
+ /**
579
+ * Builds straight, spiral, or L-shaped staircases standing on Y = 0, with beveled treads,
580
+ * stringers/risers, and optional railings.
581
+ */
582
+ export function buildStairsMesh(scene, name, opts = {}) {
583
+ const style = (opts.style || 'straight').toLowerCase();
584
+ const steps = Math.max(2, Math.min(80, Math.round(opts.steps ?? 10)));
585
+ const width = Math.max(0.3, Number(opts.width ?? 1.2));
586
+ const stepRise = opts.stepRise !== undefined
587
+ ? Math.max(0.05, Number(opts.stepRise))
588
+ : opts.height !== undefined
589
+ ? Math.max(0.05, Number(opts.height) / steps)
590
+ : 0.18;
591
+ const stepRun = opts.stepRun !== undefined
592
+ ? Math.max(0.08, Number(opts.stepRun))
593
+ : opts.length !== undefined
594
+ ? Math.max(0.08, Number(opts.length) / steps)
595
+ : 0.28;
596
+ const nosing = Math.max(0, Number(opts.nosing ?? 0.025));
597
+ const railingOpt = opts.railing === true
598
+ ? 'both'
599
+ : opts.railing === false || !opts.railing
600
+ ? 'none'
601
+ : String(opts.railing).toLowerCase();
602
+ const railingHeight = Math.max(0.4, Number(opts.railingHeight ?? 0.9));
603
+ const parts = [];
604
+ if (style === 'spiral') {
605
+ const innerR = Math.max(0.08, Number(opts.innerRadius ?? 0.2));
606
+ const outerR = innerR + width;
607
+ const totalAngleRad = ((Number(opts.totalAngleDegrees ?? 270) || 270) * Math.PI) / 180;
608
+ const dAngle = totalAngleRad / steps;
609
+ const totalH = steps * stepRise;
610
+ // Central column
611
+ const centerPole = MeshBuilder.CreateCylinder(`${name}_center_pole`, { height: totalH + railingHeight, diameter: innerR * 2, tessellation: 20 }, scene);
612
+ centerPole.position.set(0, (totalH + railingHeight) * 0.5, 0);
613
+ centerPole.bakeCurrentTransformIntoVertices();
614
+ parts.push(centerPole);
615
+ const railPath = [];
616
+ for (let i = 0; i < steps; i++) {
617
+ const a = (i + 0.5) * dAngle;
618
+ const yTop = (i + 1) * stepRise;
619
+ const midR = (innerR + outerR) * 0.5;
620
+ const arcChord = Math.max(0.12, midR * Math.abs(dAngle) + nosing);
621
+ const treadH = Math.min(0.06, stepRise * 0.4);
622
+ const tread = createRoundedBoxMesh(`${name}_spiral_tread_${i}`, { width: outerR - innerR, height: treadH, depth: arcChord, radius: 0.01, segments: 2 }, scene);
623
+ tread.rotation.y = -a;
624
+ tread.position.set(Math.cos(a) * midR, yTop - treadH * 0.5, Math.sin(a) * midR);
625
+ tread.bakeCurrentTransformIntoVertices();
626
+ parts.push(tread);
627
+ if (railingOpt !== 'none') {
628
+ const rx = Math.cos(a) * (outerR - 0.05);
629
+ const rz = Math.sin(a) * (outerR - 0.05);
630
+ const post = MeshBuilder.CreateCylinder(`${name}_spiral_post_${i}`, { height: railingHeight, diameter: 0.035, tessellation: 10 }, scene);
631
+ post.position.set(rx, yTop + railingHeight * 0.5, rz);
632
+ post.bakeCurrentTransformIntoVertices();
633
+ parts.push(post);
634
+ railPath.push(new Vector3(rx, yTop + railingHeight, rz));
635
+ }
636
+ }
637
+ if (railPath.length >= 2) {
638
+ const handrail = MeshBuilder.CreateTube(`${name}_spiral_rail`, { path: railPath, radius: 0.028, tessellation: 12 }, scene);
639
+ parts.push(handrail);
640
+ }
641
+ return makeUpdatableMerged(parts, name, scene);
642
+ }
643
+ // Straight staircase along +Z (from z = 0 to z = steps * stepRun, y = 0 to y = steps * stepRise)
644
+ const treadThickness = Math.min(0.055, stepRise * 0.35);
645
+ const includeSolidSteps = opts.stringers !== false;
646
+ for (let i = 0; i < steps; i++) {
647
+ const yTop = (i + 1) * stepRise;
648
+ const zCenter = (i + 0.5) * stepRun;
649
+ // Beveled tread with front nosing overhang
650
+ const tread = createRoundedBoxMesh(`${name}_tread_${i}`, {
651
+ width: width + nosing,
652
+ height: treadThickness,
653
+ depth: stepRun + nosing,
654
+ radius: Math.min(0.012, treadThickness * 0.35),
655
+ segments: 2,
656
+ }, scene);
657
+ tread.position.set(0, yTop - treadThickness * 0.5, zCenter - nosing * 0.5);
658
+ tread.bakeCurrentTransformIntoVertices();
659
+ parts.push(tread);
660
+ // Riser / step body
661
+ if (includeSolidSteps) {
662
+ const bodyH = yTop - treadThickness;
663
+ if (bodyH > 0.01) {
664
+ const riserBox = MeshBuilder.CreateBox(`${name}_riser_${i}`, { width, height: bodyH, depth: stepRun }, scene);
665
+ riserBox.position.set(0, bodyH * 0.5, zCenter);
666
+ riserBox.bakeCurrentTransformIntoVertices();
667
+ parts.push(riserBox);
668
+ }
669
+ }
670
+ }
671
+ // Optional Railings (left: -X, right: +X, or both)
672
+ const sides = [];
673
+ if (railingOpt === 'left' || railingOpt === 'both')
674
+ sides.push(-1);
675
+ if (railingOpt === 'right' || railingOpt === 'both')
676
+ sides.push(1);
677
+ for (const side of sides) {
678
+ const rx = side * (width * 0.5 - 0.04);
679
+ const railPts = [];
680
+ for (let i = 0; i < steps; i++) {
681
+ const yTop = (i + 1) * stepRise;
682
+ const rz = (i + 0.5) * stepRun;
683
+ const isEndPost = i === 0 || i === steps - 1;
684
+ const post = MeshBuilder.CreateCylinder(`${name}_post_${side}_${i}`, {
685
+ height: railingHeight,
686
+ diameter: isEndPost ? 0.065 : 0.032,
687
+ tessellation: 10,
688
+ }, scene);
689
+ post.position.set(rx, yTop + railingHeight * 0.5, rz);
690
+ post.bakeCurrentTransformIntoVertices();
691
+ parts.push(post);
692
+ railPts.push(new Vector3(rx, yTop + railingHeight, rz));
693
+ }
694
+ if (railPts.length >= 2) {
695
+ const rail = MeshBuilder.CreateTube(`${name}_handrail_${side}`, { path: railPts, radius: 0.028, tessellation: 12, cap: Mesh.CAP_ALL }, scene);
696
+ parts.push(rail);
697
+ }
698
+ }
699
+ return makeUpdatableMerged(parts, name, scene);
700
+ }
701
+ /**
702
+ * Builds a parametric architectural roof standing on Y = 0 with eaves overhang,
703
+ * real fascia slab thickness, ridge cap, and gable end walls.
704
+ */
705
+ export function buildRoofMesh(scene, name, opts = {}) {
706
+ const style = (opts.style || 'gable').toLowerCase();
707
+ const width = Math.max(0.4, Number(opts.width ?? 4.0));
708
+ const depth = Math.max(0.4, Number(opts.depth ?? 4.0));
709
+ const overhang = Math.max(0, Number(opts.overhang ?? 0.25));
710
+ const thickness = Math.max(0.04, Number(opts.thickness ?? 0.14));
711
+ const pitchRad = opts.pitchDegrees
712
+ ? (Math.max(5, Math.min(75, Number(opts.pitchDegrees))) * Math.PI) / 180
713
+ : Math.atan2(1.4, width * 0.5);
714
+ const height = opts.height !== undefined
715
+ ? Math.max(0.15, Number(opts.height))
716
+ : Math.max(0.2, width * 0.5 * Math.tan(pitchRad));
717
+ const totalW = width + overhang * 2;
718
+ const totalD = depth + overhang * 2;
719
+ const hw = totalW * 0.5;
720
+ const hd = totalD * 0.5;
721
+ const parts = [];
722
+ if (style === 'dome') {
723
+ const dome = MeshBuilder.CreateSphere(`${name}_dome`, {
724
+ diameterX: totalW,
725
+ diameterY: height * 2,
726
+ diameterZ: totalD,
727
+ slice: 0.5,
728
+ segments: 24,
729
+ sideOrientation: Mesh.DOUBLESIDE,
730
+ }, scene);
731
+ const vd = VertexData.ExtractFromMesh(dome);
732
+ vd.applyToMesh(dome, true);
733
+ return dome;
734
+ }
735
+ if (style === 'pyramid' || style === 'hip') {
736
+ const ridgeHalfLen = style === 'pyramid' ? 0 : Math.max(0, hd - hw);
737
+ const positions = [
738
+ // Bottom perimeter (0..3)
739
+ -hw, 0, -hd,
740
+ hw, 0, -hd,
741
+ hw, 0, hd,
742
+ -hw, 0, hd,
743
+ // Ridge endpoints (4, 5)
744
+ 0, height, -ridgeHalfLen,
745
+ 0, height, ridgeHalfLen,
746
+ ];
747
+ const indices = [
748
+ // Bottom quad
749
+ 0, 1, 2, 0, 2, 3,
750
+ // Front slope (-Z)
751
+ 0, 4, 1,
752
+ // Back slope (+Z)
753
+ 2, 5, 3,
754
+ // Right slope (+X)
755
+ 1, 4, 5, 1, 5, 2,
756
+ // Left slope (-X)
757
+ 3, 5, 4, 3, 4, 0,
758
+ ];
759
+ // Convert to flat-shaded crisp architectural faces by unsharing vertices per triangle
760
+ const flatPos = [];
761
+ const flatIdx = [];
762
+ const flatUV = [];
763
+ for (let i = 0; i < indices.length; i += 3) {
764
+ const a = indices[i] * 3;
765
+ const b = indices[i + 1] * 3;
766
+ const c = indices[i + 2] * 3;
767
+ const baseV = flatPos.length / 3;
768
+ flatPos.push(positions[a], positions[a + 1], positions[a + 2], positions[b], positions[b + 1], positions[b + 2], positions[c], positions[c + 1], positions[c + 2]);
769
+ flatUV.push(0, 0, 1, 0, 0.5, 1);
770
+ flatIdx.push(baseV, baseV + 1, baseV + 2);
771
+ }
772
+ const hipMesh = new Mesh(`${name}_hip`, scene);
773
+ const vd = new VertexData();
774
+ const normals = new Float32Array(flatPos.length);
775
+ VertexData.ComputeNormals(flatPos, flatIdx, normals);
776
+ vd.positions = flatPos;
777
+ vd.indices = flatIdx;
778
+ vd.normals = normals;
779
+ vd.uvs = flatUV;
780
+ vd.applyToMesh(hipMesh, true);
781
+ parts.push(hipMesh);
782
+ // Fascia eave trim band around base
783
+ const fascia = createRoundedBoxMesh(`${name}_fascia`, { width: totalW, height: thickness, depth: totalD, radius: 0.01, segments: 1 }, scene);
784
+ fascia.position.set(0, thickness * 0.5, 0);
785
+ fascia.bakeCurrentTransformIntoVertices();
786
+ parts.push(fascia);
787
+ return makeUpdatableMerged(parts, name, scene);
788
+ }
789
+ if (style === 'shed') {
790
+ const slopeLen = Math.hypot(totalW, height);
791
+ const angle = Math.atan2(height, totalW);
792
+ const slab = createRoundedBoxMesh(`${name}_shed_slab`, { width: slopeLen, height: thickness, depth: totalD, radius: 0.012, segments: 2 }, scene);
793
+ slab.rotation.z = angle;
794
+ slab.position.set(0, height * 0.5 + thickness * 0.5, 0);
795
+ slab.bakeCurrentTransformIntoVertices();
796
+ parts.push(slab);
797
+ return makeUpdatableMerged(parts, name, scene);
798
+ }
799
+ // Gable, Gambrel, or Mansard roof
800
+ const slopeRun = hw;
801
+ const slopeLen = Math.hypot(slopeRun, height);
802
+ const slopeAngle = Math.atan2(height, slopeRun);
803
+ for (const side of [-1, 1]) {
804
+ const slab = createRoundedBoxMesh(`${name}_slope_${side}`, { width: slopeLen + thickness * 0.5, height: thickness, depth: totalD, radius: 0.012, segments: 2 }, scene);
805
+ slab.rotation.z = -side * slopeAngle;
806
+ slab.position.set(side * (slopeRun * 0.5), height * 0.5 + thickness * 0.4, 0);
807
+ slab.bakeCurrentTransformIntoVertices();
808
+ parts.push(slab);
809
+ }
810
+ // Optional Ridge Cap beam along the peak
811
+ if (opts.ridgeCap !== false) {
812
+ const ridge = createRoundedBoxMesh(`${name}_ridge_cap`, { width: thickness * 1.6, height: thickness * 1.2, depth: totalD + 0.04, radius: 0.015, segments: 2 }, scene);
813
+ ridge.position.set(0, height + thickness * 0.45, 0);
814
+ ridge.bakeCurrentTransformIntoVertices();
815
+ parts.push(ridge);
816
+ }
817
+ // Optional Gable Triangular End Walls (inward of overhang)
818
+ if (opts.gableWall !== false) {
819
+ const gw = width * 0.5;
820
+ const gableShape = [
821
+ new Vector3(-gw, 0, 0),
822
+ new Vector3(gw, 0, 0),
823
+ new Vector3(0, 0, height),
824
+ ];
825
+ const gableMesh = MeshBuilder.ExtrudePolygon(`${name}_gable_core`, { shape: gableShape, depth, sideOrientation: Mesh.DOUBLESIDE, updatable: true }, scene, earcut);
826
+ gableMesh.rotation.x = -Math.PI / 2;
827
+ gableMesh.position.set(0, 0, -depth * 0.5);
828
+ gableMesh.bakeCurrentTransformIntoVertices();
829
+ parts.push(gableMesh);
830
+ }
831
+ return makeUpdatableMerged(parts, name, scene);
832
+ }