diff --git a/src/main/java/com/portofino/polygontrainmod/client/renderer/CarRenderer.java b/src/main/java/com/portofino/polygontrainmod/client/renderer/CarRenderer.java index fbf8fd8..9c0f08a 100644 --- a/src/main/java/com/portofino/polygontrainmod/client/renderer/CarRenderer.java +++ b/src/main/java/com/portofino/polygontrainmod/client/renderer/CarRenderer.java @@ -1,18 +1,24 @@ package com.portofino.polygontrainmod.client.renderer; import com.mojang.blaze3d.vertex.PoseStack; +import com.mojang.blaze3d.vertex.VertexConsumer; import com.portofino.polygontrainmod.PolygonTrainMod; import com.portofino.polygontrainmod.entity.CarEntity; import net.minecraft.client.renderer.MultiBufferSource; +import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.entity.EntityRenderer; import net.minecraft.client.renderer.entity.EntityRendererProvider; +import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.resources.ResourceLocation; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.api.distmarker.OnlyIn; import org.jetbrains.annotations.NotNull; +import org.joml.Matrix4f; +import org.joml.Vector3f; +@OnlyIn(Dist.CLIENT) public class CarRenderer extends EntityRenderer { - - public static final ResourceLocation TEXTURE - = ResourceLocation.fromNamespaceAndPath(PolygonTrainMod.MODID, "textures/block/test_block"); + public static final ResourceLocation TEXTURE = ResourceLocation.fromNamespaceAndPath(PolygonTrainMod.MODID, "textures/uv-checker-1024px.png"); public CarRenderer(EntityRendererProvider.Context context) { super(context); @@ -25,14 +31,38 @@ public ResourceLocation getTextureLocation(@NotNull CarEntity entity) { } @Override - public void render( - CarEntity entity, - float entityYaw, - float partialTick, - PoseStack posestack, - MultiBufferSource buffer, - int packedLight - ) { - super.render(entity, entityYaw, partialTick, posestack, buffer, packedLight); + public void render(@NotNull CarEntity entity, float entityYaw, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight) { + poseStack.pushPose(); + + VertexConsumer buffer = bufferSource.getBuffer(RenderType.entityTranslucentCull(TEXTURE)); + + Matrix4f matrix = poseStack.last().pose(); + + buildQuad(buffer, matrix, packedLight); + + poseStack.popPose(); + super.render(entity, entityYaw, partialTick, poseStack, bufferSource, packedLight); + } + + private void buildQuad(VertexConsumer buffer, Matrix4f matrix, int light) { + Vector3f testNormalVector = new Vector3f(1, 0, 1).normalize(); + + float x = testNormalVector.x; + float y = testNormalVector.y; + float z = testNormalVector.z; + + // ポリゴン1 + buffer.addVertex(matrix, 0, 1, 0).setColor(255, 255, 255, 255).setUv(0, 0).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(0, 0, 1); + buffer.addVertex(matrix, 0, 0, 0).setColor(255, 255, 255, 255).setUv(0, 1).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(0, 0, 1); + buffer.addVertex(matrix, 1, 0, 0).setColor(255, 255, 255, 255).setUv(1, 1).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(x, y, z); + buffer.addVertex(matrix, 1, 1, 0).setColor(255, 255, 255, 255).setUv(1, 0).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(x, y, z); + + // 両ポリゴンの重なる2頂点について、ナナメ45度の方向の法線ベクトルを追加して、簡易的なスムースシェーディング処理を行う + + // ポリゴン2 + buffer.addVertex(matrix, 1, 1, 0).setColor(255, 255, 255, 255).setUv(0, 0).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(x, y, z); + buffer.addVertex(matrix, 1, 0, 0).setColor(255, 255, 255, 255).setUv(0, 1).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(x, y, z); + buffer.addVertex(matrix, 1, 0, -1).setColor(255, 255, 255, 255).setUv(1, 1).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(1, 0, 0); + buffer.addVertex(matrix, 1, 1, -1).setColor(255, 255, 255, 255).setUv(1, 0).setOverlay(OverlayTexture.NO_OVERLAY).setLight(light).setNormal(1, 0, 0); } -} +} \ No newline at end of file diff --git a/src/main/java/com/portofino/polygontrainmod/entity/CarEntity.java b/src/main/java/com/portofino/polygontrainmod/entity/CarEntity.java index b0864e9..5025dd2 100644 --- a/src/main/java/com/portofino/polygontrainmod/entity/CarEntity.java +++ b/src/main/java/com/portofino/polygontrainmod/entity/CarEntity.java @@ -16,10 +16,10 @@ import net.minecraft.world.phys.Vec3; import org.jetbrains.annotations.NotNull; + // Entityについて + // Entityは、BlockやItemと異なり、1つの実体に対して必ず1つのインスタンスを持つ。それによって、より多くの状態と処理を実装できる。 /** - * 自動車Entityクラス
- * Entityについて
- * Entityは、BlockやItemと異なり、1つの実体に対して必ず1つのインスタンスを持つ。それによって、より多くの状態と処理を実装できる。 + * 自動車Entityクラス */ public class CarEntity extends Entity { // private static final EntityDataAccessor DATA_SPEED = @@ -74,10 +74,9 @@ public InteractionResult interact(Player player, InteractionHand hand) { } /** - * 操縦している乗客
- * 本来は常にnullなので乗客がいた時にそれを返却するように更新 + * 操縦しているLivingEntity * - * @return 乗客 + * @return あればそのLivingEntity、なければnull */ @Override public LivingEntity getControllingPassenger() { @@ -118,7 +117,7 @@ public boolean isPushable() { } /** - * クリック判定を発生させるかどうかのようだ + * クリック判定を発生させるかどうかだと思われる * * @return もちろん発生させる じゃないと乗れない */ @@ -241,24 +240,25 @@ private void handlePlayerInput(Player player) { // 前後進 float forward = 0.0f; // 前進0.98, 後進-0.98 - float W_S = player.zza; + float wS = player.zza; // 左0.98, 右-0.98 - float A_D = player.xxa; + float aD = player.xxa; // PolygonTrainMod.LOGGER.info(String.valueOf(W_S) + ',' + A_D); // 前進 - if (W_S > 0) forward = 1.0f; + if (wS > 0) forward = 1.0f; // 後進 - if (W_S < 0) forward = -1.0f; + if (wS < 0) forward = -1.0f; float turn = 0.0f; // 左旋回 - if (A_D > 0) turn = 1.0f; + if (aD > 0) turn = 1.0f; // 右旋回 - if (A_D < 0) turn = -1.0f; + if (aD < 0) turn = -1.0f; // それはそうと適当に操作を反映 - this.setYRot(this.getYRot() + turn); + this.setYRot(this.getYRot() - turn); + // TODO: 旋回に対応 this.setDeltaMovement(this.getDeltaMovement().x, this.getDeltaMovement().y, -forward); // diff --git a/src/main/java/com/portofino/polygontrainmod/registry/PolygonTrainModEntities.java b/src/main/java/com/portofino/polygontrainmod/registry/PolygonTrainModEntities.java index adc99dd..6e62d24 100644 --- a/src/main/java/com/portofino/polygontrainmod/registry/PolygonTrainModEntities.java +++ b/src/main/java/com/portofino/polygontrainmod/registry/PolygonTrainModEntities.java @@ -16,6 +16,7 @@ public class PolygonTrainModEntities { public static final Supplier> CAR = ENTITY_TYPES.register( "car", () -> EntityType.Builder.of(CarEntity::new, MobCategory.MISC) + .sized(3.0f, 2.0f) .clientTrackingRange(10) .updateInterval(1) .build("car") diff --git a/src/main/resources/assets/polygontrainmod/textures/uv-checker-1024px.png b/src/main/resources/assets/polygontrainmod/textures/uv-checker-1024px.png new file mode 100644 index 0000000..5cbc7c4 Binary files /dev/null and b/src/main/resources/assets/polygontrainmod/textures/uv-checker-1024px.png differ diff --git a/src/main/resources/assets/polygontrainmod/textures/white-1024px.png b/src/main/resources/assets/polygontrainmod/textures/white-1024px.png new file mode 100644 index 0000000..f8273a4 Binary files /dev/null and b/src/main/resources/assets/polygontrainmod/textures/white-1024px.png differ