Skip to content

Instantly share code, notes, and snippets.

@lyuma
Created April 16, 2020 08:00
Show Gist options
  • Save lyuma/63a584ff2f6e030d7ce484c879c2720f to your computer and use it in GitHub Desktop.
Save lyuma/63a584ff2f6e030d7ce484c879c2720f to your computer and use it in GitHub Desktop.
Cubemap shader - Added second and third third skybox if light is at sunset, or below the horizon respectively.
// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)
// Edited by Lyuma - Added second and third third skybox if light is at sunset, or below the horizon respectively.
Shader "Skybox/CubemapTrio"
{
Properties
{
_Rotation ("Rotation", Range(0, 360)) = 0
[NoScaleOffset] _Tex ("Cubemap Day", Cube) = "grey" {}
[NoScaleOffset] _TexSunset ("Cubemap Sunset", Cube) = "grey" {}
[NoScaleOffset] _TexNight ("Cubemap Night", Cube) = "grey" {}
_SunsetDotThreshold ("Sunset Dot Threshold", Range(-1, 1)) = 0.1
_NightDotThreshold ("Night Dot Threshold", Range(-1, 1)) = -0.05
}
SubShader
{
Tags { "Queue"="Background" "RenderType"="Background" "PreviewType"="Skybox" }
Cull Off
ZWrite Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 2.0
#include "UnityCG.cginc"
samplerCUBE _Tex;
samplerCUBE _TexNight;
samplerCUBE _TexSunset;
float _Rotation;
float _SunsetDotThreshold;
float _NightDotThreshold;
float3 RotateAroundYInDegrees (float3 vertex, float degrees)
{
float alpha = degrees * UNITY_PI / 180.0;
float sina, cosa;
sincos(alpha, sina, cosa);
float2x2 m = float2x2(cosa, -sina, sina, cosa);
return float3(mul(m, vertex.xz), vertex.y).xzy;
}
struct appdata_t
{
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 texcoord : TEXCOORD0;
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert (appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
float3 rotated = RotateAroundYInDegrees(v.vertex, _Rotation);
o.vertex = UnityObjectToClipPos(rotated);
o.texcoord = v.vertex.xyz;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
half4 tex;
[branch]
if (dot(float3(0,1,0), normalize(_WorldSpaceLightPos0.xyz + float3(0,0.01,0))) < _NightDotThreshold) {
tex = texCUBE(_TexNight, i.texcoord);
} else if (dot(float3(0,1,0), normalize(_WorldSpaceLightPos0.xyz + float3(0,0.01,0))) < _SunsetDotThreshold) {
tex = texCUBE(_TexSunset, i.texcoord);
} else {
tex = texCUBE(_Tex, i.texcoord);
}
return tex;
}
ENDCG
}
}
Fallback Off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment