2

I'm trying to achieve this look in my Unity Game:

enter image description here

I like how the color of the mountain gets lighter as the altitude increases.

I'm still new to game development and while I understand what shaders do, I'm having trouble trying to use them in practice.

I know I need to do something like this in my surface shader:

float4 mountainColor = lerp(_BottomColor,_TopColor,IN.vertex.z);

...to lerp between the darker color and lighter color based on the z value.

But I'm not sure how to pragmatically lighten a color in the shader. I'm not using vertex colors, the color comes from a texture. Any help/pointers would be appreciated.

EDIT:

So, duh, I realized I just have to multiply the rgb value to either lighten or darken it.

The issue is, if I just do something like this:

o.Albedo = c.rgb * 1.5;

... yes, it lightens it, but it also slightly changes the hue and becomes overly saturated.

How can I fix this?

1 Answer 1

1

Here's my code:

Shader "Custom/Altitude Gradient" {
Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Albedo (RGB)", 2D) = "white" {}
    _Glossiness ("Smoothness", Range(0,1)) = 0.5
    _Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
    Tags { "RenderType"="Opaque" }
    LOD 200

    CGPROGRAM
    // Physically based Standard lighting model, and enable shadows on all light types
    #pragma surface surf Standard fullforwardshadows vertex:vert

    // Use shader model 3.0 target, to get nicer looking lighting
    #pragma target 3.0

    sampler2D _MainTex;

    struct Input {
        float2 uv_MainTex;
        float3 localPos;
    };

    half _Glossiness;
    half _Metallic;
    fixed4 _Color;

    void vert(inout appdata_full v, out Input o) {
        UNITY_INITIALIZE_OUTPUT(Input, o);
        o.localPos = v.vertex.xyz;
    }

    void surf (Input IN, inout SurfaceOutputStandard o) {
        // Albedo comes from a texture tinted by color
        fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
        o.Albedo = lerp(c.rgb * 0.5, 1, IN.localPos.y);
        // Metallic and smoothness come from slider variables
        o.Metallic = _Metallic;
        o.Smoothness = _Glossiness;
        o.Alpha = c.a;
    }
    ENDCG
}
FallBack "Diffuse"
}

It works good enough for my needs. I'll have to continue to fiddle with it to get the exact look I want, but the base is here.

Sign up to request clarification or add additional context in comments.

2 Comments

Was this meant to be an answer to your own question? If not, it should be edited into your question instead.
@AquaGeneral Yes, it's my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.