Skip to content

Commit

Permalink
Fix misunderstanding of BufferSource behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Sep 17, 2023
1 parent 0bc508f commit 4a266a4
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ClientConfig {

private static Path path;

public static boolean enableOptimization = false;
public static boolean enableOptimization = true;
public static boolean enableBbModelPreload = false;
public static boolean enableTranslucentRender = true;

Expand Down
6 changes: 2 additions & 4 deletions common/src/main/java/cn/zbx1425/sowcerext/model/RawMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
import cn.zbx1425.sowcer.vertex.VertAttrMapping;
import cn.zbx1425.sowcer.vertex.VertAttrSrc;
import cn.zbx1425.sowcer.vertex.VertAttrType;
import cn.zbx1425.sowcerext.model.integration.BufferBuilderProxy;
import com.mojang.blaze3d.vertex.VertexConsumer;
import cn.zbx1425.sowcerext.model.integration.FaceList;
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import net.minecraft.client.renderer.texture.OverlayTexture;
import org.lwjgl.opengl.GL11;

import java.io.DataInputStream;
Expand Down Expand Up @@ -371,7 +369,7 @@ public void setRenderType(String type) {
}
}

public void writeBlazeBuffer(BufferBuilderProxy vertexConsumer, Matrix4f matrix, int color, int light, Profiler profiler) {
public void writeBlazeBuffer(FaceList vertexConsumer, Matrix4f matrix, int color, int light, Profiler profiler) {
if (profiler != null) profiler.recordBlazeAction(faces.size());
for (Face face : faces) {
assert face.vertices.length == 3;
Expand Down
2 changes: 0 additions & 2 deletions common/src/main/java/cn/zbx1425/sowcerext/model/RawModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcer.vertex.VertAttrState;
import cn.zbx1425.sowcerext.model.integration.BufferBuilderProxy;
import cn.zbx1425.sowcerext.model.integration.BufferSourceProxy;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.resources.ResourceLocation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@
public class BufferSourceProxy {

private final MultiBufferSource bufferSource;
private final Map<RenderType, BufferBuilderProxy> builders = new HashMap<>();
private final Map<RenderType, FaceList> builders = new HashMap<>();

public BufferSourceProxy(MultiBufferSource bufferSource) {
this.bufferSource = bufferSource;
}

public BufferBuilderProxy getBuffer(RenderType renderType, boolean needSorting) {
public FaceList getBuffer(RenderType renderType, boolean needSorting) {
return builders.computeIfAbsent(renderType,
type -> new BufferBuilderProxy(bufferSource.getBuffer(renderType), needSorting));
type -> new FaceList(renderType, needSorting));
}

public void commit() {
for (BufferBuilderProxy builder : builders.values()) {
builder.commit();
for (FaceList builder : builders.values()) {
builder.commit(bufferSource);
}
builders.clear();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
package cn.zbx1425.sowcerext.model.integration;

import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.Vertex;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.OverlayTexture;

import java.util.ArrayList;
import java.util.List;

public class BufferBuilderProxy {
public class FaceList {

private final VertexConsumer vertexConsumer;
private final RenderType renderType;
private final boolean needSorting;
private final List<TransformedFace> queuedFaces = new ArrayList<>();

public BufferBuilderProxy(VertexConsumer vertexConsumer, boolean needSorting) {
this.vertexConsumer = vertexConsumer;
public FaceList(RenderType renderType, boolean needSorting) {
this.renderType = renderType;
this.needSorting = needSorting;
}

public void addFace(Vertex[] vertices, int color, int light) {
if (needSorting) {
queuedFaces.add(new TransformedFace(vertices, color, light));
} else {
for (Vertex vertex : vertices) {
vertexConsumer
.vertex(vertex.position.x(), vertex.position.y(), vertex.position.z())
.color((byte)(color >>> 24), (byte)(color >>> 16), (byte)(color >>> 8), (byte)(int)color)
.uv(vertex.u, vertex.v)
.overlayCoords(OverlayTexture.NO_OVERLAY)
.uv2(light)
.normal(vertex.normal.x(), vertex.normal.y(), vertex.normal.z())
.endVertex();
}
}
queuedFaces.add(new TransformedFace(vertices, color, light));
}

public void commit() {
if (!needSorting) return;
queuedFaces.sort((a, b) ->
-Float.compare(a.sortingVector.distanceSq(Vector3f.ZERO), b.sortingVector.distanceSq(Vector3f.ZERO))
);
public void commit(MultiBufferSource bufferSource) {
VertexConsumer vertexConsumer = bufferSource.getBuffer(renderType);
if (needSorting) {
queuedFaces.sort((a, b) ->
-Float.compare(a.sortingVector.distanceSq(Vector3f.ZERO), b.sortingVector.distanceSq(Vector3f.ZERO))
);
}
for (TransformedFace face : queuedFaces) {
for (Vertex vertex : face.vertices) {
vertexConsumer
Expand Down

0 comments on commit 4a266a4

Please sign in to comment.