diff --git a/rtiow/renderer/src/bvh_triangles.rs b/rtiow/renderer/src/bvh_triangles.rs index 8833bd5..4b15359 100644 --- a/rtiow/renderer/src/bvh_triangles.rs +++ b/rtiow/renderer/src/bvh_triangles.rs @@ -219,27 +219,31 @@ where } fn find_best_split_plane(&self, node: &BVHNode) -> SplitCost { - let mut best_axis = usize::MAX; - let mut best_pos = 0.; - let mut best_cost = f32::MAX; + let mut best = SplitCost { + pos: 0., + cost: f32::MAX, + axis: usize::MAX, + }; for axis in 0..3 { - for i in 0..node.tri_count { - let triangle = &self.triangles[self.triangle_index[(node.left_first + i) as usize]]; - let candidate_pos = triangle.centroid[axis]; + let bounds_min = node.aabb.min()[axis]; + let bounds_max = node.aabb.max()[axis]; + if bounds_min == bounds_max { + continue; + } + let scale = (bounds_max - bounds_min) / 100.; + + for i in 0..100 { + let candidate_pos = bounds_min + (i as f32) * scale; let cost = self.evaluate_sah(node, axis, candidate_pos); - if cost <= best_cost { - best_pos = candidate_pos; - best_axis = axis; - best_cost = cost; + if cost <= best.cost { + best.pos = candidate_pos; + best.axis = axis; + best.cost = cost; } } } - SplitCost { - pos: best_pos, - cost: best_cost, - axis: best_axis, - } + best } fn subdivide(&mut self, idx: usize) {