Created
June 26, 2023 13:47
-
-
Save IonelPopJara/2429db11389b8a0d23db661139fa59c5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Custom/Starbox" | |
{ | |
Properties | |
{ | |
// Color gradient | |
_SkyColor ("Sky Color", Color) = (1, 1, 1, 1) | |
_HorizonColor ("Horizon Color", Color) = (1, 1, 1, 1) | |
_ColorStart ("Color Start", Range(0, 1)) = 0 | |
_ColorEnd ("Color End", Range(0, 1)) = 1 | |
// stars noise | |
_Speed ("Speed", Range(0, 100)) = 1 | |
_Size ("Size", Range(0, 100)) = 4 | |
_TilingOffset ("Tiling Offset", Vector) = (8, 2, 0, 0) | |
_Power ("Power", Range(50, 200)) = 100 | |
} | |
SubShader | |
{ | |
Tags { "RenderType" = "Background" } | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
float4 _SkyColor; | |
float4 _HorizonColor; | |
float _ColorStart; | |
float _ColorEnd; | |
float _Speed; | |
float _Size; | |
float4 _TilingOffset; | |
float _Power; | |
struct MeshData | |
{ | |
float4 vertex : POSITION; | |
float3 normals: NORMAL; | |
float4 uv0: TEXCOORD0; | |
}; | |
struct Interpolators | |
{ | |
float4 vertex : SV_POSITION; | |
float3 worldPos : TEXCOORD0; | |
float3 normal : TEXCOORD1; | |
float2 uv : TEXCOORD2; | |
}; | |
Interpolators vert (MeshData v) | |
{ | |
Interpolators o; | |
o.vertex = UnityObjectToClipPos(v.vertex); | |
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; | |
o.normal = UnityObjectToWorldNormal(v.normals); | |
o.uv = v.uv0; | |
return o; | |
} | |
float2 noise2x2(float2 p) | |
{ | |
float x = dot(p, float2(123.4, 234.5)); | |
float y = dot(p, float2(345.6, 456.7)); | |
float2 noise = float2(x, y); | |
noise = sin(noise); | |
noise = noise * 43748.5453; | |
noise = frac(noise); | |
return noise; | |
} | |
float InverseLerp (float a, float b, float v) | |
{ | |
return (v - a) / (b - a); | |
} | |
float4 frag (Interpolators i) : SV_Target | |
{ | |
// Calculate UV | |
float3 normalizedWorldPos = normalize(i.worldPos); | |
float2 uv; | |
uv.y = asin(normalizedWorldPos.y) / (0.5 * 3.14159); // Arcsine of Y | |
uv.x = atan2(normalizedWorldPos.x, normalizedWorldPos.z) /(6.28318); // Arctangent2 of X and Z | |
// Apply tiling and offeset | |
uv = uv * _TilingOffset.xy + _TilingOffset.zw; | |
// stars | |
float2 uv1 = uv * _Size; | |
float2 currentGridId = floor(uv1); | |
float2 currentGridCoord = frac(uv1); | |
currentGridCoord = currentGridCoord - 0.5; | |
float pointsOnGrid = 0; | |
float minDistFromPixel = 100; | |
float2 cameraPos = UnityObjectToViewPos(i.vertex).xy; | |
float2 screenPos = (cameraPos / cameraPos.y) * 0.5 + 0.5; | |
float2 jitter = noise2x2(currentGridId + floor(screenPos)); | |
float2 offset = jitter * _Speed * _Time.y; | |
for (float a = -1.0; a <= 1.0; a++) | |
{ | |
for (float b = -1.0; b <= 1.0; b++) | |
{ | |
float2 adjGridCoords = float2(a, b); | |
float2 pointOnAdjGrid = adjGridCoords; | |
// Randomizing | |
float2 noise = noise2x2(currentGridId + adjGridCoords); | |
pointOnAdjGrid = adjGridCoords + sin(_Time.y * noise * _Speed) * 0.5; | |
float dist = length(currentGridCoord - pointOnAdjGrid); | |
minDistFromPixel = min(dist, minDistFromPixel); | |
pointsOnGrid += smoothstep(0.95, 0.96, 1.0 - dist); | |
} | |
} | |
float stars = minDistFromPixel; | |
stars = saturate(stars); | |
stars = 1 - stars; | |
stars = pow(stars, _Power); | |
// SKY | |
// Blend between two colors based on the uv coordinates | |
float t = saturate(InverseLerp(_ColorStart, _ColorEnd, uv.y)); | |
float4 skyColor = lerp(_HorizonColor, _SkyColor, t); | |
// return skyColor + stars; | |
return skyColor; | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment