Skip to content

Commit 5a72ad8

Browse files
dakersankhesh
authored andcommitted
feat(Texture): add support for ImageBitmap texture
1 parent 071ee0a commit 5a72ad8

File tree

2 files changed

+102
-20
lines changed

2 files changed

+102
-20
lines changed

Sources/Rendering/Core/Texture/index.d.ts

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { vtkAlgorithm } from '../../../interfaces';
2+
import { Nullable } from '../../../types';
23

34
/**
45
*
@@ -14,68 +15,110 @@ export interface ITextureInitialValues {
1415
}
1516

1617
export interface vtkTexture extends vtkAlgorithm {
18+
19+
/**
20+
* Returns the canvas used by the texture.
21+
*/
22+
getCanvas(): Nullable<HTMLCanvasElement>;
23+
1724
/**
18-
*
25+
* Returns true if the texture is set to repeat at the edges.
1926
*/
2027
getRepeat(): boolean;
2128

2229
/**
23-
*
30+
* Returns true if the texture is set to clamp at the edges.
2431
*/
2532
getEdgeClamp(): boolean;
2633

2734
/**
28-
*
35+
* Returns true if the texture is set to interpolate between texels.
2936
*/
3037
getInterpolate(): boolean;
3138

3239
/**
33-
*
40+
* Returns the image used by the texture.
41+
*/
42+
getImage(): HTMLImageElement;
43+
44+
/**
45+
* Returns an ImageBitmap object.
3446
*/
35-
getImage(): any;
47+
getImageBitmap(): ImageBitmap;
3648

3749
/**
38-
*
50+
* Returns true if the image is loaded.
3951
*/
4052
getImageLoaded(): boolean;
4153

4254
/**
43-
*
55+
* Returns the input image data as a JavaScript ImageData object.
56+
*/
57+
getInputAsJsImageData(): any;
58+
59+
/**
60+
* Returns the current mip level of the texture.
4461
*/
4562
getMipLevel(): number;
4663

4764
/**
48-
*
49-
* @param repeat
50-
* @default false
65+
* Returns true if the texture can be resized at run time.
66+
* This is useful for dynamic textures that may change size based on user
67+
* interaction or other factors.
5168
*/
52-
setRepeat(repeat: boolean): boolean;
69+
getResizable(): boolean;
70+
71+
/**
72+
* Returns the canvas used by the texture.
73+
*/
74+
setCanvas(canvas: HTMLCanvasElement): void;
5375

5476
/**
55-
*
77+
* Sets the texture to clamp at the edges.
5678
* @param edgeClamp
5779
* @default false
5880
*/
5981
setEdgeClamp(edgeClamp: boolean): boolean;
6082

6183
/**
62-
*
84+
* Sets the texture to interpolate between texels.
6385
* @param interpolate
6486
* @default false
6587
*/
6688
setInterpolate(interpolate: boolean): boolean;
6789

6890
/**
69-
*
91+
* Sets the image used by the texture.
7092
* @param image
7193
* @default null
7294
*/
73-
setImage(image: any): void;
95+
setImage(image: HTMLImageElement): void;
96+
97+
/**
98+
* Sets the image as an ImageBitmap object.
99+
* Supported in WebGPU only.
100+
* @param imageBitmap
101+
*/
102+
setImageBitmap(imageBitmap: ImageBitmap): void;
103+
104+
/**
105+
* Sets the input image data as a JavaScript ImageData object.
106+
* @param imageData
107+
*/
108+
setJsImageData(imageData: any): void;
74109

75110
/**
111+
* Sets the current mip level of the texture.
76112
* @param level
77113
*/
78114
setMipLevel(level: number): boolean;
115+
116+
/**
117+
* Sets the texture to repeat at the edges.
118+
* @param repeat
119+
* @default false
120+
*/
121+
setRepeat(repeat: boolean): boolean;
79122
}
80123

81124
/**
@@ -116,10 +159,11 @@ export function generateMipmaps(
116159
): Array<Uint8ClampedArray>;
117160

118161
/**
119-
* vtkTexture is an image algorithm that handles loading and binding of texture maps.
120-
* It obtains its data from an input image data dataset type.
121-
* Thus you can create visualization pipelines to read, process, and construct textures.
122-
* Note that textures will only work if texture coordinates are also defined, and if the rendering system supports texture.
162+
* vtkTexture is an image algorithm that handles loading and binding of texture
163+
* maps. It obtains its data from an input image data dataset type. Thus you can
164+
* create visualization pipelines to read, process, and construct textures. Note
165+
* that textures will only work if texture coordinates are also defined, and if
166+
* the rendering system supports texture.
123167
*/
124168
export declare const vtkTexture: {
125169
newInstance: typeof newInstance;

Sources/Rendering/Core/Texture/index.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,34 @@ function vtkTexture(publicAPI, model) {
2626
publicAPI.setInputConnection(null);
2727
model.image = null;
2828
model.canvas = null;
29+
model.imageBitmap = null;
2930
}
3031

3132
model.jsImageData = imageData;
3233
model.imageLoaded = true;
3334
publicAPI.modified();
3435
};
3536

37+
publicAPI.setImageBitmap = (imageBitmap) => {
38+
if (model.imageBitmap === imageBitmap) {
39+
return;
40+
}
41+
42+
// clear other entries
43+
if (imageBitmap !== null) {
44+
publicAPI.setInputData(null);
45+
publicAPI.setInputConnection(null);
46+
model.image = null;
47+
model.canvas = null;
48+
model.jsImageData = null;
49+
}
50+
51+
model.imageBitmap = imageBitmap;
52+
model.imageLoaded = true;
53+
54+
publicAPI.modified();
55+
};
56+
3657
publicAPI.setCanvas = (canvas) => {
3758
if (model.canvas === canvas) {
3859
return;
@@ -43,6 +64,7 @@ function vtkTexture(publicAPI, model) {
4364
publicAPI.setInputData(null);
4465
publicAPI.setInputConnection(null);
4566
model.image = null;
67+
model.imageBitmap = null;
4668
model.jsImageData = null;
4769
}
4870

@@ -61,6 +83,7 @@ function vtkTexture(publicAPI, model) {
6183
publicAPI.setInputConnection(null);
6284
model.canvas = null;
6385
model.jsImageData = null;
86+
model.imageBitmap = null;
6487
}
6588

6689
model.image = image;
@@ -98,6 +121,11 @@ function vtkTexture(publicAPI, model) {
98121
width = model.image.width;
99122
height = model.image.height;
100123
}
124+
if (model.imageBitmap) {
125+
width = model.imageBitmap.width;
126+
height = model.imageBitmap.height;
127+
}
128+
101129
const dimensionality = (width > 1) + (height > 1) + (depth > 1);
102130
return dimensionality;
103131
};
@@ -108,6 +136,11 @@ function vtkTexture(publicAPI, model) {
108136
if (model.jsImageData) {
109137
return model.jsImageData();
110138
}
139+
140+
if (model.imageBitmap) {
141+
return model.imageBitmap();
142+
}
143+
111144
if (model.canvas) {
112145
const context = model.canvas.getContext('2d');
113146
const imageData = context.getImageData(
@@ -231,6 +264,7 @@ const generateMipmaps = (device, texture, mipLevelCount) => {
231264
});
232265

233266
const pipeline = device.createComputePipeline({
267+
label: 'ComputeMipmapPipeline',
234268
layout: pipelineLayout,
235269
compute: {
236270
module: computeShader,
@@ -264,7 +298,9 @@ const generateMipmaps = (device, texture, mipLevelCount) => {
264298
],
265299
});
266300

267-
const commandEncoder = device.createCommandEncoder();
301+
const commandEncoder = device.createCommandEncoder({
302+
label: `MipmapGenerateCommandEncoder`,
303+
});
268304
const computePass = commandEncoder.beginComputePass();
269305

270306
computePass.setPipeline(pipeline);
@@ -290,6 +326,7 @@ const DEFAULT_VALUES = {
290326
image: null,
291327
canvas: null,
292328
jsImageData: null,
329+
imageBitmap: null,
293330
imageLoaded: false,
294331
repeat: false,
295332
interpolate: false,
@@ -311,6 +348,7 @@ export function extend(publicAPI, model, initialValues = {}) {
311348
'canvas',
312349
'image',
313350
'jsImageData',
351+
'imageBitmap',
314352
'imageLoaded',
315353
'resizable',
316354
]);

0 commit comments

Comments
 (0)