ModelDiscovery .java

net.minecraft.client.resources.model.ModelDiscovery (class): members: parentDiscoveryQueue, uncachedResolver, addSpecialModel, modelWrappers, ModelWrapper.

net.minecraft.client.resources.modelclass248 linesclient9.1 KB

API Surface (13)

void addRoot(final ResolvableModel model)
void addSpecialModel(final Identifier id, final UnbakedModel model)
ResolvedModel missingModel()
Map<Identifier, ResolvedModel> resolve()
UnbakedModel wrapped()
String debugName()
boolean getTopAmbientOcclusion()
UnbakedModel.GuiLight getTopGuiLight()
ItemTransforms getTopTransforms()
UnbakedGeometry getTopGeometry()
TextureSlots getTopTextureSlots()
Material.Baked resolveParticleMaterial(final TextureSlots textureSlots, final ModelBaker baker)
QuadCollection bakeTopGeometry(final TextureSlots textureSlots, final ModelBaker baker, final ModelState state)

Fields & Constants (6)

LOGGER
modelWrappers
missingModel
uncachedResolver
resolver
parentDiscoveryQueue

Full Source

ModelDiscovery.java248 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.client.resources.model;
2
3
import com.google.common.collect.ImmutableMap;
4
import com.google.common.collect.ImmutableMap.Builder;
5
import com.mojang.logging.LogUtils;
6
import it.unimi.dsi.fastutil.objects.Object2ObjectFunction;
7
import it.unimi.dsi.fastutil.objects.Object2ObjectMap;
8
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
9
import java.util.ArrayDeque;
10
import java.util.ArrayList;
11
import java.util.Iterator;
12
import java.util.List;
13
import java.util.Map;
14
import java.util.Objects;
15
import java.util.Queue;
16
import java.util.concurrent.ConcurrentHashMap;
17
import java.util.concurrent.atomic.AtomicReferenceArray;
18
import java.util.function.Function;
19
import net.fabricmc.api.EnvType;
20
import net.fabricmc.api.Environment;
21
import net.minecraft.client.renderer.block.dispatch.BlockModelRotation;
22
import net.minecraft.client.renderer.block.dispatch.ModelState;
23
import net.minecraft.client.resources.model.cuboid.ItemTransforms;
24
import net.minecraft.client.resources.model.cuboid.MissingCuboidModel;
25
import net.minecraft.client.resources.model.geometry.QuadCollection;
26
import net.minecraft.client.resources.model.geometry.UnbakedGeometry;
27
import net.minecraft.client.resources.model.sprite.Material;
28
import net.minecraft.client.resources.model.sprite.TextureSlots;
29
import net.minecraft.resources.Identifier;
30
import org.jspecify.annotations.Nullable;
31
import org.slf4j.Logger;
32
33
@Environment(EnvType.CLIENT)
34
public class ModelDiscovery {
35
	private static final Logger LOGGER = LogUtils.getLogger();
36
	private final Object2ObjectMap<Identifier, ModelDiscovery.ModelWrapper> modelWrappers = new Object2ObjectOpenHashMap<>();
37
	private final ModelDiscovery.ModelWrapper missingModel;
38
	private final Object2ObjectFunction<Identifier, ModelDiscovery.ModelWrapper> uncachedResolver;
39
	private final ResolvableModel.Resolver resolver;
40
	private final Queue<ModelDiscovery.ModelWrapper> parentDiscoveryQueue = new ArrayDeque<>();
41
42
	public ModelDiscovery(final Map<Identifier, UnbakedModel> unbakedModels, final UnbakedModel missingUnbakedModel) {
43
		this.missingModel = new ModelDiscovery.ModelWrapper(MissingCuboidModel.LOCATION, missingUnbakedModel, true);
44
		this.modelWrappers.put(MissingCuboidModel.LOCATION, this.missingModel);
45
		this.uncachedResolver = rawId -> {
46
			Identifier id = (Identifier)rawId;
47
			UnbakedModel rawModel = unbakedModels.get(id);
48
			if (rawModel == null) {
49
				LOGGER.warn("Missing block model: {}", id);
50
				return this.missingModel;
51
			} else {
52
				return this.createAndQueueWrapper(id, rawModel);
53
			}
54
		};
55
		this.resolver = this::getOrCreateModel;
56
	}
57
58
	private static boolean isRoot(final UnbakedModel model) {
59
		return model.parent() == null;
60
	}
61
62
	private ModelDiscovery.ModelWrapper getOrCreateModel(final Identifier id) {
63
		return this.modelWrappers.computeIfAbsent(id, this.uncachedResolver);
64
	}
65
66
	private ModelDiscovery.ModelWrapper createAndQueueWrapper(final Identifier id, final UnbakedModel rawModel) {
67
		boolean isRoot = isRoot(rawModel);
68
		ModelDiscovery.ModelWrapper result = new ModelDiscovery.ModelWrapper(id, rawModel, isRoot);
69
		if (!isRoot) {
70
			this.parentDiscoveryQueue.add(result);
71
		}
72
73
		return result;
74
	}
75
76
	public void addRoot(final ResolvableModel model) {
77
		model.resolveDependencies(this.resolver);
78
	}
79
80
	public void addSpecialModel(final Identifier id, final UnbakedModel model) {
81
		if (!isRoot(model)) {
82
			LOGGER.warn("Trying to add non-root special model {}, ignoring", id);
83
		} else {
84
			ModelDiscovery.ModelWrapper previous = this.modelWrappers.put(id, this.createAndQueueWrapper(id, model));
85
			if (previous != null) {
86
				LOGGER.warn("Duplicate special model {}", id);
87
			}
88
		}
89
	}
90
91
	public ResolvedModel missingModel() {
92
		return this.missingModel;
93
	}
94
95
	public Map<Identifier, ResolvedModel> resolve() {
96
		List<ModelDiscovery.ModelWrapper> toValidate = new ArrayList<>();
97
		this.discoverDependencies(toValidate);
98
		propagateValidity(toValidate);
99
		Builder<Identifier, ResolvedModel> result = ImmutableMap.builder();
100
		this.modelWrappers.forEach((location, model) -> {
101
			if (model.valid) {
102
				result.put(location, model);
103
			} else {
104
				LOGGER.warn("Model {} ignored due to cyclic dependency", location);
105
			}
106
		});
107
		return result.build();
108
	}
109
110
	private void discoverDependencies(final List<ModelDiscovery.ModelWrapper> toValidate) {
111
		ModelDiscovery.ModelWrapper current;
112
		while ((current = this.parentDiscoveryQueue.poll()) != null) {
113
			Identifier parentLocation = Objects.requireNonNull(current.wrapped.parent());
114
			ModelDiscovery.ModelWrapper parent = this.getOrCreateModel(parentLocation);
115
			current.parent = parent;
116
			if (parent.valid) {
117
				current.valid = true;
118
			} else {
119
				toValidate.add(current);
120
			}
121
		}
122
	}
123
124
	private static void propagateValidity(final List<ModelDiscovery.ModelWrapper> toValidate) {
125
		boolean progressed = true;
126
127
		while (progressed) {
128
			progressed = false;
129
			Iterator<ModelDiscovery.ModelWrapper> iterator = toValidate.iterator();
130
131
			while (iterator.hasNext()) {
132
				ModelDiscovery.ModelWrapper model = iterator.next();
133
				if (Objects.requireNonNull(model.parent).valid) {
134
					model.valid = true;
135
					iterator.remove();
136
					progressed = true;
137
				}
138
			}
139
		}
140
	}
141
142
	@Environment(EnvType.CLIENT)
143
	private static class ModelWrapper implements ResolvedModel {
144
		private static final ModelDiscovery.Slot<Boolean> KEY_AMBIENT_OCCLUSION = slot(0);
145
		private static final ModelDiscovery.Slot<UnbakedModel.GuiLight> KEY_GUI_LIGHT = slot(1);
146
		private static final ModelDiscovery.Slot<UnbakedGeometry> KEY_GEOMETRY = slot(2);
147
		private static final ModelDiscovery.Slot<ItemTransforms> KEY_TRANSFORMS = slot(3);
148
		private static final ModelDiscovery.Slot<TextureSlots> KEY_TEXTURE_SLOTS = slot(4);
149
		private static final ModelDiscovery.Slot<Material.Baked> KEY_PARTICLE_SPRITE = slot(5);
150
		private static final ModelDiscovery.Slot<QuadCollection> KEY_DEFAULT_GEOMETRY = slot(6);
151
		private static final int SLOT_COUNT = 7;
152
		private final Identifier id;
153
		private boolean valid;
154
		private ModelDiscovery.@Nullable ModelWrapper parent;
155
		private final UnbakedModel wrapped;
156
		private final AtomicReferenceArray<@Nullable Object> fixedSlots = new AtomicReferenceArray<>(7);
157
		private final Map<ModelState, QuadCollection> modelBakeCache = new ConcurrentHashMap<>();
158
159
		private static <T> ModelDiscovery.Slot<T> slot(final int index) {
160
			Objects.checkIndex(index, 7);
161
			return new ModelDiscovery.Slot<>(index);
162
		}
163
164
		private ModelWrapper(final Identifier id, final UnbakedModel wrapped, final boolean valid) {
165
			this.id = id;
166
			this.wrapped = wrapped;
167
			this.valid = valid;
168
		}
169
170
		@Override
171
		public UnbakedModel wrapped() {
172
			return this.wrapped;
173
		}
174
175
		@Override
176
		public @Nullable ResolvedModel parent() {
177
			return this.parent;
178
		}
179
180
		@Override
181
		public String debugName() {
182
			return this.id.toString();
183
		}
184
185
		private <T> @Nullable T getSlot(final ModelDiscovery.Slot<T> key) {
186
			return (T)this.fixedSlots.get(key.index);
187
		}
188
189
		private <T> T updateSlot(final ModelDiscovery.Slot<T> key, final T value) {
190
			T currentValue = (T)this.fixedSlots.compareAndExchange(key.index, null, value);
191
			return currentValue == null ? value : currentValue;
192
		}
193
194
		private <T> T getSimpleProperty(final ModelDiscovery.Slot<T> key, final Function<ResolvedModel, T> getter) {
195
			T result = this.getSlot(key);
196
			return result != null ? result : this.updateSlot(key, getter.apply(this));
197
		}
198
199
		@Override
200
		public boolean getTopAmbientOcclusion() {
201
			return this.getSimpleProperty(KEY_AMBIENT_OCCLUSION, ResolvedModel::findTopAmbientOcclusion);
202
		}
203
204
		@Override
205
		public UnbakedModel.GuiLight getTopGuiLight() {
206
			return this.getSimpleProperty(KEY_GUI_LIGHT, ResolvedModel::findTopGuiLight);
207
		}
208
209
		@Override
210
		public ItemTransforms getTopTransforms() {
211
			return this.getSimpleProperty(KEY_TRANSFORMS, ResolvedModel::findTopTransforms);
212
		}
213
214
		@Override
215
		public UnbakedGeometry getTopGeometry() {
216
			return this.getSimpleProperty(KEY_GEOMETRY, ResolvedModel::findTopGeometry);
217
		}
218
219
		@Override
220
		public TextureSlots getTopTextureSlots() {
221
			return this.getSimpleProperty(KEY_TEXTURE_SLOTS, ResolvedModel::findTopTextureSlots);
222
		}
223
224
		@Override
225
		public Material.Baked resolveParticleMaterial(final TextureSlots textureSlots, final ModelBaker baker) {
226
			Material.Baked result = this.getSlot(KEY_PARTICLE_SPRITE);
227
			return result != null ? result : this.updateSlot(KEY_PARTICLE_SPRITE, ResolvedModel.resolveParticleMaterial(textureSlots, baker, this));
228
		}
229
230
		private QuadCollection bakeDefaultState(final TextureSlots textureSlots, final ModelBaker baker, final ModelState state) {
231
			QuadCollection result = this.getSlot(KEY_DEFAULT_GEOMETRY);
232
			return result != null ? result : this.updateSlot(KEY_DEFAULT_GEOMETRY, this.getTopGeometry().bake(textureSlots, baker, state, this));
233
		}
234
235
		@Override
236
		public QuadCollection bakeTopGeometry(final TextureSlots textureSlots, final ModelBaker baker, final ModelState state) {
237
			return state == BlockModelRotation.IDENTITY ? this.bakeDefaultState(textureSlots, baker, state) : this.modelBakeCache.computeIfAbsent(state, s -> {
238
				UnbakedGeometry topGeometry = this.getTopGeometry();
239
				return topGeometry.bake(textureSlots, baker, s, this);
240
			});
241
		}
242
	}
243
244
	@Environment(EnvType.CLIENT)
245
	private record Slot<T>(int index) {
246
	}
247
}
248

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