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 ThisGuyThatsHere · Apr 21, 2017 at 12:58 PM · shadershadersnormalssurface shadernormal map

Problem with Normal Maps

Hey,

So I'm working on a procedural terrain and Standard Shader simply doesn't work for me. Instead I have created my own shader (evaluated from standard surface shader) that doesn't require UV and It works great with textures, however, when I try to add normal maps (more accurately I add "o.Normal = stuff") everything breaks (e.g. object becomes perfect black) so how do I add normal map support?

Here is shader in the albedo state:

    Shader "Custom/Terrain" {
         Properties {
             _TopTexture ("Top Texture", 2D) = "white" {}
             _TopNormal ("Top NormalMap", 2D) = "bump" {}
             _TopTexScale ("Top Texture Scale", float) = 0
             _Glossiness ("Smoothness", Range(0,1)) = 0.0
             _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
     
             // Use shader model 3.0 target, to get nicer looking lighting
             #pragma target 3.0
     
             struct Input {
                 float3 worldPos;
                 float3 worldNormal; INTERNAL_DATA
             };
     
             half _Glossiness;
             half _Metallic;
             float _TopTexScale;
             sampler2D _TopTexture;
             sampler2D _TopNormal;
     
             void surf (Input IN, inout SurfaceOutputStandard o) {
     
                 float3 scaledWorldPos = IN.worldPos / _TopTexScale;
                 float3 blendAxes = abs(IN.worldNormal);
                 blendAxes /= blendAxes.x + blendAxes.y + blendAxes.z;
                 float3 xProjection = tex2D(_TopTexture, scaledWorldPos.yz) * blendAxes.x;
                 float3 yProjection = tex2D(_TopTexture, scaledWorldPos.xz) * blendAxes.y;
                 float3 zProjection = tex2D(_TopTexture, scaledWorldPos.xy) * blendAxes.z;
                 o.Albedo = xProjection + yProjection + zProjection;
     
                 o.Metallic = _Metallic;
                 o.Smoothness = _Glossiness;
             }
             ENDCG
         }
         FallBack "Diffuse"
     }

Comment
Add comment · Show 3
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 AtGfx · Apr 21, 2017 at 02:03 PM 0
Share

I don't really understand what you are trying to achieve with your shader... Normally for normal mapping you should have a normal map UV in your Input structure and then you simply do something like

o.Normal = text2D(_TopNormal, input.normalUV);

$$anonymous$$aybe you can have a look here and start with the provided example of normal mapping shader and try to make the effect you want. Step by step you could isolate the problem that renders your object completely black.

Hope it helps ;)

avatar image ThisGuyThatsHere AtGfx · Apr 21, 2017 at 02:27 PM 0
Share

Well, this helped atleast a bit, It seems like #pragma surface surf Standard fullforwardshadows doesn't like normal maps, but is there any documentation on what I can choose? #pragma surface surf Lambert that appears in examples seems kinda outdated...

avatar image AtGfx ThisGuyThatsHere · Apr 21, 2017 at 03:00 PM 0
Share

Yes the documentation on pragma tag is here

EDIT : Did you find the solution to your problem ?

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Namey5 · Apr 24, 2017 at 10:29 AM

There are some problems with using triplanar texturing for normal maps, although from memory the shader I created for this works perfectly fine;

 Shader "Custom/Triplanar" {
     Properties {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _BumpMap ("Normal Map", 2D) = "bump" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
         _Scale ("Scale", Float) = 2.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;
         sampler2D _BumpMap;
 
         struct Input {
             float3 coords;
             float3 normal;
         };
 
         half _Glossiness;
         half _Metallic;
         half _Scale;
         fixed4 _Color;
 
         void vert (inout appdata_full v, out Input o) {
             UNITY_INITIALIZE_OUTPUT (Input, o);
             o.coords = mul (unity_ObjectToWorld, v.vertex) * _Scale;
             o.normal = mul ((float3x3)unity_ObjectToWorld, v.normal);
         }
 
         void surf (Input IN, inout SurfaceOutputStandard o) {
             float3 blend = abs (IN.normal);
             blend /= dot (blend, 1.0);
 
             fixed4 bx = tex2D (_BumpMap, IN.coords.zy);
             fixed4 bz = tex2D (_BumpMap, IN.coords.xy);
             fixed4 by = tex2D (_BumpMap, IN.coords.xz);
             fixed4 b = bx * blend.x + bz * blend.z + by * blend.y;
             o.Normal = UnpackNormal (b);
             // Albedo comes from a texture tinted by color
             fixed4 cx = tex2D (_MainTex, IN.coords.zy);
             fixed4 cz = tex2D (_MainTex, IN.coords.xy);
             fixed4 cy = tex2D (_MainTex, IN.coords.xz);
             fixed4 c = cx * blend.x + cz * blend.z + cy * blend.y;
             o.Albedo = c.rgb;
             // Metallic and smoothness come from slider variables
             o.Metallic = _Metallic;
             o.Smoothness = _Glossiness;
             o.Alpha = c.a;
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

I think it's down to using the default surface shader normal value. From memory you have to pass your own values in order for it to work, because the default values are overwritten when you write to o.Normal.

Comment
Add comment · Share
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

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

97 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

Related Questions

Surface Shader apply normal map to object normal without pluggin it into o.Normal 1 Answer

Referencing the current pixel's world coordinates in the SURF block of a shader 1 Answer

How to add "fat" feature to the Standard.shader? 1 Answer

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

How to blend color when using shadergraph wich uses normals? 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