|
|
@ -1,6 +1,7 @@
|
|
|
|
package eu.jonahbauer.raytracing.material;
|
|
|
|
package eu.jonahbauer.raytracing.material;
|
|
|
|
|
|
|
|
|
|
|
|
import eu.jonahbauer.raytracing.math.Ray;
|
|
|
|
import eu.jonahbauer.raytracing.math.Ray;
|
|
|
|
|
|
|
|
import eu.jonahbauer.raytracing.math.Vec3;
|
|
|
|
import eu.jonahbauer.raytracing.render.Color;
|
|
|
|
import eu.jonahbauer.raytracing.render.Color;
|
|
|
|
import eu.jonahbauer.raytracing.scene.HitResult;
|
|
|
|
import eu.jonahbauer.raytracing.scene.HitResult;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
@ -8,16 +9,23 @@ import org.jetbrains.annotations.NotNull;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
|
|
|
|
public record MetallicMaterial(@NotNull Color albedo) implements Material {
|
|
|
|
public record MetallicMaterial(@NotNull Color albedo, double fuzz) implements Material {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public MetallicMaterial(@NotNull Color albedo) {
|
|
|
|
|
|
|
|
this(albedo, 0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public MetallicMaterial {
|
|
|
|
public MetallicMaterial {
|
|
|
|
Objects.requireNonNull(albedo, "albedo");
|
|
|
|
Objects.requireNonNull(albedo, "albedo");
|
|
|
|
|
|
|
|
if (fuzz < 0 || !Double.isFinite(fuzz)) throw new IllegalArgumentException("fuzz must be non-negative");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public @NotNull Optional<ScatterResult> scatter(@NotNull Ray ray, @NotNull HitResult hit) {
|
|
|
|
public @NotNull Optional<ScatterResult> scatter(@NotNull Ray ray, @NotNull HitResult hit) {
|
|
|
|
var direction = ray.direction();
|
|
|
|
var newDirection = Vec3.reflect(ray.direction(), hit.normal());
|
|
|
|
var normal = hit.normal();
|
|
|
|
if (fuzz > 0) {
|
|
|
|
var newDirection = direction.minus(normal.times(2 * normal.times(direction)));
|
|
|
|
newDirection = newDirection.unit().plus(Vec3.random(true).times(fuzz));
|
|
|
|
|
|
|
|
}
|
|
|
|
return Optional.of(new ScatterResult(new Ray(hit.position(), newDirection), albedo));
|
|
|
|
return Optional.of(new ScatterResult(new Ray(hit.position(), newDirection), albedo));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|