-
Notifications
You must be signed in to change notification settings - Fork 1
Offscreencanvas #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Offscreencanvas #39
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
49bf09d
feat: add support for OffscreenCanvas in a Worker
jsulpis 73fdc6d
feat: add support for async uniforms
jsulpis d558845
refactor: remove placeholder from ImageTextureParams
jsulpis aa5a861
refactor: update the signature of the onUpdated callback
jsulpis eb522f7
refactor: extract type guards
jsulpis bc6acf7
fix small issues
jsulpis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/gl/playground/src/pages/core/offscreencanvas.astro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| --- | ||
| import Layout from "../../layouts/Layout.astro"; | ||
| --- | ||
|
|
||
| <script> | ||
| import { onResize } from "@radiance/helpers"; | ||
| import { incrementRenderCount } from "../../components/renderCount"; | ||
|
|
||
| const canvas = document.querySelector("canvas")!; | ||
| const offscreenCanvas = canvas.transferControlToOffscreen(); | ||
|
|
||
| const worker = new Worker(new URL("../../workers/offscreenGradient.worker.ts", import.meta.url), { | ||
| type: "module", | ||
| }); | ||
|
|
||
| worker.addEventListener("message", incrementRenderCount); | ||
|
|
||
| worker.postMessage({ type: "init", canvas: offscreenCanvas }, [offscreenCanvas]); | ||
|
|
||
| onResize(canvas, ({ devicePixelSize }) => { | ||
| worker.postMessage({ type: "resize", size: devicePixelSize }); | ||
| }); | ||
jsulpis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| </script> | ||
|
|
||
| <Layout title="Offscreen gradient"> | ||
| <canvas></canvas> | ||
| </Layout> | ||
|
|
||
| <style> | ||
| canvas { | ||
| aspect-ratio: 3 / 2; | ||
| } | ||
| </style> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/gl/playground/src/workers/offscreenGradient.worker.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { glCanvas, loadTexture, type GLCanvas } from "@radiance/gl"; | ||
|
|
||
| type InitMessage = { | ||
| type: "init"; | ||
| canvas: OffscreenCanvas; | ||
| }; | ||
|
|
||
| type ResizeMessage = { | ||
| type: "resize"; | ||
| size: { | ||
| width: number; | ||
| height: number; | ||
| }; | ||
| }; | ||
|
|
||
| type WorkerMessage = InitMessage | ResizeMessage; | ||
|
|
||
| addEventListener("message", (event: MessageEvent<WorkerMessage>) => { | ||
| switch (event.data.type) { | ||
| case "init": { | ||
| init(event.data); | ||
| break; | ||
| } | ||
| case "resize": { | ||
| resize(event.data); | ||
| break; | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| let scene: GLCanvas; | ||
|
|
||
| function init(message: InitMessage) { | ||
| scene = glCanvas({ | ||
| canvas: message.canvas, | ||
| fragment: /* glsl */ ` | ||
| in vec2 vUv; | ||
| uniform sampler2D uTexture; | ||
| uniform vec2 uResolution; | ||
| out vec4 fragColor; | ||
|
|
||
| #define CONTAIN 1 | ||
| #define COVER 2 | ||
| #define OBJECT_FIT CONTAIN | ||
|
|
||
| void main() { | ||
| vec2 textureResolution = vec2(textureSize(uTexture, 0)); | ||
| float canvasRatio = uResolution.x / uResolution.y; | ||
| float textureRatio = textureResolution.x / textureResolution.y; | ||
|
|
||
| vec2 uv = vUv - 0.5; | ||
| if (OBJECT_FIT == CONTAIN ? canvasRatio > textureRatio : canvasRatio < textureRatio) { | ||
| uv.x *= canvasRatio / textureRatio; | ||
| } else { | ||
| uv.y *= textureRatio / canvasRatio; | ||
| } | ||
| uv += 0.5; | ||
|
|
||
| vec3 color = texture(uTexture, uv).rgb; | ||
| color *= step(0., uv.x) * (1. - step(1., uv.x)); | ||
| color *= step(0., uv.y) * (1. - step(1., uv.y)); | ||
|
|
||
| fragColor = vec4(color, 1.); | ||
| } | ||
| `, | ||
| }); | ||
|
|
||
| scene.onAfterRender(() => { | ||
| postMessage({ type: "rendered" }); | ||
| }); | ||
|
|
||
| scene.uniforms.uTexture = loadTexture("https://picsum.photos/id/669/600/400"); | ||
jsulpis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| function resize(message: ResizeMessage) { | ||
| scene?.setSize(message.size); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.