From 33ff847bbe7fcf1644dbff9cb3cad78013b16cb3 Mon Sep 17 00:00:00 2001 From: capdevon Date: Fri, 13 Oct 2023 19:46:52 +0200 Subject: [PATCH] DecalProjector: add validateMeshes & collectSources methods + javadoc --- .../java/mygame/decals/DecalProjector.java | 83 +++++++++++-------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/game/src/main/java/mygame/decals/DecalProjector.java b/game/src/main/java/mygame/decals/DecalProjector.java index 343899b..25d2c3a 100644 --- a/game/src/main/java/mygame/decals/DecalProjector.java +++ b/game/src/main/java/mygame/decals/DecalProjector.java @@ -2,7 +2,6 @@ import java.nio.FloatBuffer; import java.util.ArrayList; -import java.util.Collection; import java.util.List; import com.jme3.math.FastMath; @@ -19,9 +18,12 @@ import com.jme3.scene.VertexBuffer.Type; import com.jme3.scene.mesh.IndexBuffer; import com.jme3.util.BufferUtils; + import jme3utilities.MyMesh; public class DecalProjector { + + private static final float DEFAULT_SEPARATION = 0.0001f; private List geometries; private Vector3f size; @@ -30,53 +32,68 @@ public class DecalProjector { /** * direction in which to project decals (unit vector in world coordinates) */ - final private Vector3f projectionDirection; + private final Vector3f projectionDirection; private float separation; + /** + * Instantiate a DecalProjector. + */ public DecalProjector(Spatial subtree, Vector3f position, Quaternion rotation, Vector3f size) { - List geometries = new ArrayList<>(); - subtree.depthFirstTraversal(new SceneGraphVisitorAdapter() { - @Override - public void visit(Geometry geom) { - Mesh mesh = geom.getMesh(); - if (!MyMesh.hasNormals(mesh)) { - throw new IllegalArgumentException( - "mesh lacks normals in " + geom.getName()); - } - if (!MyMesh.hasTriangles(mesh)) { - throw new IllegalArgumentException( - "unsupported mesh mode " + mesh.getMode() + " in " + geom.getName()); - } - geometries.add(geom); - } - }); - setGeometries(geometries); - setSize(size); - setSeparation(0.0001f); - setTransform(new Transform(position, rotation, new Vector3f(1, 1, 1))); - this.projectionDirection = rotation.mult(Vector3f.UNIT_Z); + this(collectSources(subtree), position, rotation, size, DEFAULT_SEPARATION); } - - public DecalProjector(Collection geometries, Vector3f position, Quaternion rotation, Vector3f size) { - this(geometries, position, rotation, size, 0.0001f); + + /** + * Instantiate a DecalProjector. + */ + public DecalProjector(List geometryList, Vector3f position, Quaternion rotation, Vector3f size) { + this(geometryList, position, rotation, size, DEFAULT_SEPARATION); } - public DecalProjector(Collection geometries, Vector3f position, Quaternion rotation, Vector3f size, float separation) { + /** + * Instantiate a DecalProjector. + */ + public DecalProjector(List geometryList, Vector3f position, Quaternion rotation, Vector3f size, float separation) { setSize(size); - setGeometries(geometries); + setGeometries(geometryList); setSeparation(separation); setTransform(new Transform(position, rotation, new Vector3f(1, 1, 1))); this.projectionDirection = rotation.mult(Vector3f.UNIT_Z); } + + private static List collectSources(Spatial subtree) { + List result = new ArrayList<>(); + subtree.depthFirstTraversal(new SceneGraphVisitorAdapter() { + @Override + public void visit(Geometry geom) { + result.add(geom); + } + }); + return result; + } + + private void validateMeshes() { + for (Geometry geom : geometries) { + Mesh mesh = geom.getMesh(); + if (!MyMesh.hasNormals(mesh)) { + throw new IllegalArgumentException( + "Mesh lacks normals in " + geom.getName()); + } + if (!MyMesh.hasTriangles(mesh)) { + throw new IllegalArgumentException( + "Unsupported mesh mode " + mesh.getMode() + " in " + geom.getName()); + } + } + } + public void setGeometries(List geometryList) { + this.geometries = List.copyOf(geometryList); + validateMeshes(); + } + public void setSize(Vector3f size) { this.size = size; } - public void setGeometries(Collection geometries) { - this.geometries = List.copyOf(geometries); - } - public void setSeparation(float separation) { this.separation = separation; } @@ -165,7 +182,7 @@ public Geometry project() { return new Geometry("decal", decalMesh); } - protected List clipVertices(List decalVertices) { + private List clipVertices(List decalVertices) { decalVertices = clipGeometry(decalVertices, new Vector3f(1, 0, 0)); decalVertices = clipGeometry(decalVertices, new Vector3f(-1, 0, 0)); decalVertices = clipGeometry(decalVertices, new Vector3f(0, 1, 0));