Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Double sided lighting

Discussion in 'Shaders' started by UnityLighting, Sep 14, 2016.

  1. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    Hello

    I want to add double-sided lighting to unity transparent shader. I test some ways but doesn't work.
    I want to use transparent smoke as fake volumetric smoke that facing to camera.

    thanks



    here is Unity(4.7) Transparent-Diffuse shader code:

    Code (CSharp):
    1. Shader "Transparent/Cutout/Diffuse" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    10.     LOD 200
    11.    
    12. CGPROGRAM
    13. #pragma surface surf Lambert alphatest:_Cutoff
    14.  
    15. sampler2D _MainTex;
    16. fixed4 _Color;
    17.  
    18. struct Input {
    19.     float2 uv_MainTex;
    20. };
    21.  
    22. void surf (Input IN, inout SurfaceOutput o) {
    23.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    24.     o.Albedo = c.rgb;
    25.     o.Alpha = c.a;
    26. }
    27. ENDCG
    28. }
    29.  
    30. Fallback "Transparent/Cutout/VertexLit"
    31. }
    32.  
     
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,628
    Your best bet is to duplicate the geometry and flip the normals so you have "proper" double sided geometry. Then any "normal" shader will work.

    Unless of course there is a very specific reason why you want to avoid that (although I'd argue, the cases where you need that are very few).

    Other than that, as far as I remember, the way to have proper double sided shader, is to have it do two passes, one with cull back and the other with cull front.
     
    UnityLighting likes this.
  3. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    Code (CG):
    1. Shader "Transparent/Cutout/Diffuse" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6. }
    7. SubShader {
    8.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    9.     LOD 200
    10.     Cull Off  
    11.  
    12. CGPROGRAM
    13. #pragma surface surf Lambert alphatest:_Cutoff vertex:vert
    14. sampler2D _MainTex;
    15. fixed4 _Color;
    16. struct appdata {
    17.     float3 normal : NORMAL;
    18.     fixed vface : VFACE;
    19. };
    20. void vert (inout appdata v) {
    21.     v.normal *= vface > 0 ? 1 : -1;
    22. }
    23. struct Input {
    24.     float2 uv_MainTex;
    25. };
    26. void surf (Input IN, inout SurfaceOutput o) {
    27.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    28.     o.Albedo = c.rgb;
    29.     o.Alpha = c.a;
    30. }
    31. ENDCG
    32. }
    33. Fallback "Transparent/Cutout/VertexLit"
    34. }
     
    UnityLighting likes this.
  4. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866

    Shader error in 'Transparent/DiffuseALA': Surface shader's vertex input struct (appdata) needs to have a 'float4 vertex' member (on )
     
  5. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,628
    Wow, does that work? Last time I was researching this vface support wasn't fully implemented yet (although it was in the 4.x era).

    (about the error, add float4 vertex : POSITION; in the appdata struct, although you might still get more errors)
     
    UnityLighting likes this.
  6. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    I test it on 4.7 and 5.3.4
    Same error
     
  7. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    188
    To be perfectly honest, I've never used the VFACE semantic before in Unity, I prefer the 2 pass technique. From memory there were problems with compatibility in surface shaders, but I figured they would be fixed. Oh well, you can do this instead;

    Code (CG):
    1.     Shader "Transparent/Cutout/Diffuse" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6.     }
    7.     SubShader {
    8.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    9.         LOD 200
    10.  
    11.         Cull Back
    12.    
    13.     CGPROGRAM
    14.     #pragma surface surf Lambert alphatest:_Cutoff
    15.     sampler2D _MainTex;
    16.     fixed4 _Color;
    17.     struct Input {
    18.         float2 uv_MainTex;
    19.     };
    20.     void surf (Input IN, inout SurfaceOutput o) {
    21.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    22.         o.Albedo = c.rgb;
    23.         o.Alpha = c.a;
    24.     }
    25.     ENDCG
    26.  
    27.         Cull Front
    28.    
    29.     CGPROGRAM
    30.     #pragma surface surf Lambert alphatest:_Cutoff vertex:vert
    31.     sampler2D _MainTex;
    32.     fixed4 _Color;
    33.     struct Input {
    34.         float2 uv_MainTex;
    35.     };
    36.     void vert (inout appdata_base v) {
    37.         v.normal *= -1;
    38.     }
    39.     void surf (Input IN, inout SurfaceOutput o) {
    40.         fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    41.         o.Albedo = c.rgb;
    42.         o.Alpha = c.a;
    43.     }
    44.     ENDCG
    45.     }
    46.     Fallback "Transparent/Cutout/VertexLit"
    47.     }
    48.  
     
    UnityLighting likes this.
  8. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    thank's but doesn't work. only one side has lighting
     
  9. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    The issue is @AcidArrow and @Namey5 both thinking "double sided lighting" in the same way, which is not the way @aliyeredon2 is thinking of "double sided lighting".

    They're thinking about it as a single flat plane where both sides are lit as if they are opaque surface normal facing away from the surface, which normally a two sided shader will light both sides with the normal facing a single direction so the interiors of an object will look wrong.

    @aliyeredon2 - I think you're talking about double sided translucent lighting, like light scattering you see through plant leaves or thin curtains, which is "double sided" in that it takes into account lighting from both the the surface direction and light coming from behind it.

    Something like this: http://forum.unity3d.com/threads/translucent-translucency-shader.150788/
     
  10. UnityLighting

    UnityLighting

    Joined:
    Mar 31, 2015
    Posts:
    3,866
    Golden answer
     
  11. Gooren

    Gooren

    Joined:
    Nov 20, 2015
    Posts:
    331
    Sorry for necromancy, but I believe I need something a little bit different.

    I have light that is falling on octahedron from outside but I need to capture the exact amount of that light with a second camera from the inside, without any other objects getting in the way.

    I can't get the shader right. Translucency makes sense, except that is for transparent objects, right?
    My object is not transparent, I just want to get the light and shadows that are falling on it from the outside. Nothing else. Because I'm then analyzing a render texture pixel by pixel, only looking for incoming light color and intensity.

    First thing I tried was using a 2 sided shader and inverting the normals. My best result so far can be seen in the first attached image.
    The image "shows me" looking towards one direct light from within the octahedron. It looks weird. I want the nice, smooth, spread out light just like I get from the outside (which can be seen in the second image where I look on the octahedron from the outside). Shame on me for knowing nothing about rendering :(

    Plz halp! @AcidArrow
     

    Attached Files:

    Last edited: Jan 14, 2022