Skip to content

Commit ab3c661

Browse files
committed
random fixes of raymarching shaders
1 parent f55e695 commit ab3c661

5 files changed

Lines changed: 50 additions & 46 deletions

File tree

vr-dive/Demos/Floreus/FloreusShaders.metal

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ struct FloreusVertexOut {
3838

3939
static constant float3 FL_BOX_HALF = float3(1.0f);
4040
static constant float FL_TRACE_EPSILON = 0.0015f;
41-
static constant int FL_STEPS = 100;
41+
static constant int FL_STEPS = 140;
4242
static constant int FL_INNER_STEPS = 5;
43-
static constant float FL_SCENE_SCALE = 8.0f;
43+
static constant float FL_SCENE_SCALE = 2.4f;
4444

4545
vertex FloreusVertexOut floreusVertex(
4646
ushort amplificationID [[amplification_id]],
@@ -140,7 +140,7 @@ fragment float4 floreusFragment(
140140
accum += float4(3.0f, 2.0f, 1.0f, 1.0f) * 20.0f * d / max(s, 1.0e-4f);
141141
}
142142

143-
float4 color = tanh(accum / 1.0e7f);
143+
float4 color = tanh(accum / 3.5e6f);
144144
float2 q = flFaceUV(hit);
145145
float vignette = 1.0f - 0.35f * dot(q * 2.0f - 1.0f, q * 2.0f - 1.0f);
146146
color.rgb *= vignette;

vr-dive/Demos/Fractal49Gaz/Fractal49GazShaders.metal

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ static float3 hue(float h) {
4141
return cos(h * 6.3f + float3(0.0f, 23.0f, 21.0f)) * 0.5f + 0.5f;
4242
}
4343

44+
static float3 fgTonemap(float3 color) {
45+
return color / (1.0f + color);
46+
}
47+
4448
static float3 rotateAroundAxis(float3 p, float3 axis, float angle) {
4549
float c = cos(angle);
4650
float s = sin(angle);
@@ -107,8 +111,8 @@ static FractalAccum traceFractal(float3 ro, float3 rd, float time) {
107111
lastError = length(cross(p, FG_EQ)) / s - 5.0e-4f;
108112
float3 tint = mix(float3(1.0f), hue(log(max(s, 1.0e-4f)) * 0.3f), 0.8f);
109113
float falloff = exp(-12.0f * iteration * iteration * max(lastError, 0.0f));
110-
accum.color += 0.1f * tint * falloff;
111-
accum.glow += 0.015f / (0.0005f + lastError * lastError);
114+
accum.color += 0.03f * tint * falloff;
115+
accum.glow += min(0.04f, 0.0016f / (0.002f + lastError * lastError));
112116

113117
g += lastError;
114118
if (g > 18.0f) {
@@ -176,18 +180,18 @@ fragment float4 fractal49GazFragment(
176180
float3(0.03f, 0.02f, 0.06f),
177181
float3(0.24f, 0.08f, 0.32f),
178182
horizon);
179-
background += hue(uniforms.time * 0.08f + swirl * 0.25f) * (0.08f + 0.22f * facing);
180-
background += float3(0.14f, 0.28f, 0.1f) * swirl * 0.3f;
183+
background += hue(uniforms.time * 0.08f + swirl * 0.25f) * (0.025f + 0.08f * facing);
184+
background += float3(0.14f, 0.28f, 0.1f) * swirl * 0.08f;
181185

182186
float2 faceUV = fgFaceUV(surfacePos) * 2.0f - 1.0f;
183187
float vignette = 1.0f - 0.16f * dot(faceUV, faceUV);
184188

185-
float glow = accum.glow * 0.007f;
186-
float3 color = background * 0.75f + accum.color;
189+
float glow = min(accum.glow, 6.0f) * 0.02f;
190+
float3 color = background * 0.22f + accum.color;
187191
color += glow * float3(0.45f, 0.82f, 0.52f);
188192
color *= vignette;
189193

190-
color = clamp(color, 0.0f, 1.0f);
191-
color = pow(color, float3(0.92f));
194+
color = fgTonemap(max(color, 0.0f));
195+
color = pow(color, float3(0.96f));
192196
return float4(color, 1.0f);
193197
}

vr-dive/Demos/Fractal77Gaz/Fractal77GazShaders.metal

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ static float3 hue77(float h) {
3939
return cos(h * 6.3f + float3(0.0f, 23.0f, 21.0f)) * 0.5f + 0.5f;
4040
}
4141

42+
static float3 f77Tonemap(float3 color) {
43+
return color / (1.0f + color);
44+
}
45+
4246
static float3 rotateAroundAxis77(float3 p, float3 axis, float angle) {
4347
float c = cos(angle);
4448
float s = sin(angle);
@@ -98,16 +102,16 @@ static Fractal77Accum traceFractal77(float3 ro, float3 rd, float time) {
98102
e = length(p.xz) / max(s, 1.0e-4f);
99103
float3 tint = mix(float3(0.09f, 0.12f, 0.18f), hue77(log(max(s, 1.0e-4f))), 0.7f);
100104
float falloff = exp(-iteration * iteration * e);
101-
accum.color += tint * 0.08f * falloff;
102-
accum.energy += 0.018f / (0.002f + e * e * 12.0f);
105+
accum.color += tint * 0.04f * falloff;
106+
accum.energy += min(0.03f, 0.008f / (0.01f + e * e * 14.0f));
103107

104108
g += e;
105109
if (g > 18.0f) {
106110
break;
107111
}
108112
}
109113

110-
accum.color = pow(clamp(accum.color, 0.0f, 3.0f), float3(5.0f));
114+
accum.color = pow(f77Tonemap(clamp(accum.color, 0.0f, 4.0f)), float3(3.2f));
111115
return accum;
112116
}
113117

@@ -165,15 +169,15 @@ fragment float4 fractal77GazFragment(
165169
float3(0.015f, 0.02f, 0.04f),
166170
float3(0.16f, 0.08f, 0.23f),
167171
horizon);
168-
background += hue77(uniforms.time * 0.1f + axisFacing * 0.3f) * (0.05f + 0.18f * axisFacing);
169-
background += float3(0.28f, 0.34f, 0.5f) * accum.energy * 0.03f;
172+
background += hue77(uniforms.time * 0.1f + axisFacing * 0.3f) * (0.025f + 0.08f * axisFacing);
173+
background += float3(0.28f, 0.34f, 0.5f) * accum.energy * 0.012f;
170174

171175
float2 faceUV = f77FaceUV(surfacePos) * 2.0f - 1.0f;
172176
float vignette = 1.0f - 0.16f * dot(faceUV, faceUV);
173177

174-
float3 color = background * 0.8f + accum.color;
175-
color += accum.energy * float3(0.55f, 0.78f, 1.05f) * 0.01f;
178+
float3 color = background * 0.35f + accum.color;
179+
color += accum.energy * float3(0.55f, 0.78f, 1.05f) * 0.004f;
176180
color *= vignette;
177-
color = clamp(color, 0.0f, 1.0f);
181+
color = f77Tonemap(max(color, 0.0f));
178182
return float4(color, 1.0f);
179183
}

vr-dive/Demos/SaturdayWeirdness/SaturdayWeirdnessShaders.metal

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,9 @@ static float2 swRotate2D(float2 p, float a) {
7171
return float2(c * p.x - s * p.y, s * p.x + c * p.y);
7272
}
7373

74-
static float3 swFold(float3 p, float time) {
75-
p.xz = swRotate2D(p.xz, time * 0.22f + p.y * 0.08f);
76-
p.xy = swRotate2D(p.xy, time * -0.17f + sin(p.z * 0.5f) * 0.6f);
77-
p = abs(p) - float3(1.15f, 0.85f, 1.15f);
78-
p.yz = swRotate2D(p.yz, 0.75f + sin(time * 0.33f) * 0.4f);
79-
return p;
74+
static float swRoundBox(float3 p, float3 b, float r) {
75+
float3 q = abs(p) - b;
76+
return length(max(q, 0.0f)) + min(max(q.x, max(q.y, q.z)), 0.0f) - r;
8077
}
8178

8279
static float swGyroid(float3 p) {
@@ -114,23 +111,18 @@ static float2 swFaceUV(float3 p) {
114111
static SWHit swMap(float3 p, float time) {
115112
SWHit hit;
116113

117-
float3 q = p * 2.35f;
118-
q = swFold(q, time);
114+
float3 q = p * 1.55f;
115+
q.xz = swRotate2D(q.xz, time * 0.2f);
116+
q.yz = swRotate2D(q.yz, 0.65f + sin(time * 0.33f) * 0.25f);
119117

120-
float gyroid = swGyroid(q * 1.55f + time * 0.7f);
121-
float bandsA = sin(q.x * 6.0f + sin(q.z * 2.0f - time * 1.6f) * 1.6f);
122-
float bandsB = sin(q.y * 7.0f - cos(q.x * 2.6f + time * 1.2f) * 1.4f);
123-
float bandsC = sin(q.z * 6.8f + q.x * 1.8f - time * 0.9f);
124-
float ribbons = abs(bandsA + bandsB * 0.75f + bandsC * 0.6f + gyroid * 0.55f) - 0.24f;
118+
float core = swRoundBox(q, float3(0.7f), 0.28f);
119+
float gyroid = swGyroid(q * 3.1f + time * 0.7f);
120+
float grooves = sin(q.x * 7.0f) + sin(q.y * 7.6f) + sin(q.z * 6.8f);
121+
float detail = (abs(gyroid) - 0.24f) * 0.045f + grooves * 0.012f;
122+
float sdf = core + detail;
125123

126-
float shellRadius = length(q.xz) - (1.35f + 0.22f * sin(q.y * 3.0f - time * 1.1f));
127-
float shell = abs(shellRadius) - (0.11f + 0.04f * sin(q.y * 8.0f + time * 1.9f));
128-
129-
float knots = length(float2(abs(gyroid), ribbons)) - 0.2f;
130-
float sdf = min(ribbons * 0.16f, min(shell * 0.24f, knots * 0.22f));
131-
132-
float edgeSignal = abs(ribbons) + abs(shell) * 0.35f + abs(gyroid) * 0.25f;
133-
float colorPhase = 0.16f * q.y + 0.08f * q.z + sin(time * 0.45f) * 0.2f;
124+
float edgeSignal = abs(detail) + abs(gyroid) * 0.08f;
125+
float colorPhase = 0.14f * q.y + 0.1f * q.z + sin(time * 0.45f) * 0.2f;
134126

135127
hit.distance = 0.0f;
136128
hit.sdf = sdf;
@@ -179,7 +171,7 @@ fragment float4 saturdayWeirdnessFragment(
179171
}
180172

181173
float tStart = insideBox ? 0.0f : max(tBox.x, 0.0f);
182-
float3 ro = (eye + rd * (tStart + SW_HIT_EPSILON)) * 3.5f;
174+
float3 ro = (eye + rd * (tStart + SW_HIT_EPSILON)) * 2.0f;
183175

184176
float travel = 0.0f;
185177
SWHit state = swMap(ro, uniforms.time);
@@ -189,7 +181,7 @@ fragment float4 saturdayWeirdnessFragment(
189181
if (abs(state.sdf) < SW_HIT_EPSILON || travel > SW_MAX_DIST) {
190182
break;
191183
}
192-
travel += clamp(abs(state.sdf), 0.01f, 0.18f);
184+
travel += clamp(abs(state.sdf), 0.008f, 0.14f);
193185
}
194186

195187
if (travel > SW_MAX_DIST || abs(state.sdf) >= 0.02f) {

vr-dive/Demos/SineBud/SineBudShaders.metal

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static constant float3 SB_BOX_HALF = float3(1.0f);
4040
static constant float SB_TRACE_EPSILON = 0.0015f;
4141
static constant int SB_STEPS = 200;
4242
static constant float SB_MAX_DIST = 100.0f;
43-
static constant float SB_SCENE_SCALE = 18.0f;
43+
static constant float SB_SCENE_SCALE = 8.0f;
4444

4545
vertex SineBudVertexOut sineBudVertex(
4646
ushort amplificationID [[amplification_id]],
@@ -144,7 +144,8 @@ fragment float4 sineBudFragment(
144144
float d = 0.0f;
145145
float s = 1.0f;
146146
float3 k = float3(0.0f);
147-
float3 c = float3(0.0f, 1.0f, 2.0f) * length(rd.xy);
147+
float3 c = float3(0.0f);
148+
bool hitBud = false;
148149

149150
for (int step = 0; step < SB_STEPS; ++step) {
150151
float3 p = ro + rd * d;
@@ -167,13 +168,16 @@ fragment float4 sineBudFragment(
167168

168169
s = min(s, min(k.z, min(k.x, k.y)));
169170
if (s < 0.001f || d > SB_MAX_DIST) {
171+
hitBud = s < 0.001f;
170172
break;
171173
}
172174
d += s * 0.3f;
173175
}
174176

175-
c += max(cos(d * SB_PI * 2.0f) - s * sqrt(max(d, 0.0f)) - k, 0.0f);
176-
float3 color = c + c.brg + c * c;
177+
if (hitBud) {
178+
c += max(cos(d * SB_PI * 2.0f) - s * sqrt(max(d, 0.0f)) - k, 0.0f);
179+
}
180+
float3 color = hitBud ? (c + c.brg + c * c) : float3(0.0f);
177181

178182
float2 q = sbFaceUV(hit);
179183
float vignette = 1.0f - 0.35f * dot(q * 2.0f - 1.0f, q * 2.0f - 1.0f);

0 commit comments

Comments
 (0)