Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() { }
}
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() { }
}
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");
}
}
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))
Copy link
Copy Markdown
Member

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_ATLASES is 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.

Copy link
Copy Markdown
Contributor Author

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.

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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"package": "net.fabricmc.fabric.mixin.client.rendering",
"compatibilityLevel": "JAVA_25",
"client": [
"AtlasManagerAccessor",
"AtlasManagerMixin",
"BlockColorsMixin",
"BlockEntityRenderersMixin",
"CapeLayerMixin",
Expand Down
1 change: 1 addition & 0 deletions fabric-rendering-v1/src/testmod/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"client": [
"net.fabricmc.fabric.test.rendering.client.AdvancementRenderingTests",
"net.fabricmc.fabric.test.rendering.client.AtlasTests",
"net.fabricmc.fabric.test.rendering.client.ArmorRenderingTests",
"net.fabricmc.fabric.test.rendering.client.CustomSpriteSourcesTest",
"net.fabricmc.fabric.test.rendering.client.CustomColorResolverTest",
Expand Down
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;
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.server.packs.resources.ResourceMetadata;
import net.minecraft.util.ARGB;
import net.minecraft.util.Mth;

import net.fabricmc.api.ClientModInitializer;
Expand Down Expand Up @@ -129,7 +130,7 @@ public SpriteContents get(SpriteResourceLoader spriteResourceLoader) {
int offsetX = frameWidth / 16;
int offsetY = frameHeight / 16;

NativeImage doubleImage = new NativeImage(image.format(), image.getWidth(), image.getHeight(), false);
NativeImage doubleImage = new NativeImage(image.format(), image.getWidth(), image.getHeight(), true);

for (int frameY = 0; frameY < frameCountY; frameY++) {
for (int frameX = 0; frameX < frameCountX; frameX++) {
Expand All @@ -138,14 +139,15 @@ public SpriteContents get(SpriteResourceLoader spriteResourceLoader) {
}
}

return new SpriteContents(spriteId, dimensions, doubleImage, Optional.of(animationMetadata), List.of(), Optional.empty());
return new SpriteContents(spriteId, dimensions, doubleImage, Optional.of(animationMetadata), List.of(AtlasTests.COLOR.withValue(0xFFFFAAAA)), Optional.empty());
}

private static void blendRect(NativeImage src, NativeImage dst, int srcX, int srcY, int destX, int destY, int width, int height) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
int c = src.getPixel(srcX + x, srcY + y);
dst.setPixel(destX + x, destY + y, c);
int sc = src.getPixel(srcX + x, srcY + y);
int dc = dst.getPixel(destX + x, destY + y);
dst.setPixel(destX + x, destY + y, ARGB.alphaBlend(dc, sc));
}
}
}
Expand Down
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"
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"color": "#FFAAAAFF"
}
Loading