Multiple environment maps for groups of meshes?

Hello,

ive recently looked into the possibility of using multiple environment maps for different meshes or groups of meshes in a scene.

After some initial digging ive found there was a similiar question a while ago: Multiple environment maps (.env)

While it seems to be possible to assign a unique reflection texture to different materials, would it also be possible to do that on a mesh basis? Ideally id love to be able to define groups of meshes using a different environment/reflection texture.

Looking at the documentation there doesn’t seem to be a intuitive way to do this, my first thought would be to duplicate the materials based on their group (predefined collection of meshes) and assign a different reflection texture. However this quickly leads to overhead, since changes to the initial (source) material are not being reflected to the duplicated materials - this seems messy.

Ive added a simple playground which illustrates the shortcomings of this exact approach (cloning materials, changes not being propagated) and my expected functionality (at the bottom of the code, theres a boolean to toggle the expected example usage id like to have - which obviously doesn’t work as expected).

Playground Example

Any ideas on how to approach this problem in a clean and robust way?

I’m not all too sure about the ‘robust’ way :grin: but I can tell you how I am doing it.
First, I create clones of my env (texture) with a new source for the texture.
Then, I assign either env to my materials. Simple enough, to answer your question, and with the exception of IBL that works at scene level, env can only be defined at material level, not with mesh.

Now, this is my trick, for assigning the env depending on mesh (not on material) I use the mesh.id.
I assign a different id (or part of id as an identifier for the env) i.e. mesh.id = “1_col_env1” (where 1 is the mesh number identifier, ‘col’ means it’s a colliding mesh, ‘env1’ means it uses environment #1).
Then I can look for all meshes.id that includes “env1”, do anything with their material including changing to a different material, env/reflection texture.

Edit: As for your question/logic in the PG saying (quote)

        // ideally we want to use the same material and change its cubemap on a mesh basis,
        // so we set all meshes to the same material1 first
        // then we change the cubemaps, ideally this should be on a mesh basis

I don’t think there’s currently a way. I sort of remember discussions around this but as far as I remember it seemed pretty difficult to achieve. Of course, I could have missed something so best would be to wait for the expert answer (once he returns from his holidays :beach_umbrella:) @sebavan

2 Likes
    // ideally we want to use the same material and change its cubemap on a mesh basis,
    // so we set all meshes to the same material1 first
    // then we change the cubemaps, ideally this should be on a mesh basis

You should not do that.

Instead, you should create as many materials as necessary, so that each mesh gets the right environment map through its material.

1 Like

@mawa @Evgeni_Popov Thanks for the replies and suggestions!

I assumed as much, was just wondering if there was a way of doing this without cloning and using ‘references’ to materials. I’ll probably proceed to implement it like both of you suggested.

I don’t think there is. I believe evgeni would have known about it. I remembered some discussion for something similar so I wanted to ask but I’m pretty sure now this is not on the list as of today. But the cloning of materials and textures works fairly well. I didn’t notice an enormeous impact on performance using a few dozens of these (with 3 different env) that’s what I have currently in my scene.