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

Support WebP in environmentTextureTools #11102

Merged
merged 5 commits into from
Sep 17, 2021
Merged

Conversation

simonihmig
Copy link
Contributor

This adds support for encoding and decoding WebP images in .env files. See https://forum.babylonjs.com/t/support-webp-for-env-maps/23644

The default type remains PNG, so the changes should be fully backwards compatible. I tested this by locally changing the arguments to CreateEnvTextureAsync in the Inspector, exporting some sample environments with different settings, and dropping them again into the sandbox. Everything seems to work fine. Not sure if there is some additional test coverage we can add here?

The second commit adds an optional quality argument to a bunch of Blob-related tools, so that it can be passed finally to canvas.toBlob(), which supports this argument for WebP images (at least in Chrome).

The results are quite satisfying. The playground's original environment.dds gave these results when converted to .env:

  • using PNG: 271KB
  • using WebP w/ default quality (0.8): 71KB
  • using WebP w/ quality=0.5: 64KB
  • using WebP w/ quality=0.1: 57KB

You can find these sample files here

I didn't update the Inspector to allow exporting .env files using WebP yet, but can do this in a separate PR when this is merged!

@sebavan
Copy link
Member

sebavan commented Sep 17, 2021

@simonihmig This is huge !!! I ll review in a couple hours thanks a lot !!!

@@ -1,328 +1,329 @@
# 5.0.0
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you check what is wrong with this file? We cannot compare them :(

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is weird. The diff in my IDE looked all good. Will check...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Seems my IDE changed line endings to CRLF, no idea why, which made every line change. Fixed this now!

Copy link
Member

@sebavan sebavan left a comment

Choose a reason for hiding this comment

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

Would be great if you could fix the changes in the what s new and I ll then merge it all :-)

@@ -218,6 +249,7 @@ export class EnvironmentTextureTools {
let info: EnvironmentTextureInfo = {
version: 1,
Copy link
Member

Choose a reason for hiding this comment

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

Should we bump the version ? like 1.1 as it is not a breaking change ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, my thinking was we can keep it as is, as nothing has been removed from the JSON data. But after thinking about this again, I am beginning to think that actually it's not only about the json, but also about what kind of data is in these array buffers.

Like when there is an .env files created with the changes here, using WebP, any application that still uses old BJS code without these changes will break reading it, as it will wrongly assume the array buffers contains png data, which they don't. And this will probably break in unexpected ways, basically with corrupted image data, instead of giving a meaningful error message.

So maybe we should bump that indeed to 2! What do you guys think?

Copy link
Member

Choose a reason for hiding this comment

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

Yup sounds like a good plan to me :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did that now (bump version to 2)!

Changed the types a bit, EnvironmentTextureInfo is now a discriminated union of the now separate v1 and v2 interfaces. So by checking the version, TS will narrow the type to the appropriate version.

Added a normalize method, which takes any version and always returns the latest, so we don't have to care about differences between these versions in other parts of the codebase. This also removed the need for this Required<> type, as we now always return a EnvironmentTextureInfoV2, which is guaranteed to have imageType set. /cc @deltakosh

Tested again locally, so I think this should be fine. But please re-review the latest changes! :)

src/Misc/tools.ts Show resolved Hide resolved
src/Misc/tools.ts Show resolved Hide resolved
Copy link
Contributor

@Popov72 Popov72 left a comment

Choose a reason for hiding this comment

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

LGTM!

if (info.version !== 1) {
Logger.Warn('Unsupported babylon environment map version "' + info.version + '"');
}
info = EnvironmentTextureTools.normalizeEnvInfo(info);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that this could be seen as some kind of breaking change, as an unsupported version will now throw, while previously it only issued a warning. But I believe this was wrong, as the schema could have been totally different. At least it was inconsistent with the other places where an error was already thrown...

@sebavan
Copy link
Member

sebavan commented Sep 17, 2021

Thanks a ton !!! I ll merge it right after the build !!!

@sebavan
Copy link
Member

sebavan commented Sep 17, 2021

@simonihmig were you able to validate we are not using values when alpha is high ? just to be sure :-)

@simonihmig
Copy link
Contributor Author

were you able to validate we are not using values when alpha is high ?

Not sure I understand what you mean here? What exactly (and how) should I validate?

@sebavan
Copy link
Member

sebavan commented Sep 17, 2021

Basically I am wondering if for compression sake they are premultiplying alpha with the rest of the channels so when we would have high intensity in some places, we could lost the color info. like white + alpha of 0.1 would result in webp of 0.1, 0.1, 0.1, 0.1

This happens in IE with pngs for instance so I bet it is not the case but it is more of a sanity check

@sebavan sebavan merged commit 283ab98 into BabylonJS:master Sep 17, 2021
@simonihmig simonihmig deleted the env-webp branch September 20, 2021 08:27
@simonihmig
Copy link
Contributor Author

I regularly contribute to other OSS projects for quite a while, but having a PR reviewed, re-reviewed and merged(!) by three maintainers within literally just a few hours, was a quite unique and enjoyable experience! Thanks folks! 😍

simonihmig added a commit to simonihmig/Babylon.js that referenced this pull request Sep 22, 2021
simonihmig added a commit to simonihmig/Babylon.js that referenced this pull request Sep 22, 2021
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.

4 participants