prevent StackOverflowError in SimpleRenderer

main
jbb01 6 months ago
parent 580d8eca12
commit c002b8215a

@ -86,20 +86,28 @@ public final class SimpleRenderer implements Renderer {
}
private @NotNull Color getColor0(@NotNull Scene scene, @NotNull Ray ray, int depth) {
if (depth <= 0) return Color.BLACK;
var color = Color.BLACK;
var attenuation = Color.WHITE;
while (depth-- > 0) {
var optional = scene.hit(ray, new Range(0.001, Double.POSITIVE_INFINITY));
if (optional.isEmpty()) {
color = Color.add(color, Color.multiply(attenuation, scene.getBackgroundColor(ray)));
break;
}
var optional = scene.hit(ray, new Range(0.001, Double.POSITIVE_INFINITY));
if (optional.isPresent()) {
var hit = optional.get();
var material = hit.material();
var emitted = material.emitted(hit);
return material.scatter(ray, hit)
.map(scatter -> Color.multiply(scatter.attenuation(), getColor0(scene, scatter.ray(), depth - 1)))
.map(color -> Color.add(color, emitted))
.orElse(emitted);
} else {
return scene.getBackgroundColor(ray);
var scatter = material.scatter(ray, hit);
color = Color.add(color, Color.multiply(attenuation, emitted));
if (scatter.isEmpty()) break;
attenuation = Color.multiply(attenuation, scatter.get().attenuation());
ray = scatter.get().ray();
}
return color;
}
/**

Loading…
Cancel
Save