Skip to content

Commit

Permalink
Add drawConnStretchTexture
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Aug 14, 2023
1 parent ac3b900 commit 13a75a9
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -17,6 +26,7 @@ public class TrainDrawCalls {

private final List<ClusterDrawCall>[] carDrawLists;
private final List<ClusterDrawCall>[] connDrawLists;
private final ResourceLocation[] connStretchTextures;
private final List<PlayCarSoundCall>[] carSoundLists;

@SuppressWarnings("unchecked")
Expand All @@ -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<>());
}
Expand Down Expand Up @@ -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();
Expand All @@ -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<ClusterDrawCall> list : carDrawLists) list.clear();
for (List<ClusterDrawCall> list : connDrawLists) list.clear();
for (List<PlayCarSoundCall> list : carSoundLists) list.clear();
Arrays.fill(connStretchTextures, null);
}

private static class ClusterDrawCall {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("<JS> " + str);
}
Expand Down

0 comments on commit 13a75a9

Please sign in to comment.