XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

MyDimension-Lord

【我的世界】我的维度附属模组:维度之主

公开
关注 0 Fork 0 Star 0
UTF-8
// Native Minecraft 1.21.11
package com.xfestudio.mydimensionlord.platform;

import com.mojang.blaze3d.buffers.GpuBuffer;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.MeshData;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.minecraft.client.renderer.rendertype.RenderType;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import java.util.OptionalInt;
import java.util.OptionalDouble;

/** Persistent GPU geometry; each pass explicitly binds buffers, textures and uniforms. */
public final class VertexBuffer implements AutoCloseable {
    public enum Usage { STATIC }
    private GpuBuffer vertices;
    private GpuBuffer indices;
    private MeshData.DrawState state;
    public VertexBuffer(Usage usage) {}
    public void upload(MeshData mesh) {
        close();
        try (mesh) {
            state = mesh.drawState();
            vertices = RenderSystem.getDevice().createBuffer(() -> "MyDimension preview vertices",
                    GpuBuffer.USAGE_VERTEX, mesh.vertexBuffer());
            if (mesh.indexBuffer() != null) indices = RenderSystem.getDevice().createBuffer(
                    () -> "MyDimension preview indices", GpuBuffer.USAGE_INDEX, mesh.indexBuffer());
        }
    }
    public boolean isInvalid() { return vertices == null || vertices.isClosed(); }
    public void drawWithShader(Matrix4f modelView, Matrix4f projection, RenderType type) {
        if (isInvalid()) return;
        var setup = type.state;
        var target = setup.outputTarget.getRenderTarget();
        var colorTarget = RenderSystem.outputColorTextureOverride != null ? RenderSystem.outputColorTextureOverride : target.getColorTextureView();
        var depthTarget = target.useDepth ? (RenderSystem.outputDepthTextureOverride != null ? RenderSystem.outputDepthTextureOverride : target.getDepthTextureView()) : null;
        var transform = RenderSystem.getDynamicUniforms().writeTransform(modelView, PreviewDrawState.color(),
                new Vector3f(), setup.textureTransform.getMatrix());
        var sequence = RenderSystem.getSequentialBuffer(state.mode());
        var indexBuffer = indices == null ? sequence.getBuffer(state.indexCount()) : indices;
        var indexType = indices == null ? sequence.type() : state.indexType();
        try (var pass = RenderSystem.getDevice().createCommandEncoder().createRenderPass(
                () -> "MyDimension cached preview", colorTarget, OptionalInt.empty(), depthTarget, OptionalDouble.empty())) {
            pass.setPipeline(type.pipeline());
            RenderSystem.bindDefaultUniforms(pass);
            pass.setUniform("DynamicTransforms", transform);
            pass.setVertexBuffer(0, vertices);
            setup.getTextures().forEach((name, texture) -> pass.bindTexture(name, texture.textureView(), texture.sampler()));
            pass.setIndexBuffer(indexBuffer, indexType);
            pass.drawIndexed(0, 0, state.indexCount(), 1);
        }
    }
    @Override public void close() {
        if (vertices != null) { vertices.close(); vertices = null; }
        if (indices != null) { indices.close(); indices = null; }
    }
}