Octree .java

net.minecraft.client.renderer.Octree (class): members: cameraSectionCenter, OctreeVisitor, AxisSorting, visitNodes, Branch, Leaf, Node, maxZ, root, add.

net.minecraft.client.rendererclass289 linesclient9.8 KB

API Surface (9)

boolean add(final SectionRenderDispatcher.RenderSection section)
void visitNodes(final Octree.OctreeVisitor visitor, final Frustum frustum, final int closeDistance)
Octree.AxisSorting getAxisSorting(final int absXDiff, final int absYDiff, final int absZDiff)
boolean add(final SectionRenderDispatcher.RenderSection section)
void visitNodes(final Octree.OctreeVisitor visitor, boolean skipFrustumCheck, final Frustum frustum, final int depth, final int closeDistance, boolean isClose)
AABB getAABB()
void visitNodes(final Octree.OctreeVisitor visitor, final boolean skipFrustumCheck, final Frustum frustum, final int depth, final int closeDistance, boolean isClose)
SectionRenderDispatcher.RenderSection getSection()
AABB getAABB()

Fields & Constants (3)

root
cameraSectionCenter
maxZ

Full Source

Octree.java289 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.renderer;
2
3
import net.fabricmc.api.EnvType;
4
import net.fabricmc.api.Environment;
5
import net.minecraft.client.renderer.chunk.SectionRenderDispatcher;
6
import net.minecraft.client.renderer.culling.Frustum;
7
import net.minecraft.core.BlockPos;
8
import net.minecraft.core.SectionPos;
9
import net.minecraft.util.Mth;
10
import net.minecraft.world.level.levelgen.structure.BoundingBox;
11
import net.minecraft.world.phys.AABB;
12
import org.jspecify.annotations.Nullable;
13
14
@Environment(EnvType.CLIENT)
15
public class Octree {
16
	private final Octree.Branch root;
17
	private final BlockPos cameraSectionCenter;
18
19
	public Octree(final SectionPos cameraSection, final int renderDistance, final int sectionsPerChunk, final int minBlockY) {
20
		int visibleAreaDiameterInSections = renderDistance * 2 + 1;
21
		int boundingBoxSizeInSections = Mth.smallestEncompassingPowerOfTwo(visibleAreaDiameterInSections);
22
		int distanceToBBEdgeInBlocks = renderDistance * 16;
23
		BlockPos cameraSectionOrigin = cameraSection.origin();
24
		this.cameraSectionCenter = cameraSection.center();
25
		int minX = cameraSectionOrigin.getX() - distanceToBBEdgeInBlocks;
26
		int maxX = minX + boundingBoxSizeInSections * 16 - 1;
27
		int minY = boundingBoxSizeInSections >= sectionsPerChunk ? minBlockY : cameraSectionOrigin.getY() - distanceToBBEdgeInBlocks;
28
		int maxY = minY + boundingBoxSizeInSections * 16 - 1;
29
		int minZ = cameraSectionOrigin.getZ() - distanceToBBEdgeInBlocks;
30
		int maxZ = minZ + boundingBoxSizeInSections * 16 - 1;
31
		this.root = new Octree.Branch(new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ));
32
	}
33
34
	public boolean add(final SectionRenderDispatcher.RenderSection section) {
35
		return this.root.add(section);
36
	}
37
38
	public void visitNodes(final Octree.OctreeVisitor visitor, final Frustum frustum, final int closeDistance) {
39
		this.root.visitNodes(visitor, false, frustum, 0, closeDistance, true);
40
	}
41
42
	private boolean isClose(
43
		final double minX, final double minY, final double minZ, final double maxX, final double maxY, final double maxZ, final int closeDistance
44
	) {
45
		int cameraX = this.cameraSectionCenter.getX();
46
		int cameraY = this.cameraSectionCenter.getY();
47
		int cameraZ = this.cameraSectionCenter.getZ();
48
		return cameraX > minX - closeDistance
49
			&& cameraX < maxX + closeDistance
50
			&& cameraY > minY - closeDistance
51
			&& cameraY < maxY + closeDistance
52
			&& cameraZ > minZ - closeDistance
53
			&& cameraZ < maxZ + closeDistance;
54
	}
55
56
	@Environment(EnvType.CLIENT)
57
	private enum AxisSorting {
58
		XYZ(4, 2, 1),
59
		XZY(4, 1, 2),
60
		YXZ(2, 4, 1),
61
		YZX(1, 4, 2),
62
		ZXY(2, 1, 4),
63
		ZYX(1, 2, 4);
64
65
		private final int xShift;
66
		private final int yShift;
67
		private final int zShift;
68
69
		AxisSorting(final int xShift, final int yShift, final int zShift) {
70
			this.xShift = xShift;
71
			this.yShift = yShift;
72
			this.zShift = zShift;
73
		}
74
75
		public static Octree.AxisSorting getAxisSorting(final int absXDiff, final int absYDiff, final int absZDiff) {
76
			if (absXDiff > absYDiff && absXDiff > absZDiff) {
77
				return absYDiff > absZDiff ? XYZ : XZY;
78
			} else if (absYDiff > absXDiff && absYDiff > absZDiff) {
79
				return absXDiff > absZDiff ? YXZ : YZX;
80
			} else {
81
				return absXDiff > absYDiff ? ZXY : ZYX;
82
			}
83
		}
84
	}
85
86
	@Environment(EnvType.CLIENT)
87
	private class Branch implements Octree.Node {
88
		private final Octree.@Nullable Node[] nodes = new Octree.Node[8];
89
		private final BoundingBox boundingBox;
90
		private final int bbCenterX;
91
		private final int bbCenterY;
92
		private final int bbCenterZ;
93
		private final Octree.AxisSorting sorting;
94
		private final boolean cameraXDiffNegative;
95
		private final boolean cameraYDiffNegative;
96
		private final boolean cameraZDiffNegative;
97
98
		public Branch(final BoundingBox boundingBox) {
99
			this.boundingBox = boundingBox;
100
			this.bbCenterX = this.boundingBox.minX() + this.boundingBox.getXSpan() / 2;
101
			this.bbCenterY = this.boundingBox.minY() + this.boundingBox.getYSpan() / 2;
102
			this.bbCenterZ = this.boundingBox.minZ() + this.boundingBox.getZSpan() / 2;
103
			int cameraXDiff = Octree.this.cameraSectionCenter.getX() - this.bbCenterX;
104
			int cameraYDiff = Octree.this.cameraSectionCenter.getY() - this.bbCenterY;
105
			int cameraZDiff = Octree.this.cameraSectionCenter.getZ() - this.bbCenterZ;
106
			this.sorting = Octree.AxisSorting.getAxisSorting(Math.abs(cameraXDiff), Math.abs(cameraYDiff), Math.abs(cameraZDiff));
107
			this.cameraXDiffNegative = cameraXDiff < 0;
108
			this.cameraYDiffNegative = cameraYDiff < 0;
109
			this.cameraZDiffNegative = cameraZDiff < 0;
110
		}
111
112
		public boolean add(final SectionRenderDispatcher.RenderSection section) {
113
			long sectionNode = section.getSectionNode();
114
			boolean sectionXDiffNegative = SectionPos.sectionToBlockCoord(SectionPos.x(sectionNode)) - this.bbCenterX < 0;
115
			boolean sectionYDiffNegative = SectionPos.sectionToBlockCoord(SectionPos.y(sectionNode)) - this.bbCenterY < 0;
116
			boolean sectionZDiffNegative = SectionPos.sectionToBlockCoord(SectionPos.z(sectionNode)) - this.bbCenterZ < 0;
117
			boolean xDiffsOppositeSides = sectionXDiffNegative != this.cameraXDiffNegative;
118
			boolean yDiffsOppositeSides = sectionYDiffNegative != this.cameraYDiffNegative;
119
			boolean zDiffsOppositeSides = sectionZDiffNegative != this.cameraZDiffNegative;
120
			int nodeIndex = getNodeIndex(this.sorting, xDiffsOppositeSides, yDiffsOppositeSides, zDiffsOppositeSides);
121
			if (this.areChildrenLeaves()) {
122
				boolean alreadyExisted = this.nodes[nodeIndex] != null;
123
				this.nodes[nodeIndex] = Octree.this.new Leaf(section);
124
				return !alreadyExisted;
125
			} else if (this.nodes[nodeIndex] != null) {
126
				Octree.Branch branch = (Octree.Branch)this.nodes[nodeIndex];
127
				return branch.add(section);
128
			} else {
129
				BoundingBox childBoundingBox = this.createChildBoundingBox(sectionXDiffNegative, sectionYDiffNegative, sectionZDiffNegative);
130
				Octree.Branch branch = Octree.this.new Branch(childBoundingBox);
131
				this.nodes[nodeIndex] = branch;
132
				return branch.add(section);
133
			}
134
		}
135
136
		private static int getNodeIndex(
137
			final Octree.AxisSorting sorting, final boolean xDiffsOppositeSides, final boolean yDiffsOppositeSides, final boolean zDiffsOppositeSides
138
		) {
139
			int index = 0;
140
			if (xDiffsOppositeSides) {
141
				index += sorting.xShift;
142
			}
143
144
			if (yDiffsOppositeSides) {
145
				index += sorting.yShift;
146
			}
147
148
			if (zDiffsOppositeSides) {
149
				index += sorting.zShift;
150
			}
151
152
			return index;
153
		}
154
155
		private boolean areChildrenLeaves() {
156
			return this.boundingBox.getXSpan() == 32;
157
		}
158
159
		private BoundingBox createChildBoundingBox(final boolean sectionXDiffNegative, final boolean sectionYDiffNegative, final boolean sectionZDiffNegative) {
160
			int minX;
161
			int maxX;
162
			if (sectionXDiffNegative) {
163
				minX = this.boundingBox.minX();
164
				maxX = this.bbCenterX - 1;
165
			} else {
166
				minX = this.bbCenterX;
167
				maxX = this.boundingBox.maxX();
168
			}
169
170
			int minY;
171
			int maxY;
172
			if (sectionYDiffNegative) {
173
				minY = this.boundingBox.minY();
174
				maxY = this.bbCenterY - 1;
175
			} else {
176
				minY = this.bbCenterY;
177
				maxY = this.boundingBox.maxY();
178
			}
179
180
			int minZ;
181
			int maxZ;
182
			if (sectionZDiffNegative) {
183
				minZ = this.boundingBox.minZ();
184
				maxZ = this.bbCenterZ - 1;
185
			} else {
186
				minZ = this.bbCenterZ;
187
				maxZ = this.boundingBox.maxZ();
188
			}
189
190
			return new BoundingBox(minX, minY, minZ, maxX, maxY, maxZ);
191
		}
192
193
		@Override
194
		public void visitNodes(
195
			final Octree.OctreeVisitor visitor, boolean skipFrustumCheck, final Frustum frustum, final int depth, final int closeDistance, boolean isClose
196
		) {
197
			boolean isVisible = skipFrustumCheck;
198
			if (!skipFrustumCheck) {
199
				int checkResult = frustum.cubeInFrustum(this.boundingBox);
200
				skipFrustumCheck = checkResult == -2;
201
				isVisible = checkResult == -2 || checkResult == -1;
202
			}
203
204
			if (isVisible) {
205
				isClose = isClose
206
					&& Octree.this.isClose(
207
						this.boundingBox.minX(),
208
						this.boundingBox.minY(),
209
						this.boundingBox.minZ(),
210
						this.boundingBox.maxX(),
211
						this.boundingBox.maxY(),
212
						this.boundingBox.maxZ(),
213
						closeDistance
214
					);
215
				visitor.visit(this, skipFrustumCheck, depth, isClose);
216
217
				for (Octree.Node node : this.nodes) {
218
					if (node != null) {
219
						node.visitNodes(visitor, skipFrustumCheck, frustum, depth + 1, closeDistance, isClose);
220
					}
221
				}
222
			}
223
		}
224
225
		@Override
226
		public SectionRenderDispatcher.@Nullable RenderSection getSection() {
227
			return null;
228
		}
229
230
		@Override
231
		public AABB getAABB() {
232
			return new AABB(
233
				this.boundingBox.minX(),
234
				this.boundingBox.minY(),
235
				this.boundingBox.minZ(),
236
				this.boundingBox.maxX() + 1,
237
				this.boundingBox.maxY() + 1,
238
				this.boundingBox.maxZ() + 1
239
			);
240
		}
241
	}
242
243
	@Environment(EnvType.CLIENT)
244
	private final class Leaf implements Octree.Node {
245
		private final SectionRenderDispatcher.RenderSection section;
246
247
		private Leaf(final SectionRenderDispatcher.RenderSection section) {
248
			this.section = section;
249
		}
250
251
		@Override
252
		public void visitNodes(
253
			final Octree.OctreeVisitor visitor, final boolean skipFrustumCheck, final Frustum frustum, final int depth, final int closeDistance, boolean isClose
254
		) {
255
			AABB boundingBox = this.section.getBoundingBox();
256
			if (skipFrustumCheck || frustum.isVisible(this.getSection().getBoundingBox())) {
257
				isClose = isClose
258
					&& Octree.this.isClose(boundingBox.minX, boundingBox.minY, boundingBox.minZ, boundingBox.maxX, boundingBox.maxY, boundingBox.maxZ, closeDistance);
259
				visitor.visit(this, skipFrustumCheck, depth, isClose);
260
			}
261
		}
262
263
		@Override
264
		public SectionRenderDispatcher.RenderSection getSection() {
265
			return this.section;
266
		}
267
268
		@Override
269
		public AABB getAABB() {
270
			return this.section.getBoundingBox();
271
		}
272
	}
273
274
	@Environment(EnvType.CLIENT)
275
	public interface Node {
276
		void visitNodes(Octree.OctreeVisitor visitor, boolean skipFrustumCheck, Frustum frustum, int depth, final int closeDistance, boolean isClose);
277
278
		SectionRenderDispatcher.@Nullable RenderSection getSection();
279
280
		AABB getAABB();
281
	}
282
283
	@FunctionalInterface
284
	@Environment(EnvType.CLIENT)
285
	public interface OctreeVisitor {
286
		void visit(final Octree.Node node, final boolean fullyVisible, int depth, boolean isClose);
287
	}
288
}
289

Decompiled Minecraft 26.3 source of Octree (net/minecraft/client/renderer/Octree.java) with Mojang official mappings, stored verbatim on the site.