Search Unity

Is there a way to apply texture from the bottom of the mesh?

Discussion in 'Shaders' started by Zaine7673, Jun 3, 2020.

  1. Zaine7673

    Zaine7673

    Joined:
    Feb 15, 2018
    Posts:
    238
    I have a shader I've put together (I'm no way an expert on shaders but have written a few so know the basics at least) and this one blends two textures based on normals. Or at least that's what I tried to do and it works. I'm now trying to get it to blend the second texture but from the bottom of the mesh and it works also however, it doesn't just blend from the bottom it blends every normal that is facing downwards.

    Im guessing it's because of this line here

    Code (CSharp):
    1. WorldNormalVector(IN, normals)
    So on the image, you can see that the tree has the white snow not only at the bottom of the tree where I want it (circled in red) but also at the bottom of the branches (circled on green) and I don't want this.

    upload_2020-6-3_16-55-37.png

    Is it even possible to achieve what I want here or am I just approaching this the completely wrong way?

    For anyone who would like it as is, the shader is:

    Code (CSharp):
    1. Shader "Custom/ElementCover" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _MainNormal ("MainNormal", 2D) = "bump" {}
    6.         _Occlusion ("Ambient Occlusion", 2D) = "bump" {}
    7.         _OcclusionAmount ("Occlsuion Amount", Range(0, 1)) = 0.5
    8.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    9.         _Metallic ("Metallic", Range(0,1)) = 0.0
    10.  
    11.  
    12.         [Space(50)]
    13.        
    14.         [Header(Element info)]
    15.         _ElementTexture("Element texture", 2D) = "white" {}
    16.         _ElementNormal("Element normal", 2D) = "bump" {}
    17.         _ElementColor("Element color", color) = (1,1,1,1)
    18.         _ElementGlossiness("Element glossiness", Range(0, 1)) = 0.5
    19.         _ElementMetallic ("Element Metallic", Range(0,1)) = 0.0
    20.  
    21.         [Space(50)]
    22.  
    23.         [Header(Element Direction)]
    24.         _ElementLevel ("Element level", Range(-1, 1)) = 0
    25.         _ElementDirX("Element Direction X", Range(-1,1)) = 0
    26.         _ElementDirY("Element Direction Y", Range(-1,1)) = 0
    27.         _ElementDirZ("Element Direction Z", Range(-1,1)) = 0
    28.  
    29.         [Space(50)]
    30.  
    31.         [Toggle(EnableLayer)]
    32.         _EnableLayer("Enable Layer 2", float) = 0
    33.         _ElementLevel2("Element level", Range(-1, 1)) = 0
    34.         _ElementDirX2("Element Direction X", Range(-1,1)) = 0
    35.         _ElementDirY2("Element Direction Y", Range(-1,1)) = 0
    36.         _ElementDirZ2("Element Direction Z", Range(-1,1)) = 0
    37.  
    38.      
    39.     }
    40.     SubShader {
    41.         Tags { "RenderType"="Opaque" }
    42.         LOD 200
    43.        
    44.         CGPROGRAM
    45.         #pragma surface surf Standard fullforwardshadows
    46.         #pragma target 3.0
    47.  
    48.         #pragma shader_feature EnableLayer
    49.         struct Input {
    50.             float2 uv_MainTex;
    51.             float2 uv_MainNormal;
    52.             float2 uv_ElementNormal;
    53.             float2 uv_ElementTexture;
    54.             float3 worldNormal;
    55.             INTERNAL_DATA
    56.         };
    57.         sampler2D _MainTex;
    58.         half _Glossiness;
    59.         half _Metallic;
    60.         fixed4 _Color;
    61.         sampler2D _MainNormal;
    62.         sampler2D _ElementTexture;
    63.         sampler2D _ElementNormal;
    64.         fixed4 _ElementColor;
    65.         float4 _ElementDirection;
    66.         float _ElementDirX;
    67.         float _ElementDirY;
    68.         float _ElementDirZ;
    69.         float _ElementLevel;
    70.         float _ElementGlossiness;
    71.         float _ElementMetallic;
    72.         sampler2D _Occlusion;
    73.         float _OcclusionAmount;
    74.  
    75.         float _ElementLevel2;
    76.         float _ElementDirX2;
    77.         float _ElementDirY2;
    78.         float _ElementDirZ2;
    79.         float4 _ElementDirection2;
    80.  
    81.         struct appdata {
    82.             float4 vertex : POSITION;
    83.             float4 tangent : TANGENT;
    84.             float3 normal : NORMAL;
    85.             float2 texcoord : TEXCOORD0;
    86.             float2 texcoord1 : TEXCOORD1;
    87.             float2 texcoord2 : TEXCOORD2;
    88.         };
    89.  
    90.         UNITY_INSTANCING_BUFFER_START(Props)
    91.         UNITY_INSTANCING_BUFFER_END(Props)
    92.         void surf (Input IN, inout SurfaceOutputStandard o) {
    93.             //Color and normals of the main textures
    94.             fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    95.             float3 normals = UnpackNormal (tex2D(_MainNormal, IN.uv_MainNormal));
    96.             float3 occlusion = tex2D(_Occlusion, IN.uv_MainNormal);
    97.             //Color and normals of the Element textures
    98.             fixed4 ElementColor = tex2D(_ElementTexture, IN.uv_ElementTexture) * _ElementColor;
    99.             float3 ElementNormals = UnpackNormal(tex2D(_ElementNormal, IN.uv_ElementNormal));
    100.             //Element direction calculation
    101.             _ElementDirection = float4(_ElementDirX, _ElementDirY, _ElementDirZ, 0);
    102.             half ElementDot = step(_ElementLevel, dot(WorldNormalVector(IN, normals), normalize(_ElementDirection)));
    103.  
    104.             fixed3 texOcc = c.rgb * lerp(1, occlusion, _OcclusionAmount);
    105.  
    106.             #ifdef EnableLayer
    107.                 _ElementDirection2 = float4(_ElementDirX2, _ElementDirY2, _ElementDirZ2, 0);
    108.                 half ElementDot2 = step(_ElementLevel2, dot(WorldNormalVector(IN, normals), normalize(_ElementDirection2)));
    109.  
    110.                 o.Normal = lerp(normals, ElementNormals, ElementDot);
    111.                 o.Albedo = lerp(texOcc, ElementColor.rgb, ElementDot+ElementDot2);
    112.                 o.Metallic = lerp(_Metallic, _ElementMetallic, ElementDot+ElementDot2);
    113.                 o.Smoothness = lerp(_Glossiness, _ElementGlossiness, ElementDot+ElementDot2);
    114.                 o.Alpha = c.a;
    115.             #else
    116.                 o.Normal = lerp(normals, ElementNormals, ElementDot);
    117.                 o.Albedo = lerp(texOcc, ElementColor.rgb, ElementDot);
    118.                 o.Metallic = lerp(_Metallic, _ElementMetallic, ElementDot);
    119.                 o.Smoothness = lerp(_Glossiness, _ElementGlossiness, ElementDot);
    120.                 o.Alpha = c.a;
    121.             #endif      
    122.  
    123.         }
    124.         ENDCG
    125.     }
    126.     FallBack "Diffuse"
    127. }
     
    Last edited: Jun 3, 2020