Search Unity

Help with Copying Shadow map and putting it on a custom shader.

Discussion in 'Shaders' started by alexander11, Mar 10, 2018.

  1. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    Hi guys.

    I need some help on this tutorial since its for an outdated Unity(Unity 5.1.1. And I am currently using Unity 2017.3.1f1), I was wondering if someone could help me out with the errors that I am receiving.

    What I am trying to do is this in the picture. Copying the shadowmap and placing it on the noise generated volumetric fog.


    I have been getting two errors one for the shadowmap and the other for the image effect script.

    Error 1 : ("Shader error in 'Custom/PostProcess': undeclared identifier 'samplerMyShadowMap' at line 121 (on d3d11)")

    Error 2 : ("
    The image effect Camera (UnityStandardAssets.ImageEffects.MyPostEffect) has been disabled as it's not supported on the current platform.
    UnityEngine."
    ) I have no idea why its doing this to the image effects script.


    You can download the package(with all the files) which the tutorial has linked below. or just click here to the download page.





     
  2. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    So I made it kind of work but not to the specifications that I want like in the picture above.
    So what I did to get rid of error one is replacing "sampler2D MyShadowMap;" to "UNITY_DECLARE_SHADOWMAP(MyShadowMap);", and I don't remember how I got rid of the second error(I know that is not really helpful, that's why I will add a unity package below). I was getting an Error in the ShadowCopySetup.cs which I forgot to mention, but that was a simple fix by adding the following code to the script "using UnityEngine.Rendering;".

    This is what it looks like. Still not what I want since the shadows don't seem visible on the fog like in the picture above. Does anyone have any idea to make look like the one above?

    Captureh3.PNG

    [*UPDATED PROJECT FILES*] Here are the project files https://www.dropbox.com/s/m53huky9cjey80s/PostProcessingShadowC.rar?dl=0
     
    Last edited: Mar 12, 2018
  3. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    704
    I looked at the project files you've just attached, and I can't find the post process where you sample the shadow map, only the "ShadowCopySetup.cs". Did you forget to add it in the archive ?
     
  4. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
  5. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    704
    I did some tests and i confirm your shadowed fog is colored : here's the screenshot with some "extreme" light color and shadow color :
    Screen.png

    The fog viewed when disabling blending with the color :
    Screen2.png

    Looks like the fog is blended as additive, that's probably why a darkened fog displays as ... invisible.

    Your blending function is: float4 result = colourSample * fogSample.a + fogSample;
    fogSample.a is the total transmitance and seems to be always 1.
    Haven't taken time to search if the transmittance calculation is correct.
     
  6. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    @Remy_Unity From what I understand is that if you turn "float4 result = colourSample * fogSample.a + fogSample;"
    to "float4 result = fogSample.a + fogSample;" to disable the color blending and with that I get a result like this.
    Captureh4.PNG

    And in order for me to get this I have to change my settings which makes the scene look dark, is this due to the DownscaleDepth.shader being rendered?
    *Here are my Settings Configuration for MyPostEffect.cs*
    Captureh5.PNG
     
  7. Remy_Unity

    Remy_Unity

    Unity Technologies

    Joined:
    Oct 3, 2017
    Posts:
    704
    So, I played a bit with the code : Having dark shadowed fog beeing almos invisble is normal, because it's how it is ment to be in the tutorial : the fog scaters light, so if there is no light (shadowed color), it just does nothing.

    To be able to have, let's say, black fog in the shadowed area, you must change how the light is accumulated, and how it is blended.

    I was able to do something like this by changing a few lines of code :
    In CalculateLightshafts.shader:
    Code (CSharp):
    1.  
    2. //accumulate light
    3. result += fColour;
    4.  
    5. //raymarch towards the camera
    6. currentPos += rayDir * stepSize;  
    7. }
    8.  
    9. result *= NUM_SAMPLES_RCP;
    In ApplyLightShafts.shader:
    Code (CSharp):
    1. float4 result = lerp( fogSample, colourSample, fogSample.a);
    And got something like this:
    Screen3.png
     
    alexander11 likes this.
  8. alexander11

    alexander11

    Joined:
    Aug 11, 2014
    Posts:
    94
    That makes sense, thanks for the help @Remy_Unity.

    in the future I'll ask for your help.