Search Unity

Custom ShadowMap Generation for point lights

Discussion in 'Shaders' started by qornflex, Sep 15, 2015.

  1. qornflex

    qornflex

    Joined:
    Dec 6, 2012
    Posts:
    36
    Hi guys,

    While waiting for Lighting/Shadowing integration in Graphics.DrawProcedural method, I'm trying to generate point light shadowmaps by myself.

    To do that, I render a depth cubemap of the scene at light position.
    I give this cubemap to my shader to be able to sample in it and proceed to a depth comparaison.
    It seems to work but I have strange result due to bad LightMatrix calculation.

    Then I looked at AutoLight.cginc, to see how Unity handles shadowmaps for point lights.

    I have a question about the TRANSFER_SHADOW() method:

    a._ShadowCoord = mul (unity_World2Shadow[0], mul(_Object2World,v.vertex));

    How unity_World2Shadow[0] is calculated?

    Thanks a lot

    Q.
     
  2. Zuntatos

    Zuntatos

    Joined:
    Nov 18, 2012
    Posts:
    586
    unity_World2Shadow is for directional lights iirc

    my depth shader is this;

    Code (CSharp):
    1.             float4 My_LightPositionRange;
    2.          
    3.             struct v2f {
    4.                 float4 pos : SV_POSITION;
    5.                 float3 vec : TEXCOORD0;
    6.             };
    7.          
    8.             struct appdata {
    9.                 float4 vertex : POSITION;
    10.             };
    11.  
    12.             v2f vert(appdata v) {
    13.                 v2f o;
    14.                 o.vec = mul(_Object2World, v.vertex).xyz - My_LightPositionRange.xyz;
    15.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    16.                 return o;
    17.             }
    18.  
    19.             float4 frag(v2f i) : SV_Target {
    20.                 return length(i.vec) * My_LightPositionRange.w;
    21.             }
    where My_LightPositionRange has xyz as world pos, w as 1/range

    UnityCG defines the transfer_shadow macro's for SHADOWS_CUBE:

    Code (CSharp):
    1. #ifdef SHADOWS_CUBE
    2.     // Rendering into point light (cubemap) shadows
    3.     #define V2F_SHADOW_CASTER_NOPOS float3 vec : TEXCOORD0;
    4.     #define TRANSFER_SHADOW_CASTER_NOPOS_LEGACY(o,opos) o.vec = mul(_Object2World, v.vertex).xyz - _LightPositionRange.xyz; opos = mul(UNITY_MATRIX_MVP, v.vertex);
    5.     #define TRANSFER_SHADOW_CASTER_NOPOS(o,opos) o.vec = mul(_Object2World, v.vertex).xyz - _LightPositionRange.xyz; opos = mul(UNITY_MATRIX_MVP, v.vertex);
    6.     #define SHADOW_CASTER_FRAGMENT(i) return UnityEncodeCubeShadowDepth (length(i.vec) * _LightPositionRange.w);
    7. #else
    which is basically all the depth shader is.

    more at http://forum.unity3d.com/threads/casting-shadows-from-your-custom-lights-shader.354352/ & links there
     
    Last edited: Sep 15, 2015
    qornflex likes this.
  3. qornflex

    qornflex

    Joined:
    Dec 6, 2012
    Posts:
    36
    S***!
    Indeed I made a mistake by looking at Spot light :(

    // ---- Spot light shadows
    #if defined (SHADOWS_DEPTH) && defined (SPOT)
    #define SHADOW_COORDS(idx1) unityShadowCoord4 _ShadowCoord : TEXCOORD##idx1;
    #define TRANSFER_SHADOW(a) a._ShadowCoord = mul (unity_World2Shadow[0], mul(_Object2World,v.vertex));
    #define SHADOW_ATTENUATION(a) UnitySampleShadowmap(a._ShadowCoord)
    #endif

    Sorry for that and Thank you!
     
  4. cyberjoys

    cyberjoys

    Joined:
    Dec 1, 2020
    Posts:
    27
    did you guys experence shadow flickering when moving that custom point light very fast in editor? did you guys resolve this issue?
    i think the flickering is caused by the RenderToCubeMap is not fast enough to feed into the shadow sampling process, while the point light pos is a bit faster got fed into the sampling shadow process. and once you move the point light slow, you dont see this kind of like catching up flickering happening.
    one way to go is to crank up the bias, but once your move speed is again fast enough, this catching up flickering of the shadow will still show up
     
unityunity