ja lisää optimointeja

This commit is contained in:
2024-07-29 08:56:13 +03:00
parent f5cfde0373
commit e8f64b78bb
2 changed files with 251 additions and 280 deletions

View File

@ -1,12 +1,11 @@
//precision mediump float;
precision mediump float;
uniform vec2 u_resolution;
uniform float u_time;
const float PI = 22./7.;
// Rotate
mat2 rot2D(float angle) {
float s = sin(angle);
float c = cos(angle);
float s = sin(angle), c = cos(angle);
return mat2(c, -s, s, c);
}
@ -18,15 +17,15 @@ float smax( float a, float b, float k )
float hash(vec2 p)
{
p = 50.*fract( p*0.3183099);
p = 50.*fract( p*0.3183099);
return fract( p.x*p.y*(p.x+p.y) );
}
float noise( in vec2 p, float scale )
float noise(vec2 p, float scale )
{
vec2 i = floor( p );
vec2 f = fract( p );
vec2 u = f*f*(3.-2.*f);
vec2 i = floor( p ),
f = fract( p ),
u = f*f*(3.-2.*f);
return -scale+scale*mix( mix( hash( i + vec2(0.0,0.0) ),
hash( i + vec2(1.0,0.0) ), u.x),
mix( hash( i + vec2(0.0,1.0) ),
@ -51,7 +50,7 @@ float sdCappedCylinder( vec3 p, float h, float r )
return min(max(d.x,d.y),0.0) + length(max(d,0.0));
}
float sdBox( in vec2 p, in vec2 r )
float sdBox(vec2 p, vec2 r)
{
return length( max(abs(p)-r,0.) );
}
@ -85,39 +84,34 @@ const vec3 glyph7Pos = vec3(1.7 * -1.,-1.1,0.0);
*/
// DELAYS
const float STARTDELAY = 9.;
const float glyphPhase = 2.;
float STARTDELAY = 9.,
glyphPhase = 2.;
// COLORS
vec3 fogColor1 = vec3(.4, .2, .4); // fog color1
vec3 fogColor2 = vec3(.7, .4, .2); // fog color2
vec3 indirColor = vec3(.2, .1, .3); // indirect light color
vec3 light1Color = vec3(.9, .5, .7);
vec3 light2Color = vec3(.8, .7, .5);
vec3 chevronColor = vec3(.9, .3, 0.);
vec3 fogColor1 = vec3(.4, .2, .4), // fog color1
fogColor2 = vec3(.7, .4, .2), // fog color2
indirColor = vec3(.2, .1, .3), // indirect light color
light1Color = vec3(.9, .5, .7),
light2Color = vec3(.8, .7, .5),
chevronColor = vec3(.9, .3, 0.);
// with duration d, startTime s and speed up with timeFact
float timedSine(float maxCycles, float startTime, float timeFact) {
startTime *= timeFact;
float phase = ((u_time * timeFact) - startTime) / PI;
return PI * min(phase, maxCycles);
float timedSine(vec3 i) {
i.y *= i.z;
return PI * min(((u_time * i.z) - i.y) / PI, i.x);
}
float calcFactor (float startTime)
{
float factor = 9. + 9.*clamp(sin(timedSine(1.5, startTime, 4.) * 0.06), -.9, .9);
return factor;
return 9. + 9.*clamp(sin(timedSine(vec3(1.5, startTime, 4.)) * 0.06), -.9, .9);
}
// Animate ring movements
vec3 ringAnim( in vec3 p) {
vec3 ringAnim(vec3 p) {
for (float i = 0.; i < 7.; i++) {
float rotateDelay = STARTDELAY + (i * glyphPhase);
if(u_time > rotateDelay) {
float o = 1.;
if(mod(i,2.) >= 1.) {
o = -1.;
}
float rotateDelay = STARTDELAY + (i * glyphPhase), o=1.;
if(u_time > rotateDelay) {
if(mod(i,2.) >= 1.) o = -1.;
p.xy *= rot2D(calcFactor(rotateDelay)*o);
}
}
@ -125,32 +119,30 @@ vec3 ringAnim( in vec3 p) {
}
// Animate prism movements
float prismAnim(in float x, in float delay) {
if(u_time >= delay) {
x += (.045*clamp(sin((timedSine(3., delay, 40.) * .4)),-.8, .8));
float prismAnim(vec2 i) {
if(u_time >= i.y) {
i.x += (.045*clamp(sin((timedSine(vec3(3., i.y, 40.)) * .4)),-.8, .8));
}
return x;
return i.x;
}
//////////////
// SCENE //
//////////////
vec2 map(in vec3 p)
vec2 map(vec3 p)
{
float mat = 0.;
// Stargate
float mat = 0.,
// Ring boxes
float an = PI/12.;
float anRot = floor(atan(p.y,p.x)/an + .5)*an;
an = PI/12.,
anRot = floor(atan(p.y,p.x)/an + .5)*an;
vec3 q = p;
q.xy = mat2(cos(anRot),-sin(anRot),
sin(anRot), cos(anRot))*q.xy;
float d = sdBox( q.xy - vec2(1.8,0.), vec2(0.24,0.14) ) - .02;
float d = sdBox( q.xy - vec2(1.8,0.), vec2(0.24,0.14) ) - .02,
// Main ring
float d2 = abs(length(p.xy) - 1.8) - .2;
d2 = abs(length(p.xy) - 1.8) - .2;
d = min(d,d2);
// Inner ring
@ -162,6 +154,7 @@ vec2 map(in vec3 p)
d = smax( d, abs(p.z)-.1, .02 );
// Prisms
for(float i = 0.; i < 8.; i++ ) {
an = 24.67 / (PI * 2.); // sector size
q = p;
@ -178,7 +171,7 @@ vec2 map(in vec3 p)
if (i > 0.) {
q.x -= 1.95;
vec3 q2 = q; // new point for movable parts
if(u_time > rotateDelay) q2.x = prismAnim(q2.x, rotateDelay);
if(u_time > rotateDelay) q2.x = prismAnim(vec2(q2.x, rotateDelay));
float prismTop = sdTriPrism(vec3(q.x - .06, q.y, q.z), vec2(.1,.15), .5)-.01;
d2 = max(- sdTriPrism(vec3(q2.x - .14, q2.y - 0. , q2.z), vec2(.22,.22), .5) - .01 , sdTriPrism(vec3(q2.x, q2.y, q2.z ), vec2(.2,.12), .5) - .02);
d2 = min(d2, prismTop);
@ -191,8 +184,7 @@ vec2 map(in vec3 p)
}
//Rotating glyphs
vec3 p2 = ringAnim(p);
vec3 q2 = p2;
vec3 p2 = ringAnim(p), q2=p2;
an = PI/16.;
q2.xy = rot2D(floor((atan(p2.y,p2.x)/an) + .5)*an)*q2.xy;
d2 = sdBox2( q2.xyz - vec3(1.75,0.,0.), vec3(.04, .14, .05) ) - .02;
@ -227,21 +219,18 @@ vec2 map(in vec3 p)
////////////////
float rayMarch(vec3 ro, vec3 rd) {
float t = 0.; // total distance travelled
float d;
float d,t = 0.; // total distance travelled
// Raymarching
for (int i = 0; i < 90; i++) {
vec3 p = ro + rd * t; // "cast" rays
d = map(p).x; // Get distance to objects
for (int i = 0; i < 90; i++) {
d = map(ro + rd * t).x; // Get distance to objects
t += d; // "march" the ray
if (abs(d) < .002 || t > 400.) break;
}
return t;
}
vec3 getNormal(vec3 p) {
float d = map(p).x;
vec3 getNormal(vec3 p) {
vec2 e = vec2(.01, 0.);
vec3 n = d - vec3(
vec3 n = map(p).x - vec3(
map(p-e.xyy).x,
map(p-e.yxy).x,
map(p-e.yyx).x);
@ -249,20 +238,17 @@ vec3 getNormal(vec3 p) {
}
float getLight(vec3 p, vec3 lightPos, float intensity, float shadow) {
vec3 l = normalize(lightPos - p);
vec3 n = getNormal(p);
float dif = clamp(dot(n, l), 0., intensity);
// Shadows
float d = rayMarch(p+n*.0025, l);
vec3 l = normalize(lightPos - p), n = getNormal(p);
float dif = clamp(dot(n, l), 0., intensity),
d = rayMarch(p+n*.0025, l);
if(abs(d)<length(lightPos-p)) dif *= shadow;
return dif;
}
vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float b ) {
float sunAmount = max( dot(rd, lightDir), 0. );
vec3 fogColor = mix( fogColor1, // blue
fogColor2, // yellow
pow(sunAmount,8.));
vec3 applyFog(vec3 col, float t, vec3 rd, vec3 lightDir, float b ) {
vec3 fogColor = mix( fogColor1, // blue
fogColor2, // yellow
pow(max( dot(rd, lightDir), 0.) ,8.));
return mix( col, fogColor, 1.0 - exp(-t*b) );
}
@ -278,9 +264,8 @@ vec3 getCameraRayDir(vec2 uv, vec3 p, vec3 l, float z)
}
vec3 postProcess(vec3 col) {
// Vignette
float radius = .9;
col *= smoothstep(radius, radius-.4, length(gl_FragCoord.xy/u_resolution.xy-vec2(.5)));
// Vignette
col *= smoothstep(0.9, 0.5, length(gl_FragCoord.xy/u_resolution.xy-vec2(.5)));
// Colour mapping
col *= vec3(.9, 0.8, 0.7);
// gamma
@ -295,10 +280,9 @@ vec3 cameraPos()
// first zoom in to the gate
vec3 cPos = vec3(0., clamp(6.-u_time,2.,6.), clamp((75.-u_time*8.),6.,75.));
if (u_time > 7.5) cPos += vec3(0., clamp(7.5-u_time,-1.,0.), 0.);
// close up shot for 1st glyph lock
if (u_time > 10.5) cPos = vec3(2.,-1.,1.);
//zoom away for 2nd glyph animation
// zoom away for 2nd glyph animation
if (u_time > 12.) cPos = vec3(-1., -1., 4.);
//close up shot for the 2nd glyph lock
@ -321,10 +305,9 @@ vec3 cameraPointAt() {
}
// flash screen with glyph color when it is locked
vec3 colorFlashesAnim(in vec3 col) {
float delay = 10.75;
if(u_time > delay) {
float x = smoothstep(-1.,.8,sin((timedSine(.8, delay, 8.)*2.)));
vec3 colorFlashesAnim(vec3 col) {
if(u_time > 10.75) {
float x = smoothstep(-1.,.8,sin((timedSine(vec3(.8, 10.75, 8.))*2.)));
col = mix(col, chevronColor * (x * 1.4), x *.76);
}
return col;
@ -333,15 +316,15 @@ vec3 colorFlashesAnim(in vec3 col) {
vec3 sceneGate(vec2 uv)
{
// Initialization
vec3 ro = cameraPos();
vec3 rd = getCameraRayDir(uv, ro, cameraPointAt(), 2.); // ray direction
vec3 col = vec3(0.); // color
vec3 ro = cameraPos(),
rd = getCameraRayDir(uv, ro, cameraPointAt(), 2.),
col = vec3(0.);
float d = rayMarch(ro, rd);
if (d < 150.) {
// Lighting
vec3 p = ro + rd * d;
float mat = map(p).y;
float mat = map(p).y,
// Light 1 Arguments
// 1: Ray starting point
@ -350,7 +333,7 @@ vec3 sceneGate(vec2 uv)
// 4: Shadow intensity
// Light 2
float dif = getLight(p, vec3( 10., 15., 25.), 1., .2);
dif = getLight(p, vec3( 10., 15., 25.), 1., .2);
col += dif * light1Color;
dif = getLight(p, vec3( 4., 2., -15.), 1., 1.);
@ -372,16 +355,14 @@ vec3 sceneGate(vec2 uv)
else if(mat ==5.)
col *= chevronColor; // activated glyph color
}
float fogAmount = .01;
col = applyFog(col, d, rd, vec3(0., -.1, -1.), fogAmount);
}
col = applyFog(col, d, rd, vec3(0., -.1, -1.), .01);
col = colorFlashesAnim(col); // animated color flash
return postProcess(col);
}
void pMod1(inout float p, float size) {
float halfsize = size*0.5;
p = mod(p + halfsize, size) - halfsize;
p = mod(p + size*0.5, size) - size*0.5;
}
void getQ(inout vec3 q, float i, float speed, float radius) {
@ -393,8 +374,7 @@ void getQ(inout vec3 q, float i, float speed, float radius) {
}
vec2 map2(vec3 pos) {
float d = 1e3;
float mat;
float d = 1e3, mat;
pMod1(pos.z, 1.);
for (float i = 0.; i < 90.; i++) {
vec3 q = pos;
@ -412,19 +392,17 @@ vec2 map2(vec3 pos) {
}
vec3 palette(float t) {
vec3 a = vec3(0.5, 0.5, 0.5);
vec3 b = vec3(1.0, 1.0, 1.0);
vec3 c = vec3(1.0, 1.0, 1.0);
vec3 d = vec3(0.0, 0.66, 0.33);
vec3 a = vec3(0.5, 0.5, 0.5),
b = vec3(1.0, 1.0, 1.0),
c = vec3(1.0, 1.0, 1.0),
d = vec3(0.0, 0.66, 0.33);
return a + b * cos(6.3 * (c*t+d));
}
float rayMarch2(vec3 ro, vec3 rd) {
float t = 0.;
float d;
for (int i = 0; i < 60; i++) {
vec3 p = ro + rd * t;
d = map2(p).x;
float t = 0.,d;
for (int i = 0; i < 60; i++) {
d = map2(ro + rd * t).x;
t += d;
if (d < .002 || t > 20.) break;
}
@ -432,12 +410,11 @@ float rayMarch2(vec3 ro, vec3 rd) {
}
vec3 sceneTunnel(vec2 uv) {
vec3 ro = vec3(0.,0.,u_time*2. );
vec3 rd = normalize(vec3(uv, .7));
float d = rayMarch2(ro, rd);
vec3 p = ro + rd * d;
float mat = map2(p).y;
vec3 col = vec3(0., 0., .1);
vec3 ro = vec3(0.,0.,u_time*2. ),
col = vec3(0., 0., .1),
rd = normalize(vec3(uv, .7));
float d = rayMarch2(ro, rd),
mat = map2( ro + rd * d).y;
if (d < 10.) {
if (mat == 0.) col = vec3( 0., .3, .6);
if (mat == 1.) col = palette(1. - .4*d);