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
Have you read through this? (All 5 parts.) http://www.alanzucconi.com/2015/06/10/a-gentle-introduction-to-shaders-in-unity3d/
Thanks. Looks good so i am starting with reading Edit: This should be packed with unity instalation... thanks
Ok so after some time i have this: Code (csharp): Shader "Custom/GEN_GEAR" { Properties { _Primary ("Primary Color:", Color) = (1,1,1) _Secondary ("Secondary Color:", Color) = (1,1,1) _Range ("Colour Range:", Range(0.1,1)) = 0.5 [NoScaleOffset] _Texture ("Grayscale Map:", 2D) = "white" {} [NoScaleOffset] _Normal ("Normal Map:",2D) = "bump" {} _Glossiness ("Smoothness:", Range(0,1)) = 0.0 _Metallic ("Metallic:", Range(0,1)) = 0.0 } SubShader { Tags { "RenderType"="Opaque" } Cull Off LOD 200 CGPROGRAM #pragma surface surf Standard vertex:vert nolightmap noshadow noambient novertexlights nodynlightmap nodirlightmap #pragma target 2.0 sampler2D _Texture; sampler2D _Normal; struct Input { float2 uv_Texture; float2 uv_Normal; half4 vcolor; }; float4 _Primary; float4 _Secondary; half _Range; half _Glossiness; half _Metallic; void vert(inout appdata_full v, out Input o) { UNITY_INITIALIZE_OUTPUT(Input,o); o.vcolor = v.color.r; } void surf (Input IN, inout SurfaceOutputStandard o) { 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; 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; 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; fixed4 c = (x * _Primary) + (y * _Secondary) - z; o.Albedo = c.rgb; o.Normal = UnpackNormal (tex2D(_Normal, IN.uv_Normal)); o.Metallic = _Metallic; o.Smoothness = _Glossiness; } ENDCG } } 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.
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; }
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