Hey Ryan,
Thanks for responding!
Here’s the shader code:
// Spherical coordinate values
float r = 0, theta = 0, phi = 0;
int i = 0;
float dr = 1.0;
float3 ray = InputPosition + OriginOffset;
//float m = dot(ray, ray);
for(i = 0; i < MaxIterations; ++i)
{
// Optimization suggested by Inigo Quilez - commented as it causes issues right now
/*
m = dot(ray, ray);
if(m > MaxDistance)
break;
float m2 = m*m; float m4 = m2*m2;
dr = Order * sqrt(m4 * m2 * m) * dr + 1.0;
float x = ray.x; float x2 = x*x; float x4 = x2*x2;
float y = ray.y; float y2 = y*y; float y4 = y2*y2;
float z = ray.z; float z2 = z*z; float z4 = z2*z2;
float k3 = x2 + z2;
float k2 = rsqrt( k3*k3*k3*k3*k3*k3*k3 );
float k1 = x4 + y4 + z4 - 6.0*y2*z2 - 6.0*x2*y2 + 2.0*z2*x2;
float k4 = x2 - y2 + z2;
ray.x = InputPosition.x + 64.0*x*y*z*(x2-z2)*k4*(x4-6.0*x2*z2+z4)*k1*k2;
ray.y = InputPosition.y + -16.0*y2*k3*k4*k4 + k1*k1;
ray.z = InputPosition.z + -8.0*y*k4*(x4*x4 - 28.0*x4*x2*z2 + 70.0*x4*z4 - 28.0*x2*z2*z4 + z4*z4)*k1*k2;
*/
r = length(ray);
if(r > MaxDistance)
break;
// Calculate spherical system coordinates
theta = acos(ray.y/r);
phi = atan2(ray.x, ray.z);
dr = (pow(r, Order - 1.0) * Order * dr) + 1.0f;
// Scale and restore the point
float zr = pow(r, Order);
theta = theta * Order;
phi = phi * Order;
// Convert back to cartesian coordinates
ray = zr * float3(sin(theta) * sin(phi), cos(theta), cos(phi) * sin(theta));
ray += InputPosition;
}
// Channel pack
// Result of distance estimator into Red
// Iteration count into Blue
// Distance from ray start into Green
return float3(0.5f * log(r) * r/dr, i, r);
Here’s the calling Material:
Nothing too fancy going on here. I’ll admit the intricacies of the math go over my head but it’s a distance estimator function for a Mandelbulb volume.
Code taken primarily from here: Distance Estimated 3D Fractals (Part I) | Syntopia
Inigo Quilez had an article where he posted a polynomial that could be used for avoiding the expensive trig operations for transcendental equation solving. That’s probably what I’ll do first when I get the code that’s commented out to work.
EDIT:
Hey @RyanB I threw in Inigo’s optimizations but it didn’t help any, I think they rely on the compiler folding the expressions or something, but that doesn’t happen with custom nodes right?
Anyway, kind of out of ideas right now, Googling for general material optimization tips, would appreciate any input you had.