Search Unity

Question LightMode: ForwardAdd makes material invinsible in prefab preview

Discussion in 'Shaders' started by Isomania, Nov 9, 2021.

  1. Isomania

    Isomania

    Joined:
    Jan 28, 2021
    Posts:
    16
    As shown in this image:
    The prefab is visible in the project folder and in the game view however it is transparrent in "prefab view". Changing the ForwardAdd tag to ForwardBase only kinda fixes this issue as shown here:
    However then my shader breaks in game. How can i make the shader visible in "prefab view" and work in game? The light is directional and uses a lightcookie, do plan on supporting multiple lights (pointlights, etc).

    Shader:
    1. Shader "Custom/Stone Shader"
    2. {
    3. Properties
    4. {
    5. _MainTex ("Texture", 2D) = "white" {}
    6. _Color ("Color", Color) = (1, 1, 1, 1)
    7. _ShadowSoftness("_ShadowSoftness", Range(0,1)) = 0.5
    8. _DarkestValue("Darkest Value", Range(0, 1)) = 0.0
    9. }
    10. SubShader
    11. {
    12. Pass
    13. {
    14. Tags
    15. {
    16. "RenderType" = "Opaque"
    17. "Queue"="Geometry+2"
    18. "LightMode" = "ForwardAdd"
    19. "PassFlags" = "OnlyDirectional"
    20. }
    21. CGPROGRAM
    22. #pragma target 3.0
    23. #pragma vertex vert
    24. #pragma fragment frag
    25. #pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight
    26. #include "/Assets/Graphics/CGincFiles/NormalShading.cginc"
    27. float4 _Color;
    28. fixed4 frag(v2f i) : SV_Target
    29. {
    30. fixed light = CalculateLight(i);
    31. fixed4 light_color = _LightColor0.rgba * light;
    32. return fixed4(_Color.rgb * light_color.rgb, 1);
    33. }
    34. ENDCG
    35. }
    36. UsePass "Legacy Shaders/VertexLit/SHADOWCASTER"
    37. }
    38. }
    NormalShading.cginc:
    1. #include "UnityCG.cginc"
    2. #include "AutoLight.cginc"
    3. #include "Lighting.cginc"
    4. sampler2D _LightTexture0;
    5. float4x4 unity_WorldToLight;
    6. float _ShadowSoftness;
    7. float _DarkestValue;
    8. float _DayNightTime;
    9. struct v2f
    10. {
    11. float2 uv : TEXCOORD0;
    12. SHADOW_COORDS(1)
    13. fixed3 diff : COLOR0;
    14. fixed3 ambient : COLOR1;
    15. float4 pos : SV_POSITION;
    16. float3 worldPos : TEXCOORD2;
    17. };
    18. v2f vert(appdata_base v)
    19. {
    20. v2f o;
    21. o.pos = UnityObjectToClipPos(v.vertex);
    22. o.worldPos = mul (unity_ObjectToWorld, v.vertex);
    23. o.uv = v.texcoord;
    24. half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    25. half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    26. o.diff = nl * _LightColor0.rgb;
    27. o.ambient = ShadeSH9(half4(worldNormal,1));
    28. TRANSFER_SHADOW(o)
    29. return o;
    30. }
    31. fixed CalculateLight(v2f i)
    32. {
    33. float shadow = SHADOW_ATTENUATION(i);
    34. shadow = saturate(shadow + _ShadowSoftness * _DayNightTime);
    35. fixed2 uvCookie = mul(unity_WorldToLight, float4(i.worldPos, 1)).xy;
    36. fixed attenuation = tex2D(_LightTexture0, uvCookie).w;
    37. fixed3 lighting = i.diff * shadow * attenuation + i.ambient;
    38. return max(saturate(lighting.x), _DarkestValue * _DayNightTime);
    39. }
     
    suntabu likes this.
  2. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    77
    same issue in URP
     
  3. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,550
    Include both passes. Make a ForwardBase and ForwardAdd in your shader.
    The pragma for ForwardAdd should be
    multi_compile_fwdadd
    and for ForwardBase
    multi_compile_fwdbase 
    .

    You want both passes in your shader because they are both used in the forward rendering process. Base is used for the basic lighting and Add is for every additional light in the scene. The default Prefab preview window does not have Additional lights, so ForwardAdd isn't being used in that context.

    Writing shaders for URP is different, you can't use the old syntax that the example in this thread is using. It only needs a pass named
    "ForwardLit"
    and in the tags
    "LightMode" = "UniversalForward"
    .
    But also the pass needs to be written with URP references and syntax in mind. You can see an example here:
    https://github.com/phi-lira/Univers...Scenes/51_LitPhysicallyBased/CustomLit.shader
    and
    https://github.com/ColinLeung-NiloC.../master/SimpleURPToonLitOutlineExample.shader
     
    Last edited: Sep 15, 2022
  4. suntabu

    suntabu

    Joined:
    Dec 21, 2013
    Posts:
    77
    Thanks for replying, I've fixed this issue by changing the default behaviour mode from 3D to 2D,
    after deleting the library folder, all prefab's preview images are right.