| 1 | package net.minecraft.client.renderer.culling;
|
| 2 |
|
| 3 | import net.fabricmc.api.EnvType;
|
| 4 | import net.fabricmc.api.Environment;
|
| 5 | import net.minecraft.world.level.levelgen.structure.BoundingBox;
|
| 6 | import net.minecraft.world.phys.AABB;
|
| 7 | import net.minecraft.world.phys.AABB.Builder;
|
| 8 | import org.joml.FrustumIntersection;
|
| 9 | import org.joml.Matrix4f;
|
| 10 | import org.joml.Matrix4fc;
|
| 11 | import org.joml.Vector3f;
|
| 12 | import org.joml.Vector4f;
|
| 13 |
|
| 14 | @Environment(EnvType.CLIENT)
|
| 15 | public class Frustum {
|
| 16 | public static final int OFFSET_STEP = 4;
|
| 17 | private final FrustumIntersection intersection = new FrustumIntersection();
|
| 18 | private final Matrix4f matrix = new Matrix4f();
|
| 19 | private Vector4f viewVector;
|
| 20 | private double camX;
|
| 21 | private double camY;
|
| 22 | private double camZ;
|
| 23 |
|
| 24 | public Frustum(final Matrix4fc modelView, final Matrix4f projection) {
|
| 25 | this.calculateFrustum(modelView, projection);
|
| 26 | }
|
| 27 |
|
| 28 | public Frustum(final Frustum frustum) {
|
| 29 | this.set(frustum);
|
| 30 | }
|
| 31 |
|
| 32 | public void set(final Frustum frustum) {
|
| 33 | this.intersection.set(frustum.matrix);
|
| 34 | this.matrix.set(frustum.matrix);
|
| 35 | this.camX = frustum.camX;
|
| 36 | this.camY = frustum.camY;
|
| 37 | this.camZ = frustum.camZ;
|
| 38 | this.viewVector = frustum.viewVector;
|
| 39 | }
|
| 40 |
|
| 41 | public Frustum offset(final float offset) {
|
| 42 | this.camX = this.camX + this.viewVector.x * offset;
|
| 43 | this.camY = this.camY + this.viewVector.y * offset;
|
| 44 | this.camZ = this.camZ + this.viewVector.z * offset;
|
| 45 | return this;
|
| 46 | }
|
| 47 |
|
| 48 | public Frustum offsetToFullyIncludeCameraCube(final int cubeSize) {
|
| 49 | double camX1 = Math.floor(this.camX / cubeSize) * cubeSize;
|
| 50 | double camY1 = Math.floor(this.camY / cubeSize) * cubeSize;
|
| 51 | double camZ1 = Math.floor(this.camZ / cubeSize) * cubeSize;
|
| 52 | double camX2 = Math.ceil(this.camX / cubeSize) * cubeSize;
|
| 53 | double camY2 = Math.ceil(this.camY / cubeSize) * cubeSize;
|
| 54 |
|
| 55 | for (double camZ2 = Math.ceil(this.camZ / cubeSize) * cubeSize;
|
| 56 | this.intersection
|
| 57 | .intersectAab(
|
| 58 | (float)(camX1 - this.camX),
|
| 59 | (float)(camY1 - this.camY),
|
| 60 | (float)(camZ1 - this.camZ),
|
| 61 | (float)(camX2 - this.camX),
|
| 62 | (float)(camY2 - this.camY),
|
| 63 | (float)(camZ2 - this.camZ)
|
| 64 | )
|
| 65 | != -2;
|
| 66 | this.camZ = this.camZ - this.viewVector.z() * 4.0F
|
| 67 | ) {
|
| 68 | this.camX = this.camX - this.viewVector.x() * 4.0F;
|
| 69 | this.camY = this.camY - this.viewVector.y() * 4.0F;
|
| 70 | }
|
| 71 |
|
| 72 | return this;
|
| 73 | }
|
| 74 |
|
| 75 | public void prepare(final double camX, final double camY, final double camZ) {
|
| 76 | this.camX = camX;
|
| 77 | this.camY = camY;
|
| 78 | this.camZ = camZ;
|
| 79 | }
|
| 80 |
|
| 81 | private void calculateFrustum(final Matrix4fc modelView, final Matrix4f projection) {
|
| 82 | projection.mul(modelView, this.matrix);
|
| 83 | this.intersection.set(this.matrix);
|
| 84 | this.viewVector = this.matrix.transformTranspose(new Vector4f(0.0F, 0.0F, 1.0F, 0.0F));
|
| 85 | }
|
| 86 |
|
| 87 | public boolean isVisible(final AABB bb) {
|
| 88 | int intersectionResult = this.cubeInFrustum(bb.minX, bb.minY, bb.minZ, bb.maxX, bb.maxY, bb.maxZ);
|
| 89 | return intersectionResult == -2 || intersectionResult == -1;
|
| 90 | }
|
| 91 |
|
| 92 | public int cubeInFrustum(final BoundingBox bb) {
|
| 93 | return this.cubeInFrustum(bb.minX(), bb.minY(), bb.minZ(), bb.maxX() + 1, bb.maxY() + 1, bb.maxZ() + 1);
|
| 94 | }
|
| 95 |
|
| 96 | private int cubeInFrustum(final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ) {
|
| 97 | float x1 = (float)(minX - this.camX);
|
| 98 | float y1 = (float)(minY - this.camY);
|
| 99 | float z1 = (float)(minZ - this.camZ);
|
| 100 | float x2 = (float)(maxX - this.camX);
|
| 101 | float y2 = (float)(maxY - this.camY);
|
| 102 | float z2 = (float)(maxZ - this.camZ);
|
| 103 | return this.intersection.intersectAab(x1, y1, z1, x2, y2, z2);
|
| 104 | }
|
| 105 |
|
| 106 | public boolean pointInFrustum(final double x, final double y, final double z) {
|
| 107 | return this.intersection.testPoint((float)(x - this.camX), (float)(y - this.camY), (float)(z - this.camZ));
|
| 108 | }
|
| 109 |
|
| 110 | public Vector4f[] getFrustumPoints() {
|
| 111 | Vector4f[] frustumPoints = new Vector4f[]{
|
| 112 | new Vector4f(-1.0F, -1.0F, -1.0F, 1.0F),
|
| 113 | new Vector4f(1.0F, -1.0F, -1.0F, 1.0F),
|
| 114 | new Vector4f(1.0F, 1.0F, -1.0F, 1.0F),
|
| 115 | new Vector4f(-1.0F, 1.0F, -1.0F, 1.0F),
|
| 116 | new Vector4f(-1.0F, -1.0F, 1.0F, 1.0F),
|
| 117 | new Vector4f(1.0F, -1.0F, 1.0F, 1.0F),
|
| 118 | new Vector4f(1.0F, 1.0F, 1.0F, 1.0F),
|
| 119 | new Vector4f(-1.0F, 1.0F, 1.0F, 1.0F)
|
| 120 | };
|
| 121 | Matrix4f clipToWorldMatrix = this.matrix.invert(new Matrix4f());
|
| 122 |
|
| 123 | for (int i = 0; i < 8; i++) {
|
| 124 | clipToWorldMatrix.transform(frustumPoints[i]);
|
| 125 | frustumPoints[i].div(frustumPoints[i].w());
|
| 126 | }
|
| 127 |
|
| 128 | return frustumPoints;
|
| 129 | }
|
| 130 |
|
| 131 | public AABB getNearPlaneBounds() {
|
| 132 | Matrix4f clipToWorldMatrix = this.matrix.invert(new Matrix4f());
|
| 133 | Builder builder = new Builder();
|
| 134 | builder.include(clipToWorldMatrix.transformProject(new Vector3f(-1.0F, -1.0F, -1.0F)));
|
| 135 | builder.include(clipToWorldMatrix.transformProject(new Vector3f(1.0F, -1.0F, -1.0F)));
|
| 136 | builder.include(clipToWorldMatrix.transformProject(new Vector3f(1.0F, 1.0F, -1.0F)));
|
| 137 | builder.include(clipToWorldMatrix.transformProject(new Vector3f(-1.0F, 1.0F, -1.0F)));
|
| 138 | return builder.build();
|
| 139 | }
|
| 140 |
|
| 141 | public double getCamX() {
|
| 142 | return this.camX;
|
| 143 | }
|
| 144 |
|
| 145 | public double getCamY() {
|
| 146 | return this.camY;
|
| 147 | }
|
| 148 |
|
| 149 | public double getCamZ() {
|
| 150 | return this.camZ;
|
| 151 | }
|
| 152 | }
|
| 153 |
|