Skip to content

Commit 9bf9d6c

Browse files
authored
fix: change ColorTexture to use Uint8Array instead of ImageData (#449)
Fixes running on Chrome 38 Fixes #389 Before: ![image](https://github.com/user-attachments/assets/d9dc8ae5-d7b6-45c1-9b23-e909a29158ce) After: ![image](https://github.com/user-attachments/assets/421936ad-788c-4a17-9a5e-e09db2d7da55)
2 parents 9b7d807 + c0377fa commit 9bf9d6c

File tree

5 files changed

+46
-4
lines changed

5 files changed

+46
-4
lines changed

src/core/lib/WebGlContextWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ export class WebGlContextWrapper {
299299
internalformat: GLint,
300300
format: GLenum,
301301
type: GLenum,
302-
source: TexImageSource,
302+
source: TexImageSource | Uint8Array,
303303
): void;
304304
texImage2D(
305305
level: any,

src/core/renderers/webgl/WebGlCoreCtxSubTexture.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export class WebGlCoreCtxSubTexture extends WebGlCoreCtxTexture {
3434

3535
override async onLoadRequest(): Promise<Dimensions> {
3636
const props = await (this.textureSource as SubTexture).getTextureData();
37+
38+
if (props.data instanceof Uint8Array) {
39+
// its a 1x1 Color Texture
40+
return { width: 1, height: 1 };
41+
}
42+
3743
return {
3844
width: props.data?.width || 0,
3945
height: props.data?.height || 0,

src/core/renderers/webgl/WebGlCoreCtxTexture.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,29 @@ export class WebGlCoreCtxTexture extends CoreContextTexture {
204204
glw.texParameteri(glw.TEXTURE_MIN_FILTER, glw.LINEAR);
205205

206206
this.setTextureMemUse(view.byteLength);
207+
} else if (textureData.data && textureData.data instanceof Uint8Array) {
208+
// Color Texture
209+
width = 1;
210+
height = 1;
211+
212+
glw.bindTexture(this._nativeCtxTexture);
213+
glw.pixelStorei(
214+
glw.UNPACK_PREMULTIPLY_ALPHA_WEBGL,
215+
!!textureData.premultiplyAlpha,
216+
);
217+
218+
glw.texImage2D(
219+
0,
220+
glw.RGBA,
221+
width,
222+
height,
223+
0,
224+
glw.RGBA,
225+
glw.UNSIGNED_BYTE,
226+
textureData.data,
227+
);
228+
229+
this.setTextureMemUse(width * height * 4);
207230
} else {
208231
console.error(
209232
`WebGlCoreCtxTexture.onLoadRequest: Unexpected textureData returned`,

src/core/textures/ColorTexture.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,22 @@ export class ColorTexture extends Texture {
6363
}
6464

6565
override async getTextureData(): Promise<TextureData> {
66-
const pixelData32 = new Uint32Array([this.color]);
67-
const pixelData8 = new Uint8ClampedArray(pixelData32.buffer);
66+
const pixelData = new Uint8Array(4);
67+
68+
if (this.color === 0xffffffff) {
69+
pixelData[0] = 255;
70+
pixelData[1] = 255;
71+
pixelData[2] = 255;
72+
pixelData[3] = 255;
73+
} else {
74+
pixelData[0] = (this.color >> 16) & 0xff; // Red
75+
pixelData[1] = (this.color >> 8) & 0xff; // Green
76+
pixelData[2] = this.color & 0xff; // Blue
77+
pixelData[3] = (this.color >>> 24) & 0xff; // Alpha
78+
}
79+
6880
return {
69-
data: new ImageData(pixelData8, 1, 1),
81+
data: pixelData,
7082
premultiplyAlpha: true,
7183
};
7284
}

src/core/textures/Texture.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export interface TextureData {
9191
| SubTextureProps
9292
| CompressedData
9393
| HTMLImageElement
94+
| Uint8Array
9495
| null;
9596
/**
9697
* Premultiply alpha when uploading texture data to the GPU

0 commit comments

Comments
 (0)