Skip to content

Commit

Permalink
feat(Texture): update image representation w/wgpu spec (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Oct 21, 2023
1 parent 3f336a3 commit 241ddea
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 176 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ A `Texture` transports or stores image or video data to the GPU as well as data

```ts
const pixel = new Uint8ClampedArray([76, 51, 128, 255])
const image = await createImageBitmap(new ImageData(pixel, 1, 1))
const image = new ImageData(pixel, 1, 1)
const texture = new Texture(image)
```

Expand Down
2 changes: 1 addition & 1 deletion examples/webgl-fullscreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ document.body.appendChild(renderer.canvas)
const material = new Material({
uniforms: {
time: 0,
color: new Texture(await createImageBitmap(new ImageData(new Uint8ClampedArray([76, 51, 128, 255]), 1, 1))),
color: new Texture(new ImageData(new Uint8ClampedArray([76, 51, 128, 255]), 1, 1)),
},
vertex: /* glsl */ `#version 300 es
out vec2 vUv;
Expand Down
4 changes: 2 additions & 2 deletions examples/webgpu-fullscreen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WebGPURenderer, Geometry, Material, Mesh, Texture } from 'four'
import { WebGPURenderer, Material, Mesh, Texture } from 'four'

const renderer = new WebGPURenderer()
renderer.setSize(window.innerWidth, window.innerHeight)
Expand All @@ -7,7 +7,7 @@ document.body.appendChild(renderer.canvas)
const material = new Material({
uniforms: {
time: 0,
color: new Texture(await createImageBitmap(new ImageData(new Uint8ClampedArray([76, 51, 128, 255]), 1, 1))),
color: new Texture(new ImageData(new Uint8ClampedArray([76, 51, 128, 255]), 1, 1)),
},
vertex: /* wgsl */ `
struct Uniforms {
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
"exports": "./dist/index.js",
"sideEffects": false,
"devDependencies": {
"@types/node": "^20.4.2",
"typescript": "^5.1.6",
"vite": "^4.4.3"
"@types/node": "^20.8.7",
"typescript": "^5.2.2",
"vite": "^4.5.0"
},
"dependencies": {
"@webgpu/types": "^0.1.34"
"@webgpu/types": "^0.1.38"
},
"scripts": {
"dev": "vite",
Expand Down
9 changes: 8 additions & 1 deletion src/Texture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { Sampler } from './Sampler'
/**
* Represents a texture image source.
*/
export type ImageRepresentation = ImageBitmap | HTMLCanvasElement | HTMLVideoElement | OffscreenCanvas
export type ImageRepresentation =
| ImageBitmap
| ImageData
| HTMLImageElement
| HTMLVideoElement
| VideoFrame
| HTMLCanvasElement
| OffscreenCanvas

/**
* Constructs a texture. Useful for displaying and storing image data.
Expand Down
4 changes: 2 additions & 2 deletions src/WebGPURenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ export class WebGPURenderer {
*/
private _updateTexture(
texture: Texture,
width = texture.image?.width ?? 0,
height = texture.image?.height ?? 0,
width = (texture.image as any)?.width ?? 0,
height = (texture.image as any)?.height ?? 0,
): GPUTexture | GPUExternalTexture {
let target = this._textures.get(texture)
if (!target || texture.needsUpdate) {
Expand Down
337 changes: 172 additions & 165 deletions yarn.lock

Large diffs are not rendered by default.

0 comments on commit 241ddea

Please sign in to comment.