Skip to content

Commit

Permalink
Add useBaseRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Aug 27, 2023
1 parent 149c7c4 commit d58bf59
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import cn.zbx1425.mtrsteamloco.Main;
import cn.zbx1425.mtrsteamloco.render.integration.MtrModelRegistryUtil;
import cn.zbx1425.mtrsteamloco.render.scripting.train.RenderTrainScripted;
import cn.zbx1425.mtrsteamloco.render.scripting.train.ScriptedTrainRenderer;
import cn.zbx1425.mtrsteamloco.render.scripting.ScriptHolder;
import cn.zbx1425.mtrsteamloco.sound.BveTrainSoundFix;
import cn.zbx1425.sowcerext.util.ResourceUtil;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
Expand Down Expand Up @@ -35,6 +34,7 @@
public class ScriptedCustomTrains implements IResourcePackCreatorProperties, ICustomResources {

public static void init(ResourceManager resourceManager) {
ScriptedTrainRenderer.reInitiateScripts();
readResource(resourceManager, mtr.MTR.MOD_ID + ":" + CUSTOM_RESOURCES_ID + ".json", jsonConfig -> {
try {
jsonConfig.get(CUSTOM_TRAINS_KEY).getAsJsonObject().entrySet().forEach(entry -> {
Expand Down Expand Up @@ -101,7 +101,7 @@ public static void init(ResourceManager resourceManager) {
newBaseTrainType, Text.literal(name),
description, wikipediaArticle, color,
riderOffset, riderOffset, 0, false, false,
new RenderTrainScripted(scriptContext),
new ScriptedTrainRenderer(scriptContext),
trainSound
));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import cn.zbx1425.mtrsteamloco.render.RailPicker;
import cn.zbx1425.mtrsteamloco.render.RenderUtil;
import cn.zbx1425.mtrsteamloco.render.rail.RailRenderDispatcher;
import cn.zbx1425.mtrsteamloco.render.scripting.train.RenderTrainScripted;
import cn.zbx1425.mtrsteamloco.render.scripting.train.ScriptedTrainRenderer;
import cn.zbx1425.sowcer.util.GlStateTracker;
import com.mojang.blaze3d.vertex.PoseStack;
import cn.zbx1425.sowcer.math.Matrix4f;
Expand Down Expand Up @@ -64,7 +64,7 @@ private static void renderTail(EntitySeat entity, float tickDelta, PoseStack mat
RailPicker.pickedRail = null;
}

RenderTrainScripted.disposeInactiveScripts();
ScriptedTrainRenderer.disposeInactiveScripts();
}

@Inject(at = @At("HEAD"), cancellable = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.RawMesh;
import cn.zbx1425.sowcerext.model.RawModel;
import cn.zbx1425.sowcerext.model.integration.RawMeshBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceLocation;
import vendor.cn.zbx1425.mtrsteamloco.org.mozilla.javascript.*;
Expand Down Expand Up @@ -38,6 +39,8 @@ public void load(Map<ResourceLocation, String> scripts) {

scope.put("include", scope, new NativeJavaMethod(
ScriptResourceUtil.class.getMethod("includeScript", Object.class), "includeScript"));
scope.put("print", scope, new NativeJavaMethod(
ScriptResourceUtil.class.getMethod("print", Object[].class), "print"));

scope.put("ModelManager", scope, Context.toObject(MainClient.modelManager, scope));
scope.put("Resources", scope, new NativeJavaClass(scope, ScriptResourceUtil.class));
Expand All @@ -51,6 +54,7 @@ public void load(Map<ResourceLocation, String> scripts) {

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("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
@@ -1,6 +1,7 @@
package cn.zbx1425.mtrsteamloco.render.scripting;

import cn.zbx1425.mtrsteamloco.BuildConfig;
import cn.zbx1425.mtrsteamloco.Main;
import cn.zbx1425.mtrsteamloco.mixin.ClientCacheAccessor;
import cn.zbx1425.mtrsteamloco.render.integration.MtrModelRegistryUtil;
import cn.zbx1425.sowcerext.util.ResourceUtil;
Expand All @@ -20,6 +21,8 @@
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.*;
Expand Down Expand Up @@ -75,6 +78,17 @@ public static void includeScript(Object pathOrIdentifier) throws IOException {
scriptsToExecute.add(new AbstractMap.SimpleEntry<>(identifier, ResourceUtil.readResource(manager(), identifier)));
}

private static final Logger LOGGER = LoggerFactory.getLogger("MTR-NTE JS");

public static void print(Object... objects) {
StringBuilder sb = new StringBuilder();
for (Object object : objects) {
sb.append(object.toString());
sb.append(" ");
}
Main.LOGGER.info(sb.toString().trim());
}

public static Font getBuiltinFont(boolean supportCjk, boolean serif) {
ClientCacheAccessor clientCache = (ClientCacheAccessor) ClientData.DATA_CACHE;
if (clientCache.getFont() == null || clientCache.getFontCjk() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,58 @@
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.PoseStackUtil;
import cn.zbx1425.sowcer.math.Vector3f;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import mtr.client.IDrawing;
import mtr.data.TrainClient;
import mtr.render.MoreRenderLayers;
import mtr.render.TrainRendererBase;
import net.minecraft.client.renderer.LightTexture;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.LightLayer;
import net.minecraft.world.phys.Vec3;

import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;

public class RenderTrainScripted extends TrainRendererBase {
public class ScriptedTrainRenderer extends TrainRendererBase {

private final ScriptHolder typeScripting;
private final TrainClient train;
private final TrainScriptContext trainScripting;

public RenderTrainScripted(ScriptHolder typeScripting) {
public ScriptedTrainRenderer(ScriptHolder typeScripting) {
this.typeScripting = typeScripting;
this.train = null;
this.trainScripting = null;
}

private RenderTrainScripted(RenderTrainScripted base, TrainClient trainClient) {
private ScriptedTrainRenderer(ScriptedTrainRenderer base, TrainClient trainClient) {
this.typeScripting = base.typeScripting;
this.train = trainClient;
this.trainScripting = new TrainScriptContext(trainClient);
}

private static final WeakHashMap<TrainClient, RenderTrainScripted> activeRenderers = new WeakHashMap<>();
private static final WeakHashMap<TrainClient, ScriptedTrainRenderer> activeRenderers = new WeakHashMap<>();

@Override
public TrainRendererBase createTrainInstance(TrainClient trainClient) {
synchronized (activeRenderers) {
RenderTrainScripted result = new RenderTrainScripted(this, trainClient);
ScriptedTrainRenderer result = new ScriptedTrainRenderer(this, trainClient);
activeRenderers.put(trainClient, result);
return result;
}
}

public static void reInitiateScripts() {
synchronized (activeRenderers) {
for (Map.Entry<TrainClient, ScriptedTrainRenderer> entry : activeRenderers.entrySet()) {
entry.getValue().trainScripting.tryCallDispose(entry.getValue().typeScripting);
}
}
}

public static void disposeInactiveScripts() {
synchronized (activeRenderers) {
for (Iterator<Map.Entry<TrainClient, RenderTrainScripted>> it = activeRenderers.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<TrainClient, RenderTrainScripted> entry = it.next();
for (Iterator<Map.Entry<TrainClient, ScriptedTrainRenderer>> it = activeRenderers.entrySet().iterator(); it.hasNext(); ) {
Map.Entry<TrainClient, ScriptedTrainRenderer> entry = it.next();
if (entry.getKey().isRemoved) {
entry.getValue().trainScripting.tryCallDispose(entry.getValue().typeScripting);
it.remove();
Expand All @@ -67,6 +70,11 @@ public static void disposeInactiveScripts() {
public void renderCar(int carIndex, double x, double y, double z, float yaw, float pitch, boolean doorLeftOpen, boolean doorRightOpen) {
assert train != null && trainScripting != null;
if (RenderUtil.shouldSkipRenderTrain(train)) return;

if (trainScripting.baseRenderer != null) {
trainScripting.baseRenderer.renderCar(carIndex, x, y, z, yaw, pitch, doorLeftOpen, doorRightOpen);
}

if (isTranslucentBatch) return;

final BlockPos posAverage = applyAverageTransform(train.getViewOffset(), x, y, z);
Expand Down Expand Up @@ -106,6 +114,11 @@ public void renderCar(int carIndex, double x, double y, double z, float yaw, flo
public void renderConnection(Vec3 prevPos1, Vec3 prevPos2, Vec3 prevPos3, Vec3 prevPos4, Vec3 thisPos1, Vec3 thisPos2, Vec3 thisPos3, Vec3 thisPos4, double x, double y, double z, float yaw, float pitch) {
assert train != null && trainScripting != null;
if (RenderUtil.shouldSkipRenderTrain(train)) return;

if (trainScripting.baseRenderer != null) {
trainScripting.baseRenderer.renderConnection(prevPos1, prevPos2, prevPos3, prevPos4, thisPos1, thisPos2, thisPos3, thisPos4, x, y, z, yaw, pitch);
}

if (isTranslucentBatch) return;

final BlockPos posAverage = applyAverageTransform(train.getViewOffset(), x, y, z);
Expand All @@ -128,8 +141,13 @@ public void renderConnection(Vec3 prevPos1, Vec3 prevPos2, Vec3 prevPos3, Vec3 p
}

@Override
public void renderBarrier(Vec3 vec3, Vec3 vec31, Vec3 vec32, Vec3 vec33, Vec3 vec34, Vec3 vec35, Vec3 vec36, Vec3 vec37, double v, double v1, double v2, float v3, float v4) {
public void renderBarrier(Vec3 prevPos1, Vec3 prevPos2, Vec3 prevPos3, Vec3 prevPos4, Vec3 thisPos1, Vec3 thisPos2, Vec3 thisPos3, Vec3 thisPos4, double x, double y, double z, float yaw, float pitch) {
assert train != null && trainScripting != null;
if (RenderUtil.shouldSkipRenderTrain(train)) return;

if (trainScripting.baseRenderer != null) {
trainScripting.baseRenderer.renderConnection(prevPos1, prevPos2, prevPos3, prevPos4, thisPos1, thisPos2, thisPos3, thisPos4, x, y, z, yaw, pitch);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import cn.zbx1425.sowcer.math.Matrix4f;
import cn.zbx1425.sowcer.math.Vector3f;
import cn.zbx1425.sowcerext.model.ModelCluster;
import mtr.client.TrainClientRegistry;
import mtr.data.TrainClient;
import mtr.render.TrainRendererBase;
import net.minecraft.client.Minecraft;
import net.minecraft.client.player.LocalPlayer;
import net.minecraft.resources.ResourceLocation;
Expand All @@ -27,6 +29,8 @@ public class TrainScriptContext {
public TrainDrawCalls scriptResult;
private TrainDrawCalls scriptResultWriting;

public TrainRendererBase baseRenderer = null;

public Scriptable state;

private boolean created = false;
Expand Down Expand Up @@ -89,10 +93,6 @@ public void drawConnStretchTexture(ResourceLocation location, int carIndex) {
scriptResultWriting.drawConnStretchTexture(carIndex, location);
}

public void print(String str) {
Main.LOGGER.info("<JS> " + str);
}

public void playCarSound(ResourceLocation sound, int carIndex, float x, float y, float z, float volume, float pitch) {
scriptResultWriting.addCarSound(
carIndex,
Expand Down Expand Up @@ -120,4 +120,12 @@ public void playAnnSound(ResourceLocation sound, float volume, float pitch) {
}
});
}

public void useBaseRenderer(String trainId) {
if (trainId == null || trainId.isEmpty()) {
baseRenderer = null;
return;
}
baseRenderer = TrainClientRegistry.getTrainProperties(trainId).renderer.createTrainInstance(train);
}
}
Original file line number Diff line number Diff line change
@@ -1,84 +1,75 @@
package cn.zbx1425.sowcerext.model.integration;

import cn.zbx1425.sowcer.batch.MaterialProp;
import cn.zbx1425.sowcerext.model.Face;
import cn.zbx1425.sowcerext.model.RawMesh;
import cn.zbx1425.sowcerext.model.Vertex;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.blaze3d.vertex.VertexFormat;
import cn.zbx1425.sowcer.math.Vector3f;
import net.minecraft.resources.ResourceLocation;

public class RawMeshBuilder implements VertexConsumer {
import java.util.stream.IntStream;

public RawMesh mesh;
public int light;
public int color;
public class RawMeshBuilder {

private final VertexFormat.Mode mode;
private final RawMesh mesh;

private final int faceSize;
private Vertex buildingVertex = new Vertex();

public RawMeshBuilder(RawMesh mesh, VertexFormat.Mode mode) {
this.mesh = mesh;
this.mode = mode;
public RawMeshBuilder(int faceSize, String renderType, ResourceLocation texture) {
this.faceSize = faceSize;
this.mesh = new RawMesh(new MaterialProp());
mesh.setRenderType(renderType);
mesh.materialProp.texture = texture;
}

public RawMesh getMesh() {
return mesh;
}

public RawMeshBuilder reset() {
mesh.vertices.clear();
mesh.faces.clear();
buildingVertex = new Vertex();
return this;
}

public RawMeshBuilder vertex(Vector3f position) {
buildingVertex.position = position;
return this;
}

@Override
public VertexConsumer vertex(double d, double e, double f) {
public RawMeshBuilder vertex(double d, double e, double f) {
buildingVertex.position = new Vector3f((float) d, (float) e, (float) f);
return this;
}

@Override
public VertexConsumer normal(float f, float g, float h) {
public RawMeshBuilder normal(float f, float g, float h) {
buildingVertex.normal = new Vector3f(f, g, h);
return this;
}

@Override
public VertexConsumer uv(float f, float g) {
public RawMeshBuilder uv(float f, float g) {
buildingVertex.u = f;
buildingVertex.v = g;
return this;
}

@Override
public void endVertex() {
mesh.vertices.add(buildingVertex);
buildingVertex = new Vertex();
if (mesh.vertices.size() % mode.primitiveLength == 0) {
mesh.faces.addAll(Face.triangulate(mesh.vertices.size() - mode.primitiveLength, mesh.vertices.size() - 1, false));
if (mesh.vertices.size() % faceSize == 0) {
mesh.faces.add(new Face(IntStream.range(mesh.vertices.size() - faceSize, mesh.vertices.size()).toArray()));
}
}

@Override
public VertexConsumer color(int i, int j, int k, int l) {
defaultColor(i, j, k, l);
return this;
}

@Override
public VertexConsumer overlayCoords(int i, int j) {
public RawMeshBuilder color(int r, int g, int b, int a) {
mesh.materialProp.attrState.setColor(r, g, b, a);
return this;
}

@Override
public VertexConsumer uv2(int i, int j) {
light = i + j << 16;
public RawMeshBuilder lightMapUV(short u, short v) {
mesh.materialProp.attrState.setLightmapUV(u, v);
return this;
}


@Override
public void defaultColor(int r, int g, int b, int a) {
color = r << 24 | g << 16 | b << 8 | a;
}

@Override
public void unsetDefaultColor() {

}
}

0 comments on commit d58bf59

Please sign in to comment.