Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. How can we improve our URP documentation to cover your needs? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Mobile Diffuse with Color

Discussion in 'Shaders' started by denis_bogdanov, Jun 16, 2015.

  1. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    122
    Hello!
    Please tell me, how to change the default shader mobile so that you can change the color of the texture

    A great big thank you!Sorry for my English:)

    Code (CSharp):
    1. // Simplified Diffuse shader. Differences from regular Diffuse one:
    2. // - no Main Color
    3. // - fully supports only 1 directional light. Other lights can affect it, but it will be per-vertex/SH.
    4.  
    5. Shader "Mobile/Diffuse" {
    6. Properties {
    7.     _MainTex ("Base (RGB)", 2D) = "white" {}
    8. }
    9. SubShader {
    10.     Tags { "RenderType"="Opaque" }
    11.     LOD 150
    12.  
    13. CGPROGRAM
    14. #pragma surface surf Lambert noforwardadd
    15.  
    16. sampler2D _MainTex;
    17.  
    18. struct Input {
    19.     float2 uv_MainTex;
    20. };
    21.  
    22. void surf (Input IN, inout SurfaceOutput o) {
    23.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    24.     o.Albedo = c.rgb;
    25.     o.Alpha = c.a;
    26. }
    27. ENDCG
    28. }
    29.  
    30. Fallback "Mobile/VertexLit"
    31. }
     
  2. alienheretic

    alienheretic

    Joined:
    Oct 15, 2008
    Posts:
    59
    Shader "Mobile/Diffuse"
    {
    Properties
    {
    _Color("Color",COLOR)=(0.5,0.5,0.5,1.0)
    _MainTex ("Base (RGB)", 2D) = "white" {}
    }

    SubShader
    {
    Tags { "RenderType"="Opaque" }
    LOD 150
    CGPROGRAM
    #pragma surface surf Lambert noforwardadd

    sampler2D _MainTex;

    struct Input
    {
    float2 uv_MainTex;
    };

    void surf (Input IN, inout SurfaceOutput o)
    {

    fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    o.Albedo = c.rgb;
    o.Alpha = c.a;
    }
    ENDCG​
    }
    Fallback "Mobile/VertexLit"
    }
     
  3. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    122
    Thanks,but Iwrotean error here: fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    I kind of got it changed, but I don't know my way to affecton performance

    Code (CSharp):
    1. Shader "Mobile/Diffuse Color" {
    2. Properties {
    3.     _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     _Color("Color",Color)=(0.5,0.5,0.5,1.0)
    5. }
    6. SubShader {
    7.     Tags { "RenderType"="Opaque" }
    8.  
    9.     LOD 150
    10. CGPROGRAM
    11. #pragma surface surf Lambert noforwardadd finalcolor:myColor
    12. sampler2D _MainTex;
    13. struct Input {
    14.     float2 uv_MainTex;
    15.  
    16. };
    17.  
    18.       fixed4 _Color;
    19.       void myColor (Input IN, SurfaceOutput o, inout fixed4 color)
    20.       {
    21.           color *= _Color;
    22.       }
    23. void surf (Input IN, inout SurfaceOutput o) {
    24.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
    25.     o.Albedo = c.rgb;
    26.     o.Alpha = c.a;
    27. }
    28. ENDCG
    29. }
    30. Fallback "Mobile/VertexLit"
    31. }
     
  4. alienheretic

    alienheretic

    Joined:
    Oct 15, 2008
    Posts:
    59
    correct way is here copying and pasting previous had errors this works i tested it

    Code (CSharp):
    1. Shader "Mobile/DiffuseX"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color",COLOR)=(0.5,0.5,0.5,1.0)
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}
    7.     }
    8.  
    9.     SubShader
    10.     {
    11.         Tags { "RenderType"="Opaque" }
    12.         LOD 150
    13.         CGPROGRAM
    14.         #pragma surface surf Lambert noforwardadd
    15.  
    16.         sampler2D _MainTex;
    17.         fixed4 _Color;
    18.  
    19.         struct Input
    20.         {
    21.             float2 uv_MainTex;
    22.         };
    23.  
    24.         void surf (Input IN, inout SurfaceOutput o)
    25.         {
    26.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    27.             o.Albedo = c.rgb;
    28.             o.Alpha = c.a;
    29.         }
    30.         ENDCG
    31.     }
    32.     Fallback "Mobile/VertexLit"
    33. }
     
  5. denis_bogdanov

    denis_bogdanov

    Joined:
    Apr 20, 2015
    Posts:
    122

    Thanks, works perfectly)

    Thank you for your time
     
  6. Garrom

    Garrom

    Joined:
    Oct 26, 2016
    Posts:
    45
    UNITY 2017 NOTE(Important) :
    This will crash your Unity3D editor and GPU driver. To avoid crash remove line #28 "o.Alpha = c.a;" because vertexLit cannot work with alpha in color.

    Can be hardware specific. Must not occur on all graphics cards.
    Hope this help :)
     
  7. VLukianenko

    VLukianenko

    Joined:
    Mar 27, 2017
    Posts:
    30
    Surface shader, however, produces darker shadows than mobile/VertexLit - so the result is not identical

    I dug a little bit into old fixed-function shaders, and I believe this would produce the exact same result as Mobile-vertex-lit, but with a single color instead of texture:

    Code (CSharp):
    1. Shader "Custom/VertexLit - Single color" {
    2. Properties {
    3.     _Color ("Color", color) = (1,1,1,1)
    4. }
    5.  
    6. SubShader {
    7.     Tags { "RenderType"="Opaque" }
    8.     LOD 80
    9.  
    10.     // Non-lightmapped
    11.     Pass {
    12.         Tags { "LightMode" = "Vertex" }
    13.  
    14.         Material {
    15.             Diffuse [_Color]
    16.             Ambient [_Color]
    17.         }
    18.         Lighting On
    19.         SetTexture [_MainTex] {
    20.             constantColor [_Color]
    21.             Combine primary DOUBLE, constant // UNITY_OPAQUE_ALPHA_FFP
    22.         }
    23.     }
    24.  
    25.     // Lightmapped, encoded as dLDR
    26.     Pass {
    27.         Tags { "LightMode" = "VertexLM" }
    28.  
    29.         BindChannels {
    30.             Bind "Vertex", vertex
    31.             Bind "normal", normal
    32.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    33.         }
    34.  
    35.         SetTexture [unity_Lightmap] {
    36.             matrix [unity_LightmapMatrix]
    37.             combine texture
    38.         }
    39.         SetTexture [_MainTex] {
    40.             constantColor [_Color]
    41.             combine previous, constant // UNITY_OPAQUE_ALPHA_FFP
    42.         }
    43.     }
    44.  
    45.     // Lightmapped, encoded as RGBM
    46.     Pass {
    47.         Tags { "LightMode" = "VertexLMRGBM" }
    48.  
    49.         BindChannels {
    50.             Bind "Vertex", vertex
    51.             Bind "normal", normal
    52.             Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    53.             Bind "texcoord", texcoord1 // main uses 1st uv
    54.         }
    55.  
    56.         SetTexture [unity_Lightmap] {
    57.             matrix [unity_LightmapMatrix]
    58.             combine texture * texture alpha DOUBLE
    59.         }
    60.         SetTexture [_MainTex] {
    61.             constantColor [_Color]
    62.             combine previous QUAD, constant // UNITY_OPAQUE_ALPHA_FFP
    63.         }
    64.     }
    65.  
    66.     // Pass to render object as a shadow caster
    67.     Pass
    68.     {
    69.         Name "ShadowCaster"
    70.         Tags { "LightMode" = "ShadowCaster" }
    71.  
    72.         ZWrite On ZTest LEqual Cull Off
    73.  
    74.         CGPROGRAM
    75.         #pragma vertex vert
    76.         #pragma fragment frag
    77.         #pragma target 2.0
    78.         #pragma multi_compile_shadowcaster
    79.         #include "UnityCG.cginc"
    80.  
    81.         struct v2f {
    82.             V2F_SHADOW_CASTER;
    83.             UNITY_VERTEX_OUTPUT_STEREO
    84.         };
    85.  
    86.         v2f vert( appdata_base v )
    87.         {
    88.             v2f o;
    89.             UNITY_SETUP_INSTANCE_ID(v);
    90.             UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
    91.             TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
    92.             return o;
    93.         }
    94.  
    95.         float4 frag( v2f i ) : SV_Target
    96.         {
    97.             SHADOW_CASTER_FRAGMENT(i)
    98.         }
    99.         ENDCG
    100.     }
    101. }
    102. }
    103.  
     
    Last edited: Jun 28, 2018
  8. The0Warrior

    The0Warrior

    Joined:
    Nov 19, 2017
    Posts:
    9

    Thank you soo soo much, that's exactly what I needed
     
    VLukianenko likes this.
unityunity