1. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  2. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  3. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  4. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  5. Unity 5.5 is now released.
    Dismiss Notice
  6. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice
  7. Unity 5.6 beta is now available for download.
    Dismiss Notice

Help with personal reaserch

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

  1. koucek

    koucek

    Joined:
    Apr 17, 2016
    Posts:
    9
    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:
    2,438
  3. koucek

    koucek

    Joined:
    Apr 17, 2016
    Posts:
    9
    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:
    9
    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:
    2,438
    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:
    9
    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