Skip to content

Commit 6518243

Browse files
committed
feat: add srgb to linear rgb color space conversion for textures
1 parent a665bb2 commit 6518243

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/src/core/texture.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
export type TextureData = ArrayBufferView | null;
22

3-
type BaseTextureParams = {
3+
type MagFilter = "linear" | "nearest";
4+
type MinFilter = "linear" | "nearest" | "linear-mipmap-linear" | "nearest-mipmap-linear";
5+
type ColorSpace = "srgb" | "linear-rgb";
6+
type WrappingMode = "clamp-to-edge" | "repeat" | "mirrored-repeat";
7+
8+
export type BaseTextureParams = {
49
/**
510
* @default "linear-mipmap-linear" if generateMipmaps is true, "linear" otherwise
611
*/
@@ -37,6 +42,10 @@ type BaseTextureParams = {
3742
* @default WebGL2RenderingContext.RGBA
3843
*/
3944
internalFormat?: number;
45+
/**
46+
* @default "linear-rgb"
47+
*/
48+
colorSpace?: ColorSpace;
4049
/**
4150
* @default WebGL2RenderingContext.RGBA
4251
*/
@@ -75,10 +84,6 @@ export type ImageTextureParams = BaseTextureParams & {
7584

7685
export type TextureParams = DataTextureParams | ImageTextureParams;
7786

78-
type MagFilter = "linear" | "nearest";
79-
type MinFilter = "linear" | "nearest" | "linear-mipmap-linear" | "nearest-mipmap-linear";
80-
type WrappingMode = "clamp-to-edge" | "repeat" | "mirrored-repeat";
81-
8287
const minFilterMap: Record<MinFilter, number> = {
8388
linear: WebGL2RenderingContext.LINEAR,
8489
nearest: WebGL2RenderingContext.NEAREST,
@@ -118,8 +123,11 @@ export function fillTexture(
118123
const {
119124
level = 0,
120125
flipY = true,
121-
internalFormat = WebGL2RenderingContext.RGBA,
122126
format = WebGL2RenderingContext.RGBA,
127+
colorSpace = "linear-rgb",
128+
internalFormat = colorSpace === "srgb"
129+
? WebGL2RenderingContext.SRGB8_ALPHA8
130+
: WebGL2RenderingContext.RGBA,
123131
type = WebGL2RenderingContext.UNSIGNED_BYTE,
124132
generateMipmaps = true,
125133
anisotropy = navigator.hardwareConcurrency, // in most case between 4 and 12, depending on the hardware range

0 commit comments

Comments
 (0)