From e69076d19a3aece2c390296b22f7d3e66f4623bf Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 29 Dec 2022 22:22:46 +0100 Subject: [PATCH] Some wip stuff --- .../client/gui/DismissSpellScreen.java | 2 +- .../client/particle/DiskParticle.java | 1 + .../client/particle/SphereParticle.java | 1 + .../render/entity/FairyEntityRenderer.java | 2 +- .../client/render/model/BakedModel.java | 40 +++++++++++ .../client/render/model/CrystalModel.java | 71 +++++++++++++++++++ .../model}/SphereModel.java | 37 +++------- 7 files changed, 123 insertions(+), 31 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/client/render/model/BakedModel.java create mode 100644 src/main/java/com/minelittlepony/unicopia/client/render/model/CrystalModel.java rename src/main/java/com/minelittlepony/unicopia/client/{particle => render/model}/SphereModel.java (51%) diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java index e8b10f63..754739e1 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/DismissSpellScreen.java @@ -8,7 +8,7 @@ import org.joml.Vector4f; import com.minelittlepony.common.client.gui.GameGui; import com.minelittlepony.unicopia.ability.magic.spell.*; import com.minelittlepony.unicopia.client.FlowingText; -import com.minelittlepony.unicopia.client.particle.SphereModel; +import com.minelittlepony.unicopia.client.render.model.SphereModel; import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.item.UItems; import com.minelittlepony.unicopia.network.Channel; diff --git a/src/main/java/com/minelittlepony/unicopia/client/particle/DiskParticle.java b/src/main/java/com/minelittlepony/unicopia/client/particle/DiskParticle.java index 0d75c3d1..ec192449 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/particle/DiskParticle.java +++ b/src/main/java/com/minelittlepony/unicopia/client/particle/DiskParticle.java @@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.client.particle; import org.joml.Quaternionf; +import com.minelittlepony.unicopia.client.render.model.SphereModel; import com.minelittlepony.unicopia.particle.SphereParticleEffect; import net.minecraft.client.render.VertexConsumer; diff --git a/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java b/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java index 44e026f5..29eb937c 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java +++ b/src/main/java/com/minelittlepony/unicopia/client/particle/SphereParticle.java @@ -14,6 +14,7 @@ import net.minecraft.util.math.Vec3d; import com.minelittlepony.unicopia.ability.magic.Caster; import com.minelittlepony.unicopia.client.render.RenderLayers; +import com.minelittlepony.unicopia.client.render.model.SphereModel; import com.minelittlepony.unicopia.particle.SphereParticleEffect; import com.minelittlepony.unicopia.particle.ParticleHandle.Attachment; import com.minelittlepony.unicopia.particle.ParticleHandle.Link; diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/entity/FairyEntityRenderer.java b/src/main/java/com/minelittlepony/unicopia/client/render/entity/FairyEntityRenderer.java index 3a478dc3..66857c73 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/render/entity/FairyEntityRenderer.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/entity/FairyEntityRenderer.java @@ -1,7 +1,7 @@ package com.minelittlepony.unicopia.client.render.entity; -import com.minelittlepony.unicopia.client.particle.SphereModel; import com.minelittlepony.unicopia.client.render.RenderLayers; +import com.minelittlepony.unicopia.client.render.model.SphereModel; import com.minelittlepony.unicopia.entity.FairyEntity; import net.minecraft.client.render.*; import net.minecraft.client.render.entity.EntityRendererFactory; diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/model/BakedModel.java b/src/main/java/com/minelittlepony/unicopia/client/render/model/BakedModel.java new file mode 100644 index 00000000..f868ff04 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/client/render/model/BakedModel.java @@ -0,0 +1,40 @@ +package com.minelittlepony.unicopia.client.render.model; + +import java.util.ArrayList; +import java.util.List; + +import org.joml.Matrix4f; +import org.joml.Vector4f; + +import net.minecraft.client.render.VertexConsumer; +import net.minecraft.client.util.math.MatrixStack; + +public class BakedModel { + private static final Vector4f drawVert = new Vector4f(); + + protected final List vertices = new ArrayList<>(); + + protected void addVertex(Vector4f vertex) { + addVertex(vertex.x, vertex.y, vertex.z, 0, 0); + } + + protected void addVertex(float x, float y, float z, float u, float v) { + vertices.add(new Vertex(x, y, z, u, v)); + } + + public final void render(MatrixStack matrices, VertexConsumer vertexWriter, int light, int overlay, float scale, float r, float g, float b, float a) { + scale = Math.abs(scale); + if (scale < 0.001F) { + return; + } + + Matrix4f model = matrices.peek().getPositionMatrix(); + for (Vertex vertex : vertices) { + drawVert.set(vertex.x() * scale, vertex.y() * scale, vertex.z() * scale, 1); + drawVert.mul(model); + vertexWriter.vertex(drawVert.x, drawVert.y, drawVert.z, r, g, b, a, vertex.u(), vertex.v(), overlay, light, 0, 0, 0); + } + } + + record Vertex(float x, float y, float z, float u, float v) {} +} diff --git a/src/main/java/com/minelittlepony/unicopia/client/render/model/CrystalModel.java b/src/main/java/com/minelittlepony/unicopia/client/render/model/CrystalModel.java new file mode 100644 index 00000000..e4ffbd2f --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/client/render/model/CrystalModel.java @@ -0,0 +1,71 @@ +package com.minelittlepony.unicopia.client.render.model; + +public class CrystalModel extends BakedModel { + public CrystalModel(int x, int z, int height) { + // top + buildFace((xx, xz) -> { + addVertex( + x * xx, + xx * 2 + xz * 2, + z * xz, + xx, xz + ); + }); + // north + buildFace((xx, xy) -> { + addVertex( + x * xx, + height * xy - (xx * (xy - 1)) * 2F, + 0, + xx, xy + ); + }); + // south + buildFace((xx, xy) -> { + addVertex( + x * xx, + height * xy - (xy - 1) * 2 - (xx * (xy - 1)) * 2F, + z, + xx, xy + ); + }); + // east + buildFace((zz, xy) -> { + addVertex( + 0, + height * xy - (zz * (xy - 1)) * 2F, + z * zz, + zz, xy + ); + }); + // west + buildFace((zz, xy) -> { + addVertex( + x, + height * xy - (xy - 1) * 2 - (zz * (xy - 1)) * 2F, + z * zz, + zz, xy + ); + }); + // bottom + buildFace((xx, xz) -> { + addVertex( + x * xx, + height, + z * xz, + xx, xz + ); + }); + } + + static void buildFace(BiFloatConsumer consumer) { + consumer.accept(0, 0); + consumer.accept(1, 0); + consumer.accept(1, 1); + consumer.accept(0, 1); + } + + interface BiFloatConsumer { + void accept(float a, float b); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/client/particle/SphereModel.java b/src/main/java/com/minelittlepony/unicopia/client/render/model/SphereModel.java similarity index 51% rename from src/main/java/com/minelittlepony/unicopia/client/particle/SphereModel.java rename to src/main/java/com/minelittlepony/unicopia/client/render/model/SphereModel.java index 588ed1bb..1d94ae8a 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/particle/SphereModel.java +++ b/src/main/java/com/minelittlepony/unicopia/client/render/model/SphereModel.java @@ -1,54 +1,33 @@ -package com.minelittlepony.unicopia.client.particle; +package com.minelittlepony.unicopia.client.render.model; -import java.util.ArrayList; -import java.util.List; import java.util.function.Consumer; -import org.joml.Matrix4f; import org.joml.Vector4f; import com.minelittlepony.unicopia.client.gui.DrawableUtil; -import net.minecraft.client.render.VertexConsumer; import net.minecraft.util.math.MathHelper; -import net.minecraft.client.util.math.MatrixStack; -public class SphereModel { +public class SphereModel extends BakedModel { public static final SphereModel SPHERE = new SphereModel(40, 40, DrawableUtil.TAU); public static final SphereModel DISK = new SphereModel(40, 2, DrawableUtil.PI); public static final SphereModel HEXAGON = new SphereModel(3, 2, DrawableUtil.TAU); public static final SphereModel PRISM = new SphereModel(6, 6, DrawableUtil.TAU); - private final List vertices = new ArrayList<>(); - private final Vector4f drawVert = new Vector4f(); - public SphereModel(double rings, double sectors, double azimuthRange) { double zenithIncrement = DrawableUtil.PI / rings; double azimuthIncrement = DrawableUtil.TAU / sectors; - compileVertices(azimuthRange, zenithIncrement, azimuthIncrement, vertices::add); - } - - public final void render(MatrixStack matrices, VertexConsumer vertexWriter, int light, int overlay, float radius, float r, float g, float b, float a) { - radius = Math.abs(radius); - if (radius < 0.001F) { - return; - } - - Matrix4f model = matrices.peek().getPositionMatrix(); - for (Vector4f vertex : vertices) { - drawVert.set(vertex.x * radius, vertex.y * radius, vertex.z * radius, vertex.w); - drawVert.mul(model); - vertexWriter.vertex(drawVert.x, drawVert.y, drawVert.z, r, g, b, a, 0, 0, overlay, light, 0, 0, 0); - } + compileVertices(azimuthRange, zenithIncrement, azimuthIncrement, this::addVertex); } private static void compileVertices(double azimuthRange, double zenithIncrement, double azimuthIncrement, Consumer collector) { + Vector4f vector = new Vector4f(); for (double zenith = 0; zenith < DrawableUtil.PI; zenith += zenithIncrement) { for (double azimuth = 0; azimuth < azimuthRange; azimuth += azimuthIncrement) { - collector.accept(convertToCartesianCoord(new Vector4f(), 1, zenith, azimuth)); - collector.accept(convertToCartesianCoord(new Vector4f(), 1, zenith + zenithIncrement, azimuth)); - collector.accept(convertToCartesianCoord(new Vector4f(), 1, zenith + zenithIncrement, azimuth + azimuthIncrement)); - collector.accept(convertToCartesianCoord(new Vector4f(), 1, zenith, azimuth + azimuthIncrement)); + collector.accept(convertToCartesianCoord(vector, 1, zenith, azimuth)); + collector.accept(convertToCartesianCoord(vector, 1, zenith + zenithIncrement, azimuth)); + collector.accept(convertToCartesianCoord(vector, 1, zenith + zenithIncrement, azimuth + azimuthIncrement)); + collector.accept(convertToCartesianCoord(vector, 1, zenith, azimuth + azimuthIncrement)); } } }