Search Unity

Render part of the mesh with shadow only

Discussion in 'Shaders' started by VictorKs, Mar 30, 2019.

  1. VictorKs

    VictorKs

    Joined:
    Jun 2, 2013
    Posts:
    242
    I have weird gaps in my shadows at the seams of the mesh so I decided to add a shadow-only cube to fix this but doing so I get double shadow casters and double gameobjects so is it possible to add the cube in the mesh and give the shadow-only cube a different UV.z so to make the shader work normaly on the rest of the mesh and shadow-only in the specified part? If so how should I go about it? I guess this will mitigate the processing cost from CPU to GPU.

    Edit: Well I managed to fix my problem by setting normal bias to 0 and increasing bias to 0.4 but still is the above solution doable?
     
    Last edited: Mar 30, 2019
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    This will happen with any mesh that uses hard edged normals. The solution is to use smooth normals, and reproduce the hard edges with a normal map. This obviously isn’t a perfect solution since there are a lot of cases where having a full mesh normal map isn’t feasible.

    However having two meshes, one for the visible mesh and one for the shadows, is a viable solution. The shadows mesh should be smoothed, and set to shadow only. The visible mesh should have cast shadows disabled. And you’re done! If the objects are static, the extra game objects won’t be a significant issue. And really unless this is something you’re doing on every object and you have a lot of objects, it likely won’t be an issue.

    The alternative would be to bake smoothed normals into the vertex color or some extra UVs and use a custom shader that uses those smoothed normals in the shader caster’s bias.
     
    Csf91 likes this.