Skip to content

Commit 7193197

Browse files
committed
feat: add a colorSpace option to facilitate the usage of display-p3
1 parent 84d4556 commit 7193197

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

lib/playground/src/pages/core/scissor.astro

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Layout from "../../layouts/Layout.astro";
33
---
44

55
<script>
6-
import { useWebGLCanvas } from "usegl";
6+
import { useQuadRenderPass, useWebGLCanvas } from "usegl";
77
import { incrementRenderCount } from "../../components/renderCount";
88

99
const canvas = document.querySelector("canvas");
@@ -18,14 +18,12 @@ import Layout from "../../layouts/Layout.astro";
1818
renderMode: "manual",
1919
});
2020

21-
const rightShader = useWebGLCanvas({
22-
canvas,
21+
const rightShader = useQuadRenderPass(leftShader.gl, {
2322
fragment: /* glsl */ `
2423
void main() {
2524
gl_FragColor = vec4(0, 1, 0, 1);
2625
}
2726
`,
28-
renderMode: "manual",
2927
});
3028

3129
leftShader.onAfterRender(incrementRenderCount);

lib/src/hooks/useWebGLCanvas.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import { useResizeObserver } from "./useResizeObserver";
2-
import type { Attribute, DrawMode, PostEffect, Uniforms } from "../types";
2+
import type { PostEffect, Uniforms } from "../types";
33
import { useWebGLContext } from "./useWebGLContext";
4+
import type { QuadPassOptions } from "./useQuadRenderPass";
45
import { useQuadRenderPass } from "./useQuadRenderPass";
56
import { useCompositor } from "./useCompositor";
67
import { findUniformName } from "../internal/findName";
78
import type { UseLoopOptions } from "./useLoop";
89
import { useLoop } from "./useLoop";
910

10-
interface Props<U extends Uniforms> extends UseLoopOptions {
11+
interface Props<U extends Uniforms> extends UseLoopOptions, QuadPassOptions<U> {
1112
canvas: HTMLCanvasElement | OffscreenCanvas | string;
12-
fragment: string;
13-
vertex?: string;
14-
uniforms?: U;
15-
attributes?: Record<string, Attribute>;
1613
webglOptions?: WebGLContextAttributes;
17-
drawMode?: DrawMode;
1814
dpr?: number;
1915
postEffects?: PostEffect[];
2016
renderMode?: "manual" | "auto";
17+
colorSpace?: PredefinedColorSpace;
2118
}
2219

2320
export const useWebGLCanvas = <U extends Uniforms>(props: Props<U>) => {
@@ -29,9 +26,15 @@ export const useWebGLCanvas = <U extends Uniforms>(props: Props<U>) => {
2926
postEffects = [],
3027
immediate,
3128
renderMode = "auto",
29+
colorSpace,
30+
webglOptions,
3231
} = props;
3332

34-
const { gl, canvas, setSize: setCanvasSize } = useWebGLContext(canvasProp);
33+
const {
34+
gl,
35+
canvas,
36+
setSize: setCanvasSize,
37+
} = useWebGLContext(canvasProp, { ...webglOptions, colorSpace });
3538

3639
const primaryPass = useQuadRenderPass(gl, props);
3740
const compositor = useCompositor(gl, primaryPass, postEffects);

lib/src/hooks/useWebGLContext.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
export type WebGLContextOptions = WebGLContextAttributes & {
2+
colorSpace?: PredefinedColorSpace;
3+
};
4+
15
export function useWebGLContext(
26
canvas: HTMLCanvasElement | OffscreenCanvas | string,
3-
options?: WebGLContextAttributes,
7+
options?: WebGLContextOptions,
48
) {
59
const canvasElement =
610
typeof canvas === "string" ? document.querySelector<HTMLCanvasElement>(canvas) : canvas;
@@ -14,6 +18,10 @@ export function useWebGLContext(
1418
throw new Error("No WebGL2 context available.");
1519
}
1620

21+
if ("drawingBufferColorSpace" in gl && options?.colorSpace != undefined) {
22+
gl.drawingBufferColorSpace = options.colorSpace;
23+
}
24+
1725
function setSize(width: number, height: number) {
1826
canvasElement!.width = width;
1927
canvasElement!.height = height;

0 commit comments

Comments
 (0)