| 1 | package net.minecraft.client.renderer.texture;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.NativeImage;
|
| 4 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 5 | import com.mojang.renderpearl.api.GpuFormat;
|
| 6 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 7 | import com.mojang.renderpearl.api.device.GpuDevice;
|
| 8 | import java.io.IOException;
|
| 9 | import net.fabricmc.api.EnvType;
|
| 10 | import net.fabricmc.api.Environment;
|
| 11 | import net.minecraft.client.resources.metadata.texture.TextureMetadataSection;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.server.packs.resources.ResourceManager;
|
| 14 |
|
| 15 | @Environment(EnvType.CLIENT)
|
| 16 | public class CubeMapTexture extends ReloadableTexture {
|
| 17 | private static final String[] SUFFIXES = new String[]{"_1.png", "_3.png", "_5.png", "_4.png", "_0.png", "_2.png"};
|
| 18 |
|
| 19 | public CubeMapTexture(final Identifier resourceId) {
|
| 20 | super(resourceId);
|
| 21 | }
|
| 22 |
|
| 23 | @Override
|
| 24 | public TextureContents loadContents(final ResourceManager resourceManager) throws IOException {
|
| 25 | Identifier location = this.resourceId();
|
| 26 |
|
| 27 | try (TextureContents first = TextureContents.load(resourceManager, location.withSuffix(SUFFIXES[0]))) {
|
| 28 | int width = first.image().getWidth();
|
| 29 | int height = first.image().getHeight();
|
| 30 | NativeImage stackedImage = new NativeImage(width, height * 6, false);
|
| 31 | first.image().copyRect(stackedImage, 0, 0, 0, 0, width, height, false, true);
|
| 32 |
|
| 33 | for (int i = 1; i < 6; i++) {
|
| 34 | try (TextureContents part = TextureContents.load(resourceManager, location.withSuffix(SUFFIXES[i]))) {
|
| 35 | if (part.image().getWidth() != width || part.image().getHeight() != height) {
|
| 36 | throw new IOException(
|
| 37 | "Image dimensions of cubemap '"
|
| 38 | + location
|
| 39 | + "' sides do not match: part 0 is "
|
| 40 | + width
|
| 41 | + "x"
|
| 42 | + height
|
| 43 | + ", but part "
|
| 44 | + i
|
| 45 | + " is "
|
| 46 | + part.image().getWidth()
|
| 47 | + "x"
|
| 48 | + part.image().getHeight()
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | part.image().copyRect(stackedImage, 0, 0, 0, i * height, width, height, false, true);
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | return new TextureContents(stackedImage, new TextureMetadataSection(true, false, MipmapStrategy.MEAN, 0.0F));
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | protected void doLoad(final NativeImage image) {
|
| 62 | GpuDevice device = RenderSystem.getDevice();
|
| 63 | int width = image.getWidth();
|
| 64 | int height = image.getHeight() / 6;
|
| 65 | this.close();
|
| 66 | this.texture = device.createTexture(this.resourceId()::toString, 21, GpuFormat.RGBA8_UNORM, width, height, 6, 1);
|
| 67 | this.textureView = device.createTextureView(this.texture);
|
| 68 | GpuBufferSlice stagingBuffer = device.createCommandEncoder().transientMemory().uploadStaging(image.getPixelBytes(), 1L, 16);
|
| 69 |
|
| 70 | for (int i = 0; i < 6; i++) {
|
| 71 | device.createCommandEncoder()
|
| 72 | .copyBufferToTexture(stagingBuffer, 0, height * i, image.getWidth(), image.getHeight(), this.texture, 0, 0, width, height, 0, i);
|
| 73 | }
|
| 74 | }
|
| 75 | }
|
| 76 |
|