Search Unity

Shader to combine texture with diffuse

Discussion in 'Shaders' started by bobby1212, Jan 18, 2017.

  1. bobby1212

    bobby1212

    Joined:
    Jan 18, 2017
    Posts:
    2
    Hey,
    can someone tell me how i manage to combine a diffuse and a texture shader?
    I try to create something like this:
    Rendersmall.png
    This rendering is created in blender by mixing a texture and a diffuse shader.
    The grid is a texture the rest makes the diffuse shader.

    How I can create this look in Unity?

    Thank you in advance!
     

    Attached Files:

  2. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    I suggest to just map the grid on top of the mesh (Kind of like box mapping but with just mapping from the top).
    Here is a simple shader implementation:

    Code (CSharp):
    1. //
    2. // Simple box mapping shader that projects one texture on the surface
    3. // The direction of the projection is always from top
    4. //
    5. Shader "Supyrb/Mapping/Vertical Mapping"
    6. {
    7.     Properties
    8.     {
    9.         _Color("Color", Color) = (1,1,1,1)
    10.         _MainTex("Albedo", 2D) = "white" {}
    11.     }
    12.     SubShader
    13.     {
    14.         Tags{ "RenderType" = "Opaque" }
    15.  
    16.         CGPROGRAM
    17.  
    18.         #pragma surface surf Lambert
    19.         #include "UnityCG.cginc"
    20.  
    21.         half4 _Color;
    22.         sampler2D _MainTex;
    23.         half4 _MainTex_ST;
    24.  
    25.         struct Input
    26.         {
    27.             float3 worldPos;
    28.         };
    29.  
    30.         void surf(Input IN, inout SurfaceOutput o)
    31.         {
    32.  
    33.             // Get texture coordinates.
    34.             float2 texCoordinate = (IN.worldPos.xz + _MainTex_ST.zw) * _MainTex_ST.xy;
    35.  
    36.             // Apply scale and offset
    37.             texCoordinate = texCoordinate * _MainTex_ST.xy + _MainTex_ST.zw;
    38.  
    39.  
    40.             fixed4 color = tex2D(_MainTex, texCoordinate) * _Color;
    41.  
    42.             o.Albedo = color.rgb;
    43.             o.Alpha = color.a;
    44.         }
    45.         ENDCG
    46.     }
    47.     FallBack "Diffuse"
    48. }
     
  3. bobby1212

    bobby1212

    Joined:
    Jan 18, 2017
    Posts:
    2
    Thank you for your help!
    but now I have the problem, that the color overrides the yellow color of the grid.
    Is there any possibility to lay the grid with his natural color over the diffuse without changing the color of the grid?
     
  4. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    826
    Well, if you just want a blue yellow thingy and nothing else you can just put everything in the texture and ignore the color property in the shader. Otherwise I think a nice solution would be to use the alpha channel of the grid texture to lerp between the underlying color and the grid color.