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

Help with personal reaserch

Discussion in 'Shaders' started by koucek, Mar 16, 2017.

  1. koucek

    koucek

    Joined:
    Apr 17, 2016
    Posts:
    10
    Hi. I have readed a lot of tutorial and still dont getting this shader stuff. I need a simple shader wich will take my grayscale texture with normal map and tint it with primary and secondary colour in some ratio(from 0 to 1) and apply smooth and metalic effect. It looks easy.... well with great tutorial. Have someone great tutorial source about shaders? Doesnt matter if it will be video or text. Thanks
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
  3. koucek

    koucek

    Joined:
    Apr 17, 2016
    Posts:
    10
    Thanks. Looks good so i am starting with reading :)
    Edit: This should be packed with unity instalation... thanks
     
    Last edited: Mar 16, 2017
  4. koucek

    koucek

    Joined:
    Apr 17, 2016
    Posts:
    10
    Ok so after some time i have this:
    Code (csharp):
    1.  
    2. Shader "Custom/GEN_GEAR" {
    3.     Properties {
    4.         _Primary ("Primary Color:", Color) = (1,1,1)
    5.         _Secondary ("Secondary Color:", Color) = (1,1,1)
    6.         _Range ("Colour Range:", Range(0.1,1)) = 0.5
    7.         [NoScaleOffset] _Texture ("Grayscale Map:", 2D) = "white" {}
    8.         [NoScaleOffset] _Normal ("Normal Map:",2D) = "bump" {}
    9.         _Glossiness ("Smoothness:", Range(0,1)) = 0.0
    10.         _Metallic ("Metallic:", Range(0,1)) = 0.0
    11.     }
    12.     SubShader {
    13.         Tags { "RenderType"="Opaque" }
    14.         Cull Off
    15.         LOD 200
    16.        
    17.         CGPROGRAM
    18.         #pragma surface surf Standard vertex:vert nolightmap noshadow noambient novertexlights nodynlightmap nodirlightmap
    19.         #pragma target 2.0
    20.  
    21.         sampler2D _Texture;
    22.         sampler2D _Normal;
    23.  
    24.         struct Input {
    25.             float2 uv_Texture;
    26.             float2 uv_Normal;
    27.             half4 vcolor;
    28.         };
    29.  
    30.         float4 _Primary;
    31.         float4 _Secondary;
    32.         half _Range;
    33.         half _Glossiness;
    34.         half _Metallic;
    35.  
    36.  
    37.         void vert(inout appdata_full v, out Input o)
    38.         {
    39.             UNITY_INITIALIZE_OUTPUT(Input,o);
    40.             o.vcolor = v.color.r;
    41.         }
    42.  
    43.         void surf (Input IN, inout SurfaceOutputStandard o) {
    44.             fixed4 x = (tex2D (_Texture, IN.uv_Texture).r * 0.21 + tex2D (_Texture, IN.uv_Texture).g * 0.72 + tex2D (_Texture, IN.uv_Texture).b * 0.07) / _Range;
    45.             fixed4 y = 1 - (tex2D (_Texture, IN.uv_Texture).r * 0.21 + tex2D (_Texture, IN.uv_Texture).g * 0.72 + tex2D (_Texture, IN.uv_Texture).b * 0.07) / _Range;
    46.             fixed4 z = tex2D(_Texture, IN.uv_Texture).r *0.21 + tex2D (_Texture, IN.uv_Texture).g*0.72 + tex2D (_Texture, IN.uv_Texture).b* 0.07;
    47.  
    48.             fixed4 c = (x * _Primary) + (y * _Secondary) - z;
    49.  
    50.             o.Albedo = c.rgb;
    51.             o.Normal = UnpackNormal (tex2D(_Normal, IN.uv_Normal));
    52.             o.Metallic = _Metallic;
    53.             o.Smoothness = _Glossiness;
    54.         }
    55.         ENDCG
    56.     }
    57. }
    58.  
    I want to know if it is mobile friendly shader, without unusefull stuff. Is it enought to say bye bye to alpha just by rewrite colour properties to 3? Its better to use averge than luminity? And finaly how to handle extremes of function without exponencional function. Thanks for suggestions.
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,329
    Don't call tex2D multiple times on the same texture like that, there's a cost to each time you read from a texture, and you're doing it 9 times for something you only need to do once. You're also doing the same luminance calculation over and over. Shader compilers might be smart enough to optimize this for you, but the below should be identical.

    fixed4 tex = tex2D(_Texture, IN.uv_Texture);
    fixed lum = dot(tex.rgb, fixed3(0.21, 0.72, 0.07));
    fixed3 c = lerp(_Seconday, _Primary, lum / _Range) - lum;


    You also don't need a custom vert function to get the vertex color, though you don't use it, just use:

    struct Input {
    fixed4 color : COLOR0;
    }
     
  6. koucek

    koucek

    Joined:
    Apr 17, 2016
    Posts:
    10
    Yeah i noticed texture calls and lerp too, but i had bad function in it. Big thanks for help, now i even know what i am doing :D