Skip to content

Commit

Permalink
Add DynamicModelHolder
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Jun 8, 2024
1 parent 72e09a7 commit b3f8bb1
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import net.minecraft.server.packs.resources.ResourceManager;
import vendor.cn.zbx1425.mtrsteamloco.org.mozilla.javascript.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -68,6 +67,7 @@ public void load(String name, String contextTypeName, ResourceManager resourceMa
scope.put("RawModel", scope, new NativeJavaClass(scope, RawModel.class));
scope.put("RawMesh", scope, new NativeJavaClass(scope, RawMesh.class));
scope.put("RawMeshBuilder", scope, new NativeJavaClass(scope, RawMeshBuilder.class));
scope.put("DynamicModelHolder", scope, new NativeJavaClass(scope, DynamicModelHolder.class));
scope.put("Matrices", scope, new NativeJavaClass(scope, Matrices.class));
scope.put("Matrix4f", scope, new NativeJavaClass(scope, Matrix4f.class));
scope.put("Vector3f", scope, new NativeJavaClass(scope, Vector3f.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public boolean isBearerAlive() {
}

public void drawModel(ModelCluster model, Matrices poseStack) {
if (model == null) return;
scriptResultWriting.addModel(model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ public void extraFinished() {
}

public void drawCarModel(ModelCluster model, int carIndex, Matrices poseStack) {
if (model == null) return;
scriptResultWriting.addCarModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

public void drawConnModel(ModelCluster model, int carIndex, Matrices poseStack) {
if (model == null) return;
scriptResultWriting.addConnModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cn.zbx1425.mtrsteamloco.render.scripting.util;

import cn.zbx1425.sowcer.util.GlStateTracker;
import cn.zbx1425.sowcerext.model.ModelCluster;
import cn.zbx1425.sowcerext.model.RawModel;
import cn.zbx1425.sowcerext.reuse.ModelManager;
import com.mojang.blaze3d.systems.RenderSystem;

public class DynamicModelHolder {

private static ModelCluster uploadedModel;

public void uploadLater(RawModel rawModel) {
RawModel finalRawModel = rawModel.copyForMaterialChanges();
finalRawModel.sourceLocation = null;
RenderSystem.recordRenderCall(() -> {
boolean needProtection = !GlStateTracker.isStateProtected;
if (needProtection) GlStateTracker.capture();
uploadedModel = new ModelCluster(finalRawModel, ModelManager.DEFAULT_MAPPING);
if (needProtection) GlStateTracker.restore();
});
}

public ModelCluster getUploadedModel() {
return uploadedModel;
}

public void close() {
RenderSystem.recordRenderCall(() -> {
if (uploadedModel != null) {
uploadedModel.close();
uploadedModel = null;
}
});
}
}
17 changes: 17 additions & 0 deletions common/src/main/java/cn/zbx1425/sowcerext/model/ModelCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ public ModelCluster(RawModel source, VertAttrMapping mapping, ModelManager model
modelManager.uploadModel(translucentParts), mapping, null);
}

public ModelCluster(RawModel source, VertAttrMapping mapping) {
// Untracked variant
this.translucentParts = new RawModel();
this.opaqueParts = new RawModel();
for (RawMesh mesh : source.meshList.values()) {
if (mesh.materialProp.translucent) {
translucentParts.append(mesh);
} else {
opaqueParts.append(mesh);
}
}
this.uploadedOpaqueParts = VertArrays.createAll(
opaqueParts.upload(mapping), mapping, null);
this.uploadedTranslucentParts = VertArrays.createAll(
translucentParts.upload(mapping), mapping, null);
}

private ModelCluster(VertArrays uploadedOpaqueParts, RawModel opaqueParts, VertArrays uploadedTranslucentParts, RawModel translucentParts) {
this.uploadedOpaqueParts = uploadedOpaqueParts;
this.opaqueParts = opaqueParts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public RawMeshBuilder(int faceSize, String renderType, ResourceLocation texture)
this.mesh = new RawMesh(new MaterialProp());
mesh.setRenderType(renderType);
mesh.materialProp.texture = texture;
mesh.materialProp.attrState.setColor(255, 255, 255, 255);
}

public RawMesh getMesh() {
Expand All @@ -30,7 +31,7 @@ public RawMesh getMesh() {
public RawMeshBuilder reset() {
mesh.vertices.clear();
mesh.faces.clear();
buildingVertex = new Vertex();
setNewDefaultVertex();
return this;
}

Expand All @@ -55,12 +56,13 @@ public RawMeshBuilder uv(float f, float g) {
return this;
}

public void endVertex() {
public RawMeshBuilder endVertex() {
mesh.vertices.add(buildingVertex);
buildingVertex = new Vertex();
setNewDefaultVertex();
if (mesh.vertices.size() % faceSize == 0) {
mesh.faces.add(new Face(IntStream.range(mesh.vertices.size() - faceSize, mesh.vertices.size()).toArray()));
mesh.faces.addAll(Face.triangulate(IntStream.range(mesh.vertices.size() - faceSize, mesh.vertices.size()).toArray(), false));
}
return this;
}

public RawMeshBuilder color(int r, int g, int b, int a) {
Expand All @@ -72,4 +74,11 @@ public RawMeshBuilder lightMapUV(short u, short v) {
mesh.materialProp.attrState.setLightmapUV(u, v);
return this;
}

private void setNewDefaultVertex() {
buildingVertex = new Vertex();
buildingVertex.normal = new Vector3f(0, 1, 0);
buildingVertex.u = 0;
buildingVertex.v = 0;
}
}

This file was deleted.

0 comments on commit b3f8bb1

Please sign in to comment.