Shadows on Batch Mesh + Instanced Geometry

So I implemented BatchMesh. Worked great. The Shader that is attached to the BatchMesh is blocking the shadows on the objects using that material. I tried making some tweaks but no luck. What’s the best way to get Shadows on this Shader?

I’m using a ( THREE.PointLight( 0xffffff, 4 ) )


renderer.shadowMap.enabled = true; 
renderer.shadowMap.type = THREE.PCFSoftShadowMap;


const m2 = new THREE.MeshStandardMaterial({
  // const m2 = new THREE.ShadowMaterial({
  // material.opacity = 0.5,
  // depthPacking: THREE.RGBADepthPacking,
  // alphaTest: 0.5,
  // side: THREE.FrontSide, 
  color: 0x797979,
  wireframe: false,
  roughness: 0.5,
  metalness: 0.7,
 m2.onBeforeCompile = ( shader, shader1 ) => {

   shader.uniforms.instanceMatrices = m2.userData.instanceMatrices;                   
   shader.vertexShader = `
   uniform mat4[ ${ gs.length } ] instanceMatrices;
   attribute float meshIdx;
   `#include <uv_vertex>`,
   `mat4 instanceMatrix = instanceMatrices[ int( floor( meshIdx + 1.0 ))];               
   #include <uv_vertex>`
   `#include <default      normal_vertex>`,
   `#include <fog_vertex>`,
   `vec3 transformedNormal = objectNormal;
    mat3 m2 = mat3( instanceMatrix );
    transformedNormal /= vec3( dot( m2[ 0 ], m2[ 0 ] ), dot( m2[ 1 ], m2[ 1 ] ), dot( m2[ 2 ], m2[ 2 ] ) );
    transformedNormal = m2 * transformedNormal;
    transformedNormal = normalMatrix * transformedNormal;
    `#include <project_vertex>;
     #include <fog_vertex>`,
    `vec4 mvPosition = vec4( transformed, 1.0 );
    mvPosition = instanceMatrix * mvPosition;
    mvPosition = modelViewMatrix * mvPosition;
    gl_Position = projectionMatrix * mvPosition;


Thanks in Advance!


Add .customDepthMaterial and/or .customDistanceMaterial, depends on the type of light source(s), copying the same changes in vertex shaders like you did in MeshStandardMaterial.
A simple example is here: Mesh points to the camera on only 2 axis with shaders - #10 by prisoner849

So not possible with the addition of a Fragment Shader? I rigged one in and was able to get some shadows out of the how the geometries mat receives light itself but its not offset in the direction of the light source ( which is always the same as the camera in this case ). I’ll try your idea out now on the new material now.

So I was able to get it working but my model allows a user to increase the number of instances to thousands… shadows on thousands of objects was not a good idea. In fact, using even StandardMaterials costs a lot. Even though my entire model is 1 draw call the performance difference between the model with no shadows and only BasicMaterials is incredible.

Thank you for the help though. I’m getting a lot better with Shaders now and thats what’s important.

1 Like

I was able to get this to be somewhat performant by continuing to massage the text geometry. Turning bevel off and reducing the initial geometry to a completely flat geometry with minimal curves and a basic material allowed me to get quite a lot of text on the model at 60FPS. Also merging all the text to a single set of mat4s’ and not including anything extra. Im still going to continue to play with the vector text approach to see how far I can go with it but this has been an eye opener.