Skip to content

Commit 58fac3c

Browse files
committed
Culling function
Test is currently empty because I don't know how to pass the camera
1 parent 58a008e commit 58fac3c

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/main/java/com/cleanroommc/kirino/engine/render/geometry/component/MeshletComponent.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import org.joml.Vector4f;
1212
import org.jspecify.annotations.NonNull;
1313

14+
import java.util.Arrays;
15+
1416
@CleanComponent
1517
public class MeshletComponent implements ICleanComponent {
1618
public AABB aabb;
@@ -65,6 +67,8 @@ public static boolean cull(@NonNull MeshletComponent meshlet,
6567
Vector3f direction = new Vector3f();
6668
Vector4f tmp = new Vector4f();
6769

70+
boolean isFront = false, isOutsideView = true;
71+
6872
// Bounding box points
6973
Vector3f[] points = {
7074
// Meshlet
@@ -87,14 +91,41 @@ public static boolean cull(@NonNull MeshletComponent meshlet,
8791
new Vector3f(aabb.xMax, aabb.yMax, aabb.zMax)
8892
};
8993

94+
Integer[] indices = new Integer[16];
95+
96+
for (int i = 0; i < indices.length; i++) {
97+
indices[i] = i;
98+
}
99+
90100
// Convert points to use camera origin as (0;0)
91101
for (int i = 0; i < points.length; i++) {
92102
points[i].sub(camera.getWorldOffset(), points[i]);
93103
}
94104

105+
Arrays.sort(indices, (a, b) -> (int) (points[a].lengthSquared()-points[b].lengthSquared()));
106+
107+
for (int i = 0; i < 8; i++) {
108+
if (indices[i] >= 8) {
109+
isFront = true;
110+
break;
111+
}
112+
}
113+
114+
if (!isFront) {
115+
return true;
116+
}
117+
95118
camera.getViewRotationMatrix().getColumn(2, tmp);
96119
tmp.xyz(direction);
97120

98-
return true;
121+
direction.normalize(); // not sure if I need to do that
122+
for (int i = 8; i < points.length; i++) {
123+
float simm = direction.dot(points[i])/(direction.length()*points[i].length());
124+
if (simm < 0.f) {
125+
return true;
126+
}
127+
}
128+
129+
return false;
99130
}
100131
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.cleanroommc.test.kirino.meshlet;
2+
3+
import com.cleanroommc.kirino.engine.render.geometry.AABB;
4+
import com.cleanroommc.kirino.engine.render.geometry.component.MeshletComponent;
5+
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
6+
import org.joml.Vector3f;
7+
import org.junit.Before;
8+
9+
import java.util.List;
10+
11+
public class CullingTest {
12+
13+
private List<MeshletAABBPair> meshlets = new ObjectArrayList<>();
14+
15+
@Before
16+
public void setup() {
17+
for (int i = 0; i < 36; i++) {
18+
float multiplier = i % 2 == 0 ? 0.25f : 1.75f;
19+
Vector3f baseAABBPosition = new Vector3f((float) Math.sin(multiplier*10*i), (float) Math.cos(multiplier*10*i), 0.f);
20+
Vector3f baseMeshletPoint = new Vector3f((float) Math.sin(10*i), (float) Math.cos(10*i), 0.f);
21+
MeshletComponent component = new MeshletComponent();
22+
component.transparent = false;
23+
component.aabb = new AABB(baseMeshletPoint.x, baseMeshletPoint.y, baseMeshletPoint.z, baseMeshletPoint.x+ 3.f, baseMeshletPoint.y + 3.f, baseMeshletPoint.z + 3.f);
24+
AABB entity = new AABB(baseAABBPosition.x, baseAABBPosition.y, baseAABBPosition.z, baseAABBPosition.x+ 1.f, baseAABBPosition.y + 1.f, baseAABBPosition.z + 1.f);
25+
meshlets.add(new MeshletAABBPair(component, entity, i % 2 == 0));
26+
}
27+
}
28+
29+
private record MeshletAABBPair(MeshletComponent meshlet, AABB aabb, boolean expectedCullingResult) {}
30+
}

0 commit comments

Comments
 (0)