Sunday, November 19, 2023

Dynamic Day night sky blend or dynamic day night cycle in unity

 

Dynamic day night sky tutorial



Download Link :

Download Dynamic Sky Blend Project


Shader Code:

  //ASSALAM o alaikum 
    Shader "Custom/DynaimcSkyBlend" {
    Properties{
        _DaySkyMap("Day Sky Map", 2D) = "white" {}
        _NightSkyMap("Night Sky Map", 2D) = "black" {}
        _BlendTime("Blend Time", Range(0.0, 100.0)) = 0.5
    }

        SubShader{
            Tags { "Queue" = "Background" "RenderType" = "Background" }
            LOD 100

            Pass {
                CGPROGRAM
                #pragma vertex vert
                #pragma fragment frag
                #include "UnityCG.cginc"

                struct appdata {
                    float4 vertex : POSITION;
                    float2 uv : TEXCOORD0;
                };

                struct v2f {
                    float4 vertex : SV_POSITION;
                    float2 uv : TEXCOORD0;
                };

                sampler2D _DaySkyMap;
                sampler2D _NightSkyMap;
                float _BlendTime;

                v2f vert(appdata v) {
                    v2f o;
                    o.vertex = UnityObjectToClipPos(v.vertex);
                    o.uv = v.uv;
                    return o;
                }

                fixed4 frag(v2f i) : SV_Target {
                    float blendTime = _BlendTime;
                    float totalTime = _Time.y;
                    int textureIndex = int(totalTime / blendTime) % 2;
                    float blendFactor = smoothstep(0.0, blendTime, totalTime % blendTime);

                    fixed4 dayColor = tex2D(_DaySkyMap, i.uv);
                    fixed4 nightColor = tex2D(_NightSkyMap, i.uv);
                    fixed4 color;

                    if (textureIndex == 0) {
                        // Blend from day to night
                        color = lerp(dayColor, nightColor, blendFactor);
                    }
     else {
                        // Blend from night to day
                        color = lerp(nightColor, dayColor, blendFactor);
                    }

                    return color;
                }
                ENDCG
            }
        }
}

  


Video Link :





2 comments:

Take input in Java easy tutorial

  Input taking in Java tutorial Download Link : Download input taking in Java example code ...