在光线追踪器中仅渲染 3D 对象的正面

问题描述 投票:0回答:1

我正在用 Python 实现光线追踪器,我想确保只渲染 3D 对象的正面。目前,光线追踪器同时渲染正面和背面,我想修改代码以仅累积正面的颜色。

这是我的代码的相关部分:

def extract(cond, x):
    if isinstance(x, (int, float, complex)):
        return x
    return np.extract(cond, x)
class Ray:
    def __init__(self,origin, dir, depth, n, reflections, transmissions, diffuse_reflections):
        self.origin  = origin   
        self.dir = dir
        self.depth = depth     
        self.n = n
        self.reflections = reflections
        self.transmissions = transmissions
        self.diffuse_reflections = diffuse_reflections
    def extract(self,hit_check):
        return Ray(self.origin.extract(hit_check), self.dir.extract(hit_check), self.depth,  self.n.extract(hit_check), self.reflections, self.transmissions,self.diffuse_reflections)
class Hit:
    def __init__(self, distance, orientation, material, collider,surface):
        self.distance = distance
        self.orientation = orientation
        self.material = material
        self.collider = collider
        self.surface = surface
        self.u = None
        self.v = None
        self.N = None
        self.point = None
    def get_uv(self):
        if self.u is None:
            return self.collider.assigned_primitive.get_uv(self)
        return self.u, self.v
    def get_normal(self):
        if self.N is None:
            return self.collider.get_N(self)
        return self.N
def get_raycolor(ray, scene):
    distances, hit_orientation = zip(*[s.intersect(ray.origin, ray.dir) for s in scene.collider_list])
    nearest = np.minimum.reduce(distances)
    color = vec3(0., 0., 0.)
    for (coll, dis , orient) in zip(scene.collider_list, distances, hit_orientation):
        hit_check = (nearest != FARAWAY) & (dis == nearest)
        if np.any(hit_check):
            color += coll.assigned_primitive.material.get_color(scene,  ray.extract(hit_check), Hit(extract(hit_check,dis) , extract(hit_check,orient), coll.assigned_primitive.material, coll, coll.assigned_primitive)).place(hit_check)
    return color
python python-3.x raytracing
1个回答
0
投票

您的代码已经具有此功能。

如果你这样做,你会发现你的代码变得更慢。

来自:

def get_raycolor(ray, scene):
    distances, hit_orientation = zip(*[s.intersect(ray.origin, ray.dir) for s in scene.collider_list])
    nearest = np.minimum.reduce(distances)
    color = vec3(0., 0., 0.)
    for (coll, dis , orient) in zip(scene.collider_list, distances, hit_orientation):
        hit_check = (nearest != FARAWAY) & (dis == nearest)
        if np.any(hit_check):
            color += coll.assigned_primitive.material.get_color(scene,  ray.extract(hit_check), Hit(extract(hit_check,dis) , extract(hit_check,orient), coll.assigned_primitive.material, coll, coll.assigned_primitive)).place(hit_check)
    return color

致:

def get_raycolor(ray, scene):
    distances, hit_orientation = zip(*[s.intersect(ray.origin, ray.dir) for s in scene.collider_list])
    nearest = np.minimum.reduce(distances)
    color = vec3(0., 0., 0.)
    for (coll, dis , orient) in zip(scene.collider_list, distances, hit_orientation):
        hit_check = (nearest != FARAWAY) & (dis == nearest)
        color += coll.assigned_primitive.material.get_color(scene,  ray.extract(hit_check), Hit(extract(hit_check,dis) , extract(hit_check,orient), coll.assigned_primitive.material, coll, coll.assigned_primitive)).place(hit_check)
    return color
© www.soinside.com 2019 - 2024. All rights reserved.