### maven

#### Raymarching tetrahedron fractal visual artifacts

```I've been working with some basic ray marching stuff lately and I've encountered some issues with visual artifacts for a tetrahedron fractal. These artifacts occur whenever objects seem to get close to one another. The issue is most certainly within the surface distance function I am using and not my ray marching engine.
http://i.imgur.com/FmyRetV.png
http://i.imgur.com/TFKA6sV.png
Code:
float sdTet4(vec3 p, int iterations, float scale) {
//in cube from p(-1,-1,-1) to p(1,1,1)
//equilateral triangle (sorta lol)
float sideLength = 0.0; //side length
vec3 z = p;
for (int i = 0; i < iterations; i++) {
sideLength = scale/pow(2.0, float(i+1));
radius = sideLength*0.2886751346; //sqrt(3.0)/6.0 = 0.2886751346
float height = sideLength*0.8164965809; //sqrt(6.0)/3.0 = 0.8164965809
vec3 z1 = z - vec3(0.0, height, 0.0); //top center
vec3 z2 = z - vec3(0.0, -height, -height); //bottom back
vec3 z3 = z - vec3(-sideLength, -height, height); //bottom front left
vec3 z4 = z - vec3(sideLength, -height, height); //bottom front right
z = z1;
float d = d1;
if (d2 < d) { d = d2; z = z2; }
if (d3 < d) { d = d3; z = z3; }
if (d4 < d) { d = d4; z = z4; }
}
}
Alternative code that somehow works:
float sdTet4(vec3 p, int iterations, float scale) {
vec3 a1 = vec3(1.0, 1.0, 1.0);
vec3 a2 = vec3(-1.0, -1.0, 1.0);
vec3 a3 = vec3(1.0, -1.0, -1.0);
vec3 a4 = vec3(-1.0, 1.0, -1.0);
iterations = 7;
int n = 0;
float dist = 0.0;
float d = 0.0;
vec3 z = p;
vec3 c = vec3(0.0);
//scale = 1.0;
float iterationScale = 0.0;
while (n < iterations) {
iterationScale = scale/pow(2.0, float(n));
c = a1; dist = length(z-a1);
d = length(z-a2); if (d < dist) { c = a2; dist = d; }
d = length(z-a3); if (d < dist) { c = a3; dist = d; }
d = length(z-a4); if (d < dist) { c = a4; dist = d; }
z -= c*iterationScale;
n++;
}
return sdSphere(z, iterationScale);
}
Thanks for the tips!
Edit: So why does the alternative code that I found work and not the original code? Something in the original code causes the visual artifacts but I don't know what.```

### Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization