From d4b13772388d94945fd459672966d89b62753c94 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Mon, 9 Sep 2024 02:34:04 +1000 Subject: [PATCH] decrease memory usage by storing chunk colours as 8bit (at the cost of slower mesh generation) --- Sources/Voxelotl/Chunk.swift | 2 +- Sources/Voxelotl/ChunkMeshBuilder.swift | 2 +- Sources/Voxelotl/Generator/StandardWorldGenerator.swift | 4 ++-- Sources/Voxelotl/Generator/TerrorTowerGenerator.swift | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Sources/Voxelotl/Chunk.swift b/Sources/Voxelotl/Chunk.swift index d26e18d..7b62b1c 100644 --- a/Sources/Voxelotl/Chunk.swift +++ b/Sources/Voxelotl/Chunk.swift @@ -123,7 +123,7 @@ public struct Chunk: Hashable { public enum BlockType: Hashable { case air - case solid(_ color: Color) + case solid(_ color: Color) } public struct Block: Hashable { diff --git a/Sources/Voxelotl/ChunkMeshBuilder.swift b/Sources/Voxelotl/ChunkMeshBuilder.swift index 5784121..52054af 100644 --- a/Sources/Voxelotl/ChunkMeshBuilder.swift +++ b/Sources/Voxelotl/ChunkMeshBuilder.swift @@ -13,7 +13,7 @@ struct ChunkMeshBuilder { .init( position: SIMD3(position) + $0.position, normal: $0.normal, - color: SIMD4(color), + color: SIMD4(Color(color).linear), texCoord: $0.texCoord) }) indices.append(contentsOf: sideIndices.map { orig + $0 }) diff --git a/Sources/Voxelotl/Generator/StandardWorldGenerator.swift b/Sources/Voxelotl/Generator/StandardWorldGenerator.swift index 2742f4a..48b0571 100644 --- a/Sources/Voxelotl/Generator/StandardWorldGenerator.swift +++ b/Sources/Voxelotl/Generator/StandardWorldGenerator.swift @@ -32,9 +32,9 @@ struct StandardWorldGenerator: WorldGenerator { if self.ravineMask.get(position * SIMD3(1, 0.441, 1)) >= 0.8 && abs(self.ravineNoise.get(position * SIMD3(1, 0.6, 1))) <= 0.1 { return .air } #endif - return .solid(.init( + return .solid(Color(.init( hue: Float(180 + self.colorNoise.get(position) * 180), - saturation: 0.47, value: 0.9).linear) + saturation: 0.47, value: 0.9))) } let chunkOrigin = chunkID.getPosition() diff --git a/Sources/Voxelotl/Generator/TerrorTowerGenerator.swift b/Sources/Voxelotl/Generator/TerrorTowerGenerator.swift index c921591..bbbe737 100644 --- a/Sources/Voxelotl/Generator/TerrorTowerGenerator.swift +++ b/Sources/Voxelotl/Generator/TerrorTowerGenerator.swift @@ -19,11 +19,11 @@ struct TerrorTowerGenerator: WorldGenerator { let gradient = simd_length(fpos.xz) / 14.0 let value = self.noise1.get(fpos) - 0.25 return if gradient + value < threshold { - .solid(.init( + .solid(Color(.init( hue: ((fpos.x * 0.5 + fpos.y) / 30.0) * 360.0, saturation: 0.2 + noise2.get(fpos) * 0.2, value: 0.75 + noise2.get(SIMD4(fpos, 1) * 0.25) - ).linear) + ))) } else { .air }