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,630 @@
|
|
|
1
|
+
import { Texture } from '@babylonjs/core';
|
|
2
|
+
const FALLBACK_1X1_PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR4nGP4DwQACfsD/fteaysAAAAASUVORK5CYII=';
|
|
3
|
+
export class TextureAtlasManager {
|
|
4
|
+
atlases = new Map();
|
|
5
|
+
scene;
|
|
6
|
+
constructor(scene) {
|
|
7
|
+
this.scene = scene;
|
|
8
|
+
}
|
|
9
|
+
getAtlases() {
|
|
10
|
+
return Array.from(this.atlases.values());
|
|
11
|
+
}
|
|
12
|
+
getAtlas(id) {
|
|
13
|
+
return this.atlases.get(id);
|
|
14
|
+
}
|
|
15
|
+
removeAtlas(id) {
|
|
16
|
+
const atlas = this.atlases.get(id);
|
|
17
|
+
if (atlas) {
|
|
18
|
+
atlas.texture.dispose();
|
|
19
|
+
return this.atlases.delete(id);
|
|
20
|
+
}
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generates a procedural 4x4 terrain and architecture texture atlas.
|
|
25
|
+
* Useful out-of-the-box for block building without needing external files.
|
|
26
|
+
*/
|
|
27
|
+
createProceduralTerrainAtlas(name = 'Terrain & Block Atlas') {
|
|
28
|
+
const cols = 4;
|
|
29
|
+
const rows = 4;
|
|
30
|
+
const tileSize = 32;
|
|
31
|
+
const totalW = cols * tileSize;
|
|
32
|
+
const totalH = rows * tileSize;
|
|
33
|
+
const tileDefs = [
|
|
34
|
+
{ name: 'Stone', bg: '#888c94', fg: '#5a5e66', pattern: 'noise' },
|
|
35
|
+
{ name: 'Dirt', bg: '#865439', fg: '#5e3823', pattern: 'noise' },
|
|
36
|
+
{ name: 'Grass Top', bg: '#4f8a10', fg: '#3b6a0a', pattern: 'grass' },
|
|
37
|
+
{ name: 'Grass Side', bg: '#865439', fg: '#4f8a10', pattern: 'grass-side' },
|
|
38
|
+
{ name: 'Sand', bg: '#d9c077', fg: '#bfa352', pattern: 'noise' },
|
|
39
|
+
{ name: 'Water', bg: '#2980b9', fg: '#3498db', pattern: 'waves' },
|
|
40
|
+
{ name: 'Wood Planks', bg: '#966236', fg: '#663f1d', pattern: 'planks' },
|
|
41
|
+
{ name: 'Wood Log', bg: '#5c3a21', fg: '#3b2311', pattern: 'bark' },
|
|
42
|
+
{ name: 'Cobblestone', bg: '#6e737b', fg: '#44484e', pattern: 'bricks' },
|
|
43
|
+
{ name: 'Stone Bricks', bg: '#5c6168', fg: '#383b40', pattern: 'bricks' },
|
|
44
|
+
{ name: 'Red Bricks', bg: '#a64232', fg: '#68251b', pattern: 'bricks' },
|
|
45
|
+
{ name: 'Foliage', bg: '#2d7a22', fg: '#1d5415', pattern: 'leaves' },
|
|
46
|
+
{ name: 'Glass', bg: '#aed6f1', fg: '#ffffff', pattern: 'glass' },
|
|
47
|
+
{ name: 'Gold Block', bg: '#f1c40f', fg: '#b7950b', pattern: 'border' },
|
|
48
|
+
{ name: 'Iron Block', bg: '#bdc3c7', fg: '#7f8c8d', pattern: 'border' },
|
|
49
|
+
{ name: 'Obsidian', bg: '#201b2d', fg: '#4a235a', pattern: 'noise' },
|
|
50
|
+
];
|
|
51
|
+
let dataUrl = FALLBACK_1X1_PNG;
|
|
52
|
+
const frames = [];
|
|
53
|
+
let ctx = null;
|
|
54
|
+
let canvas = null;
|
|
55
|
+
if (typeof document !== 'undefined') {
|
|
56
|
+
try {
|
|
57
|
+
canvas = document.createElement('canvas');
|
|
58
|
+
canvas.width = totalW;
|
|
59
|
+
canvas.height = totalH;
|
|
60
|
+
ctx = canvas.getContext('2d');
|
|
61
|
+
}
|
|
62
|
+
catch {
|
|
63
|
+
ctx = null;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (ctx && canvas) {
|
|
67
|
+
ctx.fillStyle = '#000000';
|
|
68
|
+
ctx.fillRect(0, 0, totalW, totalH);
|
|
69
|
+
}
|
|
70
|
+
for (let i = 0; i < tileDefs.length; i++) {
|
|
71
|
+
const col = i % cols;
|
|
72
|
+
const row = Math.floor(i / cols);
|
|
73
|
+
const def = tileDefs[i];
|
|
74
|
+
let thumbUrl = FALLBACK_1X1_PNG;
|
|
75
|
+
if (ctx && canvas) {
|
|
76
|
+
const x = col * tileSize;
|
|
77
|
+
const y = row * tileSize;
|
|
78
|
+
// Draw base tile
|
|
79
|
+
ctx.fillStyle = def.bg;
|
|
80
|
+
ctx.fillRect(x, y, tileSize, tileSize);
|
|
81
|
+
// Draw procedural patterns
|
|
82
|
+
ctx.fillStyle = def.fg || '#ffffff';
|
|
83
|
+
if (def.pattern === 'noise') {
|
|
84
|
+
for (let py = 0; py < tileSize; py += 4) {
|
|
85
|
+
for (let px = 0; px < tileSize; px += 4) {
|
|
86
|
+
if ((px + py) % 8 === 0) {
|
|
87
|
+
ctx.fillRect(x + px, y + py, 3, 3);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else if (def.pattern === 'bricks') {
|
|
93
|
+
ctx.strokeStyle = def.fg || '#000';
|
|
94
|
+
ctx.lineWidth = 1;
|
|
95
|
+
ctx.strokeRect(x + 0.5, y + 0.5, tileSize - 1, tileSize - 1);
|
|
96
|
+
ctx.beginPath();
|
|
97
|
+
ctx.moveTo(x, y + 8);
|
|
98
|
+
ctx.lineTo(x + tileSize, y + 8);
|
|
99
|
+
ctx.moveTo(x, y + 16);
|
|
100
|
+
ctx.lineTo(x + tileSize, y + 16);
|
|
101
|
+
ctx.moveTo(x, y + 24);
|
|
102
|
+
ctx.lineTo(x + tileSize, y + 24);
|
|
103
|
+
ctx.moveTo(x + 16, y);
|
|
104
|
+
ctx.lineTo(x + 16, y + 8);
|
|
105
|
+
ctx.moveTo(x + 8, y + 8);
|
|
106
|
+
ctx.lineTo(x + 8, y + 16);
|
|
107
|
+
ctx.moveTo(x + 24, y + 8);
|
|
108
|
+
ctx.lineTo(x + 24, y + 16);
|
|
109
|
+
ctx.moveTo(x + 16, y + 16);
|
|
110
|
+
ctx.lineTo(x + 16, y + 24);
|
|
111
|
+
ctx.stroke();
|
|
112
|
+
}
|
|
113
|
+
else if (def.pattern === 'planks') {
|
|
114
|
+
ctx.strokeStyle = def.fg || '#000';
|
|
115
|
+
ctx.lineWidth = 1;
|
|
116
|
+
for (let p = 8; p < tileSize; p += 8) {
|
|
117
|
+
ctx.beginPath();
|
|
118
|
+
ctx.moveTo(x, y + p);
|
|
119
|
+
ctx.lineTo(x + tileSize, y + p);
|
|
120
|
+
ctx.stroke();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
else if (def.pattern === 'grass-side') {
|
|
124
|
+
ctx.fillRect(x, y, tileSize, 8);
|
|
125
|
+
ctx.beginPath();
|
|
126
|
+
ctx.moveTo(x + 4, y + 8);
|
|
127
|
+
ctx.lineTo(x + 8, y + 12);
|
|
128
|
+
ctx.lineTo(x + 12, y + 8);
|
|
129
|
+
ctx.moveTo(x + 18, y + 8);
|
|
130
|
+
ctx.lineTo(x + 22, y + 14);
|
|
131
|
+
ctx.lineTo(x + 26, y + 8);
|
|
132
|
+
ctx.fill();
|
|
133
|
+
}
|
|
134
|
+
else if (def.pattern === 'glass') {
|
|
135
|
+
ctx.fillStyle = 'rgba(255, 255, 255, 0.6)';
|
|
136
|
+
ctx.beginPath();
|
|
137
|
+
ctx.moveTo(x + 4, y + 26);
|
|
138
|
+
ctx.lineTo(x + 26, y + 4);
|
|
139
|
+
ctx.lineTo(x + 28, y + 4);
|
|
140
|
+
ctx.lineTo(x + 4, y + 28);
|
|
141
|
+
ctx.fill();
|
|
142
|
+
}
|
|
143
|
+
else if (def.pattern === 'border') {
|
|
144
|
+
ctx.strokeStyle = def.fg || '#fff';
|
|
145
|
+
ctx.lineWidth = 2;
|
|
146
|
+
ctx.strokeRect(x + 2, y + 2, tileSize - 4, tileSize - 4);
|
|
147
|
+
}
|
|
148
|
+
try {
|
|
149
|
+
const thumbCanvas = document.createElement('canvas');
|
|
150
|
+
thumbCanvas.width = tileSize;
|
|
151
|
+
thumbCanvas.height = tileSize;
|
|
152
|
+
const thumbCtx = thumbCanvas.getContext('2d');
|
|
153
|
+
if (thumbCtx) {
|
|
154
|
+
thumbCtx.drawImage(canvas, x, y, tileSize, tileSize, 0, 0, tileSize, tileSize);
|
|
155
|
+
thumbUrl = thumbCanvas.toDataURL('image/png');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
thumbUrl = FALLBACK_1X1_PNG;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
// UV coordinates (Normalized 0 to 1)
|
|
163
|
+
const uMin = col / cols;
|
|
164
|
+
const uMax = (col + 1) / cols;
|
|
165
|
+
const vMin = 1.0 - (row + 1) / rows;
|
|
166
|
+
const vMax = 1.0 - row / rows;
|
|
167
|
+
frames.push({
|
|
168
|
+
id: `tile_${i}`,
|
|
169
|
+
name: def.name,
|
|
170
|
+
col,
|
|
171
|
+
row,
|
|
172
|
+
uMin,
|
|
173
|
+
vMin,
|
|
174
|
+
uMax,
|
|
175
|
+
vMax,
|
|
176
|
+
thumbnailDataUrl: thumbUrl,
|
|
177
|
+
pixelRect: { x: col * tileSize, y: row * tileSize, w: tileSize, h: tileSize },
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
if (canvas && ctx) {
|
|
181
|
+
try {
|
|
182
|
+
dataUrl = canvas.toDataURL('image/png');
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
dataUrl = FALLBACK_1X1_PNG;
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const texture = new Texture(dataUrl, this.scene, false, true);
|
|
189
|
+
texture.name = 'atlas_terrain_procedural';
|
|
190
|
+
const id = `atlas_${Date.now().toString().slice(-4)}`;
|
|
191
|
+
const atlasDef = {
|
|
192
|
+
id,
|
|
193
|
+
name,
|
|
194
|
+
texture,
|
|
195
|
+
dataUrl,
|
|
196
|
+
width: totalW,
|
|
197
|
+
height: totalH,
|
|
198
|
+
columns: cols,
|
|
199
|
+
rows,
|
|
200
|
+
tileSize,
|
|
201
|
+
frames,
|
|
202
|
+
sourceCanvas: canvas || undefined,
|
|
203
|
+
};
|
|
204
|
+
this.atlases.set(id, atlasDef);
|
|
205
|
+
return atlasDef;
|
|
206
|
+
}
|
|
207
|
+
/**
|
|
208
|
+
* Loads a custom TextureAtlas from an image data URL / URL and grid columns & rows.
|
|
209
|
+
*/
|
|
210
|
+
async createGridAtlasFromImage(name, imageSrc, columns = 4, rows = 4, tileNames) {
|
|
211
|
+
const cols = Math.max(1, columns);
|
|
212
|
+
const r = Math.max(1, rows);
|
|
213
|
+
const buildAtlas = (totalW, totalH, ctx, canvas) => {
|
|
214
|
+
const tileW = Math.floor(totalW / cols);
|
|
215
|
+
const tileH = Math.floor(totalH / r);
|
|
216
|
+
const frames = [];
|
|
217
|
+
const totalTiles = cols * r;
|
|
218
|
+
for (let i = 0; i < totalTiles; i++) {
|
|
219
|
+
const col = i % cols;
|
|
220
|
+
const row = Math.floor(i / cols);
|
|
221
|
+
const x = col * tileW;
|
|
222
|
+
const y = row * tileH;
|
|
223
|
+
const tileName = tileNames?.[i] || `Tile (${col}, ${row})`;
|
|
224
|
+
let thumbUrl = imageSrc;
|
|
225
|
+
if (ctx && canvas) {
|
|
226
|
+
try {
|
|
227
|
+
const thumbCanvas = document.createElement('canvas');
|
|
228
|
+
thumbCanvas.width = Math.min(64, tileW);
|
|
229
|
+
thumbCanvas.height = Math.min(64, tileH);
|
|
230
|
+
const thumbCtx = thumbCanvas.getContext('2d');
|
|
231
|
+
if (thumbCtx) {
|
|
232
|
+
thumbCtx.drawImage(canvas, x, y, tileW, tileH, 0, 0, thumbCanvas.width, thumbCanvas.height);
|
|
233
|
+
thumbUrl = thumbCanvas.toDataURL('image/png');
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
catch {
|
|
237
|
+
thumbUrl = imageSrc;
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
const uMin = col / cols;
|
|
241
|
+
const uMax = (col + 1) / cols;
|
|
242
|
+
const vMin = 1.0 - (row + 1) / r;
|
|
243
|
+
const vMax = 1.0 - row / r;
|
|
244
|
+
frames.push({
|
|
245
|
+
id: `tile_${col}_${row}`,
|
|
246
|
+
name: tileName,
|
|
247
|
+
col,
|
|
248
|
+
row,
|
|
249
|
+
uMin,
|
|
250
|
+
vMin,
|
|
251
|
+
uMax,
|
|
252
|
+
vMax,
|
|
253
|
+
thumbnailDataUrl: thumbUrl,
|
|
254
|
+
pixelRect: { x, y, w: tileW, h: tileH },
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
const texture = new Texture(imageSrc, this.scene, false, true);
|
|
258
|
+
texture.name = `atlas_${name.toLowerCase().replace(/\s+/g, '_')}`;
|
|
259
|
+
const id = `atlas_${Date.now().toString().slice(-4)}`;
|
|
260
|
+
const atlasDef = {
|
|
261
|
+
id,
|
|
262
|
+
name,
|
|
263
|
+
texture,
|
|
264
|
+
dataUrl: imageSrc,
|
|
265
|
+
width: totalW,
|
|
266
|
+
height: totalH,
|
|
267
|
+
columns: cols,
|
|
268
|
+
rows: r,
|
|
269
|
+
tileSize: tileW,
|
|
270
|
+
frames,
|
|
271
|
+
sourceCanvas: canvas || undefined,
|
|
272
|
+
};
|
|
273
|
+
this.atlases.set(id, atlasDef);
|
|
274
|
+
return atlasDef;
|
|
275
|
+
};
|
|
276
|
+
return new Promise((resolve) => {
|
|
277
|
+
if (typeof Image === 'undefined') {
|
|
278
|
+
return resolve(buildAtlas(256, 256, null, null));
|
|
279
|
+
}
|
|
280
|
+
const img = new Image();
|
|
281
|
+
img.crossOrigin = 'anonymous';
|
|
282
|
+
let resolved = false;
|
|
283
|
+
const finish = (w, h) => {
|
|
284
|
+
if (resolved)
|
|
285
|
+
return;
|
|
286
|
+
resolved = true;
|
|
287
|
+
let canvas = null;
|
|
288
|
+
let ctx = null;
|
|
289
|
+
try {
|
|
290
|
+
canvas = document.createElement('canvas');
|
|
291
|
+
canvas.width = w;
|
|
292
|
+
canvas.height = h;
|
|
293
|
+
ctx = canvas.getContext('2d');
|
|
294
|
+
if (ctx)
|
|
295
|
+
ctx.drawImage(img, 0, 0);
|
|
296
|
+
}
|
|
297
|
+
catch {
|
|
298
|
+
ctx = null;
|
|
299
|
+
}
|
|
300
|
+
resolve(buildAtlas(w, h, ctx, canvas));
|
|
301
|
+
};
|
|
302
|
+
img.onload = () => {
|
|
303
|
+
finish(img.width || 256, img.height || 256);
|
|
304
|
+
};
|
|
305
|
+
img.onerror = () => {
|
|
306
|
+
finish(256, 256);
|
|
307
|
+
};
|
|
308
|
+
img.src = imageSrc;
|
|
309
|
+
if (img.complete && img.naturalWidth !== undefined) {
|
|
310
|
+
finish(img.naturalWidth || 256, img.naturalHeight || 256);
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
setTimeout(() => {
|
|
314
|
+
finish(256, 256);
|
|
315
|
+
}, 50);
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Universal parser for TexturePacker JSON (Array or Hash), XML (Starling, Cocos, Sparrow),
|
|
321
|
+
* or TPS files.
|
|
322
|
+
*/
|
|
323
|
+
parseAtlasDescriptor(descriptorInput) {
|
|
324
|
+
const rawFrames = [];
|
|
325
|
+
let imagePath;
|
|
326
|
+
let descriptor = descriptorInput;
|
|
327
|
+
if (typeof descriptor === 'string') {
|
|
328
|
+
const trimmed = descriptor.trim();
|
|
329
|
+
if (trimmed.startsWith('<')) {
|
|
330
|
+
return this.parseXmlAtlasDescriptor(trimmed);
|
|
331
|
+
}
|
|
332
|
+
try {
|
|
333
|
+
descriptor = JSON.parse(trimmed);
|
|
334
|
+
}
|
|
335
|
+
catch {
|
|
336
|
+
return this.parseXmlAtlasDescriptor(trimmed);
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
if (!descriptor || typeof descriptor !== 'object') {
|
|
340
|
+
return { frames: rawFrames };
|
|
341
|
+
}
|
|
342
|
+
// Extract image metadata if available
|
|
343
|
+
if (descriptor.meta?.image) {
|
|
344
|
+
imagePath = descriptor.meta.image;
|
|
345
|
+
}
|
|
346
|
+
else if (descriptor.imagePath) {
|
|
347
|
+
imagePath = descriptor.imagePath;
|
|
348
|
+
}
|
|
349
|
+
else if (descriptor.image) {
|
|
350
|
+
imagePath = descriptor.image;
|
|
351
|
+
}
|
|
352
|
+
// 1. TexturePacker JSON Array: { frames: [ { filename: "...", frame: { x, y, w, h } } ] }
|
|
353
|
+
if (Array.isArray(descriptor.frames)) {
|
|
354
|
+
for (let i = 0; i < descriptor.frames.length; i++) {
|
|
355
|
+
const item = descriptor.frames[i];
|
|
356
|
+
const name = item.filename || item.name || `frame_${i}`;
|
|
357
|
+
const rect = item.frame || item.rect || item;
|
|
358
|
+
rawFrames.push({
|
|
359
|
+
name,
|
|
360
|
+
x: rect.x ?? rect.left ?? 0,
|
|
361
|
+
y: rect.y ?? rect.top ?? 0,
|
|
362
|
+
w: rect.w ?? rect.width ?? 32,
|
|
363
|
+
h: rect.h ?? rect.height ?? 32,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
// 2. TexturePacker JSON Hash: { frames: { "tile1.png": { frame: { x, y, w, h } } } }
|
|
368
|
+
else if (descriptor.frames && typeof descriptor.frames === 'object') {
|
|
369
|
+
for (const [key, val] of Object.entries(descriptor.frames)) {
|
|
370
|
+
const item = val;
|
|
371
|
+
const rect = item?.frame || item?.rect || item || {};
|
|
372
|
+
rawFrames.push({
|
|
373
|
+
name: key,
|
|
374
|
+
x: rect.x ?? rect.left ?? 0,
|
|
375
|
+
y: rect.y ?? rect.top ?? 0,
|
|
376
|
+
w: rect.w ?? rect.width ?? 32,
|
|
377
|
+
h: rect.h ?? rect.height ?? 32,
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
// 3. Flat array of frames: [ { name, x, y, w, h } ]
|
|
382
|
+
else if (Array.isArray(descriptor)) {
|
|
383
|
+
for (let i = 0; i < descriptor.length; i++) {
|
|
384
|
+
const item = descriptor[i];
|
|
385
|
+
const name = item.name || item.filename || `frame_${i}`;
|
|
386
|
+
const rect = item.frame || item.rect || item;
|
|
387
|
+
rawFrames.push({
|
|
388
|
+
name,
|
|
389
|
+
x: rect.x ?? rect.left ?? 0,
|
|
390
|
+
y: rect.y ?? rect.top ?? 0,
|
|
391
|
+
w: rect.w ?? rect.width ?? 32,
|
|
392
|
+
h: rect.h ?? rect.height ?? 32,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return { frames: rawFrames, imagePath };
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Parses XML / TPS / Starling / Cocos texture atlas formats.
|
|
400
|
+
*/
|
|
401
|
+
parseXmlAtlasDescriptor(xmlText) {
|
|
402
|
+
const rawFrames = [];
|
|
403
|
+
let imagePath;
|
|
404
|
+
if (typeof DOMParser === 'undefined') {
|
|
405
|
+
return { frames: rawFrames };
|
|
406
|
+
}
|
|
407
|
+
try {
|
|
408
|
+
const parser = new DOMParser();
|
|
409
|
+
const doc = parser.parseFromString(xmlText, 'text/xml');
|
|
410
|
+
// Check for <TextureAtlas imagePath="trees.png">
|
|
411
|
+
const root = doc.documentElement;
|
|
412
|
+
if (root) {
|
|
413
|
+
imagePath = root.getAttribute('imagePath') || root.getAttribute('image') || undefined;
|
|
414
|
+
// TexturePacker / Starling SubTexture tags
|
|
415
|
+
const subTextures = root.querySelectorAll('SubTexture');
|
|
416
|
+
if (subTextures.length > 0) {
|
|
417
|
+
subTextures.forEach((node, idx) => {
|
|
418
|
+
const name = node.getAttribute('name') || `frame_${idx}`;
|
|
419
|
+
const x = parseFloat(node.getAttribute('x') || '0') || 0;
|
|
420
|
+
const y = parseFloat(node.getAttribute('y') || '0') || 0;
|
|
421
|
+
const w = parseFloat(node.getAttribute('width') || node.getAttribute('w') || '32') || 32;
|
|
422
|
+
const h = parseFloat(node.getAttribute('height') || node.getAttribute('h') || '32') || 32;
|
|
423
|
+
rawFrames.push({ name, x, y, w, h });
|
|
424
|
+
});
|
|
425
|
+
return { frames: rawFrames, imagePath };
|
|
426
|
+
}
|
|
427
|
+
// Generic <sprite n="..." x="..." y="..." w="..." h="...">
|
|
428
|
+
const sprites = root.querySelectorAll('sprite');
|
|
429
|
+
if (sprites.length > 0) {
|
|
430
|
+
sprites.forEach((node, idx) => {
|
|
431
|
+
const name = node.getAttribute('n') || node.getAttribute('name') || `sprite_${idx}`;
|
|
432
|
+
const x = parseFloat(node.getAttribute('x') || '0') || 0;
|
|
433
|
+
const y = parseFloat(node.getAttribute('y') || '0') || 0;
|
|
434
|
+
const w = parseFloat(node.getAttribute('w') || node.getAttribute('width') || '32') || 32;
|
|
435
|
+
const h = parseFloat(node.getAttribute('h') || node.getAttribute('height') || '32') || 32;
|
|
436
|
+
rawFrames.push({ name, x, y, w, h });
|
|
437
|
+
});
|
|
438
|
+
return { frames: rawFrames, imagePath };
|
|
439
|
+
}
|
|
440
|
+
// TPS XML project file: extract textureFileName if available
|
|
441
|
+
const filenameNodes = root.querySelectorAll('filename');
|
|
442
|
+
filenameNodes.forEach((fn) => {
|
|
443
|
+
const val = fn.textContent?.trim();
|
|
444
|
+
if (val && /\.(png|jpe?g|webp)$/i.test(val) && !imagePath) {
|
|
445
|
+
imagePath = val;
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
catch (e) {
|
|
451
|
+
console.warn('parseXmlAtlasDescriptor error:', e);
|
|
452
|
+
}
|
|
453
|
+
return { frames: rawFrames, imagePath };
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Loads an Atlas from an image and a JSON/XML/TPS spritesheet or frames specification.
|
|
457
|
+
*/
|
|
458
|
+
async createAtlasFromJson(name, imageSrc, jsonDescriptor) {
|
|
459
|
+
const parsed = this.parseAtlasDescriptor(jsonDescriptor);
|
|
460
|
+
const parsedFrames = parsed.frames;
|
|
461
|
+
return new Promise((resolve, reject) => {
|
|
462
|
+
const buildAtlasFromCanvas = (totalW, totalH, ctx, canvas) => {
|
|
463
|
+
const frames = [];
|
|
464
|
+
// If no frames could be extracted from descriptor, default to a 4x4 grid
|
|
465
|
+
const effectiveFrames = parsedFrames.length > 0
|
|
466
|
+
? parsedFrames
|
|
467
|
+
: Array.from({ length: 16 }, (_, i) => {
|
|
468
|
+
const cols = 4;
|
|
469
|
+
const rows = 4;
|
|
470
|
+
const tw = Math.floor(totalW / cols);
|
|
471
|
+
const th = Math.floor(totalH / rows);
|
|
472
|
+
const col = i % cols;
|
|
473
|
+
const row = Math.floor(i / cols);
|
|
474
|
+
return {
|
|
475
|
+
name: `Tile (${col}, ${row})`,
|
|
476
|
+
x: col * tw,
|
|
477
|
+
y: row * th,
|
|
478
|
+
w: tw,
|
|
479
|
+
h: th,
|
|
480
|
+
};
|
|
481
|
+
});
|
|
482
|
+
for (let idx = 0; idx < effectiveFrames.length; idx++) {
|
|
483
|
+
const f = effectiveFrames[idx];
|
|
484
|
+
const x = f.x;
|
|
485
|
+
const y = f.y;
|
|
486
|
+
const w = Math.max(1, f.w);
|
|
487
|
+
const h = Math.max(1, f.h);
|
|
488
|
+
let thumbUrl = imageSrc;
|
|
489
|
+
if (ctx && canvas) {
|
|
490
|
+
try {
|
|
491
|
+
const thumbCanvas = document.createElement('canvas');
|
|
492
|
+
thumbCanvas.width = Math.min(64, w);
|
|
493
|
+
thumbCanvas.height = Math.min(64, h);
|
|
494
|
+
const thumbCtx = thumbCanvas.getContext('2d');
|
|
495
|
+
if (thumbCtx) {
|
|
496
|
+
thumbCtx.drawImage(canvas, x, y, w, h, 0, 0, thumbCanvas.width, thumbCanvas.height);
|
|
497
|
+
thumbUrl = thumbCanvas.toDataURL('image/png');
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
catch {
|
|
501
|
+
thumbUrl = imageSrc;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
const uMin = x / totalW;
|
|
505
|
+
const uMax = (x + w) / totalW;
|
|
506
|
+
const vMin = 1.0 - (y + h) / totalH;
|
|
507
|
+
const vMax = 1.0 - y / totalH;
|
|
508
|
+
frames.push({
|
|
509
|
+
id: `tile_${idx}`,
|
|
510
|
+
name: f.name,
|
|
511
|
+
col: idx,
|
|
512
|
+
row: 0,
|
|
513
|
+
uMin,
|
|
514
|
+
vMin,
|
|
515
|
+
uMax,
|
|
516
|
+
vMax,
|
|
517
|
+
thumbnailDataUrl: thumbUrl,
|
|
518
|
+
pixelRect: { x, y, w, h },
|
|
519
|
+
});
|
|
520
|
+
}
|
|
521
|
+
const texture = new Texture(imageSrc, this.scene, false, true, Texture.NEAREST_SAMPLINGMODE);
|
|
522
|
+
texture.name = `atlas_${name.toLowerCase().replace(/\s+/g, '_')}`;
|
|
523
|
+
const id = `atlas_${Date.now().toString().slice(-4)}_${Math.floor(Math.random() * 1000)}`;
|
|
524
|
+
const atlasDef = {
|
|
525
|
+
id,
|
|
526
|
+
name,
|
|
527
|
+
texture,
|
|
528
|
+
dataUrl: imageSrc,
|
|
529
|
+
width: totalW,
|
|
530
|
+
height: totalH,
|
|
531
|
+
columns: frames.length,
|
|
532
|
+
rows: 1,
|
|
533
|
+
tileSize: frames[0] ? frames[0].pixelRect?.w || 32 : 32,
|
|
534
|
+
frames,
|
|
535
|
+
sourceCanvas: canvas || undefined,
|
|
536
|
+
};
|
|
537
|
+
this.atlases.set(id, atlasDef);
|
|
538
|
+
return atlasDef;
|
|
539
|
+
};
|
|
540
|
+
if (typeof Image === 'undefined') {
|
|
541
|
+
return resolve(buildAtlasFromCanvas(256, 256, null, null));
|
|
542
|
+
}
|
|
543
|
+
const img = new Image();
|
|
544
|
+
img.crossOrigin = 'anonymous';
|
|
545
|
+
let finished = false;
|
|
546
|
+
const finish = (w, h) => {
|
|
547
|
+
if (finished)
|
|
548
|
+
return;
|
|
549
|
+
finished = true;
|
|
550
|
+
let canvas = null;
|
|
551
|
+
let ctx = null;
|
|
552
|
+
try {
|
|
553
|
+
canvas = document.createElement('canvas');
|
|
554
|
+
canvas.width = w;
|
|
555
|
+
canvas.height = h;
|
|
556
|
+
ctx = canvas.getContext('2d');
|
|
557
|
+
if (ctx)
|
|
558
|
+
ctx.drawImage(img, 0, 0);
|
|
559
|
+
}
|
|
560
|
+
catch {
|
|
561
|
+
ctx = null;
|
|
562
|
+
}
|
|
563
|
+
resolve(buildAtlasFromCanvas(w, h, ctx, canvas));
|
|
564
|
+
};
|
|
565
|
+
img.onload = () => {
|
|
566
|
+
finish(img.width || 256, img.height || 256);
|
|
567
|
+
};
|
|
568
|
+
img.onerror = () => {
|
|
569
|
+
finish(256, 256);
|
|
570
|
+
};
|
|
571
|
+
img.src = imageSrc;
|
|
572
|
+
if (img.complete && img.naturalWidth !== undefined && img.naturalWidth > 0) {
|
|
573
|
+
finish(img.naturalWidth, img.naturalHeight || 256);
|
|
574
|
+
}
|
|
575
|
+
else {
|
|
576
|
+
setTimeout(() => finish(256, 256), 60);
|
|
577
|
+
}
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Extracts a cropped sub-image data URL for an individual frame from an atlas at native pixel resolution.
|
|
582
|
+
*/
|
|
583
|
+
extractFrameDataUrl(atlasId, frameId) {
|
|
584
|
+
const atlas = this.getAtlas(atlasId);
|
|
585
|
+
if (!atlas)
|
|
586
|
+
return null;
|
|
587
|
+
const frame = atlas.frames.find((f) => f.id === frameId || f.name === frameId);
|
|
588
|
+
if (!frame)
|
|
589
|
+
return null;
|
|
590
|
+
if (typeof document === 'undefined') {
|
|
591
|
+
return frame.thumbnailDataUrl || atlas.dataUrl || null;
|
|
592
|
+
}
|
|
593
|
+
const rect = frame.pixelRect || {
|
|
594
|
+
x: Math.round(frame.uMin * atlas.width),
|
|
595
|
+
y: Math.round((1.0 - frame.vMax) * atlas.height),
|
|
596
|
+
w: Math.max(1, Math.round((frame.uMax - frame.uMin) * atlas.width)),
|
|
597
|
+
h: Math.max(1, Math.round((frame.vMax - frame.vMin) * atlas.height)),
|
|
598
|
+
};
|
|
599
|
+
try {
|
|
600
|
+
let sourceCanvas = atlas.sourceCanvas;
|
|
601
|
+
if (!sourceCanvas && atlas.dataUrl) {
|
|
602
|
+
const c = document.createElement('canvas');
|
|
603
|
+
c.width = atlas.width;
|
|
604
|
+
c.height = atlas.height;
|
|
605
|
+
const cCtx = c.getContext('2d');
|
|
606
|
+
const img = new Image();
|
|
607
|
+
img.src = atlas.dataUrl;
|
|
608
|
+
if (cCtx) {
|
|
609
|
+
cCtx.drawImage(img, 0, 0);
|
|
610
|
+
atlas.sourceCanvas = c;
|
|
611
|
+
sourceCanvas = c;
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (sourceCanvas) {
|
|
615
|
+
const tileCanvas = document.createElement('canvas');
|
|
616
|
+
tileCanvas.width = rect.w;
|
|
617
|
+
tileCanvas.height = rect.h;
|
|
618
|
+
const ctx = tileCanvas.getContext('2d');
|
|
619
|
+
if (ctx) {
|
|
620
|
+
ctx.drawImage(sourceCanvas, rect.x, rect.y, rect.w, rect.h, 0, 0, rect.w, rect.h);
|
|
621
|
+
return tileCanvas.toDataURL('image/png');
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
catch (err) {
|
|
626
|
+
console.warn('Failed to extract frame canvas:', err);
|
|
627
|
+
}
|
|
628
|
+
return frame.thumbnailDataUrl || atlas.dataUrl || null;
|
|
629
|
+
}
|
|
630
|
+
}
|