Skip to content

allow further texture uniforms #12558

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## 1.130 - 2025-06-01

### @cesium/engine

#### Additions :tada:

- Added the ability to pass `OffscreenCanvas` & `ImageBitmap` directly to `Material` uniforms. [#12558](https://github.com/CesiumGS/cesium/pull/12558)

## 1.129 - 2025-05-01

### @cesium/engine
Expand Down
9 changes: 6 additions & 3 deletions packages/engine/Source/Renderer/Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ import TextureMinificationFilter from "./TextureMinificationFilter.js";
* @typedef {object} Texture.ConstructorOptions
*
* @property {Context} context
* @property {object} [source] The source for texel values to be loaded into the texture.
* @property {object} [source] The source for texel values to be loaded into the texture. A {@link ImageData}, {@link HTMLImageElement}, {@link HTMLCanvasElement},
* {@link HTMLVideoElement}, {@link OffscreenCanvas}, or {@link ImageBitmap},
* or an object with width, height, and arrayBufferView properties.
* @property {PixelFormat} [pixelFormat=PixelFormat.RGBA] The format of each pixel, i.e., the number of components it has and what they represent.
* @property {PixelDatatype} [pixelDatatype=PixelDatatype.UNSIGNED_BYTE] The data type of each pixel.
* @property {boolean} [flipY=true] If true, the source values will be read as if the y-axis is inverted (y=0 at the top).
Expand Down Expand Up @@ -483,7 +485,7 @@ function loadFramebufferSource(texture, source) {
* Load texel data from an Image into a texture.
*
* @param {Texture} texture The texture to which texel values will be loaded.
* @param {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement} source The source for texel values to be loaded into the texture.
* @param {ImageData|HTMLImageElement|HTMLCanvasElement|HTMLVideoElement|OffscreenCanvas|ImageBitmap} source The source for texel values to be loaded into the texture.
*
* @private
*/
Expand Down Expand Up @@ -830,7 +832,8 @@ function setupSampler(texture, sampler) {
* Copy new image data into this texture, from a source {@link ImageData}, {@link HTMLImageElement}, {@link HTMLCanvasElement}, or {@link HTMLVideoElement}.
* or an object with width, height, and arrayBufferView properties.
* @param {object} options Object with the following properties:
* @param {object} options.source The source {@link ImageData}, {@link HTMLImageElement}, {@link HTMLCanvasElement}, or {@link HTMLVideoElement},
* @param {object} options.source The source {@link ImageData}, {@link HTMLImageElement}, {@link HTMLCanvasElement}, {@link HTMLVideoElement},
* {@link OffscreenCanvas}, or {@link ImageBitmap},
* or an object with width, height, and arrayBufferView properties.
* @param {number} [options.xOffset=0] The offset in the x direction within the texture to copy into.
* @param {number} [options.yOffset=0] The offset in the y direction within the texture to copy into.
Expand Down
8 changes: 6 additions & 2 deletions packages/engine/Source/Scene/Material.js
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,9 @@ function createTexture2DUpdateFunction(uniformId) {
});
} else if (
uniformValue instanceof HTMLCanvasElement ||
uniformValue instanceof HTMLImageElement
uniformValue instanceof HTMLImageElement ||
uniformValue instanceof ImageBitmap ||
uniformValue instanceof OffscreenCanvas
) {
material._loadedImages.push({
id: uniformId,
Expand Down Expand Up @@ -1091,7 +1093,9 @@ function getUniformType(uniformValue) {
type === "string" ||
uniformValue instanceof Resource ||
uniformValue instanceof HTMLCanvasElement ||
uniformValue instanceof HTMLImageElement
uniformValue instanceof HTMLImageElement ||
uniformValue instanceof ImageBitmap ||
uniformValue instanceof OffscreenCanvas
) {
if (/^([rgba]){1,4}$/i.test(uniformValue)) {
uniformType = "channels";
Expand Down
38 changes: 38 additions & 0 deletions packages/engine/Specs/Scene/MaterialSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,44 @@ describe(
renderMaterial(material);
});

it("creates a material with an image offscreen canvas uniform", function () {
const canvas = new OffscreenCanvas(1, 1);
const context2D = canvas.getContext("2d");
context2D.fillStyle = "rgb(0,0,255)";
context2D.fillRect(0, 0, 1, 1);

const material = new Material({
strict: true,
fabric: {
type: "DiffuseMap",
uniforms: {
image: canvas,
},
},
});

renderMaterial(material);
});

it("creates a material with an image bitmap", function () {
const canvas = new OffscreenCanvas(1, 1);
const context2D = canvas.getContext("2d");
context2D.fillStyle = "rgb(0,0,255)";
context2D.fillRect(0, 0, 1, 1);

const material = new Material({
strict: true,
fabric: {
type: "DiffuseMap",
uniforms: {
image: canvas.transferToImageBitmap(),
},
},
});

renderMaterial(material);
});

it("creates a material with an KTX2 compressed image uniform", function () {
let compressedUrl;
if (FeatureDetection.supportsBasis(scene)) {
Expand Down