Hey everyone.
I’ve been blocked with this for 2 days (I’m kinda new to shaders).
I want to apply a texture, something like this:
And depending on MaxHeight and MinHeight values, which are coordinates from the world, apply the texture correctly to the middle pixels (by correcly I mean that the effect of gradient should be seen complete, from red to purple).
This is what I’ve got so far:
Shader "Custom/Gradiente"
{
Properties
{
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_MinHeight("Min Height", Float) = 0.4
_MaxHeight("Max Height", Float) = 1
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf Standard fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
struct Input
{
float2 uv_MainTex;
float3 worldPos;
};
sampler2D _MainTex;
half _Glossiness;
half _Metallic;
half _MaxHeight;
half _MinHeight;
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
// #pragma instancing_options assumeuniformscaling
UNITY_INSTANCING_BUFFER_START(Props)
// put more per-instance properties here
UNITY_INSTANCING_BUFFER_END(Props)
void surf (Input IN, inout SurfaceOutputStandard o)
{
if(IN.worldPos.y < _MinHeight){
fixed4 c = tex2D (_MainTex, half2(0,0));
o.Albedo = c.rgb;
o.Alpha = c.a;
}
else if(IN.worldPos.y > _MaxHeight ){
fixed4 c = tex2D (_MainTex, half2(1,1));
o.Albedo = c.rgb;
o.Alpha = c.a;
}
else{
fixed4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
}
o.Metallic = _Metallic;
o.Smoothness = _Glossiness;
}
ENDCG
}
FallBack "Diffuse"
}
And this is how the final result SHOULD look like: