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,945 @@
|
|
|
1
|
+
import { Color3, Mesh, PBRMaterial, Texture, } from '@babylonjs/core';
|
|
2
|
+
import { generateNormalMapFromCanvas } from '../tools/textureUtils';
|
|
3
|
+
import { ExternalImageGeneratorService } from './ExternalImageGeneratorService';
|
|
4
|
+
export const QUICK_TEXTURE_PRESETS = [
|
|
5
|
+
{
|
|
6
|
+
label: 'Dungeon Cobblestone',
|
|
7
|
+
prompt: 'Weathered dark dungeon cobblestone floor with mossy crevices',
|
|
8
|
+
style: 'cobblestone',
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
label: 'Ancient Red Brick',
|
|
12
|
+
prompt: 'Aged red clay brick wall with warm mortar joints',
|
|
13
|
+
style: 'brick',
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
label: 'Oak Wood Planks',
|
|
17
|
+
prompt: 'Warm medieval oak wood planks with natural grain and knots',
|
|
18
|
+
style: 'wood',
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
label: 'Sci-Fi Hull Panels',
|
|
22
|
+
prompt: 'Futuristic dark titanium sci-fi hull plating with glowing cyan energy seams',
|
|
23
|
+
style: 'scifi',
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
label: 'Rusted Industrial Metal',
|
|
27
|
+
prompt: 'Corroded industrial iron sheet metal with orange rust patches and scratches',
|
|
28
|
+
style: 'rust',
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
label: 'Volcanic Magma Rock',
|
|
32
|
+
prompt: 'Cracked black basalt volcanic rock with glowing orange lava veins',
|
|
33
|
+
style: 'lava',
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
label: 'Royal White Marble',
|
|
37
|
+
prompt: 'Polished white Carrara marble with subtle grey and gold mineral veins',
|
|
38
|
+
style: 'marble',
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
label: 'Arcane Glowing Runes',
|
|
42
|
+
prompt: 'Ancient obsidian stone tablet engraved with glowing arcane violet runes',
|
|
43
|
+
style: 'runes',
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
label: 'Dragon Scales',
|
|
47
|
+
prompt: 'Overlapping emerald green dragon scales with metallic gold highlights',
|
|
48
|
+
style: 'scales',
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
label: 'Forest Moss Ground',
|
|
52
|
+
prompt: 'Lush organic forest moss and lichen ground cover with dark soil',
|
|
53
|
+
style: 'moss',
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
label: 'Weathered Stone Decal',
|
|
57
|
+
prompt: 'Cracked stone emblem decal with transparent edges',
|
|
58
|
+
style: 'decal',
|
|
59
|
+
},
|
|
60
|
+
];
|
|
61
|
+
// Fallback 1x1 PNG data URL for headless environments without canvas context
|
|
62
|
+
const FALLBACK_PNG_DATA_URL = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==';
|
|
63
|
+
export class AITextureGenerator {
|
|
64
|
+
service;
|
|
65
|
+
history = [];
|
|
66
|
+
counter = 1;
|
|
67
|
+
externalImageGenerator;
|
|
68
|
+
onHistoryChanged;
|
|
69
|
+
constructor(service) {
|
|
70
|
+
this.service = service;
|
|
71
|
+
this.externalImageGenerator = new ExternalImageGeneratorService();
|
|
72
|
+
}
|
|
73
|
+
getHistory() {
|
|
74
|
+
return [...this.history];
|
|
75
|
+
}
|
|
76
|
+
getTextureById(id) {
|
|
77
|
+
return this.history.find((item) => item.id === id || item.name === id || item.embeddedAlbedoName === id);
|
|
78
|
+
}
|
|
79
|
+
getLatestTexture() {
|
|
80
|
+
return this.history[0];
|
|
81
|
+
}
|
|
82
|
+
removeTexture(id) {
|
|
83
|
+
const idx = this.history.findIndex((item) => item.id === id);
|
|
84
|
+
if (idx === -1)
|
|
85
|
+
return false;
|
|
86
|
+
this.history.splice(idx, 1);
|
|
87
|
+
this.onHistoryChanged?.(this.getHistory());
|
|
88
|
+
return true;
|
|
89
|
+
}
|
|
90
|
+
clearHistory() {
|
|
91
|
+
this.history = [];
|
|
92
|
+
this.onHistoryChanged?.([]);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Generates a new texture from a natural language prompt, embeds it in the scene,
|
|
96
|
+
* and optionally applies it to a mesh. Uses an external AI image generator if configured,
|
|
97
|
+
* or falls back to the built-in procedural synthesizer.
|
|
98
|
+
*/
|
|
99
|
+
async generateTexture(options) {
|
|
100
|
+
const prompt = (options.prompt || 'Seamless stone texture').trim();
|
|
101
|
+
const size = this.clampSize(options.size ?? 512);
|
|
102
|
+
const seamless = options.seamless !== false;
|
|
103
|
+
const generateNormal = options.generateNormalMap !== false;
|
|
104
|
+
// Check whether an external AI Image Generator should be used
|
|
105
|
+
const configuredBackend = this.externalImageGenerator.getActiveBackend();
|
|
106
|
+
const explicitBackend = options.backend;
|
|
107
|
+
const shouldUseAI = (explicitBackend !== undefined && explicitBackend !== 'procedural') ||
|
|
108
|
+
(options.useAI !== false && (configuredBackend !== 'procedural' || explicitBackend === 'auto' || options.useAI === true));
|
|
109
|
+
if (shouldUseAI) {
|
|
110
|
+
try {
|
|
111
|
+
const targetBackend = explicitBackend && explicitBackend !== 'auto' && explicitBackend !== 'procedural'
|
|
112
|
+
? explicitBackend
|
|
113
|
+
: configuredBackend !== 'procedural'
|
|
114
|
+
? configuredBackend
|
|
115
|
+
: 'pollinations';
|
|
116
|
+
const genResult = await this.externalImageGenerator.generateImage({
|
|
117
|
+
backend: targetBackend,
|
|
118
|
+
prompt,
|
|
119
|
+
size,
|
|
120
|
+
seamless,
|
|
121
|
+
seed: options.seed,
|
|
122
|
+
enhancePrompt: options.enhancePrompt,
|
|
123
|
+
});
|
|
124
|
+
const normalStrength = typeof options.normalStrength === 'number' ? options.normalStrength : 2.2;
|
|
125
|
+
const cleanBaseName = this.sanitizeName(options.name || this.deriveNameFromPrompt(prompt, 'stone'));
|
|
126
|
+
const uniqueName = `${cleanBaseName}_${this.counter++}`;
|
|
127
|
+
// Compute normal map from the generated AI image canvas
|
|
128
|
+
let normalDataUrl;
|
|
129
|
+
let actualWidth = size;
|
|
130
|
+
let actualHeight = size;
|
|
131
|
+
if (generateNormal && typeof document !== 'undefined') {
|
|
132
|
+
try {
|
|
133
|
+
const img = new Image();
|
|
134
|
+
img.crossOrigin = 'anonymous';
|
|
135
|
+
await new Promise((resolve) => {
|
|
136
|
+
const timer = setTimeout(() => resolve(), 200);
|
|
137
|
+
img.onload = () => {
|
|
138
|
+
clearTimeout(timer);
|
|
139
|
+
resolve();
|
|
140
|
+
};
|
|
141
|
+
img.onerror = () => {
|
|
142
|
+
clearTimeout(timer);
|
|
143
|
+
resolve();
|
|
144
|
+
};
|
|
145
|
+
img.src = genResult.dataUrl;
|
|
146
|
+
});
|
|
147
|
+
actualWidth = img.naturalWidth || size;
|
|
148
|
+
actualHeight = img.naturalHeight || size;
|
|
149
|
+
const canvas = document.createElement('canvas');
|
|
150
|
+
canvas.width = actualWidth;
|
|
151
|
+
canvas.height = actualHeight;
|
|
152
|
+
const ctx = canvas.getContext('2d');
|
|
153
|
+
if (ctx) {
|
|
154
|
+
ctx.drawImage(img, 0, 0);
|
|
155
|
+
normalDataUrl = this.renderNormalMap(canvas, normalStrength);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
catch {
|
|
159
|
+
normalDataUrl = undefined;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const item = {
|
|
163
|
+
id: `ai_tex_${Date.now()}_${Math.floor(Math.random() * 10000)}`,
|
|
164
|
+
name: uniqueName,
|
|
165
|
+
prompt,
|
|
166
|
+
style: (options.style && options.style !== 'auto' ? options.style : 'stone'),
|
|
167
|
+
size: Math.max(actualWidth, actualHeight),
|
|
168
|
+
seed: options.seed ?? 0,
|
|
169
|
+
seamless,
|
|
170
|
+
albedoDataUrl: genResult.dataUrl,
|
|
171
|
+
normalDataUrl,
|
|
172
|
+
roughness: options.roughness ?? 0.7,
|
|
173
|
+
metallic: options.metallic ?? 0.1,
|
|
174
|
+
normalStrength,
|
|
175
|
+
primaryColor: options.primaryColor || '#888888',
|
|
176
|
+
secondaryColor: options.secondaryColor || '#444444',
|
|
177
|
+
accentColor: options.accentColor || '#222222',
|
|
178
|
+
createdAt: Date.now(),
|
|
179
|
+
generatorBackend: genResult.providerId,
|
|
180
|
+
isAI: true,
|
|
181
|
+
};
|
|
182
|
+
if (options.embedInScene !== false) {
|
|
183
|
+
const albedoEmbedName = `${uniqueName}_albedo.png`;
|
|
184
|
+
this.service.addTextureFromDataUrl(albedoEmbedName, genResult.dataUrl);
|
|
185
|
+
item.embeddedAlbedoName = albedoEmbedName;
|
|
186
|
+
if (normalDataUrl) {
|
|
187
|
+
const normalEmbedName = `${uniqueName}_normal.png`;
|
|
188
|
+
this.service.addTextureFromDataUrl(normalEmbedName, normalDataUrl);
|
|
189
|
+
item.embeddedNormalName = normalEmbedName;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
this.history.unshift(item);
|
|
193
|
+
this.onHistoryChanged?.(this.getHistory());
|
|
194
|
+
if (options.applyToMesh) {
|
|
195
|
+
this.applyTextureToMesh(item.id, options.applyToMesh, options.uvScale ?? 1);
|
|
196
|
+
}
|
|
197
|
+
return item;
|
|
198
|
+
}
|
|
199
|
+
catch (err) {
|
|
200
|
+
if (options.fallbackToProcedural === false) {
|
|
201
|
+
throw err;
|
|
202
|
+
}
|
|
203
|
+
console.warn(`External AI generation failed (${err?.message}), falling back to procedural synthesizer.`);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
const recipe = this.analyzePrompt(prompt, options);
|
|
207
|
+
const cleanBaseName = this.sanitizeName(options.name || this.deriveNameFromPrompt(prompt, recipe.style));
|
|
208
|
+
const uniqueName = `${cleanBaseName}_${this.counter++}`;
|
|
209
|
+
// Render Albedo Canvas & Data URL
|
|
210
|
+
const { canvas: albedoCanvas, dataUrl: albedoDataUrl } = this.renderProceduralTexture(size, recipe, seamless);
|
|
211
|
+
// Render Normal Map if enabled
|
|
212
|
+
let normalDataUrl;
|
|
213
|
+
if (generateNormal) {
|
|
214
|
+
normalDataUrl = this.renderNormalMap(albedoCanvas, recipe.normalStrength);
|
|
215
|
+
}
|
|
216
|
+
const item = {
|
|
217
|
+
id: `ai_tex_${Date.now()}_${Math.floor(Math.random() * 10000)}`,
|
|
218
|
+
name: uniqueName,
|
|
219
|
+
prompt,
|
|
220
|
+
style: recipe.style,
|
|
221
|
+
size,
|
|
222
|
+
seed: recipe.seed,
|
|
223
|
+
seamless,
|
|
224
|
+
albedoDataUrl,
|
|
225
|
+
normalDataUrl,
|
|
226
|
+
roughness: recipe.roughness,
|
|
227
|
+
metallic: recipe.metallic,
|
|
228
|
+
normalStrength: recipe.normalStrength,
|
|
229
|
+
primaryColor: recipe.primaryHex,
|
|
230
|
+
secondaryColor: recipe.secondaryHex,
|
|
231
|
+
accentColor: recipe.accentHex,
|
|
232
|
+
createdAt: Date.now(),
|
|
233
|
+
generatorBackend: 'procedural',
|
|
234
|
+
isAI: false,
|
|
235
|
+
};
|
|
236
|
+
// Embed in scene texture registry by default so Materials and Decals can use it immediately
|
|
237
|
+
if (options.embedInScene !== false) {
|
|
238
|
+
const albedoEmbedName = `${uniqueName}_albedo.png`;
|
|
239
|
+
this.service.addTextureFromDataUrl(albedoEmbedName, albedoDataUrl);
|
|
240
|
+
item.embeddedAlbedoName = albedoEmbedName;
|
|
241
|
+
if (normalDataUrl) {
|
|
242
|
+
const normalEmbedName = `${uniqueName}_normal.png`;
|
|
243
|
+
this.service.addTextureFromDataUrl(normalEmbedName, normalDataUrl);
|
|
244
|
+
item.embeddedNormalName = normalEmbedName;
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
this.history.unshift(item);
|
|
248
|
+
this.onHistoryChanged?.(this.getHistory());
|
|
249
|
+
if (options.applyToMesh) {
|
|
250
|
+
this.applyTextureToMesh(item.id, options.applyToMesh, options.uvScale ?? 1);
|
|
251
|
+
}
|
|
252
|
+
return item;
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Imports an external or AI-generated texture image (PNG, JPEG, WebP data URL or image URL),
|
|
256
|
+
* automatically computes its tangent-space normal map, embeds it in the scene, and adds it to history.
|
|
257
|
+
*/
|
|
258
|
+
async addExternalTexture(name, albedoDataUrl, prompt = 'Imported Texture', normalStrength = 2.2) {
|
|
259
|
+
const cleanBaseName = this.sanitizeName(name);
|
|
260
|
+
const uniqueName = `${cleanBaseName}_${this.counter++}`;
|
|
261
|
+
let normalDataUrl;
|
|
262
|
+
let width = 512;
|
|
263
|
+
let height = 512;
|
|
264
|
+
if (typeof document !== 'undefined') {
|
|
265
|
+
try {
|
|
266
|
+
const img = new Image();
|
|
267
|
+
img.crossOrigin = 'anonymous';
|
|
268
|
+
await new Promise((resolve) => {
|
|
269
|
+
const timer = setTimeout(() => resolve(), 200);
|
|
270
|
+
img.onload = () => {
|
|
271
|
+
clearTimeout(timer);
|
|
272
|
+
resolve();
|
|
273
|
+
};
|
|
274
|
+
img.onerror = () => {
|
|
275
|
+
clearTimeout(timer);
|
|
276
|
+
resolve();
|
|
277
|
+
};
|
|
278
|
+
img.src = albedoDataUrl;
|
|
279
|
+
});
|
|
280
|
+
width = img.naturalWidth || 512;
|
|
281
|
+
height = img.naturalHeight || 512;
|
|
282
|
+
const canvas = document.createElement('canvas');
|
|
283
|
+
canvas.width = width;
|
|
284
|
+
canvas.height = height;
|
|
285
|
+
const ctx = canvas.getContext('2d');
|
|
286
|
+
if (ctx) {
|
|
287
|
+
ctx.drawImage(img, 0, 0);
|
|
288
|
+
normalDataUrl = this.renderNormalMap(canvas, normalStrength);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
catch {
|
|
292
|
+
normalDataUrl = undefined;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
const item = {
|
|
296
|
+
id: `ai_tex_${Date.now()}_${Math.floor(Math.random() * 10000)}`,
|
|
297
|
+
name: uniqueName,
|
|
298
|
+
prompt,
|
|
299
|
+
style: 'stone',
|
|
300
|
+
size: Math.max(width, height),
|
|
301
|
+
seed: 0,
|
|
302
|
+
seamless: true,
|
|
303
|
+
albedoDataUrl,
|
|
304
|
+
normalDataUrl,
|
|
305
|
+
roughness: 0.7,
|
|
306
|
+
metallic: 0.1,
|
|
307
|
+
normalStrength,
|
|
308
|
+
primaryColor: '#888888',
|
|
309
|
+
secondaryColor: '#444444',
|
|
310
|
+
accentColor: '#222222',
|
|
311
|
+
createdAt: Date.now(),
|
|
312
|
+
embeddedAlbedoName: `${uniqueName}_albedo.png`,
|
|
313
|
+
embeddedNormalName: normalDataUrl ? `${uniqueName}_normal.png` : undefined,
|
|
314
|
+
};
|
|
315
|
+
this.service.addTextureFromDataUrl(item.embeddedAlbedoName, albedoDataUrl);
|
|
316
|
+
if (normalDataUrl && item.embeddedNormalName) {
|
|
317
|
+
this.service.addTextureFromDataUrl(item.embeddedNormalName, normalDataUrl);
|
|
318
|
+
}
|
|
319
|
+
this.history.unshift(item);
|
|
320
|
+
this.onHistoryChanged?.(this.getHistory());
|
|
321
|
+
return item;
|
|
322
|
+
}
|
|
323
|
+
/**
|
|
324
|
+
* Applies a generated texture to a specific mesh, the currently selected mesh, or all scene meshes.
|
|
325
|
+
*/
|
|
326
|
+
applyTextureToMesh(textureIdOrName, targetMeshName = 'selected', uvScale = 1) {
|
|
327
|
+
const item = this.getTextureById(textureIdOrName);
|
|
328
|
+
if (!item) {
|
|
329
|
+
throw new Error(`Generated texture "${textureIdOrName}" not found.`);
|
|
330
|
+
}
|
|
331
|
+
const meshes = this.resolveTargetMeshes(targetMeshName);
|
|
332
|
+
if (meshes.length === 0) {
|
|
333
|
+
throw new Error(targetMeshName === 'selected'
|
|
334
|
+
? 'No mesh selected. Please select a mesh in the scene first or specify a target mesh name.'
|
|
335
|
+
: `Target mesh "${targetMeshName}" not found in scene.`);
|
|
336
|
+
}
|
|
337
|
+
const previousMaterials = meshes.map((m) => ({ mesh: m, material: m.material }));
|
|
338
|
+
const matName = `Mat_${item.name}`;
|
|
339
|
+
let pbr = this.service.scene.getMaterialByName(matName);
|
|
340
|
+
if (!pbr || !(pbr instanceof PBRMaterial)) {
|
|
341
|
+
pbr = new PBRMaterial(matName, this.service.scene);
|
|
342
|
+
}
|
|
343
|
+
const isDecal = item.style === 'decal';
|
|
344
|
+
pbr.roughness = item.roughness;
|
|
345
|
+
pbr.metallic = item.metallic;
|
|
346
|
+
let albedoTex = this.service.scene.textures.find((t) => t instanceof Texture && !t.isRenderTarget && t.url === item.albedoDataUrl);
|
|
347
|
+
if (!albedoTex) {
|
|
348
|
+
albedoTex = new Texture(item.albedoDataUrl, this.service.scene, false, false, Texture.TRILINEAR_SAMPLINGMODE);
|
|
349
|
+
albedoTex.name = item.embeddedAlbedoName || `${item.name}_albedo.png`;
|
|
350
|
+
}
|
|
351
|
+
albedoTex.uScale = uvScale;
|
|
352
|
+
albedoTex.vScale = uvScale;
|
|
353
|
+
albedoTex.hasAlpha = isDecal;
|
|
354
|
+
if (isDecal) {
|
|
355
|
+
albedoTex.onLoadObservable.addOnce(() => {
|
|
356
|
+
albedoTex.hasAlpha = true;
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
pbr.useAlphaFromAlbedoTexture = isDecal;
|
|
360
|
+
pbr.transparencyMode = isDecal ? PBRMaterial.PBRMATERIAL_ALPHABLEND : PBRMaterial.PBRMATERIAL_OPAQUE;
|
|
361
|
+
pbr.backFaceCulling = !isDecal;
|
|
362
|
+
pbr.albedoTexture = albedoTex;
|
|
363
|
+
// Prevent blinding white flash while texture is decoding asynchronously
|
|
364
|
+
if (albedoTex.isReady()) {
|
|
365
|
+
pbr.albedoColor = Color3.White();
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
try {
|
|
369
|
+
pbr.albedoColor = Color3.FromHexString(item.primaryColor || '#888888');
|
|
370
|
+
}
|
|
371
|
+
catch {
|
|
372
|
+
pbr.albedoColor = new Color3(0.5, 0.5, 0.5);
|
|
373
|
+
}
|
|
374
|
+
albedoTex.onLoadObservable.addOnce(() => {
|
|
375
|
+
pbr.albedoColor = Color3.White();
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
if (item.normalDataUrl) {
|
|
379
|
+
let normalTex = this.service.scene.textures.find((t) => t instanceof Texture && !t.isRenderTarget && t.url === item.normalDataUrl);
|
|
380
|
+
if (!normalTex) {
|
|
381
|
+
normalTex = new Texture(item.normalDataUrl, this.service.scene, false, false, Texture.TRILINEAR_SAMPLINGMODE);
|
|
382
|
+
normalTex.name = item.embeddedNormalName || `${item.name}_normal.png`;
|
|
383
|
+
}
|
|
384
|
+
normalTex.uScale = uvScale;
|
|
385
|
+
normalTex.vScale = uvScale;
|
|
386
|
+
if (normalTex !== albedoTex) {
|
|
387
|
+
normalTex.hasAlpha = false;
|
|
388
|
+
}
|
|
389
|
+
normalTex.level = Math.max(0.1, Math.min(3.0, item.normalStrength / 2.0));
|
|
390
|
+
pbr.bumpTexture = normalTex;
|
|
391
|
+
}
|
|
392
|
+
const appliedTo = [];
|
|
393
|
+
for (const mesh of meshes) {
|
|
394
|
+
mesh.material = pbr;
|
|
395
|
+
appliedTo.push(mesh.name);
|
|
396
|
+
}
|
|
397
|
+
const targetMat = pbr;
|
|
398
|
+
this.service.recordAction({
|
|
399
|
+
name: `Apply AI Texture (${item.name})`,
|
|
400
|
+
undo: () => {
|
|
401
|
+
for (const entry of previousMaterials) {
|
|
402
|
+
if (!entry.mesh.isDisposed()) {
|
|
403
|
+
entry.mesh.material = entry.material;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
this.service.events.onSceneModified?.();
|
|
407
|
+
},
|
|
408
|
+
redo: () => {
|
|
409
|
+
for (const entry of previousMaterials) {
|
|
410
|
+
if (!entry.mesh.isDisposed()) {
|
|
411
|
+
entry.mesh.material = targetMat;
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
this.service.events.onSceneModified?.();
|
|
415
|
+
},
|
|
416
|
+
});
|
|
417
|
+
this.service.events.onSceneModified?.();
|
|
418
|
+
return { appliedTo, materialName: pbr.name };
|
|
419
|
+
}
|
|
420
|
+
/**
|
|
421
|
+
* Triggers a browser download of the generated texture PNG (albedo or normal map).
|
|
422
|
+
*/
|
|
423
|
+
downloadTexture(textureIdOrName, mapType = 'albedo') {
|
|
424
|
+
const item = this.getTextureById(textureIdOrName);
|
|
425
|
+
if (!item)
|
|
426
|
+
return false;
|
|
427
|
+
const dataUrl = mapType === 'normal' ? item.normalDataUrl : item.albedoDataUrl;
|
|
428
|
+
if (!dataUrl || typeof document === 'undefined')
|
|
429
|
+
return false;
|
|
430
|
+
const a = document.createElement('a');
|
|
431
|
+
a.href = dataUrl;
|
|
432
|
+
a.download = `${item.name}_${mapType}.png`;
|
|
433
|
+
document.body.appendChild(a);
|
|
434
|
+
a.click();
|
|
435
|
+
document.body.removeChild(a);
|
|
436
|
+
return true;
|
|
437
|
+
}
|
|
438
|
+
resolveTargetMeshes(targetMeshName) {
|
|
439
|
+
const validSceneMeshes = this.service.scene.meshes.filter((m) => m instanceof Mesh &&
|
|
440
|
+
m.getTotalVertices() > 0 &&
|
|
441
|
+
!this.service.isEditorHelper(m) &&
|
|
442
|
+
!m.name.startsWith('decal_preview'));
|
|
443
|
+
if (!targetMeshName || targetMeshName === 'selected') {
|
|
444
|
+
const selected = this.service.selectedMesh || this.service.selectedNodes[0] || null;
|
|
445
|
+
if (selected && selected instanceof Mesh && selected.getTotalVertices() > 0) {
|
|
446
|
+
return [selected];
|
|
447
|
+
}
|
|
448
|
+
if (selected) {
|
|
449
|
+
const childMeshes = selected.getChildMeshes(false).filter((m) => m instanceof Mesh && m.getTotalVertices() > 0);
|
|
450
|
+
if (childMeshes.length > 0)
|
|
451
|
+
return childMeshes;
|
|
452
|
+
}
|
|
453
|
+
return validSceneMeshes.length === 1 ? [validSceneMeshes[0]] : [];
|
|
454
|
+
}
|
|
455
|
+
if (targetMeshName === 'all' || targetMeshName === '*') {
|
|
456
|
+
return validSceneMeshes;
|
|
457
|
+
}
|
|
458
|
+
const exact = validSceneMeshes.find((m) => m.name === targetMeshName);
|
|
459
|
+
if (exact)
|
|
460
|
+
return [exact];
|
|
461
|
+
const lower = targetMeshName.toLowerCase();
|
|
462
|
+
return validSceneMeshes.filter((m) => m.name.toLowerCase().includes(lower));
|
|
463
|
+
}
|
|
464
|
+
clampSize(size) {
|
|
465
|
+
if (size <= 256)
|
|
466
|
+
return 256;
|
|
467
|
+
if (size <= 512)
|
|
468
|
+
return 512;
|
|
469
|
+
if (size <= 1024)
|
|
470
|
+
return 1024;
|
|
471
|
+
return 2048;
|
|
472
|
+
}
|
|
473
|
+
sanitizeName(raw) {
|
|
474
|
+
const cleaned = raw
|
|
475
|
+
.toLowerCase()
|
|
476
|
+
.replace(/[^a-z0-9_]+/g, '_')
|
|
477
|
+
.replace(/^_+|_+$/g, '')
|
|
478
|
+
.slice(0, 28);
|
|
479
|
+
return cleaned || 'ai_texture';
|
|
480
|
+
}
|
|
481
|
+
deriveNameFromPrompt(prompt, style) {
|
|
482
|
+
const stopWords = new Set(['a', 'an', 'the', 'with', 'and', 'of', 'in', 'on', 'for', 'to', 'texture', 'seamless', 'tileable']);
|
|
483
|
+
const words = prompt
|
|
484
|
+
.toLowerCase()
|
|
485
|
+
.replace(/[^a-z0-9\s]/g, ' ')
|
|
486
|
+
.split(/\s+/)
|
|
487
|
+
.filter((w) => w.length > 2 && !stopWords.has(w))
|
|
488
|
+
.slice(0, 3);
|
|
489
|
+
if (words.length === 0)
|
|
490
|
+
return `tex_${style}`;
|
|
491
|
+
return words.join('_');
|
|
492
|
+
}
|
|
493
|
+
analyzePrompt(prompt, options) {
|
|
494
|
+
const lower = prompt.toLowerCase();
|
|
495
|
+
// 1. Determine style
|
|
496
|
+
let style = 'stone';
|
|
497
|
+
if (options.style && options.style !== 'auto') {
|
|
498
|
+
style = options.style;
|
|
499
|
+
}
|
|
500
|
+
else if (/(decal|sticker|badge|splatter|bullet|emblem|graffiti|stain|crack\s+overlay)/i.test(lower)) {
|
|
501
|
+
style = 'decal';
|
|
502
|
+
}
|
|
503
|
+
else if (/(cobble|paver|flagstone|dungeon\s+floor|pavement)/i.test(lower)) {
|
|
504
|
+
style = 'cobblestone';
|
|
505
|
+
}
|
|
506
|
+
else if (/(brick|masonry|terracotta\s+wall)/i.test(lower)) {
|
|
507
|
+
style = 'brick';
|
|
508
|
+
}
|
|
509
|
+
else if (/(wood|plank|timber|oak|pine|mahogany|bark|birch|walnut|board)/i.test(lower)) {
|
|
510
|
+
style = 'wood';
|
|
511
|
+
}
|
|
512
|
+
else if (/(lava|magma|volcan|molten|hellfire|ember)/i.test(lower)) {
|
|
513
|
+
style = 'lava';
|
|
514
|
+
}
|
|
515
|
+
else if (/(scifi|sci-fi|cyber|futur|hull|panel|hex|tech|circuit|neon)/i.test(lower)) {
|
|
516
|
+
style = 'scifi';
|
|
517
|
+
}
|
|
518
|
+
else if (/(rune|arcane|magic|glyph|sigil|enchant)/i.test(lower)) {
|
|
519
|
+
style = 'runes';
|
|
520
|
+
}
|
|
521
|
+
else if (/(rust|corrod|oxidiz|weathered\s+iron|scrap)/i.test(lower)) {
|
|
522
|
+
style = 'rust';
|
|
523
|
+
}
|
|
524
|
+
else if (/(metal|steel|iron|titanium|chrome|aluminum|brass|copper|gold|silver|armor|plate)/i.test(lower)) {
|
|
525
|
+
style = 'metal';
|
|
526
|
+
}
|
|
527
|
+
else if (/(marble|alabaster|travertine|quartzite|onyx)/i.test(lower)) {
|
|
528
|
+
style = 'marble';
|
|
529
|
+
}
|
|
530
|
+
else if (/(scale|dragon|reptil|snake|fish\s+scale|chainmail)/i.test(lower)) {
|
|
531
|
+
style = 'scales';
|
|
532
|
+
}
|
|
533
|
+
else if (/(fabric|cloth|linen|canvas|weave|carpet|velvet|leather|denim)/i.test(lower)) {
|
|
534
|
+
style = 'fabric';
|
|
535
|
+
}
|
|
536
|
+
else if (/(moss|grass|lichen|jungle|forest\s+floor|turf|foliage|dirt|mud)/i.test(lower)) {
|
|
537
|
+
style = 'moss';
|
|
538
|
+
}
|
|
539
|
+
else {
|
|
540
|
+
style = 'stone';
|
|
541
|
+
}
|
|
542
|
+
// 2. Default palettes per style
|
|
543
|
+
const defaultPalettes = {
|
|
544
|
+
stone: { primary: '#6e737b', secondary: '#3d4147', accent: '#9aa0a8', roughness: 0.85, metallic: 0.05, normalStrength: 2.2 },
|
|
545
|
+
cobblestone: { primary: '#5e6368', secondary: '#25282c', accent: '#4b6043', roughness: 0.82, metallic: 0.05, normalStrength: 2.8 },
|
|
546
|
+
brick: { primary: '#9c4132', secondary: '#3b3532', accent: '#c4bfb6', roughness: 0.85, metallic: 0.02, normalStrength: 2.5 },
|
|
547
|
+
wood: { primary: '#8b5a2b', secondary: '#4a2e14', accent: '#b87d4b', roughness: 0.68, metallic: 0.0, normalStrength: 1.6 },
|
|
548
|
+
metal: { primary: '#8c969e', secondary: '#495159', accent: '#c4cdd6', roughness: 0.32, metallic: 0.88, normalStrength: 1.5 },
|
|
549
|
+
rust: { primary: '#5a6065', secondary: '#8c3b1b', accent: '#c85a24', roughness: 0.72, metallic: 0.55, normalStrength: 2.4 },
|
|
550
|
+
scifi: { primary: '#2b323b', secondary: '#14181d', accent: '#00e5ff', roughness: 0.35, metallic: 0.8, normalStrength: 2.2 },
|
|
551
|
+
marble: { primary: '#eceae4', secondary: '#b8b4ac', accent: '#6d6a63', roughness: 0.2, metallic: 0.05, normalStrength: 0.9 },
|
|
552
|
+
lava: { primary: '#232023', secondary: '#110f12', accent: '#ff5500', roughness: 0.78, metallic: 0.08, normalStrength: 2.6 },
|
|
553
|
+
moss: { primary: '#3e6b2c', secondary: '#283618', accent: '#6a994e', roughness: 0.9, metallic: 0.0, normalStrength: 2.3 },
|
|
554
|
+
fabric: { primary: '#3d5a80', secondary: '#293241', accent: '#98c1d9', roughness: 0.88, metallic: 0.0, normalStrength: 1.4 },
|
|
555
|
+
scales: { primary: '#2d6a4f', secondary: '#1b4332', accent: '#d4af37', roughness: 0.42, metallic: 0.35, normalStrength: 2.4 },
|
|
556
|
+
runes: { primary: '#2b2836', secondary: '#16141c', accent: '#a855f7', roughness: 0.65, metallic: 0.2, normalStrength: 2.4 },
|
|
557
|
+
decal: { primary: '#4a4e54', secondary: '#22252a', accent: '#d97706', roughness: 0.7, metallic: 0.1, normalStrength: 2.0 },
|
|
558
|
+
};
|
|
559
|
+
const base = { ...defaultPalettes[style] };
|
|
560
|
+
// 3. Override colors from prompt keywords if not explicitly provided
|
|
561
|
+
const keywordColors = [
|
|
562
|
+
{ regex: /\b(crimson|blood|scarlet)\b/i, hex: '#991b1b' },
|
|
563
|
+
{ regex: /\b(red|ruby)\b/i, hex: '#b91c1c' },
|
|
564
|
+
{ regex: /\b(orange|amber|copper)\b/i, hex: '#d97706' },
|
|
565
|
+
{ regex: /\b(gold|golden|brass|yellow)\b/i, hex: '#d4af37' },
|
|
566
|
+
{ regex: /\b(emerald|green|jade|lime)\b/i, hex: '#15803d' },
|
|
567
|
+
{ regex: /\b(cyan|turquoise|aqua|ice|frost)\b/i, hex: '#06b6d4' },
|
|
568
|
+
{ regex: /\b(blue|sapphire|cobalt|navy|azure)\b/i, hex: '#1d4ed8' },
|
|
569
|
+
{ regex: /\b(purple|violet|amethyst|arcane|magenta)\b/i, hex: '#7e22ce' },
|
|
570
|
+
{ regex: /\b(pink|rose)\b/i, hex: '#db2777' },
|
|
571
|
+
{ regex: /\b(white|snow|ivory|pearl)\b/i, hex: '#f3f4f6' },
|
|
572
|
+
{ regex: /\b(black|obsidian|charcoal|ebony|dark)\b/i, hex: '#1f2937' },
|
|
573
|
+
{ regex: /\b(sand|desert|sandstone|beige|tan)\b/i, hex: '#d6b37c' },
|
|
574
|
+
{ regex: /\b(brown|chocolate|mud|walnut)\b/i, hex: '#6b4226' },
|
|
575
|
+
];
|
|
576
|
+
const matchedHexes = [];
|
|
577
|
+
for (const kc of keywordColors) {
|
|
578
|
+
if (kc.regex.test(lower)) {
|
|
579
|
+
matchedHexes.push(kc.hex);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
let primaryHex = options.primaryColor || base.primary;
|
|
583
|
+
let secondaryHex = options.secondaryColor || base.secondary;
|
|
584
|
+
let accentHex = options.accentColor || base.accent;
|
|
585
|
+
if (!options.primaryColor && matchedHexes.length >= 1) {
|
|
586
|
+
if (style === 'lava' || style === 'scifi' || style === 'runes') {
|
|
587
|
+
accentHex = matchedHexes[0];
|
|
588
|
+
if (matchedHexes.length >= 2)
|
|
589
|
+
primaryHex = matchedHexes[1];
|
|
590
|
+
}
|
|
591
|
+
else {
|
|
592
|
+
primaryHex = matchedHexes[0];
|
|
593
|
+
if (matchedHexes.length >= 2)
|
|
594
|
+
accentHex = matchedHexes[1];
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
// Material modifiers from prompt
|
|
598
|
+
let roughness = options.roughness ?? base.roughness;
|
|
599
|
+
let metallic = options.metallic ?? base.metallic;
|
|
600
|
+
if (options.roughness === undefined) {
|
|
601
|
+
if (/\b(polished|glossy|shiny|wet|mirror|smooth)\b/i.test(lower))
|
|
602
|
+
roughness = Math.max(0.08, roughness - 0.35);
|
|
603
|
+
if (/\b(rough|matte|coarse|weathered|dry|dusty)\b/i.test(lower))
|
|
604
|
+
roughness = Math.min(0.98, roughness + 0.15);
|
|
605
|
+
}
|
|
606
|
+
if (options.metallic === undefined) {
|
|
607
|
+
if (/\b(metallic|chrome|gold|silver|steel|iron|brass|copper)\b/i.test(lower))
|
|
608
|
+
metallic = Math.max(0.75, metallic);
|
|
609
|
+
}
|
|
610
|
+
const seed = options.seed ?? this.hashPromptToSeed(prompt);
|
|
611
|
+
const normalStrength = options.normalStrength ?? base.normalStrength;
|
|
612
|
+
return {
|
|
613
|
+
style,
|
|
614
|
+
primaryColor: this.hexToRgb(primaryHex),
|
|
615
|
+
secondaryColor: this.hexToRgb(secondaryHex),
|
|
616
|
+
accentColor: this.hexToRgb(accentHex),
|
|
617
|
+
primaryHex,
|
|
618
|
+
secondaryHex,
|
|
619
|
+
accentHex,
|
|
620
|
+
roughness,
|
|
621
|
+
metallic,
|
|
622
|
+
normalStrength,
|
|
623
|
+
scale: 8,
|
|
624
|
+
seed,
|
|
625
|
+
hasAlpha: style === 'decal',
|
|
626
|
+
};
|
|
627
|
+
}
|
|
628
|
+
hashPromptToSeed(str) {
|
|
629
|
+
let h = 2166136261;
|
|
630
|
+
for (let i = 0; i < str.length; i++) {
|
|
631
|
+
h ^= str.charCodeAt(i);
|
|
632
|
+
h = Math.imul(h, 16777619);
|
|
633
|
+
}
|
|
634
|
+
return Math.abs(h >>> 0) || 1337;
|
|
635
|
+
}
|
|
636
|
+
hexToRgb(hex) {
|
|
637
|
+
const clean = hex.replace('#', '').trim();
|
|
638
|
+
if (clean.length === 3) {
|
|
639
|
+
return [
|
|
640
|
+
parseInt(clean[0] + clean[0], 16) || 128,
|
|
641
|
+
parseInt(clean[1] + clean[1], 16) || 128,
|
|
642
|
+
parseInt(clean[2] + clean[2], 16) || 128,
|
|
643
|
+
];
|
|
644
|
+
}
|
|
645
|
+
const num = parseInt(clean.slice(0, 6), 16);
|
|
646
|
+
if (Number.isNaN(num))
|
|
647
|
+
return [128, 128, 128];
|
|
648
|
+
return [(num >> 16) & 255, (num >> 8) & 255, num & 255];
|
|
649
|
+
}
|
|
650
|
+
// Deterministic 2D hash with periodic wrapping for 100% seamless tiling
|
|
651
|
+
hash2D(ix, iy, periodX, periodY, seed) {
|
|
652
|
+
const wx = ((ix % periodX) + periodX) % periodX;
|
|
653
|
+
const wy = ((iy % periodY) + periodY) % periodY;
|
|
654
|
+
let n = Math.imul(wx, 374761393) + Math.imul(wy, 668265263) + Math.imul(seed, 1442695041);
|
|
655
|
+
n = (n ^ (n >>> 13)) * 1274126177;
|
|
656
|
+
return ((n ^ (n >>> 16)) >>> 0) / 4294967295;
|
|
657
|
+
}
|
|
658
|
+
valueNoiseSeamless(u, v, freq, seed) {
|
|
659
|
+
const x = u * freq;
|
|
660
|
+
const y = v * freq;
|
|
661
|
+
const ix = Math.floor(x);
|
|
662
|
+
const iy = Math.floor(y);
|
|
663
|
+
const fx = x - ix;
|
|
664
|
+
const fy = y - iy;
|
|
665
|
+
const sx = fx * fx * (3 - 2 * fx);
|
|
666
|
+
const sy = fy * fy * (3 - 2 * fy);
|
|
667
|
+
const n00 = this.hash2D(ix, iy, freq, freq, seed);
|
|
668
|
+
const n10 = this.hash2D(ix + 1, iy, freq, freq, seed);
|
|
669
|
+
const n01 = this.hash2D(ix, iy + 1, freq, freq, seed);
|
|
670
|
+
const n11 = this.hash2D(ix + 1, iy + 1, freq, freq, seed);
|
|
671
|
+
const nx0 = n00 + sx * (n10 - n00);
|
|
672
|
+
const nx1 = n01 + sx * (n11 - n01);
|
|
673
|
+
return nx0 + sy * (nx1 - nx0);
|
|
674
|
+
}
|
|
675
|
+
fbmSeamless(u, v, baseFreq, octaves, seed) {
|
|
676
|
+
let total = 0;
|
|
677
|
+
let amp = 0.5;
|
|
678
|
+
let maxAmp = 0;
|
|
679
|
+
let freq = baseFreq;
|
|
680
|
+
for (let i = 0; i < octaves; i++) {
|
|
681
|
+
total += this.valueNoiseSeamless(u, v, freq, seed + i * 101) * amp;
|
|
682
|
+
maxAmp += amp;
|
|
683
|
+
amp *= 0.5;
|
|
684
|
+
freq *= 2;
|
|
685
|
+
}
|
|
686
|
+
return total / (maxAmp || 1);
|
|
687
|
+
}
|
|
688
|
+
voronoiSeamless(u, v, gridCount, seed) {
|
|
689
|
+
const gx = u * gridCount;
|
|
690
|
+
const gy = v * gridCount;
|
|
691
|
+
const ix = Math.floor(gx);
|
|
692
|
+
const iy = Math.floor(gy);
|
|
693
|
+
let f1 = 10.0;
|
|
694
|
+
let f2 = 10.0;
|
|
695
|
+
let cellId = 0;
|
|
696
|
+
for (let dy = -1; dy <= 1; dy++) {
|
|
697
|
+
for (let dx = -1; dx <= 1; dx++) {
|
|
698
|
+
const cx = ix + dx;
|
|
699
|
+
const cy = iy + dy;
|
|
700
|
+
const jx = this.hash2D(cx, cy, gridCount, gridCount, seed + 17);
|
|
701
|
+
const jy = this.hash2D(cx, cy, gridCount, gridCount, seed + 53);
|
|
702
|
+
const px = cx + 0.15 + jx * 0.7;
|
|
703
|
+
const py = cy + 0.15 + jy * 0.7;
|
|
704
|
+
const dist = Math.hypot(gx - px, gy - py);
|
|
705
|
+
if (dist < f1) {
|
|
706
|
+
f2 = f1;
|
|
707
|
+
f1 = dist;
|
|
708
|
+
cellId = this.hash2D(cx, cy, gridCount, gridCount, seed + 99);
|
|
709
|
+
}
|
|
710
|
+
else if (dist < f2) {
|
|
711
|
+
f2 = dist;
|
|
712
|
+
}
|
|
713
|
+
}
|
|
714
|
+
}
|
|
715
|
+
return { f1, f2, edgeDist: f2 - f1, cellId };
|
|
716
|
+
}
|
|
717
|
+
lerpColor(c1, c2, t) {
|
|
718
|
+
const k = Math.max(0, Math.min(1, t));
|
|
719
|
+
return [
|
|
720
|
+
c1[0] + (c2[0] - c1[0]) * k,
|
|
721
|
+
c1[1] + (c2[1] - c1[1]) * k,
|
|
722
|
+
c1[2] + (c2[2] - c1[2]) * k,
|
|
723
|
+
];
|
|
724
|
+
}
|
|
725
|
+
renderProceduralTexture(size, recipe, _seamless) {
|
|
726
|
+
if (typeof document === 'undefined') {
|
|
727
|
+
return { canvas: null, dataUrl: FALLBACK_PNG_DATA_URL };
|
|
728
|
+
}
|
|
729
|
+
const canvas = document.createElement('canvas');
|
|
730
|
+
canvas.width = size;
|
|
731
|
+
canvas.height = size;
|
|
732
|
+
const ctx = canvas.getContext('2d');
|
|
733
|
+
if (!ctx) {
|
|
734
|
+
return { canvas, dataUrl: FALLBACK_PNG_DATA_URL };
|
|
735
|
+
}
|
|
736
|
+
const imgData = ctx.createImageData(size, size);
|
|
737
|
+
const data = imgData.data;
|
|
738
|
+
const { primaryColor, secondaryColor, accentColor, seed, style } = recipe;
|
|
739
|
+
for (let y = 0; y < size; y++) {
|
|
740
|
+
const v = y / size;
|
|
741
|
+
for (let x = 0; x < size; x++) {
|
|
742
|
+
const u = x / size;
|
|
743
|
+
const idx = (y * size + x) * 4;
|
|
744
|
+
let rgb = [primaryColor[0], primaryColor[1], primaryColor[2]];
|
|
745
|
+
let alpha = 255;
|
|
746
|
+
const detailNoise = this.fbmSeamless(u, v, 8, 4, seed);
|
|
747
|
+
const microNoise = this.valueNoiseSeamless(u, v, 32, seed + 777) - 0.5;
|
|
748
|
+
switch (style) {
|
|
749
|
+
case 'cobblestone': {
|
|
750
|
+
const warpU = u + (detailNoise - 0.5) * 0.04;
|
|
751
|
+
const warpV = v + (this.fbmSeamless(u, v, 8, 3, seed + 33) - 0.5) * 0.04;
|
|
752
|
+
const vor = this.voronoiSeamless(warpU, warpV, 6, seed);
|
|
753
|
+
const mortarMask = Math.max(0, Math.min(1, (vor.edgeDist - 0.07) / 0.08));
|
|
754
|
+
const stoneShade = 0.75 + vor.cellId * 0.4 + (detailNoise - 0.5) * 0.35;
|
|
755
|
+
const stoneColor = [
|
|
756
|
+
primaryColor[0] * stoneShade,
|
|
757
|
+
primaryColor[1] * stoneShade,
|
|
758
|
+
primaryColor[2] * stoneShade,
|
|
759
|
+
];
|
|
760
|
+
const mortarColor = this.lerpColor(secondaryColor, accentColor, detailNoise * 0.7);
|
|
761
|
+
rgb = this.lerpColor(mortarColor, stoneColor, mortarMask);
|
|
762
|
+
break;
|
|
763
|
+
}
|
|
764
|
+
case 'brick': {
|
|
765
|
+
const rows = 8;
|
|
766
|
+
const cols = 4;
|
|
767
|
+
const row = Math.floor(v * rows);
|
|
768
|
+
const offset = (row % 2) * 0.5;
|
|
769
|
+
const bu = (u * cols + offset) % 1;
|
|
770
|
+
const bv = (v * rows) % 1;
|
|
771
|
+
const warp = (detailNoise - 0.5) * 0.03;
|
|
772
|
+
const edgeX = Math.min(bu + warp, 1 - (bu + warp));
|
|
773
|
+
const edgeY = Math.min(bv + warp, 1 - (bv + warp));
|
|
774
|
+
const mortar = edgeX < 0.045 || edgeY < 0.07 ? 0 : 1;
|
|
775
|
+
const brickId = this.hash2D(Math.floor(u * cols + offset), row, cols, rows, seed);
|
|
776
|
+
const brickBase = this.lerpColor(primaryColor, secondaryColor, brickId * 0.45);
|
|
777
|
+
const brickTextured = [
|
|
778
|
+
brickBase[0] * (0.82 + detailNoise * 0.35),
|
|
779
|
+
brickBase[1] * (0.82 + detailNoise * 0.35),
|
|
780
|
+
brickBase[2] * (0.82 + detailNoise * 0.35),
|
|
781
|
+
];
|
|
782
|
+
rgb = mortar > 0.5 ? brickTextured : this.lerpColor(accentColor, secondaryColor, detailNoise * 0.4);
|
|
783
|
+
break;
|
|
784
|
+
}
|
|
785
|
+
case 'wood': {
|
|
786
|
+
const planks = 4;
|
|
787
|
+
const plankIdx = Math.floor(u * planks);
|
|
788
|
+
const plankU = (u * planks) % 1;
|
|
789
|
+
const plankGap = plankU < 0.02 || plankU > 0.98 ? 0.25 : 1.0;
|
|
790
|
+
const grainShift = this.fbmSeamless(u, v, 4, 3, seed + plankIdx * 19) * 0.25;
|
|
791
|
+
const rings = Math.sin((u * 24 + grainShift * 12 + plankIdx * 1.7) * Math.PI * 2) * 0.5 + 0.5;
|
|
792
|
+
const fineStreak = this.valueNoiseSeamless(u * 4, v * 0.5, 16, seed + 55);
|
|
793
|
+
const t = rings * 0.65 + fineStreak * 0.35;
|
|
794
|
+
const woodCol = this.lerpColor(secondaryColor, primaryColor, t);
|
|
795
|
+
rgb = [woodCol[0] * plankGap, woodCol[1] * plankGap, woodCol[2] * plankGap];
|
|
796
|
+
break;
|
|
797
|
+
}
|
|
798
|
+
case 'scifi': {
|
|
799
|
+
const grid = 4;
|
|
800
|
+
const gu = (u * grid) % 1;
|
|
801
|
+
const gv = (v * grid) % 1;
|
|
802
|
+
const bevelDist = Math.min(gu, 1 - gu, gv, 1 - gv);
|
|
803
|
+
const seam = bevelDist < 0.03;
|
|
804
|
+
const glowSeam = bevelDist >= 0.03 && bevelDist < 0.055 && ((Math.floor(u * grid) + Math.floor(v * grid)) % 2 === 0);
|
|
805
|
+
const panelId = this.hash2D(Math.floor(u * grid), Math.floor(v * grid), grid, grid, seed);
|
|
806
|
+
const panelCol = this.lerpColor(primaryColor, secondaryColor, panelId * 0.5 + detailNoise * 0.2);
|
|
807
|
+
if (glowSeam) {
|
|
808
|
+
rgb = accentColor;
|
|
809
|
+
}
|
|
810
|
+
else if (seam) {
|
|
811
|
+
rgb = [secondaryColor[0] * 0.4, secondaryColor[1] * 0.4, secondaryColor[2] * 0.4];
|
|
812
|
+
}
|
|
813
|
+
else {
|
|
814
|
+
rgb = panelCol;
|
|
815
|
+
}
|
|
816
|
+
break;
|
|
817
|
+
}
|
|
818
|
+
case 'rust': {
|
|
819
|
+
const rustMask = Math.max(0, Math.min(1, (detailNoise - 0.42) * 3.2));
|
|
820
|
+
const metalStreak = this.fbmSeamless(u * 2, v * 0.5, 8, 3, seed + 88);
|
|
821
|
+
const baseMetal = this.lerpColor(primaryColor, [primaryColor[0] * 0.6, primaryColor[1] * 0.6, primaryColor[2] * 0.6], metalStreak);
|
|
822
|
+
const rustCol = this.lerpColor(secondaryColor, accentColor, detailNoise);
|
|
823
|
+
rgb = this.lerpColor(baseMetal, rustCol, rustMask);
|
|
824
|
+
break;
|
|
825
|
+
}
|
|
826
|
+
case 'metal': {
|
|
827
|
+
const brushed = this.valueNoiseSeamless(u * 6, v * 0.5, 16, seed) * 0.25 + detailNoise * 0.2;
|
|
828
|
+
rgb = this.lerpColor(secondaryColor, this.lerpColor(primaryColor, accentColor, brushed), 0.6 + brushed);
|
|
829
|
+
break;
|
|
830
|
+
}
|
|
831
|
+
case 'marble': {
|
|
832
|
+
const turb = this.fbmSeamless(u, v, 4, 5, seed);
|
|
833
|
+
const vein = Math.pow(Math.abs(Math.sin((u * 3 + v * 2 + turb * 2.8) * Math.PI)), 0.35);
|
|
834
|
+
rgb = this.lerpColor(accentColor, this.lerpColor(secondaryColor, primaryColor, vein), vein);
|
|
835
|
+
break;
|
|
836
|
+
}
|
|
837
|
+
case 'lava': {
|
|
838
|
+
const warpU = u + (detailNoise - 0.5) * 0.08;
|
|
839
|
+
const warpV = v + (this.fbmSeamless(u, v, 6, 3, seed + 41) - 0.5) * 0.08;
|
|
840
|
+
const vor = this.voronoiSeamless(warpU, warpV, 5, seed);
|
|
841
|
+
const crackGlow = Math.max(0, 1 - vor.edgeDist * 9.0);
|
|
842
|
+
const rockCol = this.lerpColor(secondaryColor, primaryColor, detailNoise);
|
|
843
|
+
const hotCol = [
|
|
844
|
+
Math.min(255, accentColor[0] * 1.25),
|
|
845
|
+
Math.min(255, accentColor[1] * (0.6 + crackGlow * 0.6)),
|
|
846
|
+
accentColor[2] * crackGlow,
|
|
847
|
+
];
|
|
848
|
+
rgb = this.lerpColor(rockCol, hotCol, Math.pow(crackGlow, 1.5));
|
|
849
|
+
break;
|
|
850
|
+
}
|
|
851
|
+
case 'runes': {
|
|
852
|
+
const rockBase = this.lerpColor(secondaryColor, primaryColor, detailNoise);
|
|
853
|
+
const grid = 4;
|
|
854
|
+
const gu = (u * grid) % 1;
|
|
855
|
+
const gv = (v * grid) % 1;
|
|
856
|
+
const cellHash = this.hash2D(Math.floor(u * grid), Math.floor(v * grid), grid, grid, seed);
|
|
857
|
+
const cx = Math.abs(gu - 0.5);
|
|
858
|
+
const cy = Math.abs(gv - 0.5);
|
|
859
|
+
const ring = Math.abs(Math.hypot(gu - 0.5, gv - 0.5) - (0.18 + cellHash * 0.12));
|
|
860
|
+
const glyphStroke = (cx < 0.28 && cy < 0.28 && (ring < 0.035 || (Math.abs(cx - cy) < 0.03 && cellHash > 0.3))) ? 1 : 0;
|
|
861
|
+
rgb = glyphStroke > 0 ? accentColor : rockBase;
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
case 'scales': {
|
|
865
|
+
const scaleRows = 8;
|
|
866
|
+
const sy = v * scaleRows;
|
|
867
|
+
const row = Math.floor(sy);
|
|
868
|
+
const sx = (u * scaleRows + (row % 2) * 0.5) % 1;
|
|
869
|
+
const fy = sy % 1;
|
|
870
|
+
const dist = Math.hypot(sx - 0.5, fy * 0.85);
|
|
871
|
+
const edge = Math.max(0, Math.min(1, (0.52 - dist) * 6));
|
|
872
|
+
const highlight = Math.max(0, 1 - Math.abs(dist - 0.38) * 8);
|
|
873
|
+
const baseScale = this.lerpColor(secondaryColor, primaryColor, edge * (0.7 + detailNoise * 0.3));
|
|
874
|
+
rgb = this.lerpColor(baseScale, accentColor, highlight * 0.45);
|
|
875
|
+
break;
|
|
876
|
+
}
|
|
877
|
+
case 'fabric': {
|
|
878
|
+
const weaveFreq = 32;
|
|
879
|
+
const wx = Math.sin(u * weaveFreq * Math.PI * 2);
|
|
880
|
+
const wy = Math.sin(v * weaveFreq * Math.PI * 2);
|
|
881
|
+
const pattern = (wx * wy * 0.5 + 0.5) * 0.35 + detailNoise * 0.35;
|
|
882
|
+
rgb = this.lerpColor(secondaryColor, this.lerpColor(primaryColor, accentColor, pattern), 0.5 + pattern * 0.5);
|
|
883
|
+
break;
|
|
884
|
+
}
|
|
885
|
+
case 'moss': {
|
|
886
|
+
const patch = this.fbmSeamless(u, v, 6, 5, seed);
|
|
887
|
+
const lush = Math.max(0, Math.min(1, (patch - 0.35) * 2.2));
|
|
888
|
+
const soilCol = this.lerpColor(secondaryColor, [secondaryColor[0] * 0.7, secondaryColor[1] * 0.7, secondaryColor[2] * 0.7], detailNoise);
|
|
889
|
+
const mossCol = this.lerpColor(primaryColor, accentColor, this.valueNoiseSeamless(u, v, 24, seed + 12));
|
|
890
|
+
rgb = this.lerpColor(soilCol, mossCol, lush);
|
|
891
|
+
break;
|
|
892
|
+
}
|
|
893
|
+
case 'decal': {
|
|
894
|
+
const dx = u - 0.5;
|
|
895
|
+
const dy = v - 0.5;
|
|
896
|
+
const radial = Math.hypot(dx, dy) * 2.0;
|
|
897
|
+
const ragged = radial + (detailNoise - 0.5) * 0.38;
|
|
898
|
+
if (ragged > 0.88) {
|
|
899
|
+
alpha = 0;
|
|
900
|
+
}
|
|
901
|
+
else {
|
|
902
|
+
alpha = Math.round(Math.max(0, Math.min(1, (0.88 - ragged) * 5)) * 255);
|
|
903
|
+
}
|
|
904
|
+
const ringAccent = Math.abs(ragged - 0.55) < 0.08 ? 1 : 0;
|
|
905
|
+
const innerBase = this.lerpColor(secondaryColor, primaryColor, detailNoise);
|
|
906
|
+
rgb = ringAccent ? this.lerpColor(innerBase, accentColor, 0.75) : innerBase;
|
|
907
|
+
break;
|
|
908
|
+
}
|
|
909
|
+
case 'stone':
|
|
910
|
+
default: {
|
|
911
|
+
const cracks = this.voronoiSeamless(u, v, 4, seed).edgeDist;
|
|
912
|
+
const crackDarken = Math.max(0.45, Math.min(1, cracks * 6));
|
|
913
|
+
const baseStone = this.lerpColor(secondaryColor, primaryColor, detailNoise);
|
|
914
|
+
const specStone = this.lerpColor(baseStone, accentColor, Math.max(0, (detailNoise - 0.6) * 2));
|
|
915
|
+
rgb = [
|
|
916
|
+
specStone[0] * crackDarken,
|
|
917
|
+
specStone[1] * crackDarken,
|
|
918
|
+
specStone[2] * crackDarken,
|
|
919
|
+
];
|
|
920
|
+
break;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
data[idx] = Math.max(0, Math.min(255, Math.round(rgb[0] + microNoise * 14)));
|
|
924
|
+
data[idx + 1] = Math.max(0, Math.min(255, Math.round(rgb[1] + microNoise * 14)));
|
|
925
|
+
data[idx + 2] = Math.max(0, Math.min(255, Math.round(rgb[2] + microNoise * 14)));
|
|
926
|
+
data[idx + 3] = alpha;
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
ctx.putImageData(imgData, 0, 0);
|
|
930
|
+
return { canvas, dataUrl: canvas.toDataURL('image/png') };
|
|
931
|
+
}
|
|
932
|
+
renderNormalMap(sourceCanvas, strength) {
|
|
933
|
+
if (!sourceCanvas)
|
|
934
|
+
return FALLBACK_PNG_DATA_URL;
|
|
935
|
+
const ctx = sourceCanvas.getContext('2d');
|
|
936
|
+
if (!ctx)
|
|
937
|
+
return FALLBACK_PNG_DATA_URL;
|
|
938
|
+
try {
|
|
939
|
+
return generateNormalMapFromCanvas(sourceCanvas, strength);
|
|
940
|
+
}
|
|
941
|
+
catch {
|
|
942
|
+
return FALLBACK_PNG_DATA_URL;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
}
|