Skip to content
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

Fix type of CompressedTexture.mipmaps #1024

Merged

Conversation

HunterLarco
Copy link
Contributor

@HunterLarco HunterLarco commented Jun 18, 2024

Currently CompressedTexture and CompressedArrayTexture represent mipmaps using the browser's ImageData. This is actually inaccurate to three.js's implementation in a few ways:

  1. ImageData#colorSpace is unused by three.js.
  2. ImageData#data is the incorrect type. It is Uint8ClampedArray however, using Uint8ClampedArray will sometimes cause runtime breakages in three.js (see below for details)

Of these two, (2) is the real problem. ImageData#data is defined as Uint8ClampedArray, however, mipmap data is regularly used by threejs in texSubImage2D and compressedTexSubImage2D which both require that the source data array view matches the texture type. For example, if I have a texture using RGIntegerFormat with FloatType then compressedTexSubImage2D must be passed a Float32Array. As currently written, clients would be forced to use Uint8ClampedArray which will cause a runtime error due to misalignment between the source data and texture type. This is documented by texSubImage2D which specifies

One of the following objects can be used as a pixel source for the texture:

  • Uint8Array (Must be used if type is gl.UNSIGNED_BYTE)
  • Uint16Array (Must be used if type is either gl.UNSIGNED_SHORT_5_6_5, gl.UNSIGNED_SHORT_4_4_4_4, gl.UNSIGNED_SHORT_5_5_5_1, or ext.HALF_FLOAT_OES)
  • Float32Array (Must be used if type is gl.FLOAT)

The fix here is to replace the current usage of ImageData with a new type MipmapImageData which better aligns to three.js's internal usage, and importantly allows the data field to be populated by any TypedArray.

@HunterLarco HunterLarco marked this pull request as ready for review June 18, 2024 18:38
@Methuselah96 Methuselah96 changed the title introduce MipmapImageData Fix type of CompressedTexture.mipmaps Jun 18, 2024
Copy link
Contributor

@Methuselah96 Methuselah96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I did some cleanup to the PR, hopefully while preserving the intent of your changes.

@Methuselah96 Methuselah96 merged commit b77b069 into three-types:master Jun 18, 2024
4 checks passed
@HunterLarco
Copy link
Contributor Author

This looks great! Thanks for tidying up the PR

@HunterLarco HunterLarco deleted the hunter/replace-image-data branch June 18, 2024 22:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants