Search Unity

Double sided Shader with VFACE flipping issues

Discussion in 'Shaders' started by blaze237, Sep 7, 2017.

  1. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    Im attempting to make a two sided PBR shader.

    Ive got both sides showing using the VFACE flipping teqniue described here (https://forum.unity3d.com/threads/s...y-shiny-on-the-underside.393068/#post-2574717)

    However, the issue i'm having is that the albedo of the shader is now only appearing on the underside. It doesnt matter if i just use a colour tint or apply texture, its not visible from the top.

    upload_2017-9-7_13-36-57.png
    From above

    upload_2017-9-7_13-37-26.png
    Same plane from below.

    Any help would be greatly appreciated.

    My shader code:

    Code (CSharp):
    1. Shader "Custom/test" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _NormalMap ("bump",2D) = "bump"{}
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.         Cull Off
    13.      
    14.         CGPROGRAM
    15.         // Physically based Standard lighting model, and enable shadows on all light types
    16.         #pragma surface surf Standard fullforwardshadows
    17.  
    18.         // Use shader model 3.0 target, to get nicer looking lighting
    19.         #pragma target 3.0
    20.  
    21.         sampler2D _MainTex;
    22.  
    23.         struct Input {
    24.             float2 uv_MainTex;
    25.                 fixed facing : VFACE;
    26.         };
    27.  
    28.         half _Glossiness;
    29.         half _Metallic;
    30.         fixed4 _Color;
    31.         sampler2D _NormalMap;  
    32.  
    33.         void surf (Input IN, inout SurfaceOutputStandard o) {
    34.             // Albedo comes from a texture tinted by color
    35.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    36.             o.Albedo = c.rgb;
    37.             // Metallic and smoothness come from slider variables
    38.             o.Metallic = _Metallic;
    39.             o.Normal = UnpackNormal(tex2D(_NormalMap, IN.uv_MainTex));
    40.  
    41.             o.Smoothness = _Glossiness;
    42.             o.Alpha = c.a;
    43.          
    44.          
    45.             if (IN.facing < 0.5)
    46.                 o.Normal *= -1.0;
    47.         }
    48.         ENDCG
    49.     }
    50.     FallBack "Diffuse"
    51. }


    Without the line

    Code (CSharp):
    1.    if (IN.facing < 0.5)
    2.                 o.Normal *= -1.0;
    The opposite occurs (Texture/tint only shows on the top side)
     
    Last edited: Sep 7, 2017
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    If you set the metallic & smoothness values to zero, what do you see?
     
  3. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    Setting metalic to zero does allow some of the colour to show through, but its far less intense.

    Top side of plane with red tint applied

    upload_2017-9-7_21-56-20.png

    Bottom side:
    upload_2017-9-7_21-57-12.png

    The metallic has the biggest effect, turning down the smoothness isnt really an option though as i'm wanting this for a water shader so need the reflections.
     
    Last edited: Sep 7, 2017
  4. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    For reference this is it with both smoothness and metalic at zero:

    upload_2017-9-7_21-59-35.png upload_2017-9-7_21-59-1.png
     
  5. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    Interestingly getting rid of the VFACE flip logic doesn't seem change anything except cause the intenser side to now be the top of the mesh instead of bottom.
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    It's acting like VFACE isn't working at all. Are you on OSX, or is the editor running DX9? It should work fine for both of these, but there might be an issue with OSX & Metal?
     
  7. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    No windows 10 on DX11 :/
     
  8. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    If you do

    o.Albedo = 0;
    o.Emission = IN.facing;


    what do you get? For me this makes the top of the mesh white and the bottom black. If this isn't what you get I have no idea.
     
  9. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    No that just gives me black on both sides :/.

    Very strange, i assume that means the shader thinks both sides are the bottom for some reason
     
  10. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    Ah very interesting, i've been using unity 5 but just tried it in the latest version of unity and its working as it should. Kind of annoying as id wanted to keep support for it in my asset but there you go.

    Mind you my refraction code for my shader is now broken in the newest editor lol but at least VFACE is working haha.


    Well i say broken, its just super weak for some reason meaning ive had to up the distortion by a factor of 10
     
    Last edited: Sep 8, 2017
  11. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    Update:

    Fixed my refraction issues.

    Thanks for your help, if it hadnt been your suggestion of the black and white plane i would have probably just assumed the problem was me being an idiot and never tried a different editor version.

    Now i have the fun job of trying all the different editor versions since 5.0 to see what the minimum i can provide support is. Lucky me lol
     
  12. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,339
    I used VFACE in multiple revisions of 5.3, 5.4, and 5.6 with out issue.
     
  13. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    Ah right ok well thats a good starting point for me at least.

    Could well just be an issue on my end with my instal of 5.0 tbf
     
  14. blaze237

    blaze237

    Joined:
    May 31, 2014
    Posts:
    56
    Ok. So for anyone coming across this same issue in the future, turns out VFACE input for surface shaders was only implemented as of unity 5.1 [https://unity3d.com/unity/whats-new/unity-5.1].

    So if you're having problems on a version prior to that you just need to update your editor.