WebGLRenderer: Auto shader output definitions for MRT#27808
WebGLRenderer: Auto shader output definitions for MRT#27808vanruesc wants to merge 29 commits intomrdoob:devfrom
Conversation
📦 Bundle sizeFull ESM build, minified and gzipped.
🌳 Bundle size after tree-shakingMinimal build including a renderer, camera, empty scene, and dependencies.
|
@vanruesc This issue has been fixed, you need to pull the code again. Solution: ... All checks have passed ... |
I've fixed this now by including the render target in the material properties to detect if the current render target differs from the one that was previously associated with the material. The I've adjusted |
|
Any thoughts on this PR? I still think it's worth merging since it makes working with MRT in WebGLRenderer more practical. The only issues that appeared due to this change were preexisting configuration problems:
If there are concerns about breakage, I'd be fine with changing the default output defnition If this gets merged, I'd like to contribute improvements for the current scene background rendering system to support clear values for specific MRT attachments which would internally rely on the output macros. |
Description
This PR aims to make Multiple Render Targets (MRT) easier to use by implementing automatic shader output definitions for built-in materials and
ShaderMaterial.Context
For MRT workflows it's up to the user to define what data should be written to the respective textures. The MRT example promotes the use of
RawShaderMaterialto achieve this. However, MRT is most useful for rendering out additional pixel data during the main render pass. In most situations the main scene will consist of meshes that use built-in materials likeMeshStandardMaterial, but those materials are currently difficult to use with MRT.Improving compatibility
It's possible to use
onBeforeCompileon every object in a scene to modify built-in materials, but this has some downsides:onBeforeCompilehook, so care must be taken to preserve it.onBeforeCompilehooks multiple times.onBeforeCompilehooks.If it wasn't necessary to define shader outputs, we could avoid using
onBeforeCompilealtogether and modify theShaderChunklibrary instead (example). But at the moment there's no way aroundonBeforeCompile.Meeting halfway
With the changes in this PR, three can define shader outputs automatically based on the current render target. To avoid breakage, this is only done for built-in materials and for
ShaderMaterialif the shader doesn't already define shader outputs. This implementation also uses the names of the MRT textures to define macros of the formout_${name}for convenience which allows for conditional MRT code like the following:I've considered using the texture names as-is but opted for a prefix approach to reduce the risk of name conflicts.
Open issue
This PR is in draft mode because the material/program doesn't get updated after switching the render target. I've tried modifying WebGLPrograms.getProgramCacheKey to account for the render target, but that didn't work out. Setting material.needsUpdate to true also didn't trigger a program update.