Search Unity

Shader color is odd

Discussion in 'Shaders' started by pedroberni, May 15, 2018.

  1. pedroberni

    pedroberni

    Joined:
    Oct 26, 2017
    Posts:
    8
    Hello, I got the shader called "Simple Glass" to work for me in objects with some requirements:

    -Allow transparency change
    -Render both faces

    But the color the object presents is a bit odd, although it is set to white. I think there's a simple solution to it, but I can't find it. Below I show an object with this color and the shader code:



    Code (CSharp):
    1. Shader "Simple Glass" {
    2.     Properties {
    3.         _Color ("Main Color", Color) = (1,1,1,1)
    4.         _MainTex ("Base (RGB)", 2D) = "white" { }
    5.     }
    6.  
    7.     SubShader {
    8.         // We use the material in many passes by defining them in the subshader.
    9.         // Anything defined here becomes default values for all contained passes.
    10.         Material {
    11.             Diffuse [_Color]
    12.             Ambient [_Color]
    13.         }
    14.         Lighting On
    15.  
    16.         // Set up alpha blending
    17.         Blend SrcAlpha OneMinusSrcAlpha
    18.  
    19.         // Render the back facing parts of the object.
    20.         // If the object is convex, these will always be further away
    21.         // than the front-faces.
    22.         Pass {
    23.             Cull Front
    24.             SetTexture [_MainTex] {
    25.                 Combine Primary *Texture
    26.             }
    27.         }
    28.         // Render the parts of the object facing us.
    29.         // If the object is convex, these will be closer than the
    30.         // back-faces.
    31.         Pass {
    32.             Cull Back
    33.             SetTexture [_MainTex] {
    34.                 Combine Primary * Texture
    35.             }
    36.         }
    37.     }
    38. }
    Since the color pattern varies acording to the light direction I think the problem is related to backfaces blending issues, but I'm not sure.
     
  2. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    Try this one

    Code (CSharp):
    1.     Shader "Simple Glass" {
    2.         Properties {
    3.             _Color ("Main Color", Color) = (1,1,1,1)
    4.             _MainTex ("Base (RGB)", 2D) = "white" {}
    5.             _Alpha ("Alpha", Range(0,1)) = 1
    6.         }
    7.    
    8.         SubShader {
    9.             // We use the material in many passes by defining them in the subshader.
    10.             // Anything defined here becomes default values for all contained passes.
    11.             Material {
    12.                 Diffuse [_Color]
    13.                 Ambient [_Color]
    14.             }
    15.             Lighting On
    16.    
    17.             // Set up alpha blending
    18.             Blend SrcAlpha OneMinusSrcAlpha
    19.    
    20.             // Render the back facing parts of the object.
    21.             // If the object is convex, these will always be further away
    22.             // than the front-faces.
    23.             Pass {
    24.                 Cull Off
    25.                 SetTexture [_MainTex] {
    26.                     constantColor(1,1,1,[_Alpha])
    27.                     Combine  Texture*constant
    28.                 }
    29.             }
    30.         }
    31.     }
    32.  
     
  3. pedroberni

    pedroberni

    Joined:
    Oct 26, 2017
    Posts:
    8
    Hello, sorry for the late reply. Thanks for your answer.
    I've tried something similiar it already and also yours, but then the object is just pure white with no lightning or shadows :\
     
  4. Seyed_Morteza_Kamaly

    Seyed_Morteza_Kamaly

    Joined:
    Nov 18, 2015
    Posts:
    80
    sorry but I'm not familiar with your shader style
    I use this style and works correctly
    I hope this is what you want

    without color mask:



    Code (CSharp):
    1. Shader "Example/Diffuse Texture" {
    2.     Properties {
    3.       _Color("Color",Color) = (1,1,1,1)
    4.       _MainTex ("Texture", 2D) = "white" {}
    5.       _Alpha("Alpha",Range(0,1)) = 0.5
    6.     }
    7.     SubShader {
    8.       Tags { "RenderType" = "Transparent" }
    9.       Cull Off //Render Both Sides
    10.  
    11.       CGPROGRAM
    12.       #pragma surface surf Lambert alpha:fade
    13.  
    14.  
    15.       float4 _Color;
    16.    
    17.       struct Input {
    18.           float2 uv_MainTex;
    19.       };
    20.       float _Alpha;
    21.       sampler2D _MainTex;
    22.       void surf (Input IN, inout SurfaceOutput o) {
    23.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb*_Color;
    24.           o.Alpha = _Alpha;
    25.       }
    26.       ENDCG
    27.     }
    28.     Fallback "Diffuse"
    29.   }
    with color mask:




    Code (CSharp):
    1. Shader "Example/Diffuse Texture" {
    2.     Properties {
    3.       _Color("Color",Color) = (1,1,1,1)
    4.       _MainTex ("Texture", 2D) = "white" {}
    5.       _Alpha("Alpha",Range(0,1)) = 0.5
    6.     }
    7.     SubShader {
    8.       Tags { "RenderType" = "Transparent" }
    9.       Cull Off //Render Both Sides
    10.       pass{
    11.           ColorMask 0
    12.       }
    13.       CGPROGRAM
    14.       #pragma surface surf Lambert alpha:fade
    15.  
    16.  
    17.       float4 _Color;
    18.    
    19.       struct Input {
    20.           float2 uv_MainTex;
    21.       };
    22.       float _Alpha;
    23.       sampler2D _MainTex;
    24.       void surf (Input IN, inout SurfaceOutput o) {
    25.           o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb*_Color;
    26.           o.Alpha = _Alpha;
    27.       }
    28.       ENDCG
    29.     }
    30.     Fallback "Diffuse"
    31.   }
     
    Last edited: May 25, 2018