Search Unity

Work on SpriteRender but doesn't work on UGUI

Discussion in 'Shaders' started by mankenDeveloper, Mar 30, 2017.

  1. mankenDeveloper

    mankenDeveloper

    Joined:
    Dec 19, 2016
    Posts:
    10
    Code (CSharp):
    1. Shader "Custom/MaskedTexture"
    2. {
    3.     Properties
    4.     {
    5.         _MainColor("Main Color", Color) = (1,1,1,1)
    6.         _MainTex("Base (RGB)", 2D) = "white" {}
    7.     _BackTexture("Back Texture", 2D) = "white" {}
    8.     _Mask("Mask To Dissolve", 2D) = "white" {}
    9.     _LineTexture("Line Texture", 2D) = "white" {}
    10.     _Range("Range", Range(0,3)) = 0
    11.         _LineSize("LineSize", Float) = 0.001
    12.         _Color("Line Color", Color) = (1,1,1,1)
    13.         _BumpMap("Normalmap", 2D) = "bump" {}
    14.     }
    15.  
    16.         SubShader
    17.     {
    18.         LOD 300
    19.         ZWrite On
    20.         Cull Off
    21.  
    22.         CGPROGRAM
    23. #pragma target 2.0
    24. #include "UnityCG.cginc"
    25. #pragma surface surf Lambert
    26.  
    27.         sampler2D _MainTex;
    28.     sampler2D _LineTexture;
    29.     sampler2D _BumpMap;
    30.     sampler2D _Mask;
    31.     sampler2D _BackTexture;
    32.     half4 _Color;
    33.     half4 _MainColor;
    34.     float _Range;
    35.     float _LineSize;
    36.  
    37.     struct Input
    38.     {
    39.         float2 uv_MainTex;
    40.         float2 uv_BumpMap;
    41.         float2 uv_Detail;
    42.     };
    43.  
    44.     void surf(Input IN, inout SurfaceOutput o)
    45.     {
    46.         half4 c = tex2D(_MainTex, IN.uv_MainTex);
    47.         half4 m = tex2D(_Mask, IN.uv_MainTex);
    48.         half4 lc = tex2D(_Mask, IN.uv_MainTex - _LineSize);
    49.         half4 lc2 = tex2D(_Mask, IN.uv_MainTex + _LineSize);
    50.         half4 lc3 = tex2D(_LineTexture, IN.uv_MainTex + _SinTime) * _Color;
    51.         half4 bc = tex2D(_BackTexture, IN.uv_MainTex);
    52.         o.Albedo = c *  _MainColor;
    53.         o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    54.  
    55.         float factor = m.rgb.x + m.rgb.y + m.rgb.z;
    56.         if (factor >= _Range)
    57.         {
    58.             float factor2 = lc.rgb.x + lc.rgb.y + lc.rgb.z;
    59.             float factor3 = lc2.rgb.x + lc2.rgb.y + lc2.rgb.z;
    60.             if (factor2 < _Range || factor3 < _Range)
    61.             {
    62.                 o.Albedo = lc3;
    63.             }
    64.             else
    65.             {
    66.                 o.Albedo = bc;
    67.                 o.Normal = float3(1,1,1);
    68.             }
    69.         }
    70.     }
    71.     ENDCG
    72.     }
    73.         //Fallback "UI/Default"
    74.         //Fallback "Sprites/Default"
    75.         Fallback "Diffuse"
    76. }

    i find a reference that work as i wish, it mask with b&w texture;
    it work100% on spriterenderer, and 50% on RawImage,
    it show complete black screen with rawimage, but i can see that in the scene,

    can someone help me plz? ty