MeshData.java

com.mojang.blaze3d.vertex.MeshData

信息

  • 全限定名:com.mojang.blaze3d.vertex.MeshData
  • 类型:public class
  • 包:com.mojang.blaze3d.vertex
  • 源码路径:src/main/java/com/mojang/blaze3d/vertex/MeshData.java
  • 起始行号:L13
  • 实现:AutoCloseable
  • 职责:

    TODO

字段/常量

  • vertexBuffer

    • 类型: ByteBufferBuilder.Result
    • 修饰符: private final
    • 源码定位: L14
    • 说明:

      TODO

  • indexBuffer

    • 类型: ByteBufferBuilder.Result
    • 修饰符: private
    • 源码定位: L15
    • 说明:

      TODO

  • drawState

    • 类型: MeshData.DrawState
    • 修饰符: private final
    • 源码定位: L16
    • 说明:

      TODO

内部类/嵌套类型

  • com.mojang.blaze3d.vertex.MeshData.DrawState

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

      TODO

  • com.mojang.blaze3d.vertex.MeshData.SortState

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

      TODO

构造器

public MeshData(ByteBufferBuilder.Result vertexBuffer, MeshData.DrawState drawState) @ L18

  • 构造器名:MeshData
  • 源码定位:L18
  • 修饰符:public

参数:

  • vertexBuffer: ByteBufferBuilder.Result
  • drawState: MeshData.DrawState

说明:

TODO

方法

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

private static CompactVectorArray unpackQuadCentroids(ByteBuffer vertexBuffer, int vertices, VertexFormat format) @ L23

  • 方法名:unpackQuadCentroids
  • 源码定位:L23
  • 返回类型:CompactVectorArray
  • 修饰符:private static

参数:

  • vertexBuffer: ByteBuffer
  • vertices: int
  • format: VertexFormat

说明:

TODO

public ByteBuffer vertexBuffer() @ L53

  • 方法名:vertexBuffer
  • 源码定位:L53
  • 返回类型:ByteBuffer
  • 修饰符:public

参数:

说明:

TODO

public ByteBuffer indexBuffer() @ L57

  • 方法名:indexBuffer
  • 源码定位:L57
  • 返回类型:ByteBuffer
  • 修饰符:public

参数:

说明:

TODO

public MeshData.DrawState drawState() @ L61

  • 方法名:drawState
  • 源码定位:L61
  • 返回类型:MeshData.DrawState
  • 修饰符:public

参数:

说明:

TODO

public MeshData.SortState sortQuads(ByteBufferBuilder indexBufferTarget, VertexSorting sorting) @ L65

  • 方法名:sortQuads
  • 源码定位:L65
  • 返回类型:MeshData.SortState
  • 修饰符:public

参数:

  • indexBufferTarget: ByteBufferBuilder
  • sorting: VertexSorting

说明:

TODO

public void close() @ L76

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

参数:

说明:

TODO

代码

@OnlyIn(Dist.CLIENT)
public class MeshData implements AutoCloseable {
    private final ByteBufferBuilder.Result vertexBuffer;
    private ByteBufferBuilder.@Nullable Result indexBuffer;
    private final MeshData.DrawState drawState;
 
    public MeshData(ByteBufferBuilder.Result vertexBuffer, MeshData.DrawState drawState) {
        this.vertexBuffer = vertexBuffer;
        this.drawState = drawState;
    }
 
    private static CompactVectorArray unpackQuadCentroids(ByteBuffer vertexBuffer, int vertices, VertexFormat format) {
        int positionOffset = format.getOffset(VertexFormatElement.POSITION);
        if (positionOffset == -1) {
            throw new IllegalArgumentException("Cannot identify quad centers with no position element");
        } else {
            FloatBuffer floatBuffer = vertexBuffer.asFloatBuffer();
            int vertexStride = format.getVertexSize() / 4;
            int quadStride = vertexStride * 4;
            int quads = vertices / 4;
            CompactVectorArray sortingPoints = new CompactVectorArray(quads);
 
            for (int i = 0; i < quads; i++) {
                int firstPosOffset = i * quadStride + positionOffset;
                int secondPosOffset = firstPosOffset + vertexStride * 2;
                float x0 = floatBuffer.get(firstPosOffset + 0);
                float y0 = floatBuffer.get(firstPosOffset + 1);
                float z0 = floatBuffer.get(firstPosOffset + 2);
                float x1 = floatBuffer.get(secondPosOffset + 0);
                float y1 = floatBuffer.get(secondPosOffset + 1);
                float z1 = floatBuffer.get(secondPosOffset + 2);
                float xMid = (x0 + x1) / 2.0F;
                float yMid = (y0 + y1) / 2.0F;
                float zMid = (z0 + z1) / 2.0F;
                sortingPoints.set(i, xMid, yMid, zMid);
            }
 
            return sortingPoints;
        }
    }
 
    public ByteBuffer vertexBuffer() {
        return this.vertexBuffer.byteBuffer();
    }
 
    public @Nullable ByteBuffer indexBuffer() {
        return this.indexBuffer != null ? this.indexBuffer.byteBuffer() : null;
    }
 
    public MeshData.DrawState drawState() {
        return this.drawState;
    }
 
    public MeshData.@Nullable SortState sortQuads(ByteBufferBuilder indexBufferTarget, VertexSorting sorting) {
        if (this.drawState.mode() != VertexFormat.Mode.QUADS) {
            return null;
        } else {
            CompactVectorArray centroids = unpackQuadCentroids(this.vertexBuffer.byteBuffer(), this.drawState.vertexCount(), this.drawState.format());
            MeshData.SortState sortState = new MeshData.SortState(centroids, this.drawState.indexType());
            this.indexBuffer = sortState.buildSortedIndexBuffer(indexBufferTarget, sorting);
            return sortState;
        }
    }
 
    @Override
    public void close() {
        this.vertexBuffer.close();
        if (this.indexBuffer != null) {
            this.indexBuffer.close();
        }
    }
 
    @OnlyIn(Dist.CLIENT)
    public record DrawState(VertexFormat format, int vertexCount, int indexCount, VertexFormat.Mode mode, VertexFormat.IndexType indexType) {
    }
 
    @OnlyIn(Dist.CLIENT)
    public record SortState(CompactVectorArray centroids, VertexFormat.IndexType indexType) {
        public ByteBufferBuilder.@Nullable Result buildSortedIndexBuffer(ByteBufferBuilder target, VertexSorting sorting) {
            int[] startIndices = sorting.sort(this.centroids);
            long pointer = target.reserve(startIndices.length * 6 * this.indexType.bytes);
            IntConsumer indexWriter = this.indexWriter(pointer, this.indexType);
 
            for (int startIndex : startIndices) {
                indexWriter.accept(startIndex * 4 + 0);
                indexWriter.accept(startIndex * 4 + 1);
                indexWriter.accept(startIndex * 4 + 2);
                indexWriter.accept(startIndex * 4 + 2);
                indexWriter.accept(startIndex * 4 + 3);
                indexWriter.accept(startIndex * 4 + 0);
            }
 
            return target.build();
        }
 
        private IntConsumer indexWriter(long pointer, VertexFormat.IndexType indexType) {
            MutableLong nextIndex = new MutableLong(pointer);
 
            return switch (indexType) {
                case SHORT -> value -> MemoryUtil.memPutShort(nextIndex.getAndAdd(2L), (short)value);
                case INT -> value -> MemoryUtil.memPutInt(nextIndex.getAndAdd(4L), value);
            };
        }
    }
}

引用的其他类