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 Namnam · Mar 03, 2018 at 07:54 PM · shaderlightingvertex shaderfragmentdaynight

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
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

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

140 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

_LightMatrix0 and Directional Light 1 Answer

Glass Distort Shader lighting & mesh distortion 0 Answers

Combine Vertex / Fragment and Surface Shader 0 Answers

Shader - Directional Lights Depth 0 Answers

Vertex shader breaks render to texture - camera preview and render texture not the same 1 Answer


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