Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Last active June 6, 2021 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save partybusiness/92fed5bb609fefd3ac72c0c70c8e42a4 to your computer and use it in GitHub Desktop.
Save partybusiness/92fed5bb609fefd3ac72c0c70c8e42a4 to your computer and use it in GitHub Desktop.
Shader "Skybox/MoonSkybox"
{
//Displays a skybox with an overlayed texture that can be positioned as a moon
Properties
{
_MoonTex ("Moon Texture", 2D) = "white" {}
_SkyMap ("Sky Map", Cube) = "" {}
_MoonDirection ("Moon Direction", Vector) = (0.4, 0.8, 0, 0)
_MoonScale ("Moon Scale", Range(0.01, 1.0)) = 0.2
}
SubShader
{
Tags{ "Queue" = "Background"
"RenderType" = "Background"
"PreviewType" = "Skybox" }
Cull Back
Lighting Off
ZWrite Off
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 viewDir : TEXCOORD1;
};
sampler2D _MoonTex;
float4 _MoonTex_ST;
float4 _MoonDirection;
float _MoonScale;
uniform samplerCUBE _SkyMap;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.viewDir = mul(unity_ObjectToWorld, v.vertex).xyz - _WorldSpaceCameraPos;
o.uv = TRANSFORM_TEX(v.uv, _MoonTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 backCol = texCUBE(_SkyMap, (i.viewDir));
fixed3 moonVector = normalize(_MoonDirection.xyz);
fixed3 diff = normalize(i.viewDir) - moonVector; //difference between view dir and moon dir
//side vector
fixed3 sideVector = normalize(cross(fixed3(0,1,0), moonVector));
//up vector
fixed3 upVector = normalize(cross(sideVector, moonVector));
fixed2 moonUV = (fixed2(dot(sideVector,diff),dot(upVector,diff))/_MoonScale) +0.5;
fixed4 moonColour = tex2D(_MoonTex, moonUV);
moonColour.a = moonColour.a * (dot(normalize(i.viewDir), _MoonDirection) > 0); //Make sure only one moon
return lerp(backCol, moonColour, moonColour.a);
}
ENDCG
}
}
}
Shader "Skybox/MoonWithTransparentSkybox"
{
Properties
{
_MoonTex ("Moon Texture", 2D) = "white" {}
_SkyMap ("Sky Map", Cube) = "" {}
_MoonDirection ("Moon Direction", Vector) = (0.4, 0.8, 0, 0)
_MoonScale ("Moon Scale", Range(0.01, 1.0)) = 0.2
_TransparencyThreshold ("Sky Transparency Threshold", Range(0.01, 1.0)) = 0.2
}
SubShader
{
Tags{ "Queue" = "Background"
"RenderType" = "Background"
"PreviewType" = "Skybox" }
Cull Back
Lighting Off
ZWrite Off
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 viewDir : TEXCOORD1;
};
sampler2D _MoonTex;
float4 _MoonTex_ST;
float4 _MoonDirection;
float _MoonScale;
uniform samplerCUBE _SkyMap;
float _TransparencyThreshold;
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.viewDir = mul(unity_ObjectToWorld, v.vertex).xyz - _WorldSpaceCameraPos;
o.uv = TRANSFORM_TEX(v.uv, _MoonTex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
fixed4 backCol = texCUBE(_SkyMap, (i.viewDir));
fixed3 moonVector = normalize(_MoonDirection.xyz);
fixed3 diff = normalize(i.viewDir) - moonVector; //difference between view dir and moon dir
//side vector
fixed3 sideVector = normalize(cross(fixed3(0,1,0), moonVector));
//up vector
fixed3 upVector = normalize(cross(sideVector, moonVector));
fixed2 moonUV = (fixed2(dot(sideVector,diff),dot(upVector,diff))/_MoonScale) +0.5;
fixed4 moonColour = tex2D(_MoonTex, moonUV);
moonColour.a = moonColour.a * (dot(normalize(i.viewDir), _MoonDirection) > 0); //Make sure only one moon
fixed skyAlpha = (backCol.a-_TransparencyThreshold)/(1-_TransparencyThreshold);
//Uses threshold because compression algorithms often discard RGB data if it was really completely transparent
return lerp(backCol, lerp(moonColour,backCol,skyAlpha), moonColour.a);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment