- Home /
Help with lighting for my shader
Hi,
I have a procedurale planet with a day/night cycle. Wich means a directionnal light that rotate around the planet.
I need my shader react with this directionnal light, for the moment it react with the intensity but not with light position/direction. In other words, i want my shader react with the rotation movement of the directionnal light around the planet in order to have dark texture for night cycle.
Why it react only with intensity and not with light position/direction ?
Here is my shader :
Shader "Custom/Planet" {
Properties {
_MainColor("Spec Color", Color) = (1,1,1,1)
_SpecColor1 ("Spec Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_SpecularTex ("Specular (R) Gloss (G)", 2D) = "gray" {}
_Tex0 ("Tex 0", 2D) = "white" {}
_Tex1 ("Tex 1", 2D) = "white" {}
_Tex2 ("Tex 2", 2D) = "white" {}
_Tex3 ("Tex 3", 2D) = "white" {}
_Tex4 ("Tex 4", 2D) = "white" {}
_SpecPow ("Sec Pow", Float) = 0
_EmissionColor ("Emission Color", Color) = (0,0,0)
_Blend0to1and1to2 ("Blend between 0 and 1, 1 and 2", Vector) = (0,1,2,3)
_Blend2to3and3to4 ("Blend between 2 and 3, 3 and 4", Vector) = (0,1,2,3)
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
}
SubShader {
LOD 200
Pass
{
Lighting On Cull Off ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
Tags {"LightMode"="ForwardBase"}
CGPROGRAM
#pragma vertex vert Lambert
#pragma fragment frag Lambert
#include "UnityCG.cginc"
#include "UnityStandardMeta.cginc"
#include "AutoLight.cginc"
#include "UnityLightingCommon.cginc"
//#pragma target 3.0
sampler2D _Tex0;
sampler2D _Tex1;
sampler2D _Tex2;
sampler2D _Tex3;
sampler2D _Tex4;
sampler2D _SpecularTex;
float4 _Blend0to1and1to2;
float4 _Blend2to3and3to4;
uniform float4 _Tex0_ST;
uniform float4 _Tex1_ST;
uniform float4 _Tex2_ST;
uniform float4 _Tex3_ST;
half4 _SpecColor1;
half4 _MainColor;
half _SpecPow;
struct appData {
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv1 : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float2 uv3 : TEXCOORD2;
float2 uv4 : TEXCOORD3;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv1 : TEXCOORD0;
float2 uv2 : TEXCOORD1;
float2 uv3 : TEXCOORD2;
float2 uv4 : TEXCOORD3;
float3 lightDirT : TEXCOORD4;
float3 viewDirT : TEXCOORD5;
LIGHTING_COORDS(6,7)
float4 col : COLOR1;
float4 diff : COLOR0;
};
v2f vert (appData v) {
float4x4 modelMatrixInverse = unity_WorldToObject;
v2f output;
output.pos = UnityObjectToClipPos (v.vertex);
//output.pos = mul(UNITY_MATRIX_MVP, v.vertex);
output.uv1 = TRANSFORM_TEX(v.uv1, _Tex0);
output.uv2 = TRANSFORM_TEX(v.uv2, _Tex1);
output.uv3 = TRANSFORM_TEX(v.uv3, _Tex2);
output.uv4 = TRANSFORM_TEX(v.uv4, _Tex3);
output.col = length(v.vertex);
TANGENT_SPACE_ROTATION;
float3 ldir = normalize( ObjSpaceLightDir( v.vertex ) );
float3 binormal1 = cross( v.normal, v.tangent.xyz ) * v.tangent.w;
float3x3 rotation1 = float3x3( v.tangent.xyz, binormal1, v.normal );
output.viewDirT = ObjSpaceViewDir ( v.vertex );
output.viewDirT = mul ( rotation1 , output.viewDirT );
output.lightDirT = ObjSpaceLightDir ( v.vertex );
output.lightDirT = mul ( rotation1 , output.lightDirT );
TRANSFER_VERTEX_TO_FRAGMENT(output);
return output;
}
fixed4 frag (v2f fInput) : SV_Target {
//
//NORMALIZE
fInput.lightDirT = normalize ( fInput.lightDirT );
fInput.viewDirT = normalize ( fInput.viewDirT );
//fInput.normalWorld = normalize ( fInput.normalWorld );
//
//BUMP
float3 NMN = UnpackNormal ( tex2D (_Tex2, fInput.uv3 ));
NMN = normalize ( NMN );
//
//DIFFUSE + WRAP
//Diffuse calculation: Angle between normals and light direction.
half diffuse1 = max ( 0, dot ( NMN, _WorldSpaceLightPos0.xyz)) * 0.5 + 0.5;
//
//SPECULAR
//Specular calculation: Angle between normals and half view vector (light direction + view direction).
half3 halfView = normalize ( fInput.lightDirT + fInput.viewDirT );
half spec = max (0, dot ( halfView, NMN ));
spec = pow ( spec, ( _SpecPow * 10 ));
half4 spec4 = { spec, spec, spec, 1 };
float atten = LIGHT_ATTENUATION(fInput);
fixed4 c0 = tex2D (_Tex0, fInput.uv1);
fixed4 c1 = tex2D (_Tex1, fInput.uv2);
fixed4 c2 = tex2D (_Tex2, fInput.uv3);
//c2 = c2 + c2.a * _Glossiness;
//c2 = c2 + c2.r * _Metallic;
//c2 *= _MainColor;
fixed4 c3 = tex2D (_Tex3, fInput.uv4);
fixed4 c4 = tex2D (_Tex4, fInput.uv1);
if (fInput.col.x < _Blend0to1and1to2.x) {
return c0;
}
if (fInput.col.x > _Blend0to1and1to2.x && fInput.col.x < _Blend0to1and1to2.y) {
return lerp(c0,c1,((fInput.col.x - _Blend0to1and1to2.x)/(_Blend0to1and1to2.y-_Blend0to1and1to2.x)));
}
if (fInput.col.x > _Blend0to1and1to2.y && fInput.col.x < _Blend0to1and1to2.z) {
return c1;
}
if (fInput.col.x > _Blend0to1and1to2.z && fInput.col.x < _Blend0to1and1to2.w) {
return lerp(c1,c2,((fInput.col.x - _Blend0to1and1to2.z)/(_Blend0to1and1to2.w-_Blend0to1and1to2.z)));
}
if (fInput.col.x > _Blend0to1and1to2.w && fInput.col.x < _Blend2to3and3to4.x) {
c2 = ((c2 * diffuse1 * _LightColor0) + ( spec4 * _LightColor0 * _SpecColor )) * ( atten * 2 );
c2.a = 1;
return c2;
}
if (fInput.col.x > _Blend2to3and3to4.x && fInput.col.x < _Blend2to3and3to4.y) {
return lerp(c2,c3,((fInput.col.x - _Blend2to3and3to4.x)/(_Blend2to3and3to4.y-_Blend2to3and3to4.x)));
}
if (fInput.col.x > _Blend2to3and3to4.y && fInput.col.x < _Blend2to3and3to4.z) {
return c3;
}
if (fInput.col.x > _Blend2to3and3to4.z && fInput.col.x < _Blend2to3and3to4.w) {
return lerp(c3,c4,((fInput.col.x - _Blend2to3and3to4.z)/(_Blend2to3and3to4.w-_Blend2to3and3to4.z)));
}
return c4;
}
ENDCG
}
//FallBack "Diffuse"
}
}
Comment