| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.ProjectionType;
|
| 4 | import com.mojang.blaze3d.pipeline.RenderTarget;
|
| 5 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 6 | import com.mojang.blaze3d.vertex.BufferBuilder;
|
| 7 | import com.mojang.blaze3d.vertex.ByteBufferBuilder;
|
| 8 | import com.mojang.blaze3d.vertex.DefaultVertexFormat;
|
| 9 | import com.mojang.blaze3d.vertex.MeshData;
|
| 10 | import com.mojang.renderpearl.api.buffers.GpuBuffer;
|
| 11 | import com.mojang.renderpearl.api.buffers.GpuBufferSlice;
|
| 12 | import com.mojang.renderpearl.api.commands.RenderPass;
|
| 13 | import com.mojang.renderpearl.api.pipeline.PrimitiveTopology;
|
| 14 | import com.mojang.renderpearl.api.pipeline.RenderPipeline;
|
| 15 | import com.mojang.renderpearl.api.textures.GpuTextureView;
|
| 16 | import java.util.Optional;
|
| 17 | import java.util.OptionalDouble;
|
| 18 | import net.fabricmc.api.EnvType;
|
| 19 | import net.fabricmc.api.Environment;
|
| 20 | import net.minecraft.client.Minecraft;
|
| 21 | import net.minecraft.client.renderer.state.WindowRenderState;
|
| 22 | import net.minecraft.client.renderer.texture.AbstractTexture;
|
| 23 | import net.minecraft.client.renderer.texture.CubeMapTexture;
|
| 24 | import net.minecraft.client.renderer.texture.TextureManager;
|
| 25 | import net.minecraft.resources.Identifier;
|
| 26 | import org.joml.Matrix4f;
|
| 27 | import org.joml.Matrix4fStack;
|
| 28 |
|
| 29 | @Environment(EnvType.CLIENT)
|
| 30 | public class CubeMap implements AutoCloseable {
|
| 31 | private static final int SIDES = 6;
|
| 32 | private static final float PROJECTION_Z_NEAR = 0.05F;
|
| 33 | private static final float PROJECTION_Z_FAR = 10.0F;
|
| 34 | private static final float PROJECTION_FOV = 85.0F;
|
| 35 | private final GpuBuffer vertexBuffer;
|
| 36 | private final Projection projection;
|
| 37 | private final ProjectionMatrixBuffer projectionMatrixUbo;
|
| 38 | private final Identifier location;
|
| 39 |
|
| 40 | public CubeMap(final Identifier base) {
|
| 41 | this.location = base;
|
| 42 | this.projection = new Projection();
|
| 43 | this.projectionMatrixUbo = new ProjectionMatrixBuffer("cubemap");
|
| 44 | this.vertexBuffer = initializeVertices();
|
| 45 | }
|
| 46 |
|
| 47 | public void render(final float rotXInDegrees, final float rotYInDegrees) {
|
| 48 | Minecraft minecraft = Minecraft.getInstance();
|
| 49 | WindowRenderState windowState = minecraft.gameRenderer.gameRenderState().windowRenderState;
|
| 50 | this.projection.setupPerspective(0.05F, 10.0F, 85.0F, windowState.width, windowState.height);
|
| 51 | RenderSystem.setProjectionMatrix(this.projectionMatrixUbo.getBuffer(this.projection), ProjectionType.PERSPECTIVE);
|
| 52 | RenderPipeline renderPipeline = RenderPipelines.PANORAMA;
|
| 53 | RenderTarget mainRenderTarget = Minecraft.getInstance().gameRenderer.mainRenderTarget();
|
| 54 | GpuTextureView colorTexture = mainRenderTarget.getColorTextureView();
|
| 55 | GpuTextureView depthTexture = mainRenderTarget.getDepthTextureView();
|
| 56 | RenderSystem.AutoStorageIndexBuffer indices = RenderSystem.getSequentialBuffer(PrimitiveTopology.QUADS);
|
| 57 | GpuBuffer indexBuffer = indices.getBuffer(36);
|
| 58 | Matrix4fStack modelViewStack = RenderSystem.getModelViewStack();
|
| 59 | modelViewStack.pushMatrix();
|
| 60 | modelViewStack.rotationX((float) Math.PI);
|
| 61 | modelViewStack.rotateX(rotXInDegrees * (float) (Math.PI / 180.0));
|
| 62 | modelViewStack.rotateY(rotYInDegrees * (float) (Math.PI / 180.0));
|
| 63 | GpuBufferSlice dynamicTransforms = RenderSystem.getDynamicUniforms().writeTransform(new Matrix4f(modelViewStack));
|
| 64 | modelViewStack.popMatrix();
|
| 65 |
|
| 66 | try (RenderPass renderPass = RenderSystem.getDevice()
|
| 67 | .createCommandEncoder()
|
| 68 | .createRenderPass(() -> "Cubemap", colorTexture, Optional.empty(), depthTexture, OptionalDouble.empty())) {
|
| 69 | renderPass.setPipeline(RenderSystem.getCompiledPipeline(renderPipeline));
|
| 70 | RenderSystem.bindDefaultUniforms(renderPass);
|
| 71 | renderPass.setVertexBuffer(0, this.vertexBuffer.slice());
|
| 72 | renderPass.setIndexBuffer(indexBuffer, indices.type());
|
| 73 | renderPass.setUniform("DynamicTransforms", dynamicTransforms);
|
| 74 | AbstractTexture texture = minecraft.getTextureManager().getTexture(this.location);
|
| 75 | renderPass.setUniform("Sampler0", texture.getTextureView(), texture.getSampler());
|
| 76 | renderPass.drawIndexed(36, 1, 0, 0, 0);
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | private static GpuBuffer initializeVertices() {
|
| 81 | try (ByteBufferBuilder byteBufferBuilder = ByteBufferBuilder.exactlySized(DefaultVertexFormat.POSITION.getVertexSize() * 4 * 6)) {
|
| 82 | BufferBuilder bufferBuilder = new BufferBuilder(byteBufferBuilder, PrimitiveTopology.QUADS, DefaultVertexFormat.POSITION);
|
| 83 | bufferBuilder.addVertex(-1.0F, -1.0F, 1.0F);
|
| 84 | bufferBuilder.addVertex(-1.0F, 1.0F, 1.0F);
|
| 85 | bufferBuilder.addVertex(1.0F, 1.0F, 1.0F);
|
| 86 | bufferBuilder.addVertex(1.0F, -1.0F, 1.0F);
|
| 87 | bufferBuilder.addVertex(1.0F, -1.0F, 1.0F);
|
| 88 | bufferBuilder.addVertex(1.0F, 1.0F, 1.0F);
|
| 89 | bufferBuilder.addVertex(1.0F, 1.0F, -1.0F);
|
| 90 | bufferBuilder.addVertex(1.0F, -1.0F, -1.0F);
|
| 91 | bufferBuilder.addVertex(1.0F, -1.0F, -1.0F);
|
| 92 | bufferBuilder.addVertex(1.0F, 1.0F, -1.0F);
|
| 93 | bufferBuilder.addVertex(-1.0F, 1.0F, -1.0F);
|
| 94 | bufferBuilder.addVertex(-1.0F, -1.0F, -1.0F);
|
| 95 | bufferBuilder.addVertex(-1.0F, -1.0F, -1.0F);
|
| 96 | bufferBuilder.addVertex(-1.0F, 1.0F, -1.0F);
|
| 97 | bufferBuilder.addVertex(-1.0F, 1.0F, 1.0F);
|
| 98 | bufferBuilder.addVertex(-1.0F, -1.0F, 1.0F);
|
| 99 | bufferBuilder.addVertex(-1.0F, -1.0F, -1.0F);
|
| 100 | bufferBuilder.addVertex(-1.0F, -1.0F, 1.0F);
|
| 101 | bufferBuilder.addVertex(1.0F, -1.0F, 1.0F);
|
| 102 | bufferBuilder.addVertex(1.0F, -1.0F, -1.0F);
|
| 103 | bufferBuilder.addVertex(-1.0F, 1.0F, 1.0F);
|
| 104 | bufferBuilder.addVertex(-1.0F, 1.0F, -1.0F);
|
| 105 | bufferBuilder.addVertex(1.0F, 1.0F, -1.0F);
|
| 106 | bufferBuilder.addVertex(1.0F, 1.0F, 1.0F);
|
| 107 |
|
| 108 | try (MeshData meshData = bufferBuilder.buildOrThrow()) {
|
| 109 | return RenderSystem.getDevice().createBuffer(() -> "Cube map vertex buffer", 32, meshData.vertexBuffer());
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | public void registerTextures(final TextureManager textureManager) {
|
| 115 | textureManager.register(this.location, new CubeMapTexture(this.location));
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public void close() {
|
| 120 | this.vertexBuffer.close();
|
| 121 | this.projectionMatrixUbo.close();
|
| 122 | }
|
| 123 | }
|
| 124 |
|