From df6f52464ecd3cf1108c76bfd3b20f46a0157e82 Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Mon, 18 May 2026 16:28:59 +0200 Subject: [PATCH] Implement solid color fallback texture for terrain layers --- src/rendering/terrain-layer-material.ts | 40 +++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/src/rendering/terrain-layer-material.ts b/src/rendering/terrain-layer-material.ts index cc02fb87..5f96185f 100644 --- a/src/rendering/terrain-layer-material.ts +++ b/src/rendering/terrain-layer-material.ts @@ -34,6 +34,7 @@ interface TerrainLayerColorBlendMaterialOptions { } let fallbackTerrainLayerTexture: Texture | null = null; +const solidColorTerrainLayerTextures = new Map(); function createFallbackTerrainLayerTexture(): Texture { const texture = new DataTexture( @@ -60,13 +61,46 @@ export function getFallbackTerrainLayerTexture(): Texture { return fallbackTerrainLayerTexture; } +function getSolidColorTerrainLayerTexture(colorHex: string): Texture { + const cachedTexture = solidColorTerrainLayerTextures.get(colorHex); + + if (cachedTexture !== undefined) { + return cachedTexture; + } + + const color = new Color(colorHex); + const texture = new DataTexture( + new Uint8Array([ + Math.round(color.r * 255), + Math.round(color.g * 255), + Math.round(color.b * 255), + 255 + ]), + 1, + 1, + RGBAFormat, + UnsignedByteType + ); + + texture.colorSpace = SRGBColorSpace; + texture.minFilter = LinearFilter; + texture.magFilter = LinearFilter; + texture.wrapS = ClampToEdgeWrapping; + texture.wrapT = ClampToEdgeWrapping; + texture.needsUpdate = true; + solidColorTerrainLayerTextures.set(colorHex, texture); + return texture; +} + export function getTerrainLayerTexture( material: MaterialDef | null, textureLookup: (material: MaterialDef) => Texture | null ): Texture { - return material === null - ? getFallbackTerrainLayerTexture() - : (textureLookup(material) ?? getFallbackTerrainLayerTexture()); + if (material === null) { + return getFallbackTerrainLayerTexture(); + } + + return textureLookup(material) ?? getSolidColorTerrainLayerTexture(material.swatchColorHex); } export function getTerrainLayerPreviewColor(