Skip to content

Instantly share code, notes, and snippets.

@ufna
Created December 21, 2021 12:14
Show Gist options
  • Save ufna/22d9dc2ee14b0f397c8c7055749a9ce5 to your computer and use it in GitHub Desktop.
Save ufna/22d9dc2ee14b0f397c8c7055749a9ce5 to your computer and use it in GitHub Desktop.
Phantom Star for CineShader (UE4 version)
const MaterialFloat pi = acos(-1.0);
const MaterialFloat pi2 = pi*2.0;
struct Functions
{
float mod(float x, float y)
{
return x - y * floor(x/y);
}
float2x2 rot(MaterialFloat a) {
MaterialFloat c = cos(a), s = sin(a);
return float2x2(c,s,-s,c);
}
MaterialFloat2 pmod(MaterialFloat2 p, MaterialFloat r) {
MaterialFloat a = atan2(p.x, p.y) + pi/r;
MaterialFloat n = pi2 / r;
a = floor(a/n)*n;
return mul(rot(-a), p);
}
MaterialFloat box( MaterialFloat3 p, MaterialFloat3 b ) {
MaterialFloat3 d = abs(p) - b;
return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
}
MaterialFloat ifsBox(MaterialFloat3 p) {
for (int i=0; i<5; i++) {
p = abs(p) - 1.0;
p.xy = mul(rot(iTime*0.3), p.xy);
p.xz = mul(rot(iTime*0.1), p.xz);
}
p.xz = mul(rot(iTime), p.xz);
return box(p, MaterialFloat3(0.4,0.8,0.3));
}
MaterialFloat map(MaterialFloat3 p, MaterialFloat3 cPos) {
MaterialFloat3 p1 = p;
p1.x = mod(p1.x-5., 10.) - 5.;
p1.y = mod(p1.y-5., 10.) - 5.;
p1.z = mod(p1.z, 16.)-8.;
p1.xy = pmod(p1.xy, 5.0);
return ifsBox(p1);
}
};
Functions func;
MaterialFloat2 p = (fragCoord.xy * 2.0 - iResolution.xy) / min(iResolution.x, iResolution.y);
//MaterialFloat2 p = (2.0*fragCoord-iResolution.xy)/iResolution.y;
MaterialFloat3 cPos = MaterialFloat3(0.0,0.0, -3.0 * iTime);
// MaterialFloat3 cPos = MaterialFloat3(0.3*sin(iTime*0.8), 0.4*cos(iTime*0.3), -6.0 * iTime);
MaterialFloat3 cDir = normalize(MaterialFloat3(0.0, 0.0, -1.0));
MaterialFloat3 cUp = MaterialFloat3(sin(iTime), 1.0, 0.0);
MaterialFloat3 cSide = cross(cDir, cUp);
MaterialFloat3 ray = normalize(cSide * p.x + cUp * p.y + cDir);
// Phantom Mode https://www.shadertoy.com/view/MtScWW by aiekick
MaterialFloat acc = 0.0;
MaterialFloat acc2 = 0.0;
MaterialFloat t = 0.0;
for (int i = 0; i < 99; i++) {
MaterialFloat3 pos = cPos + ray * t;
MaterialFloat dist = func.map(pos, cPos);
dist = max(abs(dist), 0.02);
MaterialFloat a = exp(-dist*3.0);
if (func.mod(length(pos)+24.0*iTime, 30.0) < 3.0) {
a *= 2.0;
acc2 += a;
}
acc += a;
t += dist * 0.5;
}
MaterialFloat3 col = MaterialFloat3(acc * 0.01, acc * 0.011 + acc2*0.002, acc * 0.012+ acc2*0.005);
return MaterialFloat4(col, 1.0 - t * 0.03);
@ufna
Copy link
Author

ufna commented Dec 21, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment