Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How would i do this sort of shadowing in CG/HLSL???

Discussion in 'Shaders' started by alexander11, Nov 1, 2016.

  1. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Hello i am creating a glass shader and i need some help with some shadowing, i want something like this(in Pic1), where the objects with the Glass Shader don't cast there own shadows upon others with the Glass shader only to others "without" the glass shader at a controllable opacity. Would anyone know how to do this??

    -Pic1
    Capture108.png
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    This isn't really a CG/HLSL specific question, this is a general Unity rendering question. And the short answer is "it's not possible".

    Specifically the glass shader cannot receive shadows because Unity doesn't support shadow receiving on transparent objects. Now you can kind of do shadow casting from transparencies, but you have to use the standard shader or write a custom shader for the glass using the same shadow casting technique as the standard shader. That shadow isn't actually transparent, but rather stippled, so that plus soft shadowing can get an approximation of variable transparency shadowing.

    If you want more control over the shadow opacity you would have to write your own custom shadow map rendering system, which would require a lot more than just a custom shader or two, especially since proper transparent shadow support is some of the most complex techniques in shadow mapping. You might be able to fake it if you can use something like a dynamic projector system, but that still will require quite a bit of programming beyond shaders.

    For receiving shadows on transparent objects you would need a custom shadow map system, or would need to copy the shadows map from a light, recalculate the shadow matrices, and write a custom glass shader that uses them.

    So, that's a longer way of saying "it's possible, but requires a lot more deep knowledge of rendering systems than you currently have judging from your initial question."
     
  3. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    @bgolus Thanks. One more question, on a standard or any "custom" shader how do i turn off/on receive shadows and cast shadows??
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    If you don't have a shadow caster pass in your shader, or a Fallback that has a shadow caster pass, your shader won't cast shadows. It also won't show in show up in the camera depth texture or in the main directional light's shadow on non-mobile systems in forward rendering (since desktop / console renders the main directional light's shadows onto the depth), but that's not really the best way to handle it. For surface shaders there's a couple of optional parameters for disabling all shadow receiving, like noshadow, and for custom vert/frag shaders you can just leave out the shadow code or use some of the undocumented #pragma multi_compile_fwdadd optional directives, or rigorously remove shadow related variants with #pragma skip_variants, etc, etc.

    Really it's easier and more consistent to just use the shadow casting settings on the renderer component.
     
  5. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Thanks @bgolus . Would you know where there are a few tutorials on this??
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
  7. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Thanks @bgolus how would i use the noshadow command in CG the one you were talking about??
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
  9. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Thanks.
     
  10. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    @bgolus I am having a syntex error i put "noshadow" in CG like this
    Code (CSharp):
    1. ................
    2. Tags { "RenderType"="Opaque" }
    3.         LOD 200
    4.         noshadow
    5.         CGPROGRAM
    6. ...............
    7.  
    But it does not work, how would i fix this???
     
  11. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
  12. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Ok thanks
     
  13. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Hey @bgolus "noshadow" Disables receiving shadows, but where is the "Cast Shadows" command since i cant find it in the Manual you sent me?? is there even one, what would i have to do in order to enable/disable casting shadows??
     
  14. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    The Cast Shadows option isn't part of shaders, it's part of the renderer component.
    https://docs.unity3d.com/Manual/class-MeshRenderer.html

    Otherwise just don't include the Fallback line. Unity needs a shadowcaster pass to cast shadows, and most shaders don't have one, but the point to another shader that does in the Fallback line.