• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Ga2Z · Jul 10, 2015 at 07:25 AM · shaderterraincustomnormalmapping

How can I add Normal mapping to a Terrain custom shader?

I'm not a shader coder but I got a shader I liked here, the shader gets the object world position an sets a color gradient according to the vertical positions. I'm using it on a terrain and I can paint the textures as usual but I'd like to be able to use normal mapping too, so I tried to code it usisng the manual reference for surface shaders but didn't succeed on that part, also tried taking a look in the built-in shaders but as I'm not a shader coder, I couldn't figure out what I really had to do.

Here's the shader:

 Shader "Custom/HeightDependentTint" 
  {
    Properties 
    {
      _MainTex ("Base (RGB)", 2D) = "white" {}
      _Normal ("Normal (A)", 2D) = "bump" {}
      _HeightMin ("Height Min", Float) = -1
      _HeightMax ("Height Max", Float) = 1
      _ColorMin ("Tint Color At Min", Color) = (0,0,0,1)
      _ColorMax ("Tint Color At Max", Color) = (1,1,1,1)
    }
   
    SubShader
    {
      Tags { "RenderType"="Opaque" }
   
      CGPROGRAM
      #pragma surface surf Lambert
   
      sampler2D _MainTex;
      sampler2D _Normal;
      fixed4 _ColorMin;
      fixed4 _ColorMax;
      float _HeightMin;
      float _HeightMax;
   
      struct Input
      {
        float2 uv_MainTex;
        float2 uv_NormalMap;
        float3 worldPos;
      };
   
      void surf (Input IN, inout SurfaceOutput o) 
      {
        half4 c = tex2D (_MainTex, IN.uv_MainTex);
        float h = (_HeightMax-IN.worldPos.y) / (_HeightMax-_HeightMin);
        fixed4 tintColor = lerp(_ColorMax.rgba, _ColorMin.rgba, h);
        o.Albedo = c.rgb * tintColor.rgb;
        o.Alpha = c.a * tintColor.a;
        o.Normal = UnpackNormal (tex2D (_Normal, IN.uv_NormalMap));
      }
      ENDCG
    } 
    Fallback "Diffuse"
  }

Here's a picture of what I got: Here I have a nice brighter color on the top and gets darker in the bottom

terrain-gradient.jpg (66.9 kB)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

Cannot paint Texture onto Terrain using Custom Material 2018.4 1 Answer

Unity bulit in terrain shader help 0 Answers

Custom Terrain Shader not taking Textures 0 Answers

Adding BRDF lighting to custom Terrain Shader. 0 Answers

Procedural heightmap shader - Normals and tangents 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges