I’m trying to generate a depth map for a particle system, but if I render the particle system using MeshDepthMaterial, then every particle is only rendered as a single point for each vertex–not covering the entire area the texture mapped particle is displayed.
Do I need to use MeshDepthMaterial to generate a depth map, or are there other options?
Right now there is no way to get the MeshDepthMaterial to respect the size or texture of the ParticleSystem. However, it is not too hard to implement a custom ShaderMaterial that does that. First, you need a vertex shader and fragment shader.
The vertex shader is totally standard, the fragment shader takes the texture (
sampler2D map), but instead of using it for color values it just uses the alpha leveltexColor.a. For the rgb, a grayscale value based on the depth is used, just like the MeshDepthMaterial. Now to use this shader you just need to grab the html and create a THREE.ShaderMaterial like so:Here you have provided the shader with all the info it needs: the camera’s near/far range, the size of the particle and the texture it needs to map.
You can see a jsFiddle demo of it here.