diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/AbstractDrawCalls.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/AbstractDrawCalls.java new file mode 100644 index 00000000..8074c176 --- /dev/null +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/AbstractDrawCalls.java @@ -0,0 +1,63 @@ +package cn.zbx1425.mtrsteamloco.render.scripting; + +import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder; +import cn.zbx1425.sowcer.math.Matrix4f; +import cn.zbx1425.sowcer.math.Vector3f; +import cn.zbx1425.sowcerext.model.ModelCluster; +import cn.zbx1425.sowcerext.reuse.DrawScheduler; +import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.sounds.SoundSource; + +public abstract class AbstractDrawCalls { + + public static class ClusterDrawCall { + public ModelCluster model; + public DynamicModelHolder modelHolder; + public Matrix4f pose; + + public ClusterDrawCall(ModelCluster model, Matrix4f pose) { + this.model = model; + this.pose = pose; + } + + public ClusterDrawCall(DynamicModelHolder model, Matrix4f pose) { + this.modelHolder = model; + this.pose = pose; + } + + public void commit(DrawScheduler drawScheduler, Matrix4f basePose, int light) { + Matrix4f finalPose = basePose.copy(); + finalPose.multiply(pose); + if (model != null) { + drawScheduler.enqueue(model, finalPose, light); + } else { + ModelCluster model = modelHolder.getUploadedModel(); + if (model != null) { + drawScheduler.enqueue(model, finalPose, light); + } + } + } + } + + public static class PlaySoundCall { + public SoundEvent sound; + public Vector3f position; + public float volume; + public float pitch; + + public PlaySoundCall(SoundEvent sound, Vector3f position, float volume, float pitch) { + this.sound = sound; + this.position = position; + this.volume = volume; + this.pitch = pitch; + } + + public void commit(ClientLevel level, Matrix4f worldPose) { + Vector3f worldPos = worldPose.transform(position); + level.playLocalSound(worldPos.x(), worldPos.y(), worldPos.z(), + sound, SoundSource.BLOCKS, + volume, pitch, false); + } + } +} diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyDrawCalls.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyDrawCalls.java index 72c8c70e..8f3e4446 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyDrawCalls.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyDrawCalls.java @@ -1,6 +1,7 @@ package cn.zbx1425.mtrsteamloco.render.scripting.eyecandy; -import cn.zbx1425.mtrsteamloco.render.scripting.train.TrainDrawCalls; +import cn.zbx1425.mtrsteamloco.render.scripting.AbstractDrawCalls; +import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder; import cn.zbx1425.sowcer.math.Matrix4f; import cn.zbx1425.sowcer.math.Vector3f; import cn.zbx1425.sowcerext.model.ModelCluster; @@ -13,7 +14,7 @@ import java.util.ArrayList; import java.util.List; -public class EyeCandyDrawCalls { +public class EyeCandyDrawCalls extends AbstractDrawCalls { private final List drawList = new ArrayList<>(); private final List soundList = new ArrayList<>(); @@ -22,23 +23,22 @@ public void addModel(ModelCluster model, Matrix4f pose) { drawList.add(new ClusterDrawCall(model, pose)); } + public void addModel(DynamicModelHolder model, Matrix4f pose) { + drawList.add(new ClusterDrawCall(model, pose)); + } + public void addSound(SoundEvent sound, float volume, float pitch) { - soundList.add(new PlaySoundCall(sound, volume, pitch)); + soundList.add(new PlaySoundCall(sound, Vector3f.ZERO, volume, pitch)); } public void commit(DrawScheduler drawScheduler, Matrix4f basePose, int light) { for (ClusterDrawCall clusterDrawCall : drawList) { - Matrix4f finalPose = basePose.copy(); - finalPose.multiply(clusterDrawCall.pose); - drawScheduler.enqueue(clusterDrawCall.model, finalPose, light); + clusterDrawCall.commit(drawScheduler, basePose, light); } ClientLevel level = Minecraft.getInstance().level; if (level == null) return; for (PlaySoundCall playSoundCall : soundList) { - Vector3f worldPos = basePose.transform(Vector3f.ZERO); - level.playLocalSound(worldPos.x(), worldPos.y(), worldPos.z(), - playSoundCall.sound, SoundSource.BLOCKS, - playSoundCall.volume, playSoundCall.pitch, false); + playSoundCall.commit(level, basePose); } } @@ -46,26 +46,4 @@ public void reset() { drawList.clear(); soundList.clear(); } - - private static class ClusterDrawCall { - public ModelCluster model; - public Matrix4f pose; - - public ClusterDrawCall(ModelCluster model, Matrix4f pose) { - this.model = model; - this.pose = pose; - } - } - - private static class PlaySoundCall { - public SoundEvent sound; - public float volume; - public float pitch; - - public PlaySoundCall(SoundEvent sound, float volume, float pitch) { - this.sound = sound; - this.volume = volume; - this.pitch = pitch; - } - } } diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyScriptContext.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyScriptContext.java index ed4e2c67..5722b860 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyScriptContext.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/eyecandy/EyeCandyScriptContext.java @@ -3,6 +3,7 @@ import cn.zbx1425.mtrsteamloco.Main; import cn.zbx1425.mtrsteamloco.block.BlockEyeCandy; import cn.zbx1425.mtrsteamloco.render.scripting.AbstractScriptContext; +import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder; import cn.zbx1425.sowcer.math.Matrices; import cn.zbx1425.sowcer.math.Matrix4f; import cn.zbx1425.sowcerext.model.ModelCluster; @@ -43,7 +44,10 @@ public boolean isBearerAlive() { } public void drawModel(ModelCluster model, Matrices poseStack) { - if (model == null) return; + scriptResultWriting.addModel(model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy()); + } + + public void drawModel(DynamicModelHolder model, Matrices poseStack) { scriptResultWriting.addModel(model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy()); } diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainDrawCalls.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainDrawCalls.java index 5f6e35f1..9b172af9 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainDrawCalls.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainDrawCalls.java @@ -1,5 +1,7 @@ package cn.zbx1425.mtrsteamloco.render.scripting.train; +import cn.zbx1425.mtrsteamloco.render.scripting.AbstractDrawCalls; +import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder; import cn.zbx1425.sowcer.math.Matrix4f; import cn.zbx1425.sowcer.math.Vector3f; import cn.zbx1425.sowcerext.model.ModelCluster; @@ -15,19 +17,18 @@ import net.minecraft.core.Direction; import net.minecraft.resources.ResourceLocation; import net.minecraft.sounds.SoundEvent; -import net.minecraft.sounds.SoundSource; import net.minecraft.world.phys.Vec3; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -public class TrainDrawCalls { +public class TrainDrawCalls extends AbstractDrawCalls { private final List[] carDrawLists; private final List[] connDrawLists; private final ResourceLocation[] connStretchTextures; - private final List[] carSoundLists; + private final List[] carSoundLists; @SuppressWarnings("unchecked") public TrainDrawCalls(int carCount) { @@ -44,23 +45,22 @@ public void addCarModel(int car, ModelCluster model, Matrix4f pose) { carDrawLists[car].add(new ClusterDrawCall(model, pose)); } + public void addCarModel(int car, DynamicModelHolder model, Matrix4f pose) { + carDrawLists[car].add(new ClusterDrawCall(model, pose)); + } + public void addCarSound(int car, SoundEvent sound, Vector3f position, float volume, float pitch) { - carSoundLists[car].add(new PlayCarSoundCall(sound, position, volume, pitch)); + carSoundLists[car].add(new PlaySoundCall(sound, position, volume, pitch)); } public void commitCar(int car, DrawScheduler drawScheduler, Matrix4f basePose, Matrix4f worldPose, int light) { for (ClusterDrawCall clusterDrawCall : carDrawLists[car]) { - Matrix4f finalPose = basePose.copy(); - finalPose.multiply(clusterDrawCall.pose); - drawScheduler.enqueue(clusterDrawCall.model, finalPose, light); + clusterDrawCall.commit(drawScheduler, basePose, light); } ClientLevel level = Minecraft.getInstance().level; if (level == null) return; - for (PlayCarSoundCall playCarSoundCall : carSoundLists[car]) { - Vector3f worldPos = worldPose.transform(playCarSoundCall.position); - level.playLocalSound(worldPos.x(), worldPos.y(), worldPos.z(), - playCarSoundCall.sound, SoundSource.BLOCKS, - playCarSoundCall.volume, playCarSoundCall.pitch, false); + for (PlaySoundCall playSoundCall : carSoundLists[car]) { + playSoundCall.commit(level, worldPose); } } @@ -68,15 +68,17 @@ public void addConnModel(int car, ModelCluster model, Matrix4f pose) { connDrawLists[car].add(new ClusterDrawCall(model, pose)); } + public void addConnModel(int car, DynamicModelHolder model, Matrix4f pose) { + connDrawLists[car].add(new ClusterDrawCall(model, pose)); + } + public void drawConnStretchTexture(int car, ResourceLocation texture) { connStretchTextures[car] = texture; } public void commitConn(int car, DrawScheduler drawScheduler, Matrix4f basePose, int light) { for (ClusterDrawCall clusterDrawCall : connDrawLists[car]) { - Matrix4f finalPose = basePose.copy(); - finalPose.multiply(clusterDrawCall.pose); - drawScheduler.enqueue(clusterDrawCall.model, finalPose, light); + clusterDrawCall.commit(drawScheduler, basePose, light); } } @@ -107,31 +109,8 @@ private static void drawTexture(PoseStack matrices, VertexConsumer vertexConsume public void reset() { for (List list : carDrawLists) list.clear(); for (List list : connDrawLists) list.clear(); - for (List list : carSoundLists) list.clear(); + for (List list : carSoundLists) list.clear(); Arrays.fill(connStretchTextures, null); } - private static class ClusterDrawCall { - public ModelCluster model; - public Matrix4f pose; - - public ClusterDrawCall(ModelCluster model, Matrix4f pose) { - this.model = model; - this.pose = pose; - } - } - - private static class PlayCarSoundCall { - public SoundEvent sound; - public Vector3f position; - public float volume; - public float pitch; - - public PlayCarSoundCall(SoundEvent sound, Vector3f position, float volume, float pitch) { - this.sound = sound; - this.position = position; - this.volume = volume; - this.pitch = pitch; - } - } } diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainScriptContext.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainScriptContext.java index 439e1898..dde1c053 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainScriptContext.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/TrainScriptContext.java @@ -1,15 +1,13 @@ package cn.zbx1425.mtrsteamloco.render.scripting.train; import cn.zbx1425.mtrsteamloco.render.scripting.AbstractScriptContext; -import cn.zbx1425.mtrsteamloco.render.scripting.ScriptHolder; +import cn.zbx1425.mtrsteamloco.render.scripting.util.DynamicModelHolder; import cn.zbx1425.sowcer.math.Matrices; import cn.zbx1425.sowcer.math.Matrix4f; import cn.zbx1425.sowcer.math.Vector3f; import cn.zbx1425.sowcerext.model.ModelCluster; import mtr.client.ClientData; -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.client.resources.sounds.SimpleSoundInstance; @@ -66,12 +64,18 @@ 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 drawCarModel(DynamicModelHolder model, int carIndex, Matrices poseStack) { 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()); + } + + public void drawConnModel(DynamicModelHolder model, int carIndex, Matrices poseStack) { scriptResultWriting.addConnModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy()); } diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/util/DynamicModelHolder.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/util/DynamicModelHolder.java index b0cb461e..f986b1df 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/util/DynamicModelHolder.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/util/DynamicModelHolder.java @@ -16,7 +16,9 @@ public void uploadLater(RawModel rawModel) { RenderSystem.recordRenderCall(() -> { boolean needProtection = !GlStateTracker.isStateProtected; if (needProtection) GlStateTracker.capture(); + ModelCluster lastUploadedModel = uploadedModel; uploadedModel = new ModelCluster(finalRawModel, ModelManager.DEFAULT_MAPPING); + if (lastUploadedModel != null) lastUploadedModel.close(); if (needProtection) GlStateTracker.restore(); }); }