Trying to better understand (auto?) depth write in shaders?

There’s something I’m not understanding about shader depth writing.

I have separate ShaderMaterial and corresponding depth ShaderMaterial, the latter I then apply using depthRenderer.getDepthMap().setMaterialForRendering(myMesh, myCustomDepthShaderMaterial) which works as I intend, but looking at other built-in materials like for instance the GridMaterial code, I don’t understand how they are writing to the depth map. It seems to happen automagically? It makes me wonder if the approach I’ve taken with separate depth material applied using setMaterialForRendering() is unnecessary and overly complicated.

Can anyone provide some insights into this?

More context - I’m trying to get @roland’s greased line to write depth values, but I’m assuming because the vertex shader changes the vertex positions I definitely will need to use something like the approach outlined above?

Exactly :slight_smile:

The shadow generator uses a specific shader (vertex and fragment) and renders the mesh with that shader to generate the depth in the depth buffer.

If your mesh uses a special shader, you can also use the ShadowDepthWrapper class to wrap that shader and use it to render the depth (see the doc on this topic).

ShadowDepthWrapper should be what you need.


Thanks @Evgeni_Popov. I’ll try that out!

Sorry for the delayed follow-up @Evgeni_Popov but I’m having a bit of trouble getting ShadowDepthWrapper to work with a slightly modified version of @roland’s GreasedLine. Here’s my PG and what I’m trying to do is get the greased line to write to the depth map:

The ShadowDepthWrapper is used by the shadow generator to generate the shadow depth map, the depth renderer is a separate component which is not involved in shadow generation.

You will have to write your own depth material and use depthRenderer.getDepthMap().setMaterialForRendering(mesh, mat) to associate your mesh to this material for depth rendering.

Ah OK @Evgeni_Popov , sorry I interpreted your first reply from Dec 22 as being able to use ShadowDepthWrapper to write to the DepthRenderer.DepthMap - I didn’t know there was a difference between depth map for shadows and other purposes?.

I’ve started trying to migrate the custom shader material code to a material plugin, which I will later extend to write depth, but for now I can’t even get the plugin to work. No console errors - the shader compiles fine (no errors), but nothing displays.

The material plugin is instantiated around line 50 and defined at line 100.

Any ideas what I’m doing wrong here?

The UniformBuffer.updateFloat2 takes two floats as parameters, not a Vector2.

Also, worldViewProjection is automatically bound for you by the ShaderMaterial class as long as you add it to the uniform array that you pass to the constructor, but there is no such facility in a material plugin, so you have to construct the matrix and bind it yourself:

Thanks @Evgeni_Popov, that got me a step closer. I managed to get it going:

However one of my goals in migrating from ShaderMaterial to MaterialPluginBase was that I’d made the assumption that I could then use greased line emissiveColor with GlowLayer but it seems GlowLayer is not yet compatible with material plugins :slightly_frowning_face:

Got a solution I’m reasonably happy with. Greased line that writes to depth map, works with fog and no code replication between color and depth shaders. Now if only I could get it going with glow layer :slight_smile:

The effect layer supports setMaterialForRendering, so you can provide a specific material to be used when rendering in the glow layer:

Note that customEmissiveColorSelector is not used in that case but I still kept it because of the debug plane, that would have been full glowing white without it!


Thanks so much @Evgeni_Popov! Thanks for sticking with me on this long thread.