Files
spacerace/Assets/Materials/Skybox Shaders/LiquidStar.shader
T
2026-03-27 17:50:58 +11:00

234 lines
8.6 KiB
GLSL

Shader "Unlit/LiquidStar"
{
Properties
{
_Speed("Flow Speed", Range(0.01, 5.0)) = 0.8
_Scale("Surface Scale", Range(0.5, 20.0)) = 3.0
_BaseColor("Base Color", Color) = (1.0, 0.1, 0.0, 1.0)
_HotColor("Hot Spot Color", Color) = (1.0, 0.9, 0.2, 1.0)
_SpotColor("Sunspot Color", Color) = (0.3, 0.0, 0.0, 1.0)
_SpotScale("Sunspot Scale", Range(1.0, 15.0)) = 5.0
_SpotThreshold("Sunspot Threshold", Range(0.0, 1.0)) = 0.45
_GlowIntensity("Glow Intensity", Range(1.0, 20.0)) = 6.0
_Turbulence("Turbulence", Range(0.0, 3.0)) = 1.2
_EdgeSoftness("Edge Softness", Range(0.01, 0.3)) = 0.08
_CoronaSize("Corona Size", Range(0.0, 0.4)) = 0.15
_CoronaIntensity("Corona Intensity", Range(0.0, 10.0)) = 3.0
}
SubShader
{
Tags { "RenderType"="Opaque" "Queue"="Geometry" }
LOD 100
ZWrite On
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal : NORMAL;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float3 worldNormal : TEXCOORD1;
float3 worldPos : TEXCOORD2;
};
float _Speed;
float _Scale;
float4 _BaseColor;
float4 _HotColor;
float4 _SpotColor;
float _SpotScale;
float _SpotThreshold;
float _GlowIntensity;
float _Turbulence;
float _EdgeSoftness;
float _CoronaSize;
float _CoronaIntensity;
// 3D simplex-style noise
float3 mod289(float3 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
float4 mod289(float4 x) { return x - floor(x * (1.0 / 289.0)) * 289.0; }
float4 permute(float4 x) { return mod289(((x * 34.0) + 10.0) * x); }
float4 taylorInvSqrt(float4 r) { return 1.79284291400159 - 0.85373472095314 * r; }
float snoise(float3 v)
{
const float2 C = float2(1.0 / 6.0, 1.0 / 3.0);
const float4 D = float4(0.0, 0.5, 1.0, 2.0);
float3 i = floor(v + dot(v, C.yyy));
float3 x0 = v - i + dot(i, C.xxx);
float3 g = step(x0.yzx, x0.xyz);
float3 l = 1.0 - g;
float3 i1 = min(g.xyz, l.zxy);
float3 i2 = max(g.xyz, l.zxy);
float3 x1 = x0 - i1 + C.xxx;
float3 x2 = x0 - i2 + C.yyy;
float3 x3 = x0 - D.yyy;
i = mod289(i);
float4 p = permute(permute(permute(
i.z + float4(0.0, i1.z, i2.z, 1.0))
+ i.y + float4(0.0, i1.y, i2.y, 1.0))
+ i.x + float4(0.0, i1.x, i2.x, 1.0));
float n_ = 0.142857142857;
float3 ns = n_ * D.wyz - D.xzx;
float4 j = p - 49.0 * floor(p * ns.z * ns.z);
float4 x_ = floor(j * ns.z);
float4 y_ = floor(j - 7.0 * x_);
float4 x = x_ * ns.x + ns.yyyy;
float4 y = y_ * ns.x + ns.yyyy;
float4 h = 1.0 - abs(x) - abs(y);
float4 b0 = float4(x.xy, y.xy);
float4 b1 = float4(x.zw, y.zw);
float4 s0 = floor(b0) * 2.0 + 1.0;
float4 s1 = floor(b1) * 2.0 + 1.0;
float4 sh = -step(h, float4(0, 0, 0, 0));
float4 a0 = b0.xzyw + s0.xzyw * sh.xxyy;
float4 a1 = b1.xzyw + s1.xzyw * sh.zzww;
float3 p0 = float3(a0.xy, h.x);
float3 p1 = float3(a0.zw, h.y);
float3 p2 = float3(a1.xy, h.z);
float3 p3 = float3(a1.zw, h.w);
float4 norm = taylorInvSqrt(float4(dot(p0,p0), dot(p1,p1), dot(p2,p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
float4 m = max(0.6 - float4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
m = m * m;
return 42.0 * dot(m * m, float4(dot(p0,x0), dot(p1,x1), dot(p2,x2), dot(p3,x3)));
}
// Fractal brownian motion
float fbm(float3 p, int octaves)
{
float value = 0.0;
float amplitude = 0.5;
float frequency = 1.0;
for (int i = 0; i < octaves; i++)
{
value += amplitude * snoise(p * frequency);
amplitude *= 0.5;
frequency *= 2.0;
}
return value;
}
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
o.worldNormal = UnityObjectToWorldNormal(v.normal);
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
return o;
}
float4 frag(v2f i) : SV_Target
{
// Remap UV to centered [-1, 1] range
float2 centeredUV = i.uv * 2.0 - 1.0;
// Shrink the star so disk + corona fits inside the quad
float starRadius = 1.0 / (1.0 + _CoronaSize);
float dist = length(centeredUV) / starRadius;
// Discard outside the circle + corona
if (dist > 1.0 + _CoronaSize) discard;
float time = _Time.y * _Speed;
// Use UV-based 3D coordinate for noise (flat plane, no sphere artifacts)
float3 p = float3(centeredUV * _Scale, time * 0.1);
// Liquid flow - domain warping (4 octaves for island edges)
float3 q = float3(
fbm(p + float3(0.0, 0.0, time * 0.3), 4),
fbm(p + float3(5.2, 1.3, time * 0.25), 4),
fbm(p + float3(1.7, 9.2, time * 0.35), 4)
);
// Inner layers - low octaves for smooth fills
float3 r = float3(
fbm(p + _Turbulence * q + float3(1.7, 9.2, time * 0.15), 1),
fbm(p + _Turbulence * q + float3(8.3, 2.8, time * 0.2), 1),
fbm(p + _Turbulence * q + float3(3.1, 6.5, time * 0.1), 1)
);
float liquidPattern = fbm(p + _Turbulence * r, 1);
// Sunspots - low frequency
float3 spotP = float3(centeredUV * _SpotScale, time * 0.05);
float spotNoise = fbm(spotP + float3(time * 0.05, time * 0.03, time * 0.07), 2);
// Darken areas where noise is below threshold = sunspots
float spotMask = smoothstep(_SpotThreshold - 0.15, _SpotThreshold, spotNoise);
// Convection cells - granulation pattern
float granulation = snoise(float3(centeredUV * _Scale * 3.0, time * 0.4));
granulation = granulation * 0.5 + 0.5;
// Blend colors
float heatMap = liquidPattern * 0.5 + 0.5;
heatMap = saturate(heatMap + granulation * 0.2);
// Base liquid surface color
float3 col = lerp(_BaseColor.rgb, _HotColor.rgb, pow(heatMap, 1.5));
// Apply sunspots (dark cooler regions)
col = lerp(_SpotColor.rgb, col, spotMask);
// Limb darkening - darken edges of the star disk
float limbDarken = 1.0 - pow(dist, 2.0);
col *= max(limbDarken, 0.3);
// HDR glow intensity
col *= _GlowIntensity;
// Outside the star disk: corona fading to black
if (dist > 1.0)
{
float coronaDist = (dist - 1.0) / _CoronaSize;
float coronaFade = (1.0 - coronaDist) * (1.0 - coronaDist);
col = lerp(_BaseColor.rgb, _HotColor.rgb, 0.7) * _CoronaIntensity * coronaFade;
}
else if (dist > 1.0 - _EdgeSoftness)
{
// Soft edge of star disk
float edgeFade = smoothstep(1.0, 1.0 - _EdgeSoftness, dist);
col = lerp(float3(0, 0, 0), col, edgeFade);
}
return float4(col, 1.0);
}
ENDCG
}
}
}