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
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"
}
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.
Same issue than with 99% of other planet shaders, no shadow is casted on the planet.
Your answer
Follow this Question
Related Questions
[URP] Transp Shadergraph has shadows? 2 Answers
How to pass a RInt RenderTexture to a ComputeShader? 0 Answers
Grass / Details disappear when i import lightweigh render pipeline (Unity 2018.2.0f2) 0 Answers
How do you use Blend Shapes when rendering a mesh via Graphics.DrawMesh()? 0 Answers
Is there any way to make a custom written shader with hdrp? 0 Answers