Unstitcher.java

net.minecraft.client.renderer.texture.atlas.sources.Unstitcher

信息

  • 全限定名:net.minecraft.client.renderer.texture.atlas.sources.Unstitcher
  • 类型:public record
  • 包:net.minecraft.client.renderer.texture.atlas.sources
  • 源码路径:src/main/java/net/minecraft/client/renderer/texture/atlas/sources/Unstitcher.java
  • 起始行号:L25
  • 实现:SpriteSource
  • 职责:

    TODO

字段/常量

  • LOGGER

    • 类型: Logger
    • 修饰符: private static final
    • 源码定位: L26
    • 说明:

      TODO

  • MAP_CODEC

    • 类型: MapCodec<Unstitcher>
    • 修饰符: public static final
    • 源码定位: L27
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.client.renderer.texture.atlas.sources.Unstitcher.Region

    • 类型: record
    • 修饰符: public
    • 源码定位: L58
    • 说明:

      TODO

  • net.minecraft.client.renderer.texture.atlas.sources.Unstitcher.RegionInstance

    • 类型: class
    • 修饰符: private static
    • 源码定位: L72
    • 说明:

      TODO

构造器

方法

下面的方法块按源码顺序生成。

public void run(ResourceManager resourceManager, SpriteSource.Output output) @ L37

  • 方法名:run
  • 源码定位:L37
  • 返回类型:void
  • 修饰符:public

参数:

  • resourceManager: ResourceManager
  • output: SpriteSource.Output

说明:

TODO

public MapCodec<Unstitcher> codec() @ L52

  • 方法名:codec
  • 源码定位:L52
  • 返回类型:MapCodec
  • 修饰符:public

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public record Unstitcher(Identifier resource, List<Unstitcher.Region> regions, double xDivisor, double yDivisor) implements SpriteSource {
    private static final Logger LOGGER = LogUtils.getLogger();
    public static final MapCodec<Unstitcher> MAP_CODEC = RecordCodecBuilder.mapCodec(
        i -> i.group(
                Identifier.CODEC.fieldOf("resource").forGetter(Unstitcher::resource),
                ExtraCodecs.nonEmptyList(Unstitcher.Region.CODEC.listOf()).fieldOf("regions").forGetter(Unstitcher::regions),
                Codec.DOUBLE.optionalFieldOf("divisor_x", 1.0).forGetter(Unstitcher::xDivisor),
                Codec.DOUBLE.optionalFieldOf("divisor_y", 1.0).forGetter(Unstitcher::yDivisor)
            )
            .apply(i, Unstitcher::new)
    );
 
    @Override
    public void run(ResourceManager resourceManager, SpriteSource.Output output) {
        Identifier resourceId = TEXTURE_ID_CONVERTER.idToFile(this.resource);
        Optional<Resource> resource = resourceManager.getResource(resourceId);
        if (resource.isPresent()) {
            LazyLoadedImage image = new LazyLoadedImage(resourceId, resource.get(), this.regions.size());
 
            for (Unstitcher.Region region : this.regions) {
                output.add(region.sprite, new Unstitcher.RegionInstance(image, region, this.xDivisor, this.yDivisor));
            }
        } else {
            LOGGER.warn("Missing sprite: {}", resourceId);
        }
    }
 
    @Override
    public MapCodec<Unstitcher> codec() {
        return MAP_CODEC;
    }
 
    @OnlyIn(Dist.CLIENT)
    public record Region(Identifier sprite, double x, double y, double width, double height) {
        public static final Codec<Unstitcher.Region> CODEC = RecordCodecBuilder.create(
            i -> i.group(
                    Identifier.CODEC.fieldOf("sprite").forGetter(Unstitcher.Region::sprite),
                    Codec.DOUBLE.fieldOf("x").forGetter(Unstitcher.Region::x),
                    Codec.DOUBLE.fieldOf("y").forGetter(Unstitcher.Region::y),
                    Codec.DOUBLE.fieldOf("width").forGetter(Unstitcher.Region::width),
                    Codec.DOUBLE.fieldOf("height").forGetter(Unstitcher.Region::height)
                )
                .apply(i, Unstitcher.Region::new)
        );
    }
 
    @OnlyIn(Dist.CLIENT)
    private static class RegionInstance implements SpriteSource.DiscardableLoader {
        private final LazyLoadedImage image;
        private final Unstitcher.Region region;
        private final double xDivisor;
        private final double yDivisor;
 
        private RegionInstance(LazyLoadedImage image, Unstitcher.Region region, double xDivisor, double yDivisor) {
            this.image = image;
            this.region = region;
            this.xDivisor = xDivisor;
            this.yDivisor = yDivisor;
        }
 
        @Override
        public SpriteContents get(SpriteResourceLoader loader) {
            try {
                NativeImage fullImage = this.image.get();
                double xScale = fullImage.getWidth() / this.xDivisor;
                double yScale = fullImage.getHeight() / this.yDivisor;
                int x = Mth.floor(this.region.x * xScale);
                int y = Mth.floor(this.region.y * yScale);
                int width = Mth.floor(this.region.width * xScale);
                int height = Mth.floor(this.region.height * yScale);
                NativeImage target = new NativeImage(NativeImage.Format.RGBA, width, height, false);
                fullImage.copyRect(target, x, y, 0, 0, width, height, false, false);
                return new SpriteContents(this.region.sprite, new FrameSize(width, height), target);
            } catch (Exception var16) {
                Unstitcher.LOGGER.error("Failed to unstitch region {}", this.region.sprite, var16);
            } finally {
                this.image.release();
            }
 
            return MissingTextureAtlasSprite.create();
        }
 
        @Override
        public void discard() {
            this.image.release();
        }
    }
}

引用的其他类

  • NativeImage

    • 引用位置: 构造调用
    • 关联成员: NativeImage()
  • MissingTextureAtlasSprite

    • 引用位置: 方法调用
    • 关联成员: MissingTextureAtlasSprite.create()
  • SpriteContents

    • 引用位置: 构造调用
    • 关联成员: SpriteContents()
  • SpriteSource

    • 引用位置: 参数/实现
  • LazyLoadedImage

    • 引用位置: 构造调用
    • 关联成员: LazyLoadedImage()
  • FrameSize

    • 引用位置: 构造调用
    • 关联成员: FrameSize()
  • ResourceManager

    • 引用位置: 参数
  • ExtraCodecs

    • 引用位置: 方法调用
    • 关联成员: ExtraCodecs.nonEmptyList()
  • Mth

    • 引用位置: 方法调用
    • 关联成员: Mth.floor()