Skip to content

Commit

Permalink
Fix issue with copying to a texture from a HTMLVideoElement
Browse files Browse the repository at this point in the history
  • Loading branch information
ggetz committed Sep 30, 2024
1 parent e3e9837 commit 2e63d5c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

##### Fixes :wrench:

- Fix Texture errors when using a `HTMLVideoElement`. [#12219](https://github.com/CesiumGS/cesium/issues/12219)
- Use first geometryBuffer if no best match found in I3SNode [#12132](https://github.com/CesiumGS/cesium/pull/12132)
- Update type definitions to allow undefined for optional parameters [#12193](https://github.com/CesiumGS/cesium/pull/12193)
- Reverts Firefox OIT temporary fix [#4815] and Firefox test failure fix [#5047]
Expand Down
19 changes: 16 additions & 3 deletions packages/engine/Source/Renderer/Texture.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ function Texture(options) {

let { width, height } = options;
if (defined(source)) {
// Make sure we are using the element's intrinsic width and height where available
if (!defined(width)) {
width = defaultValue(source.videoWidth, source.width);
width = source.videoWidth ?? source.naturalWidth ?? source.width;
}
if (!defined(height)) {
height = defaultValue(source.videoHeight, source.height);
height = source.videoHeight ?? source.naturalHeight ?? source.height;
}
}

Expand Down Expand Up @@ -810,7 +811,19 @@ Texture.prototype.copyFrom = function (options) {
gl.activeTexture(gl.TEXTURE0);
gl.bindTexture(target, this._texture);

const { width, height, arrayBufferView } = source;
let { width, height } = source;
const arrayBufferView = source.arrayBufferView;

// Make sure we are using the element's intrinsic width and height where available
if (defined(source.naturalWidth) && defined(source.naturalHeight)) {
width = source.naturalWidth;
height = source.naturalHeight;
}

if (defined(source.videoWidth) && defined(source.videoHeight)) {
width = source.videoWidth;
height = source.videoHeight;
}

const textureWidth = this._width;
const textureHeight = this._height;
Expand Down
31 changes: 31 additions & 0 deletions packages/engine/Specs/Renderer/TextureSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,15 @@ describe(
context.destroyForSpecs();
});

let blueImageHeight, blueImageWidth;
beforeEach(function () {
blueImageHeight = blueImage.height;
blueImageWidth = blueImage.width;
});

afterEach(function () {
blueImage.height = blueImageHeight;
blueImage.width = blueImageWidth;
texture = texture && texture.destroy();
});

Expand Down Expand Up @@ -670,6 +678,29 @@ describe(
}).contextToRender([0, 0, 255, 255]);
});

it("can copy from a DOM element when display dimensions are 0", function () {
blueImage.height = 0;
blueImage.width = 0;

texture = new Texture({
context: context,
pixelFormat: PixelFormat.RGB,
pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
source: blueImage,
});

texture.copyFrom({
source: blueImage,
});

expect({
context: context,
fragmentShader: fs,
uniformMap: uniformMap,
epsilon: 1,
}).contextToRender([0, 0, 255, 255]);
});

it("can replace a subset of a texture", function () {
texture = new Texture({
context: context,
Expand Down

0 comments on commit 2e63d5c

Please sign in to comment.