From 1b02f8a96db2a9065759919e52bddb510efdae5c Mon Sep 17 00:00:00 2001 From: jbb01 <32650546+jbb01@users.noreply.github.com> Date: Wed, 7 Aug 2024 15:50:09 +0200 Subject: [PATCH] skip unnecessary UV calculations --- .../render/material/IsotropicMaterial.java | 6 ++++++ .../raytracing/render/material/Material.java | 3 +++ .../render/texture/CheckerTexture.java | 5 +++++ .../raytracing/render/texture/Color.java | 5 +++++ .../raytracing/render/texture/PerlinTexture.java | 5 +++++ .../raytracing/render/texture/Texture.java | 4 ++++ .../raytracing/scene/hittable3d/Box.java | 5 +++-- .../raytracing/scene/hittable3d/Sphere.java | 16 +++++++++++----- 8 files changed, 42 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/jonahbauer/raytracing/render/material/IsotropicMaterial.java b/src/main/java/eu/jonahbauer/raytracing/render/material/IsotropicMaterial.java index 1f4b4af..4ab3d88 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/material/IsotropicMaterial.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/material/IsotropicMaterial.java @@ -3,6 +3,7 @@ package eu.jonahbauer.raytracing.render.material; import eu.jonahbauer.raytracing.math.Ray; import eu.jonahbauer.raytracing.math.Vec3; import eu.jonahbauer.raytracing.render.texture.Color; +import eu.jonahbauer.raytracing.render.texture.Texture; import eu.jonahbauer.raytracing.scene.HitResult; import org.jetbrains.annotations.NotNull; @@ -14,4 +15,9 @@ public record IsotropicMaterial(@NotNull Color albedo) implements Material { public @NotNull Optional scatter(@NotNull Ray ray, @NotNull HitResult hit, @NotNull RandomGenerator random) { return Optional.of(new ScatterResult(new Ray(hit.position(), Vec3.random(random, true)), albedo())); } + + @Override + public @NotNull Texture texture() { + return albedo(); + } } diff --git a/src/main/java/eu/jonahbauer/raytracing/render/material/Material.java b/src/main/java/eu/jonahbauer/raytracing/render/material/Material.java index 4f430f0..7b62125 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/material/Material.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/material/Material.java @@ -2,6 +2,7 @@ package eu.jonahbauer.raytracing.render.material; import eu.jonahbauer.raytracing.math.Ray; import eu.jonahbauer.raytracing.render.texture.Color; +import eu.jonahbauer.raytracing.render.texture.Texture; import eu.jonahbauer.raytracing.scene.HitResult; import org.jetbrains.annotations.NotNull; @@ -11,6 +12,8 @@ import java.util.random.RandomGenerator; public interface Material { + @NotNull Texture texture(); + @NotNull Optional scatter(@NotNull Ray ray, @NotNull HitResult hit, @NotNull RandomGenerator random); default @NotNull Color emitted(@NotNull HitResult hit) { diff --git a/src/main/java/eu/jonahbauer/raytracing/render/texture/CheckerTexture.java b/src/main/java/eu/jonahbauer/raytracing/render/texture/CheckerTexture.java index 70b832d..aa282fa 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/texture/CheckerTexture.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/texture/CheckerTexture.java @@ -14,4 +14,9 @@ public record CheckerTexture(double scale, @NotNull Texture even, @NotNull Textu var even = (x + y + z) % 2 == 0; return even ? even().get(u, v, p) : odd().get(u, v, p); } + + @Override + public boolean isUVRequired() { + return even.isUVRequired() || odd.isUVRequired(); + } } diff --git a/src/main/java/eu/jonahbauer/raytracing/render/texture/Color.java b/src/main/java/eu/jonahbauer/raytracing/render/texture/Color.java index bf08111..3a6ee98 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/texture/Color.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/texture/Color.java @@ -94,6 +94,11 @@ public record Color(double r, double g, double b) implements Texture { return this; } + @Override + public boolean isUVRequired() { + return false; + } + private static int toInt(double value) { return Math.clamp((int) (255.99 * value), 0, 255); } diff --git a/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java b/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java index 041a2b7..1b3ebd7 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/texture/PerlinTexture.java @@ -144,4 +144,9 @@ public final class PerlinTexture implements Texture { } return accum; } + + @Override + public boolean isUVRequired() { + return false; + } } diff --git a/src/main/java/eu/jonahbauer/raytracing/render/texture/Texture.java b/src/main/java/eu/jonahbauer/raytracing/render/texture/Texture.java index ab01c5b..e8374f5 100644 --- a/src/main/java/eu/jonahbauer/raytracing/render/texture/Texture.java +++ b/src/main/java/eu/jonahbauer/raytracing/render/texture/Texture.java @@ -10,4 +10,8 @@ public interface Texture { } @NotNull Color get(double u, double v, @NotNull Vec3 p); + + default boolean isUVRequired() { + return true; + } } diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java b/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java index 947d08c..c9a1582 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Box.java @@ -82,8 +82,9 @@ public record Box(@NotNull AABB box, @NotNull Material material) implements Hitt var side = frontFace ? entry : exit; var normal = frontFace ? side.normal : side.normal.neg(); var position = ray.at(t); - var u = side.getTextureU(box, position); - var v = side.getTextureV(box, position); + var uv = material().texture().isUVRequired(); + var u = uv ? side.getTextureU(box, position) : Double.NaN; + var v = uv ? side.getTextureV(box, position) : Double.NaN; return Optional.of(new HitResult(t, position, normal, material, u, v, frontFace)); } diff --git a/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Sphere.java b/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Sphere.java index 66437b1..92bebc6 100644 --- a/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Sphere.java +++ b/src/main/java/eu/jonahbauer/raytracing/scene/hittable3d/Sphere.java @@ -52,11 +52,17 @@ public final class Sphere implements Hittable { var normal = position.minus(center).div(radius); var frontFace = normal.times(ray.direction()) < 0; - var theta = Math.acos(- normal.y()); - var phi = Math.atan2(- normal.z(), normal.x()) + Math.PI; - - var u = phi / (2 * Math.PI); - var v = theta / Math.PI; + double u; + double v; + if (material.texture().isUVRequired()) { + var theta = Math.acos(-normal.y()); + var phi = Math.atan2(-normal.z(), normal.x()) + Math.PI; + u = phi / (2 * Math.PI); + v = theta / Math.PI; + } else { + u = Double.NaN; + v = Double.NaN; + } return Optional.of(new HitResult( t, position, frontFace ? normal : normal.neg(),