MandelBulb Ray Casting Algorithm Python Example
因此,如果我理解正确的话,光线投射算法要求观察者位于3D分形的外部,在该点处,矢量从观察者朝向与矢量垂直的平面上的点并且与原点相交。
在我看来,这将严重限制分形的渲染视图,或者需要使用多个观察者位置对分形进行立体3D重建(这对我来说似乎很难)。另外,不能收集关于分形的内部结构的信息。
或者,直接体积渲染看起来足够直观,但计算上昂贵并且本身可能效率低下。使用诸如行进立方体之类的算法的间接体绘制也可能采用一些学习曲线。
在第二个链接的pdf中的某个地方,它讨论了切割平面视图,以便查看分形的切片。
为什么不使用切割平面作为渲染方法?
observer
放在点Q
处的原点(0, 0, 0)
。y
和z
点组合所跨越的入射平面发射光线,这些点组合正在切割分形。x
分量在切片平面的dx
之内,那么y
和z
坐标以及切片平面的x
值被存储为x, y, z
坐标。这些坐标现在代表x
中特定切片的表面。x
方向上有一个自由度。通过以其自由度移动平面,我们可以接收给定切片的另一组x, y, z
坐标。请参见下图作为视觉辅助(球体代表MandelBulb)。
下面是我的Python代码到目前为止,改编自第一个链接。我成功地生成了点平面,并且能够获得从原点到平面上的点的方向。在距离估计函数中肯定存在一些根本性的缺陷,因为那就是一切都崩溃了,我得到总距离的nan
s
def get_plane_points(x, y_res=500, z_res=500, y_min=-10, y_max=10, z_min=-10, z_max=10):
y = np.linspace(y_min, y_max, y_res)
z = np.linspace(z_min, z_max, z_res)
x, y, z = np.meshgrid(x, y, z)
x, y, z = x.reshape(-1), y.reshape(-1) , z.reshape(-1)
P = np.vstack((x, y, z)).T
return P
def get_directions(P):
v = np.array(P - 0)
v = v/np.linalg.norm(v, axis=1)[:, np.newaxis]
return v
@jit
def DistanceEstimator(positions, plane_loc, iterations, degree):
m = positions.shape[0]
x, y, z = np.zeros(m), np.zeros(m), np.zeros(m)
x0, y0, z0 = positions[:, 0], positions[:, 1], positions[:, 2]
dr = np.zeros(m) + 1
r = np.zeros(m)
theta = np.zeros(m)
phi = np.zeros(m)
zr = np.zeros(m)
for _ in range(iterations):
r = np.sqrt(x * x + y * y + z * z)
dx = .01
x_loc = plane_loc
idx = (x < x_loc + dx) & (x > x_loc - dx)
dr[idx] = np.power(r[idx], degree - 1) * degree * dr[idx] + 1.0
theta[idx] = np.arctan2(np.sqrt(x[idx] * x[idx] + y[idx] * y[idx]), z[idx])
phi[idx] = np.arctan2(y[idx], x[idx])
zr[idx] = r[idx] ** degree
theta[idx] = theta[idx] * degree
phi[idx] = phi[idx] * degree
x[idx] = zr[idx] * np.sin(theta[idx]) * np.cos(phi[idx]) + x0[idx]
y[idx] = zr[idx] * np.sin(theta[idx]) * np.sin(phi[idx]) + y0[idx]
z[idx] = zr[idx] * np.cos(theta[idx]) + z0[idx]
return 0.5 * np.log(r) * r / dr
def trace(directions, plane_location, max_steps=50, iterations=50, degree=8):
total_distance = np.zeros(directions.shape[0])
keep_iterations = np.ones_like(total_distance)
steps = np.zeros_like(total_distance)
for _ in range(max_steps):
positions = total_distance[:, np.newaxis] * directions
distance = DistanceEstimator(positions, plane_location, iterations, degree)
total_distance += distance * keep_iterations
steps += keep_iterations
# return 1 - (steps / max_steps) ** power
return total_distance
def run():
plane_location = 2
plane_points = get_plane_points(x=plane_location)
directions = get_directions(plane_points)
distance = trace(directions, plane_location)
return distance
我渴望听到有关这方面的想法以及我可能遇到的限制/问题。在此先感谢您的帮助!