• 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 /
  • Help Room /
avatar image
1
Question by SergioSandiaz · Oct 12, 2016 at 04:56 AM · shaderrenderinggraphicsprogrammingnormalmap

Insert normal map in a shader

Hey guys! I don´t usually ask here, because I search in google first, but I couldn´t find something that helps me, the thing is that I have a shader that simulates a planet atmosphere, but it doesn´t have normal map option and I wanna add it to simultate terrain height, I´m a crap with shader programming, so I´ll appreciate any help :)

Here´re some images alt text

alt text

And of course here´s the code:

 Shader "SpacePhysics/PlanetRendering"
 {
     Properties 
     {
         _AtmosphereColor ("Atmosphere Color", Color) = (0.1, 0.35, 1.0, 1.0)
         _AtmospherePow ("Atmosphere Power", Range(1.5, 8)) = 2
         _AtmosphereMultiply ("Atmosphere Multiply", Range(1, 3)) = 1.5
 
         _DiffuseTex("Diffuse", 2D) = "white" {}
 
         
         _CloudAndNightTex("Cloud And Night", 2D) = "black" {}
 
         _LightDir("Light Dir", Vector) = (-1,0,0,1)
     }
 
     SubShader 
     {
 
 
         ZWrite On
         ZTest LEqual
 
         pass
         {
         CGPROGRAM
             #include "UnityCG.cginc"
             #pragma vertex vert 
             #pragma fragment frag
             
             sampler2D _DiffuseTex;
             sampler2D _CloudAndNightTex;
 
             float4 _AtmosphereColor;
             float _AtmospherePow;
             float _AtmosphereMultiply;
 
             float4 _LightDir;
 
             struct vertexInput 
             {
                 float4 pos                : POSITION;
                 float3 normal            : NORMAL;
                 float2 uv                : TEXCOORD0;
             };
 
             struct vertexOutput 
             {
                 float4 pos            : POSITION;
                 float2 uv            : TEXCOORD0;
                 half diffuse        : TEXCOORD1;
                 half night            : TEXCOORD2;
                 half3 atmosphere    : TEXCOORD3;
             };
             
             vertexOutput vert(vertexInput input) 
             {
                 vertexOutput output;
                 output.pos = mul(UNITY_MATRIX_MVP, input.pos);
                 output.uv = input.uv;
 
                 output.diffuse = saturate(dot(_LightDir.xyz, input.normal) * 1.2);
                 output.night = 1 - saturate(output.diffuse * 2);
 
                 half3 viewDir = normalize(ObjSpaceViewDir(input.pos));
                 half3 normalDir = input.normal;
                 output.atmosphere = output.diffuse * _AtmosphereColor.rgb * pow(1 - saturate(dot(viewDir, normalDir)), _AtmospherePow) * _AtmosphereMultiply;
 
                 return output;
             }
 
 
             half4 frag(vertexOutput input) : Color
             {
                 half3 colorSample = tex2D(_DiffuseTex, input.uv).rgb;
 
                 half3 cloudAndNightSample = tex2D(_CloudAndNightTex, input.uv).rgb;
                 half3 nightSample = cloudAndNightSample.ggb;
                 half cloudSample = cloudAndNightSample.r;
 
                 half4 result;
                 result.rgb = (colorSample + cloudSample) * input.diffuse + nightSample * input.night + input.atmosphere;
 
                 result.a = 1;
                 return result;
             }
         ENDCG
         }
     }
     
     Fallback "Diffuse"
 }

bandicam-2016-10-11-23-49-18-905.jpg (72.7 kB)
bandicam-2016-10-11-23-50-51-278.jpg (75.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Namey5 · Oct 12, 2016 at 06:20 AM

 Shader "SpacePhysics/PlanetRendering"
 {
     Properties 
     {
         _AtmosphereColor ("Atmosphere Color", Color) = (0.1, 0.35, 1.0, 1.0)
         _AtmospherePow ("Atmosphere Power", Range(1.5, 8)) = 2
         _AtmosphereMultiply ("Atmosphere Multiply", Range(1, 3)) = 1.5
         _DiffuseTex("Diffuse", 2D) = "white" {}
         _BumpMap ("Normal Map", 2D) = "bump" {}
         _CloudAndNightTex("Cloud And Night", 2D) = "black" {}
         _LightDir("Light Dir", Vector) = (-1,0,0,1)
     }
  
     SubShader 
     {
  
  
         ZWrite On
         ZTest LEqual
  
         pass
         {
         CGPROGRAM
             #include "UnityCG.cginc"
             #pragma vertex vert 
             #pragma fragment frag
             #pragma target 3.0
              
             sampler2D _DiffuseTex;
             float4 _DiffuseTex_ST;
             sampler2D _BumpMap;
             float4 _BumpMap_ST;
             sampler2D _CloudAndNightTex;
             float4 _CloudAndNightTex_ST;
  
             float4 _AtmosphereColor;
             float _AtmospherePow;
             float _AtmosphereMultiply;
  
             float4 _LightDir;
  
             struct vertexOutput 
             {
                 float4 pos : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 half3 tspace0 : TEXCOORD1; // tangent.x, bitangent.x, normal.x
                 half3 tspace1 : TEXCOORD2; // tangent.y, bitangent.y, normal.y
                 half3 tspace2 : TEXCOORD3; // tangent.z, bitangent.z, normal.z
                 /*half diffuse : TEXCOORD1;
                 half night : TEXCOORD2;*/
                 half3 viewDir : TEXCOORD4;
                 half3 normalDir : TEXCOORD5;
             };
              
             vertexOutput vert(appdata_tan input) 
             {
                 vertexOutput output;
                 output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
                 output.uv = input.texcoord;
  
                 output.viewDir = normalize(ObjSpaceViewDir(input.vertex));
                 output.normalDir = input.normal;
  
                 half3 wNormal = UnityObjectToWorldNormal(input.normal);
                 half3 wTangent = UnityObjectToWorldDir(input.tangent.xyz);
                 // compute bitangent from cross product of normal and tangent
                 half tangentSign = input.tangent.w * unity_WorldTransformParams.w;
                 half3 wBitangent = cross(wNormal, wTangent) * tangentSign;
                 // output the tangent space matrix
                 output.tspace0 = half3(wTangent.x, wBitangent.x, wNormal.x);
                 output.tspace1 = half3(wTangent.y, wBitangent.y, wNormal.y);
                 output.tspace2 = half3(wTangent.z, wBitangent.z, wNormal.z);
  
                 return output;
             }
  
  
             half4 frag(vertexOutput input) : SV_Target
             {
                 half2 uv_MainTex = TRANSFORM_TEX (input.uv, _DiffuseTex);
                 half2 uv_BumpMap = TRANSFORM_TEX (input.uv, _BumpMap);
                 half2 uv_CloudAndNightTex = TRANSFORM_TEX (input.uv, _CloudAndNightTex);
             
                 half3 tnormal = UnpackNormal(tex2D(_BumpMap, uv_BumpMap));
                 // transform normal from tangent to world space
                 half3 worldNormal;
                 worldNormal.x = dot(input.tspace0, tnormal);
                 worldNormal.y = dot(input.tspace1, tnormal);
                 worldNormal.z = dot(input.tspace2, tnormal);
                 
                 half diffuse = saturate(dot(_LightDir.xyz, worldNormal) * 1.2);
                  
                 half night = 1 - saturate(diffuse * 2);
                 
                 half3 atmosphere = diffuse * _AtmosphereColor.rgb * pow(1 - saturate(dot(input.viewDir, input.normalDir)), _AtmospherePow) * _AtmosphereMultiply;
 
                 half3 colorSample = tex2D(_DiffuseTex, uv_MainTex).rgb;
  
                 half3 cloudAndNightSample = tex2D(_CloudAndNightTex, uv_CloudAndNightTex).rgb;
                 half3 nightSample = cloudAndNightSample.ggb;
                 half cloudSample = cloudAndNightSample.r;
  
                 half4 result;
                 result.rgb = (colorSample + cloudSample) * diffuse + nightSample * night + atmosphere;
  
                 result.a = 1;
                 return result;
             }
             ENDCG
         }
     }
      
     Fallback "Diffuse"
 }

Something like that should do it. Not tested, but should suit. Normal map will not affect atmosphere.

Comment
Add comment · Show 2 · 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
avatar image SergioSandiaz · Oct 12, 2016 at 07:13 PM 0
Share

Thank you very much man! that worked perfectly :)

avatar image EarthHobbit · May 04, 2020 at 07:32 PM 0
Share

Same issue than with 99% of other planet shaders, no shadow is casted on the planet.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

104 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

Related Questions

Is there any way to make a custom written shader with hdrp? 0 Answers

Normal map issue (direction) 0 Answers

Custom shader samples pixels per-texture rather than from the screen 1 Answer

HTC Vive Controllers are PINK in Unity 2 Answers

How can I rotate Cubemap? 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