From a3340ecc1e98fa57db19ec4ff6fa194962ef56c2 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Thu, 9 Sep 2021 20:32:05 -0700 Subject: [PATCH 1/2] feat: expand attribute scheme to allow combining meshdata --- .../assets/mesh/StandardMeshDataTest.java | 110 ++++++++++++++++++ .../assets/mesh/StandardMeshData.java | 34 ++++++ .../mesh/resource/VertexAttributeBinding.java | 21 +++- 3 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java diff --git a/engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java b/engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java new file mode 100644 index 00000000000..106e5ea0091 --- /dev/null +++ b/engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java @@ -0,0 +1,110 @@ +// Copyright 2021 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 + +package org.terasology.engine.rendering.assets.mesh; + +import org.joml.Matrix4f; +import org.joml.Vector3f; +import org.junit.jupiter.api.Test; +import org.terasology.joml.test.VectorAssert; +import org.terasology.nui.Color; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class StandardMeshDataTest { + + @Test + public void combineStandardMeshData() { + StandardMeshData m1 = new StandardMeshData(); + m1.position.put(new Vector3f(10f, 10f, 10f)); + m1.position.put(new Vector3f(15f, 20f, 10f)); + m1.position.put(new Vector3f(10f, 30f, 10f)); + + StandardMeshData m2 = new StandardMeshData(); + m2.position.put(new Vector3f(10f, 10f, 10f)); + m2.position.put(new Vector3f(15f, 20f, 10f)); + m2.position.put(new Vector3f(10f, 30f, 10f)); + + m1.combine(new Matrix4f(), m2); + + assertEquals(m1.position.getPosition(), 6); + assertEquals(m1.normal.getPosition(), 0); + assertEquals(m1.uv0.getPosition(), 0); + assertEquals(m1.uv1.getPosition(), 0); + assertEquals(m1.color0.getPosition(), 0); + + VectorAssert.assertEquals(m1.position.get(0, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(1, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(2, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f); + + VectorAssert.assertEquals(m1.position.get(3, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(4, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(5, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f); + + } + + @Test + public void combineStandardMeshDataWithTransform() { + StandardMeshData m1 = new StandardMeshData(); + m1.position.put(new Vector3f(-1f, 1f, 0f)); + m1.position.put(new Vector3f(1f, 1f, 0f)); + m1.position.put(new Vector3f(1f, -1f, 0f)); + m1.position.put(new Vector3f(-1f, -1f, 0f)); + + StandardMeshData m2 = new StandardMeshData(); + m2.position.put(new Vector3f(-1f, 1f, 0f)); + m2.position.put(new Vector3f(1f, 1f, 0f)); + m2.position.put(new Vector3f(1f, -1f, 0f)); + m2.position.put(new Vector3f(-1f, -1f, 0f)); + + m1.combine(new Matrix4f().translate(10f,0,0), m2); + + VectorAssert.assertEquals(m1.position.get(0, new Vector3f()), new Vector3f(-1f, 1f, 0f), .001f); + VectorAssert.assertEquals(m1.position.get(1, new Vector3f()), new Vector3f(1f, 1f, 0f), .001f); + VectorAssert.assertEquals(m1.position.get(2, new Vector3f()), new Vector3f(1f, -1f, 0f), .001f); + VectorAssert.assertEquals(m1.position.get(3, new Vector3f()), new Vector3f(-1f, -1f, 0f), .001f); + + VectorAssert.assertEquals(m1.position.get(4, new Vector3f()), new Vector3f(9f, 1f, 0), .001f); + VectorAssert.assertEquals(m1.position.get(5, new Vector3f()), new Vector3f(11f, 1f, 0), .001f); + VectorAssert.assertEquals(m1.position.get(6, new Vector3f()), new Vector3f(11f, -1f, 0), .001f); + VectorAssert.assertEquals(m1.position.get(7, new Vector3f()), new Vector3f(9f, -1f, 0), .001f); + } + + @Test + public void secondMeshWithColorCombineStandardMeshData() { + StandardMeshData m1 = new StandardMeshData(); + m1.position.put(new Vector3f(10f, 10f, 10f)); + m1.position.put(new Vector3f(15f, 20f, 10f)); + m1.position.put(new Vector3f(10f, 30f, 10f)); + + StandardMeshData m2 = new StandardMeshData(); + m2.position.put(new Vector3f(10f, 10f, 10f)); + m2.position.put(new Vector3f(15f, 20f, 10f)); + m2.position.put(new Vector3f(10f, 30f, 10f)); + m2.color0.put(Color.blue); + m2.color0.put(Color.blue); + m2.color0.put(Color.blue); + + m1.combine(new Matrix4f(), m2); + + assertEquals(m1.position.getPosition(), 6); + assertEquals(m1.color0.getPosition(), 6); + assertEquals(m1.normal.getPosition(), 0); + assertEquals(m1.uv0.getPosition(), 0); + assertEquals(m1.uv1.getPosition(), 0); + + VectorAssert.assertEquals(m1.position.get(0, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(1, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(2, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f); + assertEquals(m1.color0.get(0, new Color()), new Color(Color.black)); + assertEquals(m1.color0.get(1, new Color()), new Color(Color.black)); + assertEquals(m1.color0.get(2, new Color()), new Color(Color.black)); + + VectorAssert.assertEquals(m1.position.get(3, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(4, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f); + VectorAssert.assertEquals(m1.position.get(5, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f); + assertEquals(m1.color0.get(3, new Color()), new Color(Color.blue)); + assertEquals(m1.color0.get(4, new Color()), new Color(Color.blue)); + assertEquals(m1.color0.get(5, new Color()), new Color(Color.blue)); + } +} diff --git a/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/StandardMeshData.java b/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/StandardMeshData.java index 1229bfb95c5..bfab51674e0 100644 --- a/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/StandardMeshData.java +++ b/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/StandardMeshData.java @@ -3,6 +3,7 @@ package org.terasology.engine.rendering.assets.mesh; +import org.joml.Matrix4f; import org.joml.Vector2f; import org.joml.Vector2fc; import org.joml.Vector3f; @@ -123,6 +124,39 @@ public void reallocate(int numVerts, int numIndices) { indices.allocateElements(numIndices); } + + /** + * append mesh data to current mesh + * @param transform transformation to apply to target + * @param target target mesh + */ + public void combine(Matrix4f transform, StandardMeshData target) { + int start = this.position.getPosition(); + if (!position.isEmpty() || !target.position.isEmpty()) { + position.setPosition(start); + Vector3f temp = new Vector3f(); + for (int i = 0; i < target.position.elements(); i++) { + this.position.put(transform.transformPosition(target.position.get(i, temp))); + } + } + if (!normal.isEmpty() || !target.normal.isEmpty()) { + VertexAttributeBinding.copy(target.normal, start, this.normal, new Vector3f()); + } + if (!uv0.isEmpty() || !target.uv0.isEmpty()) { + VertexAttributeBinding.copy(target.uv0, start, this.uv0, new Vector2f()); + } + if (!uv1.isEmpty() || !target.uv1.isEmpty()) { + VertexAttributeBinding.copy(target.uv1, start, this.uv1, new Vector2f()); + } + if (!color0.isEmpty() || !target.color0.isEmpty()) { + VertexAttributeBinding.copy(target.color0, start, this.color0, new Color()); + } + if (!light0.isEmpty() || !target.light0.isEmpty()) { + VertexAttributeBinding.copy(target.light0, start, this.light0, new Vector3f()); + } + } + + @Override public VertexAttributeBinding positions() { return position; diff --git a/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/resource/VertexAttributeBinding.java b/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/resource/VertexAttributeBinding.java index e9354b72d61..79163fc310c 100644 --- a/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/resource/VertexAttributeBinding.java +++ b/engine/src/main/java/org/terasology/engine/rendering/assets/mesh/resource/VertexAttributeBinding.java @@ -16,11 +16,14 @@ public VertexAttributeBinding(VertexResource resource, int offset, VertexAttribu this.attribute = attribute; } - public int elements() { return getResource().elements(); } + public boolean isEmpty() { + return getResource().isEmpty(); + } + @Override public void reserve(int vertCount) { resource.reserveElements(vertCount); @@ -32,6 +35,22 @@ public void allocate(int elements) { resource.mark(); } + /** + * + * @param from data getting copied from + * @param position position to start copying to dest + * @param dest the destination to fill + * @param target a temporary object to transfer data between from and dest + * @param + * @param + */ + public static void copy(VertexAttributeBinding from, int position, VertexAttributeBinding dest, I target) { + dest.setPosition(position); + for (int i = 0; i < from.elements(); i++) { + dest.put(from.get(i, target)); + } + } + /** * write a value by the index. * From ecb9d1a6f5579b9b917d7093300adae87ad22c84 Mon Sep 17 00:00:00 2001 From: Michael Pollind Date: Thu, 9 Sep 2021 21:35:40 -0700 Subject: [PATCH 2/2] fix color --- .../engine/rendering/assets/mesh/StandardMeshDataTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java b/engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java index 106e5ea0091..318a007a514 100644 --- a/engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java +++ b/engine-tests/src/test/java/org/terasology/engine/rendering/assets/mesh/StandardMeshDataTest.java @@ -96,9 +96,9 @@ public void secondMeshWithColorCombineStandardMeshData() { VectorAssert.assertEquals(m1.position.get(0, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f); VectorAssert.assertEquals(m1.position.get(1, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f); VectorAssert.assertEquals(m1.position.get(2, new Vector3f()), new Vector3f(10f, 30f, 10f), .001f); - assertEquals(m1.color0.get(0, new Color()), new Color(Color.black)); - assertEquals(m1.color0.get(1, new Color()), new Color(Color.black)); - assertEquals(m1.color0.get(2, new Color()), new Color(Color.black)); + assertEquals(m1.color0.get(0, new Color()), new Color(Color.transparent)); + assertEquals(m1.color0.get(1, new Color()), new Color(Color.transparent)); + assertEquals(m1.color0.get(2, new Color()), new Color(Color.transparent)); VectorAssert.assertEquals(m1.position.get(3, new Vector3f()), new Vector3f(10f, 10f, 10f), .001f); VectorAssert.assertEquals(m1.position.get(4, new Vector3f()), new Vector3f(15f, 20f, 10f), .001f);