Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by MihajloNen · Jan 13, 2018 at 12:46 PM · shadercgsurface shadernormal mapbump map

How to add Bump/Normal Map to surface shader? (CG)

Hi dear community. I am trying to get a normal map texture working with a custom terrain shader I got from following a tutorial series by Sebastian Lague. I have currently tried this:

   `Shader "Custom/Terrain" {
    Properties {
    // Textures and Scale-Slot
    testTexture("Texture", 2D) = "white"{}
    testScale("Scale", Float) = 1
    _NormalMap ("Normal Map", 2D) = "bump" {}
     }

    SubShader {
 Tags { "RenderType"="Opaque" }
 LOD 200

 // SHADER-Language = CG 

 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

 const static int maxLayerCount = 8; // Max Amount of colors the terrain can have
 const static float epsilon = 1E-4; 

 int layerCount; 
 float3 baseColours[maxLayerCount]; 
 float baseStartHeights[maxLayerCount]; // ZONES FOR COLORS OF TERRAIN
 float baseBlends[maxLayerCount]; // BLENDS FoR COLORS/TEXTUREN
 float baseColourStrength[maxLayerCount];
 float baseTextureScales[maxLayerCount]; // Texture size
 float minHeight; //min. Hight of Map
 float maxHeight; //max. Hight of Map

 sampler2D testTexture; // Textur Slot
 float testScale; // Scale of the texture

 sampler2D _NormalMap;

 UNITY_DECLARE_TEX2DARRAY(baseTextures); // Texture-Array from TextureData(another Script) definieren

 struct Input {
     float3 worldPos; // Position on Map
     float3 worldNormal; // Normales of Map 
     float2 uv_Diffuse;
     INTERNAL_DATA
 };


 float inverseLerp(float a, float b, float value) // a = min.value, b = max.value, value = surrent value
 {  
    return saturate((value - a)/(b-a));
 }

 // --> TRIPLANAR MAPPING
 float3 triplanar (float3 worldPos, float scale, float3 blendAxes, int textureIndex)
 {
     float3 scaledWorldPos = worldPos/ scale;

     float3 xProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.y,scaledWorldPos.z, textureIndex)) * blendAxes.x; 
     float3 yProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.x,scaledWorldPos.z, textureIndex)) * blendAxes.y; 
     float3 zProjection = UNITY_SAMPLE_TEX2DARRAY(baseTextures, float3(scaledWorldPos.x,scaledWorldPos.y, textureIndex)) * blendAxes.z; 
     return xProjection + yProjection + zProjection; // Mix textures
 }

 // 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_CBUFFER_START(Props)
     // put more per-instance properties here
 UNITY_INSTANCING_CBUFFER_END


 void surf (Input IN, inout SurfaceOutputStandard o) {


 float heightPercent = inverseLerp(minHeight, maxHeight, IN.worldPos.y);

 float3 blendAxes = abs(IN.worldNormal); 
 blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;  

     for (int i = 0; i < layerCount; i++)
     {

         float drawStrenght = inverseLerp(-baseBlends[i]/2 - epsilon, baseBlends[i]/2, heightPercent - baseStartHeights[i]); 

         float3 baseColour = baseColours[i] * baseColourStrength[i]; 
         // Textur
         float3 textureColour = triplanar(IN.worldPos, baseTextureScales[i], blendAxes, i) * (1-baseColourStrength[i]);

         o.Albedo = o.Albedo * (1 - drawStrenght) + (baseColour + textureColour) * drawStrenght;
         o.Normal = UnpackNormal( tex2D(_NormalMap, IN.uv_Diffuse));
     }

 }
 ENDCG
 }
 FallBack "Diffuse"}` 

This currently gets me to the point of having a black terrain (instead of the original triplanar mapped main texture) and looks like this: alt text

This is the set up in the Unity inspector: alt text

my question is: How can I achieve having the terrain with the bump map on it? Thx to everyone who helps out!

bildschirmfoto-2018-01-13-um-112101.png (248.9 kB)
bildschirmfoto-2018-01-13-um-112659.png (42.3 kB)
Comment
Add comment · Show 2
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
avatar image insominx · Jan 13, 2018 at 03:51 PM 0
Share

You might wanna see what Unity does normally with their terrain shading. check out the "built-in" shaders: alt text

builtin.png (53.0 kB)
avatar image MihajloNen insominx · Jan 14, 2018 at 10:31 AM 0
Share

thx @inso$$anonymous$$x I know at least now (after trying out stuff for hours) that the issue is there because I have multiple layers of textures. These "o.Albedos" get set up in the for-loop:

  for (int i = 0; i < layerCount; i++) 

{ float drawStrenght = inverseLerp(-baseBlends[i]/2 - epsilon, baseBlends[i]/2, heightPercent - baseStartHeights[i]); float3 baseColour = baseColours[i] baseColourStrength[i]; // Textur float3 textureColour = triplanar(IN.worldPos, baseTextureScales[i], blendAxes, i) (1-baseColourStrength[i]); o.Albedo = o.Albedo (1 - drawStrenght) + (baseColour + textureColour) drawStrenght; o.Normal = UnpackNormal( tex2D(_Normal$$anonymous$$ap, IN.uv_Diffuse)); }


When I don't use " o.Normal = UnpackNormal( tex2D(_Normal$$anonymous$$ap, IN.uv_Diffuse));" in here it works fine. I need to figure out how and where exactly the normal map is applied?

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.

Follow this Question

Answers Answers and Comments

118 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Surface Shader World Normal 0 Answers

Spherical Hamonics - ShadeSH9 and Surface Shader issue 0 Answers

Shader - Object Depth 0 Answers

Shader that renders fragment behind 0 Answers

Prevent ColorMask obscuring parts of an object's mesh 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