Skip to content

Instantly share code, notes, and snippets.

@partybusiness
Last active December 29, 2020 23:22
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/eb6c75157b1d50e3b3418c9fcef978ca to your computer and use it in GitHub Desktop.
Save partybusiness/eb6c75157b1d50e3b3418c9fcef978ca to your computer and use it in GitHub Desktop.
Shader "Skybox/SpaceWarp"
{
Properties{
_Color("Star Colour", Color) = (0.5,0.5,0.5,1)
_MidColor("Midground Colour", Color) = (0.5,0.5,0.5,1)
_BackColor("Background Colour", Color) = (0.5,0.5,0.5,1)
_FadeColor("Fade Colour", Color) = (0.5,0.5,0.5,1)
_GridSize("Grid Size", int) = 50
_MinStarSize("Min Star Size", Range(0.0, 1.0)) = 0.1
_MaxStarSize("Max Star Size", Range(0.0, 1.0)) = 0.3
_PowCurve("Power Curve", float ) = 1.2
_Speed("Speed", float) = 1
}
SubShader{
Tags{ "Queue" = "Background" "RenderType" = "Background" "PreviewType" = "Skybox" }
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
float _GridSize;
float _Speed;
float4 _Color;
float4 _MidColor;
float4 _BackColor;
float4 _FadeColor;
float _MinStarSize;
float _MaxStarSize;
float _PowCurve;
struct vertexInput {
float4 vertex : POSITION;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float3 viewDir : TEXCOORD0;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
float4x4 modelMatrix = unity_ObjectToWorld;
float4x4 modelMatrixInverse = unity_WorldToObject;
output.viewDir = (mul(modelMatrix, input.vertex).xyz - _WorldSpaceCameraPos);
output.pos = UnityObjectToClipPos(input.vertex);
return output;
}
fixed2 UVsForTube(fixed3 viewDir) {
float3 oc = normalize(viewDir);
float dist = pow(length(oc.yz),_PowCurve);
float angle = atan2(oc.y, oc.z) / (3.14159 * 2);
return fixed2(oc.x / (dist*100), angle);
}
fixed2 RoundUVs(fixed2 uv, int size) {
uv.y = ((uv.y+0.5)%1)-0.5; //avoids a line where angle wraps around
return ((floor(uv*size) / size));
}
fixed2 TileUVs(fixed2 uv, fixed2 rounded) {
return ((uv - rounded)%1)*round(_GridSize);
}
fixed3 Random(fixed2 uv) {
return fixed3(
((sin(dot(uv, fixed2(12.9898, 78.233)))+1) * 43758.5453) % 1,
((sin(dot(uv, fixed2(77.0884, 50.171)))+1) * 38322.7693) % 1,
((sin(dot(uv, fixed2(19.2709, 45.893)))+1) * 26691.9734) % 1
);
}
fixed2 GetDistances(fixed3 viewDir, fixed2 offset, fixed randomOffset) {
fixed2 tubeV = UVsForTube(viewDir) + offset;
fixed2 roundUV = RoundUVs(tubeV, _GridSize);
fixed2 tileUV = TileUVs(tubeV, roundUV);
//use random values for position and radius of star
fixed3 random = Random(roundUV + randomOffset);
fixed radius = lerp(_MinStarSize, _MaxStarSize, random.z);
fixed2 starPos = lerp(0.0 + radius, 1.0 - radius, random.xy);
fixed starDist = length(tileUV - starPos);
fixed edgeDistance = 1-max(max(1-tileUV.x,tileUV.x), max(1 - tileUV.y, tileUV.y));
return fixed2(smoothstep(radius, 0, starDist), (edgeDistance*smoothstep(radius * 3, 0, starDist)));
}
float4 frag(vertexOutput input) : COLOR
{
fixed2 distances1 = GetDistances(input.viewDir, fixed2(_Time.x*_Speed, 0), 432);
fixed2 distances2 = GetDistances(input.viewDir, fixed2(_Time.x*_Speed*1.3 + 0.5 / _GridSize, 0.25/_GridSize), 17);
fixed midStrength = max(distances1.y, distances2.y);
fixed starStrength = max(distances1.x, distances2.x);
//also fade out near vanishing points
fixed vanishFade = smoothstep(0.97,0.999,abs(normalize(input.viewDir).x));
return lerp(lerp(lerp(_BackColor, _MidColor, midStrength), _Color, starStrength), _FadeColor, vanishFade);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment