0% found this document useful (0 votes)
11 views2 pages

Star Field

The document defines a ShaderUtils struct that includes methods for smooth stepping, generating random values, and rendering a star field effect in a shader. It utilizes mathematical functions to manipulate positions and create visual effects based on time and mouse input. The final output is a color value calculated from the star field rendering process.

Uploaded by

Bao Le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Star Field

The document defines a ShaderUtils struct that includes methods for smooth stepping, generating random values, and rendering a star field effect in a shader. It utilizes mathematical functions to manipulate positions and create visual effects based on time and mouse input. The final output is a color value calculated from the star field rendering process.

Uploaded by

Bao Le
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#define PI 3.

14159265359

struct ShaderUtils {
float SmoothStep(float a, float b, float t) {
t = saturate((t - a) / (b - a));
return t * t * (3.0 - 2.0 * t);
}

float Random21(float2 p) {
p = frac(p * float2(0.59, 0.28));
p += dot(p, p + 0.74);
return frac(p.x * p.y);
}

float2 Random22(float2 p) {
float n = Random21(p);
return float2(n, Random21(p + n));
}

float Line(float2 p, float2 a, float2 b) {


float2 pa = p - a;
float2 ba = b - a;
float t = clamp(dot(pa, ba) / dot(ba, ba), 0.0, 1.0);

float d = length(a - b);

float L = SmoothStep(0.03, 0.001, length(pa - ba * t));


L *= SmoothStep(1.2, 1.0, d) * 0.5 + SmoothStep(0.1, 0.099, abs(d - 1.0));

return L;
}

float2 GetPosition(float2 id, float2 offset, float time) {


float2 n = Random22(id + offset) * (time + 10.0);
return (offset + sin(n) * 0.4);
}

float StarField(float2 uv, float time) {


float2 gv = frac(uv) - 0.5;
float2 id = floor(uv);
float m = 0.0;

float2 ps[9];
int i = 0;

for (float y = -1.0; y <= 1.0; y++) {


for (float x = -1.0; x <= 1.0; x++) {
ps[i++] = GetPosition(id, float2(x, y), time);
}
}

for (i = 0; i < 9; i++) {


m += Line(gv, ps[4], ps[i]);
float sparkle = 0.0025 / length(dot(ps[i] - gv, ps[i] - gv));

m += sparkle;
}

m += Line(gv, ps[1], ps[3]);


m += Line(gv, ps[1], ps[5]);
m += Line(gv, ps[3], ps[7]);
m += Line(gv, ps[5], ps[7]);

return m;
}
};

ShaderUtils utils;

float3 col = float3(0.0, 0.0, 0.0);


float2 mouse = float2(0.5, 0.5); // Replace with dynamic inputs if necessary

float3 m = float3(0.0, 0.0, 0.0);


float s = sin(iTime * 0.1);
float c = cos(iTime * 0.1);

float2x2 rot = float2x2(c, -s, s, c);


uv = mul(rot, uv);

for (float i = 0.0; i <= 2.0; i += 1.0 / 4.0) {


float z = frac(i + iTime * 0.1);
float size = lerp(10.0, 0.5, z);
float fade = utils.SmoothStep(0.0, 0.1, z) * utils.SmoothStep(1.0, 0.8, z);

m += utils.StarField(uv * size + i * 20.0 - mouse, iTime) * fade;


}

col = m;
return float4(col, 1.0);

You might also like