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

WebGLRenderer: Add support for rotating env maps. #27758

Merged
merged 11 commits into from
Feb 20, 2024
Merged

Conversation

Mugen87
Copy link
Collaborator

@Mugen87 Mugen87 commented Feb 17, 2024

Fixed #16328.

Description

This PR adds a frequent requested feature to WebGLRenderer: The ability to rotate environment maps.

Instead of adding a new property to Texture level, I've realized a more clean approach by adding:

The new properties are of type Euler. Environment maps in the equirectangular, cube map and cuveUV (PMREM) format can be rotated now.

Copy link

github-actions bot commented Feb 17, 2024

📦 Bundle size

Full ESM build, minified and gzipped.

Filesize dev Filesize PR Diff
676.4 kB (168.2 kB) 678 kB (168.6 kB) +1.65 kB

🌳 Bundle size after tree-shaking

Minimal build including a renderer, camera, empty scene, and dependencies.

Filesize dev Filesize PR Diff
457.2 kB (111 kB) 458.4 kB (111.3 kB) +1.2 kB

@Mugen87 Mugen87 marked this pull request as ready for review February 17, 2024 10:01
@Mugen87 Mugen87 added this to the r162 milestone Feb 18, 2024
@WestLangley
Copy link
Collaborator

In what coordinate frame is the background Euler rotation applied? I assume you intend it to be the three.js right-hand coordinate frame. In the shader, the coordinate frame is left-handed.

In this PR, the background rotation around the x-axis is backwards from the expected convention. A sign flip may fix that.

@WestLangley
Copy link
Collaborator

Do you intend to add support for rotating scene.environment?

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Feb 19, 2024

I assume you intend it to be the three.js right-hand coordinate frame.

Yes. I was a bit unsure about the point of reference when judging the direction of rotations for env maps. I hope the last commit fixes the x-axis issue.

Do you intend to add support for rotating scene.environment?

Users can already achieve that by modifying the new envMapRotation property of their PBR materials. This makes it also consistent with envMapIntensity which is a per-material property only as well.

I would only add an additional option if users struggle with the existing solution.

@mrdoob
Copy link
Owner

mrdoob commented Feb 20, 2024

Hmmm... I think scene.environmentRotation is more likely to be used than material.envMapRotation.

@mrdoob
Copy link
Owner

mrdoob commented Feb 20, 2024

I think it would be better to add support for scene.environmentRotation first, and after that we can see if people really need material.envMapRotation.

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Feb 20, 2024

The problem with this approach is that Scene.environment only applies to MeshStandardMaterial. Hence, basic, lambert and phong materials which do support environment maps can't align their reflections to the rotated background.

How about we add Scene.environmentRotation additionally?

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Feb 20, 2024

I also think it's in general more flexible to have the ability to rotate environment maps directly in the material (if we don't provide a per texture setting).

@mrdoob
Copy link
Owner

mrdoob commented Feb 20, 2024

How about we add Scene.environmentRotation additionally? When set, it overshadows MeshStandardMateraial.envMapRotation like with envMap.

That sounds good to me.

I guess scene.environmentRotation would be new Euler() by default so the renderer will actually have to check if scene.environment === null to then use material.envMap and material.envMapRotation, right?

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Feb 20, 2024

Yes, I think we can nicely put this check in getProgram() and save the result in the material properties.

@Mugen87
Copy link
Collaborator Author

Mugen87 commented Feb 20, 2024

Okay, Scene.environmentRotation has been added. It only affects physical materials and only if Scene.environment is used.

@Mugen87 Mugen87 merged commit ed15131 into mrdoob:dev Feb 20, 2024
12 checks passed
@WestLangley
Copy link
Collaborator

The reflections and rotations are not correct. I'll file a PR in an attempted fix.

Unfortunately, getting these calculations correct has taken considerable effort...

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.

CubeTexture orientation
3 participants