-
Notifications
You must be signed in to change notification settings - Fork 538
Add an atlas registry #5390
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
CelDaemon
wants to merge
16
commits into
FabricMC:26.1.2
Choose a base branch
from
CelDaemon:atlas-registry
base: 26.1.2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+310
−4
Open
Add an atlas registry #5390
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b81b7c7
Add basic atlas registry
CelDaemon 46b0aaf
Fix double sprite source
CelDaemon 4bb9706
Fix checkstyle
CelDaemon 6213556
Add license
CelDaemon 9ac764d
Consistency
CelDaemon 87ef014
Better describe additionalMetadata parameters
CelDaemon 8506caf
Add metadata test, and render to HUD
CelDaemon d7ee778
Fix wording
CelDaemon 4bc46e5
Fix client gametest
CelDaemon 0e1bce5
Add method to generate standard atlas texture ids
CelDaemon 74163a5
Mention createTextureLocation in register params
CelDaemon 2415355
Include vanilla entries in collision check
CelDaemon 5a7c6a9
Clarify atlas texture id generation method name
CelDaemon e82ce31
Take atlas configs instead of constructing them internally
CelDaemon 33a9f03
Add a method to get currently registered atlases
CelDaemon f7437b5
Fix AtlasRegistry import order
CelDaemon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...ndering-v1/src/client/java/net/fabricmc/fabric/api/client/rendering/v1/AtlasRegistry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.api.client.rendering.v1; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import net.minecraft.client.resources.model.sprite.AtlasManager; | ||
| import net.minecraft.resources.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.impl.client.rendering.AtlasRegistryImpl; | ||
|
|
||
| /** | ||
| * A registry to add atlases to {@link net.minecraft.client.resources.model.sprite.AtlasManager}. | ||
| */ | ||
| public final class AtlasRegistry { | ||
| /** | ||
| * Registers an atlas using an atlas config. | ||
| * | ||
| * @param config The atlas config to register. | ||
| */ | ||
| public static void register(AtlasManager.AtlasConfig config) { | ||
| AtlasRegistryImpl.register(config); | ||
| } | ||
|
|
||
| /** | ||
| * Generates a texture id based on an atlas id. | ||
| * @param atlasId The atlas id to generate a texture id for. | ||
| * @return The generated texture id. | ||
| */ | ||
| public static Identifier generateTextureLocation(Identifier atlasId) { | ||
| return AtlasRegistryImpl.generateTextureLocation(atlasId); | ||
| } | ||
|
|
||
| /** | ||
| * Get all registered atlases. | ||
| * | ||
| * @return The currently registered atlases. | ||
| */ | ||
| public static List<AtlasManager.AtlasConfig> getAtlases() { | ||
| return AtlasRegistryImpl.getAtlases(); | ||
| } | ||
|
|
||
| private AtlasRegistry() { } | ||
| } |
80 changes: 80 additions & 0 deletions
80
...ering-v1/src/client/java/net/fabricmc/fabric/impl/client/rendering/AtlasRegistryImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.impl.client.rendering; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| import net.minecraft.client.resources.model.sprite.AtlasManager; | ||
| import net.minecraft.resources.Identifier; | ||
|
|
||
| import net.fabricmc.fabric.mixin.client.rendering.AtlasManagerAccessor; | ||
|
|
||
| public final class AtlasRegistryImpl { | ||
| private static final List<AtlasManager.AtlasConfig> REGISTERED_CONFIGS = new ArrayList<>(); | ||
|
|
||
| private static final Set<Identifier> REGISTERED_TEXTURES = new HashSet<>(); | ||
| private static final Set<Identifier> REGISTERED_ATLASES = new HashSet<>(); | ||
|
|
||
| static { | ||
| for (final AtlasManager.AtlasConfig knownAtlas : AtlasManagerAccessor.getKnownAtlases()) { | ||
| REGISTERED_TEXTURES.add(knownAtlas.textureId()); | ||
| REGISTERED_ATLASES.add(knownAtlas.definitionLocation()); | ||
| } | ||
| } | ||
|
|
||
| private static boolean frozen; | ||
|
|
||
| public static void register(AtlasManager.AtlasConfig config) { | ||
| Objects.requireNonNull(config, "config must not be null"); | ||
|
|
||
| if (frozen) { | ||
| throw new IllegalStateException("The atlas registry has already been finalized."); | ||
| } | ||
|
|
||
| if (REGISTERED_TEXTURES.contains(config.textureId())) { | ||
| throw new IllegalArgumentException("An atlas with texture " + config.textureId() + " has already been registered."); | ||
| } | ||
|
|
||
| if (REGISTERED_ATLASES.contains(config.definitionLocation())) { | ||
| throw new IllegalArgumentException("Atlas " + config.definitionLocation() + " has already been registered."); | ||
| } | ||
|
|
||
| REGISTERED_CONFIGS.add(config); | ||
| REGISTERED_ATLASES.add(config.definitionLocation()); | ||
| REGISTERED_TEXTURES.add(config.textureId()); | ||
| } | ||
|
|
||
| public static Identifier generateTextureLocation(Identifier atlasId) { | ||
| Objects.requireNonNull(atlasId, "atlasId must not be null"); | ||
| return atlasId.withPath(path -> "textures/atlas/" + path + ".png"); | ||
| } | ||
|
|
||
| public static List<AtlasManager.AtlasConfig> getAtlases() { | ||
| return List.copyOf(REGISTERED_CONFIGS); | ||
| } | ||
|
|
||
| public static List<AtlasManager.AtlasConfig> finalizeConfigs() { | ||
| frozen = true; | ||
| return getAtlases(); | ||
| } | ||
|
|
||
| private AtlasRegistryImpl() { } | ||
| } |
32 changes: 32 additions & 0 deletions
32
...g-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/AtlasManagerAccessor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.mixin.client.rendering; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.gen.Accessor; | ||
|
|
||
| import net.minecraft.client.resources.model.sprite.AtlasManager; | ||
|
|
||
| @Mixin(AtlasManager.class) | ||
| public interface AtlasManagerAccessor { | ||
| @Accessor("KNOWN_ATLASES") | ||
| static List<AtlasManager.AtlasConfig> getKnownAtlases() { | ||
| throw new AssertionError("Implemented via mixin"); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
...ring-v1/src/client/java/net/fabricmc/fabric/mixin/client/rendering/AtlasManagerMixin.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.mixin.client.rendering; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.llamalad7.mixinextras.injector.ModifyExpressionValue; | ||
| import org.objectweb.asm.Opcodes; | ||
| import org.spongepowered.asm.mixin.Mixin; | ||
| import org.spongepowered.asm.mixin.injection.At; | ||
|
|
||
| import net.minecraft.client.resources.model.sprite.AtlasManager; | ||
|
|
||
| import net.fabricmc.fabric.impl.client.rendering.AtlasRegistryImpl; | ||
|
|
||
| @Mixin(AtlasManager.class) | ||
| class AtlasManagerMixin { | ||
| @ModifyExpressionValue(method = "<init>", at = @At(value = "FIELD", target = "Lnet/minecraft/client/resources/model/sprite/AtlasManager;KNOWN_ATLASES:Ljava/util/List;", opcode = Opcodes.GETSTATIC)) | ||
| private static List<AtlasManager.AtlasConfig> addAtlases(List<AtlasManager.AtlasConfig> original) { | ||
| final ImmutableList.Builder<AtlasManager.AtlasConfig> builder = ImmutableList.builder(); | ||
| builder.addAll(original); | ||
| builder.addAll(AtlasRegistryImpl.finalizeConfigs()); | ||
| return builder.build(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...ering-v1/src/testmodClient/java/net/fabricmc/fabric/test/rendering/client/AtlasTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright (c) 2016, 2017, 2018, 2019 FabricMC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package net.fabricmc.fabric.test.rendering.client; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.client.renderer.RenderPipelines; | ||
| import net.minecraft.client.renderer.texture.SpriteContents; | ||
| import net.minecraft.client.renderer.texture.TextureAtlasSprite; | ||
| import net.minecraft.client.resources.model.sprite.AtlasManager; | ||
| import net.minecraft.client.resources.model.sprite.SpriteId; | ||
| import net.minecraft.resources.Identifier; | ||
| import net.minecraft.server.packs.metadata.MetadataSectionType; | ||
| import net.minecraft.util.ExtraCodecs; | ||
|
|
||
| import net.fabricmc.api.ClientModInitializer; | ||
| import net.fabricmc.fabric.api.client.rendering.v1.AtlasRegistry; | ||
| import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry; | ||
|
|
||
| public class AtlasTests implements ClientModInitializer { | ||
| private static final Identifier ATLAS_ID = Identifier.fromNamespaceAndPath("fabric-rendering-v1-testmod", "test_atlas"); | ||
| private static final Identifier TEXTURE_ID = AtlasRegistry.generateTextureLocation(ATLAS_ID); | ||
| private static final SpriteId[] SPRITES = new SpriteId[] { | ||
| new SpriteId( | ||
| TEXTURE_ID, | ||
| Identifier.fromNamespaceAndPath("fabric-rendering-v1-testmod", "test_atlas/double_iron_ingot") | ||
| ), | ||
| new SpriteId( | ||
| TEXTURE_ID, | ||
| Identifier.fromNamespaceAndPath("fabric-rendering-v1-testmod", "test_atlas/blank") | ||
| ) | ||
| }; | ||
| private static final Identifier HUD_ID = Identifier.fromNamespaceAndPath("fabric-rendering-v1-testmod", "atlas_hud"); | ||
| public static final MetadataSectionType<Integer> COLOR = new MetadataSectionType<>("color", ExtraCodecs.STRING_ARGB_COLOR); | ||
|
|
||
| @Override | ||
| public void onInitializeClient() { | ||
| AtlasRegistry.register(new AtlasManager.AtlasConfig(TEXTURE_ID, ATLAS_ID, false, Set.of(COLOR))); | ||
|
|
||
| HudElementRegistry.addLast( | ||
| HUD_ID, | ||
| (graphics, deltaTracker) -> { | ||
| final AtlasManager atlasManager = Minecraft.getInstance().getAtlasManager(); | ||
| final int y = 18; | ||
| int x = 0; | ||
|
|
||
| for (SpriteId spriteId : SPRITES) { | ||
| final TextureAtlasSprite sprite = atlasManager.get(spriteId); | ||
| final SpriteContents contents = sprite.contents(); | ||
| final int color = sprite.contents().getAdditionalMetadata(COLOR).orElse(-1); | ||
|
|
||
| graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, x, y, contents.width(), contents.height(), color); | ||
|
|
||
| x += contents.width() + 2; | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...v1/src/testmodClient/resources/assets/fabric-rendering-v1-testmod/atlases/test_atlas.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "sources": [ | ||
| { | ||
| "type": "fabric-rendering-v1-testmod:double", | ||
| "resource": "minecraft:item/iron_ingot", | ||
| "sprite": "fabric-rendering-v1-testmod:test_atlas/double_iron_ingot" | ||
| }, | ||
| { | ||
| "type": "directory", | ||
| "prefix": "test_atlas/", | ||
| "source": "test_atlas" | ||
| } | ||
| ] | ||
| } |
Binary file added
BIN
+76 Bytes
...ient/resources/assets/fabric-rendering-v1-testmod/textures/test_atlas/blank.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions
3
...dClient/resources/assets/fabric-rendering-v1-testmod/textures/test_atlas/blank.png.mcmeta
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "color": "#FFAAAAFF" | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like how the actual value of
KNOWN_ATLASESis not modified. Doing it on classload would be ideal for covering edge cases but is problematic in other ways (making the timing of the registry freeze unreliable and making checking against vanilla atlases in the registry impossible), so an acceptable way to handle this would be to still inject into<init>, but at head and modify the field instead of only its accesses in the constructor.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like all the possible injections have some weird caveat here. My ideas was that KNOWN_ATLASES doesn't necessarily need to contain all of the atlases.
A mod shouldn't really read from here anyway, everything will be available through the atlas manager itself. And even if we write our entries there, it'll be so late that a mod might as well just read from the atlas manager.