From d5206234921f931011331d29a264ace09c25bd3b Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Mon, 9 Sep 2024 03:02:31 +1000 Subject: [PATCH] replace expensive srgb conversion with a simple gamma ramp in chunk mesh builder --- Sources/Voxelotl/ChunkMeshBuilder.swift | 2 +- Sources/Voxelotl/Common/Color.swift | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Sources/Voxelotl/ChunkMeshBuilder.swift b/Sources/Voxelotl/ChunkMeshBuilder.swift index 52054af..423c047 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).linear), + color: SIMD4(Color(color).pow(2.2)), //FIXME: Better sRGB approximation when I can be bothered texCoord: $0.texCoord) }) indices.append(contentsOf: sideIndices.map { orig + $0 }) diff --git a/Sources/Voxelotl/Common/Color.swift b/Sources/Voxelotl/Common/Color.swift index 6aa3d72..ecd4b06 100644 --- a/Sources/Voxelotl/Common/Color.swift +++ b/Sources/Voxelotl/Common/Color.swift @@ -139,7 +139,7 @@ public extension Color where T: BinaryFloatingPoint { if x < 0.04045 { x / 12.92 } else { - T(pow((Double(x) + 0.055) / 1.055, 2.4)) + T(Darwin.pow((Double(x) + 0.055) / 1.055, 2.4)) } } @@ -151,7 +151,7 @@ public extension Color where T: BinaryFloatingPoint { } else if x < 0.0031308 { x * 12.92 } else { - T(1.055 * pow(Double(abs(x)), 1 / 2.4) - 0.055) + T(1.055 * Darwin.pow(Double(abs(x)), 1 / 2.4) - 0.055) } } } @@ -195,3 +195,15 @@ public extension Color where T: FloatingPoint { } } } + +public extension Color where T == Float { + func pow(_ exponent: T) -> Self { + Self(r: powf(r, exponent), g: powf(g, exponent), b: powf(b, exponent), a: a) + } +} +public extension Color where T == Double { + func pow(_ exponent: T) -> Self { + Self(r: Darwin.pow(r, exponent), g: Darwin.pow(g, exponent), b: Darwin.pow(b, exponent), a: a) + } +} +