From 13a75a9a1eac8a13f9fb9c643ac21c990697b15f Mon Sep 17 00:00:00 2001 From: zbx1425 Date: Mon, 14 Aug 2023 16:09:13 +0800 Subject: [PATCH] Add drawConnStretchTexture --- .../scripting/train/RenderTrainScripted.java | 10 +++++ .../scripting/train/TrainDrawCalls.java | 40 +++++++++++++++++++ .../scripting/train/TrainScriptContext.java | 5 ++- 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/RenderTrainScripted.java b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/RenderTrainScripted.java index 68db9351..1129d1ef 100644 --- a/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/RenderTrainScripted.java +++ b/common/src/main/java/cn/zbx1425/mtrsteamloco/render/scripting/train/RenderTrainScripted.java @@ -6,10 +6,15 @@ 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; @@ -84,6 +89,7 @@ public void renderConnection(Vec3 prevPos1, Vec3 prevPos2, Vec3 prevPos3, Vec3 p final BlockPos posAverage = applyAverageTransform(train.getViewOffset(), x, y, z); if (posAverage == null) return; + matrices.pushPose(); matrices.translate(x, y, z); PoseStackUtil.rotY(matrices, (float) Math.PI + yaw); final boolean hasPitch = pitch < 0 ? train.transportMode.hasPitchAscending : train.transportMode.hasPitchDescending; @@ -92,6 +98,9 @@ public void renderConnection(Vec3 prevPos1, Vec3 prevPos2, Vec3 prevPos3, Vec3 p Matrix4f pose = new Matrix4f(matrices.last().pose()); synchronized (trainScripting) { trainScripting.scriptResult.commitConn(0, MainClient.drawScheduler, pose, light); + matrices.popPose(); + trainScripting.scriptResult.commitConnImmediate(0, matrices, vertexConsumers, + prevPos1, prevPos2, prevPos3, prevPos4, thisPos1, thisPos2, thisPos3, thisPos4, light); } matrices.popPose(); @@ -101,4 +110,5 @@ public void renderConnection(Vec3 prevPos1, Vec3 prevPos2, Vec3 prevPos3, Vec3 p 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) { } + } 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 b6d5a6be..5f6e35f1 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 @@ -4,10 +4,19 @@ import cn.zbx1425.sowcer.math.Vector3f; import cn.zbx1425.sowcerext.model.ModelCluster; import cn.zbx1425.sowcerext.reuse.DrawScheduler; +import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; +import mtr.client.IDrawing; +import mtr.render.MoreRenderLayers; import net.minecraft.client.Minecraft; import net.minecraft.client.multiplayer.ClientLevel; +import net.minecraft.client.renderer.LightTexture; +import net.minecraft.client.renderer.MultiBufferSource; +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; @@ -17,6 +26,7 @@ public class TrainDrawCalls { private final List[] carDrawLists; private final List[] connDrawLists; + private final ResourceLocation[] connStretchTextures; private final List[] carSoundLists; @SuppressWarnings("unchecked") @@ -25,6 +35,7 @@ public TrainDrawCalls(int carCount) { Arrays.setAll(carDrawLists, ignored -> new ArrayList<>()); connDrawLists = new List[carCount - 1]; Arrays.setAll(connDrawLists, ignored -> new ArrayList<>()); + connStretchTextures = new ResourceLocation[carCount - 1]; carSoundLists = new List[carCount]; Arrays.setAll(carSoundLists, ignored -> new ArrayList<>()); } @@ -57,6 +68,10 @@ public void addConnModel(int car, ModelCluster 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(); @@ -65,10 +80,35 @@ public void commitConn(int car, DrawScheduler drawScheduler, Matrix4f basePose, } } + public void commitConnImmediate(int car, PoseStack matrices, MultiBufferSource vertexConsumers, Vec3 prevPos1, Vec3 prevPos2, Vec3 prevPos3, Vec3 prevPos4, + Vec3 thisPos1, Vec3 thisPos2, Vec3 thisPos3, Vec3 thisPos4, int light) { + if (connStretchTextures[car] != null) { + VertexConsumer vertexConsumerExterior = vertexConsumers.getBuffer(MoreRenderLayers.getExterior(connStretchTextures[car])); + drawTexture(matrices, vertexConsumerExterior, thisPos2, prevPos3, prevPos4, thisPos1, 0, 0, 0.5f, 0.5f, light); + drawTexture(matrices, vertexConsumerExterior, prevPos2, thisPos3, thisPos4, prevPos1, 0, 0, 0.5f, 0.5f, light); + drawTexture(matrices, vertexConsumerExterior, prevPos3, thisPos2, thisPos3, prevPos2, 0, 0, 0.5f, 0.5f, light); + drawTexture(matrices, vertexConsumerExterior, prevPos1, thisPos4, thisPos1, prevPos4, 0, 0, 0.5f, 0.5f, light); + int lightOnLevel = LightTexture.FULL_BRIGHT; + VertexConsumer vertexConsumerSide = vertexConsumers.getBuffer(MoreRenderLayers.getInterior(connStretchTextures[car])); + drawTexture(matrices, vertexConsumerSide, thisPos3, prevPos2, prevPos1, thisPos4, 0.5f, 0, 1f, 0.5f, lightOnLevel); + drawTexture(matrices, vertexConsumerSide, prevPos3, thisPos2, thisPos1, prevPos4, 0.5f, 0, 1f, 0.5f, lightOnLevel); + drawTexture(matrices, vertexConsumerSide, prevPos2, thisPos3, thisPos2, prevPos3, 0, 0.5f, 0.5f, 1, lightOnLevel); + drawTexture(matrices, vertexConsumerSide, prevPos4, thisPos1, thisPos4, prevPos1, 0.5f, 0.5f, 1, 1, lightOnLevel); + } + } + + private static void drawTexture(PoseStack matrices, VertexConsumer vertexConsumer, Vec3 pos1, Vec3 pos2, Vec3 pos3, Vec3 pos4, float u1, float v1, float u2, float v2, int light) { + IDrawing.drawTexture(matrices, vertexConsumer, + (float)pos1.x, (float)pos1.y, (float)pos1.z, (float)pos2.x, (float)pos2.y, (float)pos2.z, + (float)pos3.x, (float)pos3.y, (float)pos3.z, (float)pos4.x, (float)pos4.y, (float)pos4.z, + u1, v1, u2, v2, Direction.UP, -1, light); + } + public void reset() { for (List list : carDrawLists) list.clear(); for (List list : connDrawLists) list.clear(); for (List list : carSoundLists) list.clear(); + Arrays.fill(connStretchTextures, null); } private static class ClusterDrawCall { 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 326aed9b..597af272 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 @@ -7,7 +7,6 @@ import cn.zbx1425.sowcer.math.Vector3f; import cn.zbx1425.sowcerext.model.ModelCluster; import mtr.data.TrainClient; -import net.minecraft.Util; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; import net.minecraft.resources.ResourceLocation; @@ -77,6 +76,10 @@ public void drawConnModel(ModelCluster model, int carIndex, Matrices poseStack) scriptResultWriting.addConnModel(carIndex, model, poseStack == null ? Matrix4f.IDENTITY : poseStack.last().copy()); } + public void drawConnStretchTexture(ResourceLocation location, int carIndex) { + scriptResultWriting.drawConnStretchTexture(carIndex, location); + } + public void print(String str) { Main.LOGGER.info(" " + str); }