Skip to content

Commit

Permalink
DecalProjector: add validateMeshes & collectSources methods + javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
capdevon committed Oct 13, 2023
1 parent 568012d commit 33ff847
Showing 1 changed file with 50 additions and 33 deletions.
83 changes: 50 additions & 33 deletions game/src/main/java/mygame/decals/DecalProjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Geometry> geometries;
private Vector3f size;
Expand All @@ -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<Geometry> 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<Geometry> geometries, Vector3f position, Quaternion rotation, Vector3f size) {
this(geometries, position, rotation, size, 0.0001f);

/**
* Instantiate a DecalProjector.
*/
public DecalProjector(List<Geometry> geometryList, Vector3f position, Quaternion rotation, Vector3f size) {
this(geometryList, position, rotation, size, DEFAULT_SEPARATION);
}

public DecalProjector(Collection<Geometry> geometries, Vector3f position, Quaternion rotation, Vector3f size, float separation) {
/**
* Instantiate a DecalProjector.
*/
public DecalProjector(List<Geometry> 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<Geometry> collectSources(Spatial subtree) {
List<Geometry> 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<Geometry> geometryList) {
this.geometries = List.copyOf(geometryList);
validateMeshes();
}

public void setSize(Vector3f size) {
this.size = size;
}

public void setGeometries(Collection<Geometry> geometries) {
this.geometries = List.copyOf(geometries);
}

public void setSeparation(float separation) {
this.separation = separation;
}
Expand Down Expand Up @@ -165,7 +182,7 @@ public Geometry project() {
return new Geometry("decal", decalMesh);
}

protected List<DecalVertex> clipVertices(List<DecalVertex> decalVertices) {
private List<DecalVertex> clipVertices(List<DecalVertex> 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));
Expand Down

0 comments on commit 33ff847

Please sign in to comment.