| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.buffers.Std140Builder;
|
| 4 | import com.mojang.blaze3d.platform.Transparency;
|
| 5 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 6 | import com.mojang.renderpearl.api.textures.GpuTexture;
|
| 7 | import java.nio.ByteBuffer;
|
| 8 | import net.fabricmc.api.EnvType;
|
| 9 | import net.fabricmc.api.Environment;
|
| 10 | import net.minecraft.resources.Identifier;
|
| 11 | import org.joml.Matrix4f;
|
| 12 | import org.jspecify.annotations.Nullable;
|
| 13 | import org.lwjgl.system.MemoryUtil;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class TextureAtlasSprite implements UvMapping, AutoCloseable {
|
| 17 | private final Identifier atlasLocation;
|
| 18 | private final SpriteContents contents;
|
| 19 | private final int x;
|
| 20 | private final int y;
|
| 21 | private final float u0;
|
| 22 | private final float u1;
|
| 23 | private final float v0;
|
| 24 | private final float v1;
|
| 25 | private final int padding;
|
| 26 |
|
| 27 | protected TextureAtlasSprite(
|
| 28 | final Identifier atlasLocation, final SpriteContents contents, final int atlasWidth, final int atlasHeight, final int x, final int y, final int padding
|
| 29 | ) {
|
| 30 | this.atlasLocation = atlasLocation;
|
| 31 | this.contents = contents;
|
| 32 | this.padding = padding;
|
| 33 | this.x = x;
|
| 34 | this.y = y;
|
| 35 | this.u0 = (float)(x + padding) / atlasWidth;
|
| 36 | this.u1 = (float)(x + padding + contents.width()) / atlasWidth;
|
| 37 | this.v0 = (float)(y + padding) / atlasHeight;
|
| 38 | this.v1 = (float)(y + padding + contents.height()) / atlasHeight;
|
| 39 | }
|
| 40 |
|
| 41 | public int getX() {
|
| 42 | return this.x;
|
| 43 | }
|
| 44 |
|
| 45 | public int getY() {
|
| 46 | return this.y;
|
| 47 | }
|
| 48 |
|
| 49 | public float getU0() {
|
| 50 | return this.u0;
|
| 51 | }
|
| 52 |
|
| 53 | public float getU1() {
|
| 54 | return this.u1;
|
| 55 | }
|
| 56 |
|
| 57 | public SpriteContents contents() {
|
| 58 | return this.contents;
|
| 59 | }
|
| 60 |
|
| 61 | public SpriteContents.@Nullable AnimationState createAnimationState(final GpuBufferSlice uboSlice, final int spriteUboSize) {
|
| 62 | return this.contents.createAnimationState(uboSlice, spriteUboSize);
|
| 63 | }
|
| 64 |
|
| 65 | public Transparency transparency() {
|
| 66 | return this.contents.transparency();
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public float getU(final float offset) {
|
| 71 | float diff = this.u1 - this.u0;
|
| 72 | return this.u0 + diff * offset;
|
| 73 | }
|
| 74 |
|
| 75 | public float getV0() {
|
| 76 | return this.v0;
|
| 77 | }
|
| 78 |
|
| 79 | public float getV1() {
|
| 80 | return this.v1;
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | public float getV(final float offset) {
|
| 85 | float diff = this.v1 - this.v0;
|
| 86 | return this.v0 + diff * offset;
|
| 87 | }
|
| 88 |
|
| 89 | public Identifier atlasLocation() {
|
| 90 | return this.atlasLocation;
|
| 91 | }
|
| 92 |
|
| 93 | @Override
|
| 94 | public String toString() {
|
| 95 | return "TextureAtlasSprite{contents='" + this.contents + "', u0=" + this.u0 + ", u1=" + this.u1 + ", v0=" + this.v0 + ", v1=" + this.v1 + "}";
|
| 96 | }
|
| 97 |
|
| 98 | public void uploadFirstFrame(final GpuTexture destination, final int level) {
|
| 99 | this.contents.uploadFirstFrame(destination, level);
|
| 100 | }
|
| 101 |
|
| 102 | public boolean isAnimated() {
|
| 103 | return this.contents.isAnimated();
|
| 104 | }
|
| 105 |
|
| 106 | public void uploadSpriteUbo(
|
| 107 | final ByteBuffer uboBuffer, final int startOffset, final int maxMipLevel, final int atlasWidth, final int atlasHeight, final int spriteUboSize
|
| 108 | ) {
|
| 109 | for (int level = 0; level <= maxMipLevel; level++) {
|
| 110 | Std140Builder.intoBuffer(MemoryUtil.memSlice(uboBuffer, startOffset + level * spriteUboSize, spriteUboSize))
|
| 111 | .putMat4f(new Matrix4f().ortho2D(0.0F, atlasWidth >> level, 0.0F, atlasHeight >> level))
|
| 112 | .putMat4f(
|
| 113 | new Matrix4f()
|
| 114 | .translate(this.x >> level, this.y >> level, 0.0F)
|
| 115 | .scale(this.contents.width() + this.padding * 2 >> level, this.contents.height() + this.padding * 2 >> level, 1.0F)
|
| 116 | )
|
| 117 | .putFloat((float)this.padding / this.contents.width())
|
| 118 | .putFloat((float)this.padding / this.contents.height())
|
| 119 | .putInt(level);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public void close() {
|
| 125 | this.contents.close();
|
| 126 | }
|
| 127 | }
|
| 128 |
|