2
\$\begingroup\$

How to make unlit node cast shadows? Is is possible with the current state of Shader Graph in LWRP?

In this great article Toon Shader OP grabs pass from VertexLit shader. Is something similar achievable with Shader Graph?

\$\endgroup\$
  • \$\begingroup\$ do you need a custom shadow or you haven't any shadows? what is your version of unity? \$\endgroup\$ – Seyed Morteza Kamali Aug 5 at 19:13
  • \$\begingroup\$ @SeyedMortezaKamali let's assume the latest stable version 2019.2.0. The question is about casting and receiving shadows while using an unlit node in shader graph. \$\endgroup\$ – Iggy Aug 6 at 13:14
  • 1
    \$\begingroup\$ you can make your custom lighting and add shadows by new shadergraph feature \$\endgroup\$ – Seyed Morteza Kamali Aug 6 at 16:45
  • \$\begingroup\$ Light mainLight = GetMainLight(shadowCoord); Direction = mainLight.direction; Color = mainLight.color; DistanceAtten = mainLight.distanceAttenuation; ShadowAtten = mainLight.shadowAttenuation; \$\endgroup\$ – Seyed Morteza Kamali Aug 6 at 16:59
  • \$\begingroup\$ @SeyedMortezaKamali that is a useful feature. Wouldn't it affect whole scene though? This feature might be required for individual objects, thus making all lighting custom wouldn't work in some cases. But yeah, it should cover most of them, as usually games are in the same style. But it might be limiting. \$\endgroup\$ – Candid Moon _Max_ Aug 6 at 17:16
1
+50
\$\begingroup\$

I believe It shouldn't be a node because we use it once.it should be a something like toggle.

if you like to make this try to use #pragma multi_compile because in Standard shader #pragma multi_compile_shadowcaster used.


ShadowCaster

by default unlit shader cast shadow , if you like to remove shadow caster in unlit shader just open your shader in shadergraph and copy the shader then create another shader and paste generated shader then just remove shadow caster pass

Name "ShadowCaster"
Tags{"LightMode" = "ShadowCaster"}

Shadow Reciever

by default unlit shader don't recieve shadow we'll need to transfer texture coordinates from the vertex shader to the fragment shader.

// As a new include, below the existing ones.
#include "AutoLight.cginc"

…

// Add to the v2f struct.
SHADOW_COORDS(2)

…

// Add to the vertex shader.
TRANSFER_SHADOW(o)

but I couldn't use it in LightWeightRenderPipeline I think because it obsoleted.

so I tried to see difference of the generated shader in PBR and unlit to to understand what ShaderGraph PBR used to recieve the shadow. and I noticed it use custom lighting model and use shadowcoord.

    struct GraphVertexInput
    {
    //...
    float4 shadowCoord  : TEXCOORD2;
    };


    struct GraphVertexOutput
    {
    //...
    float4 shadowCoord  : TEXCOORD2;
    };

    half3 vertexLight = VertexLighting(vertexInput.positionWS, lwWNormal);
#ifdef _MAIN_LIGHT_SHADOWS
    o.shadowCoord = GetShadowCoord(vertexInput);
#endif

then you should use this shadowCoord in Lighting so you should make a custom lighting model.but this solution is very hard because I have to overwrite built-in hlsl files.


In Unity 2019.2, we’re bringing even more features and functionality to Shader Graph.

you can make your Custom Lighting in Shader Graph

https://blogs.unity3d.com/2019/07/31/custom-lighting-in-shader-graph-expanding-your-graphs-in-2019/

you can use Custom Function File Mode to use your custom lighting function

void MainLight_half(float3 WorldPos, out half3 Direction, out half3 Color, out half DistanceAtten, out half ShadowAtten)
{
#if SHADERGRAPH_PREVIEW
   Direction = half3(0.5, 0.5, 0);
   Color = 1;
   DistanceAtten = 1;
   ShadowAtten = 1;
#else
#if SHADOWS_SCREEN
   half4 clipPos = TransformWorldToHClip(WorldPos);
   half4 shadowCoord = ComputeScreenPos(clipPos);
#else
   half4 shadowCoord = TransformWorldToShadowCoord(WorldPos);
#endif
   Light mainLight = GetMainLight(shadowCoord);
   Direction = mainLight.direction;
   Color = mainLight.color;
   DistanceAtten = mainLight.distanceAttenuation;
   ShadowAtten = mainLight.shadowAttenuation;

#endif
}

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.