Skip to content

Commit

Permalink
Textures for our 3d glasses objects, however we hit the mc limits her…
Browse files Browse the repository at this point in the history
…e. I am unable to support textures and different colors/opacity at the same time. I either miss something or we need to make our own shaders
  • Loading branch information
SirEndii committed Nov 8, 2024
1 parent 130d91c commit c68ed10
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 64 deletions.
92 changes: 61 additions & 31 deletions src/main/java/de/srendi/advancedperipherals/client/RenderUtil.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package de.srendi.advancedperipherals.client;

import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.math.Matrix4f;
Expand All @@ -11,6 +10,7 @@
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.core.Direction;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.InventoryMenu;

public class RenderUtil {

Expand Down Expand Up @@ -89,21 +89,21 @@ public static void drawPlane(PoseStack posestack, VertexConsumer buffer, float r
posestack.popPose();
}

public static void drawTorus(PoseStack poseStack, BufferBuilder consumer, float majorRadius, float minorRadius, double pX, double pY, double pZ, float xRot, float yRot, float zRot, float r, float g, float b, float a, int sides, int rings) {
public static void drawTorus(PoseStack poseStack, VertexConsumer consumer, float majorRadius, float minorRadius, double pX, double pY, double pZ, float xRot, float yRot, float zRot, float r, float g, float b, float a, int sides, int rings) {
poseStack.pushPose();
poseStack.translate(pX, pY, pZ);
poseStack.mulPose(new Quaternion(xRot, yRot, zRot, true));

Matrix4f matrix4f = poseStack.last().pose();
TextureAtlasSprite texture = Minecraft.getInstance().getTextureAtlas(InventoryMenu.BLOCK_ATLAS).apply(new ResourceLocation("block/crimson_stem"));

float x, y, z;
float nx, ny, nz; // vertex normal
float nx, ny, nz;

float ringStep = (float) (2 * Math.PI / rings);
float sideStep = (float) (2 * Math.PI / sides);
float ringAngle, sideAngle;

// Start drawing quads
for (int i = 0; i < rings; ++i) {
ringAngle = i * ringStep;
float cosRingAngle = (float) Math.cos(ringAngle);
Expand All @@ -128,16 +128,13 @@ public static void drawTorus(PoseStack poseStack, BufferBuilder consumer, float
float nextCosSideAngle = (float) Math.cos(nextSideAngle);
float nextSinSideAngle = (float) Math.sin(nextSideAngle);

// Calculate vertex positions
x = centerX + minorRadius * cosSideAngle * cosRingAngle;
y = centerY + minorRadius * cosSideAngle * sinRingAngle;
z = minorRadius * sinSideAngle;
float s = j / sides;
float t = i / rings;

// Calculate normal (simplified - for smooth shading, average normals of adjacent faces)
nx = x - centerX;
ny = y - centerY;
nz = z;
consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).overlayCoords(OverlayTexture.WHITE_OVERLAY_V).uv(0, 0).uv2(0xFFFFFF).normal(nx, ny, nz).endVertex();
float u1 = getU(s * sides, texture.getU1(), texture.getU0(), sides);
float u2 = getU((s + 1.0f / sides) * sides, texture.getU1(), texture.getU0(), sides);
float v1 = getV(t * rings, texture.getV1(), texture.getV0(), rings);
float v2 = getV((t + 1.0f / rings) * rings, texture.getV1(), texture.getV0(), rings);

x = centerX + minorRadius * nextCosSideAngle * cosRingAngle;
y = centerY + minorRadius * nextCosSideAngle * sinRingAngle;
Expand All @@ -146,16 +143,19 @@ public static void drawTorus(PoseStack poseStack, BufferBuilder consumer, float
nx = x - centerX;
ny = y - centerY;
nz = z;
consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).overlayCoords(OverlayTexture.WHITE_OVERLAY_V).uv(1, 0).uv2(0xFFF0F0).normal(nx, ny, nz).endVertex();
consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx, ny, nz).endVertex();

x = nextCenterX + minorRadius * nextCosSideAngle * nextCosRingAngle;
y = nextCenterY + minorRadius * nextCosSideAngle * nextSinRingAngle;
z = minorRadius * nextSinSideAngle;
// Calculate vertex positions
x = centerX + minorRadius * cosSideAngle * cosRingAngle;
y = centerY + minorRadius * cosSideAngle * sinRingAngle;
z = minorRadius * sinSideAngle;

nx = x - nextCenterX;
ny = y - nextCenterY;
// Calculate normal
nx = x - centerX;
ny = y - centerY;
nz = z;
consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).overlayCoords(OverlayTexture.WHITE_OVERLAY_V).uv(0, 1).uv2(0xFFFFFF).normal(nx, ny, nz).endVertex();

consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx, ny, nz).endVertex();

x = nextCenterX + minorRadius * cosSideAngle * nextCosRingAngle;
y = nextCenterY + minorRadius * cosSideAngle * nextSinRingAngle;
Expand All @@ -164,23 +164,34 @@ public static void drawTorus(PoseStack poseStack, BufferBuilder consumer, float
nx = x - nextCenterX;
ny = y - nextCenterY;
nz = z;
consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).overlayCoords(OverlayTexture.RED_OVERLAY_V).uv(1, 1).uv2(0xF000F0).normal(nx, ny, nz).endVertex();
consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx, ny, nz).endVertex();


x = nextCenterX + minorRadius * nextCosSideAngle * nextCosRingAngle;
y = nextCenterY + minorRadius * nextCosSideAngle * nextSinRingAngle;
z = minorRadius * nextSinSideAngle;

nx = x - nextCenterX;
ny = y - nextCenterY;
nz = z;
consumer.vertex(matrix4f, x, y, z).color(r, g, b, a).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx, ny, nz).endVertex();
}
}

poseStack.popPose();
}

public static void drawSphere(PoseStack poseStack, BufferBuilder consumer, float radius, double pX, double pY, double pZ, float xRot, float yRot, float zRot, float r, float g, float b, float a, int sectors, int stacks) {
public static void drawSphere(PoseStack poseStack, VertexConsumer consumer, float radius, double pX, double pY, double pZ, float xRot, float yRot, float zRot, float r, float g, float b, float a, int sectors, int stacks) {
poseStack.pushPose();
poseStack.translate(pX, pY, pZ);
poseStack.mulPose(new Quaternion(xRot, yRot, zRot, true));

Matrix4f matrix4f = poseStack.last().pose();
TextureAtlasSprite texture = Minecraft.getInstance().getTextureAtlas(TextureAtlas.LOCATION_BLOCKS).apply(new ResourceLocation("block/dirt"));

float z, xy;
float nx1, ny1, nz1, nx2, ny2, nz2, nx3, ny3, nz3, nx4, ny4, nz4, lengthInv = (1.0f / radius); // vertex normal
float s, t; //TODO vertex texCoord
float s, t;

float sectorStep = (float) (2 * Math.PI / sectors);
float stackStep = (float) (Math.PI / stacks);
Expand Down Expand Up @@ -226,21 +237,40 @@ public static void drawSphere(PoseStack poseStack, BufferBuilder consumer, float
ny4 = y4 * lengthInv;
nz4 = z4 * lengthInv;

// Triangle 1
consumer.vertex(matrix4f, x1, y1, z).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx1, ny1, nz1).endVertex();
consumer.vertex(matrix4f, x3, y3, z3).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx3, ny3, nz3).endVertex();
consumer.vertex(matrix4f, x4, y4, z4).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx4, ny4, nz4).endVertex();
s = j / sectors;
t = i / stacks;

float u1 = getU(s * sectors, texture.getU1(), texture.getU0(), sectors);
float u2 = getU((s + 1.0d / sectors) * sectors, texture.getU1(), texture.getU0(), sectors);
float v1 = getV(t * stacks, texture.getV1(), texture.getV0(), stacks);
float v2 = getV((t + 1.0d / stacks) * stacks, texture.getV1(), texture.getV0(), stacks);

// For a reason which I am too dumb to understand, the uv coords have a one pixel offset
// So... I just reverse it and it works
v1 -= (texture.getV1() - texture.getV0()) / stacks;
v2 -= (texture.getV1() - texture.getV0()) / stacks;

consumer.vertex(matrix4f, x1, y1, z).color(r, g, b, a).uv(u1, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx1, ny1, nz1).endVertex();
consumer.vertex(matrix4f, x2, y2, z).color(r, g, b, a).uv(u2, v1).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx2, ny2, nz2).endVertex();
consumer.vertex(matrix4f, x3, y3, z3).color(r, g, b, a).uv(u2, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx3, ny3, nz3).endVertex();
consumer.vertex(matrix4f, x4, y4, z4).color(r, g, b, a).uv(u1, v2).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx4, ny4, nz4).endVertex();

// Triangle 2
consumer.vertex(matrix4f, x1, y1, z).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx1, ny1, nz1).endVertex();
consumer.vertex(matrix4f, x2, y2, z).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx2, ny2, nz2).endVertex();
consumer.vertex(matrix4f, x3, y3, z3).color(r, g, b, a).overlayCoords(OverlayTexture.NO_OVERLAY).uv2(0xF000F0).normal(nx3, ny3, nz3).endVertex();
}
}
poseStack.popPose();

}

private static float getU(double pU, float u1, float u0, float resolution) {
float f = u1 - u0;
return u0 + f * (float) pU / resolution;
}

private static float getV(double pV, float v1, float v0, float resolution) {
float f = v1 - v0;
return v0 + f * (float) pV / resolution;
}

public static void drawBox(PoseStack poseStack, VertexConsumer buffer, ResourceLocation texture, float pX, float pY, float pZ, float xRot, float yRot, float zRot, float sX, float sY, float sZ, float pUOffset, float pVOffset, float pWidth, float pHeight) {
poseStack.pushPose();
sX = sX / 16; //Sizes in pixels please
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.Tesselator;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexConsumer;
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.client.smartglasses.objects.threedim.IThreeDObjectRenderer;
Expand All @@ -18,6 +18,7 @@
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.inventory.InventoryMenu;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderLevelStageEvent;
Expand Down Expand Up @@ -64,7 +65,6 @@ public static void renderLevelState(RenderLevelStageEvent event) {
}

//TODO Everything below here is just for debugging and testing. Will be removed before we push to production

BlockPos blockPos = new BlockPos(2, 10, 0);

float[] colors = EnumColor.DARK_PURPLE.getRgb();
Expand All @@ -80,27 +80,33 @@ public static void renderLevelState(RenderLevelStageEvent event) {
BufferUploader.drawWithShader(bufferbuilder.end());
posestack.popPose();

bufferbuilder.begin(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL);
VertexConsumer boxVertexConsumer = Minecraft.getInstance().renderBuffers().bufferSource().getBuffer(RenderType.entityCutout(InventoryMenu.BLOCK_ATLAS));
//RenderSystem.setShader(GameRenderer::getPositionColorLightmapShader);

//bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_LIGHTMAP);
posestack.pushPose();
colors = EnumColor.WHITE.getRgb();

blockPos = new BlockPos(0, 10, 0);
posestack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ());

RenderUtil.drawSphere(posestack, bufferbuilder, 0.5f, 0f, 0f, 0f, 90f, 0f, 0f, colors[0], colors[1], colors[2], 0.6f, 128, 48);
RenderUtil.drawSphere(posestack, boxVertexConsumer, 2f, 0f, 0f, 0f, 270f, 0f, 0f, colors[0], colors[1], colors[2], 0.4f, 16, 128);

BufferUploader.drawWithShader(bufferbuilder.end());
//BufferUploader.drawWithShader(bufferbuilder.end());
posestack.popPose();

bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);
boxVertexConsumer = Minecraft.getInstance().renderBuffers().bufferSource().getBuffer(RenderType.entityCutout(InventoryMenu.BLOCK_ATLAS));

//bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);
posestack.pushPose();

colors = EnumColor.DARK_BLUE.getRgb();
colors = EnumColor.WHITE.getRgb();
blockPos = new BlockPos(6, 10, 0);
posestack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ());

RenderUtil.drawTorus(posestack, bufferbuilder, 0.5f, 0.09f, 0f, 0f, 0f, 0f, 0f, 0f, colors[0], colors[1], colors[2], 0.6f, 300, 32);
RenderUtil.drawTorus(posestack, boxVertexConsumer, 1f, 0.4f, 0f, 0f, 0f, 0f, 0f, 0f, colors[0], colors[1], colors[2], 1f, 48, 48);

BufferUploader.drawWithShader(bufferbuilder.end());
//BufferUploader.drawWithShader(bufferbuilder.end());
posestack.popPose();

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEven
float blue = RenderUtil.getBlue(box.color);

poseStack.translate(-view.x + box.getX(), -view.y + box.getY(), -view.z + box.getZ());
RenderUtil.drawBox(poseStack, bufferBuilder, red, green, blue, alpha, box.x, box.y, box.z, obj.xRot, obj.yRot, obj.zRot, obj.getMaxX(), obj.getMaxY(), obj.getMaxX());
RenderUtil.drawBox(poseStack, bufferBuilder, red, green, blue, alpha, box.x, box.y, box.z, obj.xRot, obj.yRot, obj.zRot, obj.maxX, obj.maxY, obj.maxZ);
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(obj);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexFormat;
import com.mojang.blaze3d.vertex.VertexConsumer;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.SphereObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;

Expand All @@ -24,7 +25,7 @@ public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEven
for (ThreeDimensionalObject obj : batch) {
poseStack.pushPose();
onPreRender(obj);
bufferBuilder.begin(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL);
VertexConsumer boxVertexConsumer = Minecraft.getInstance().renderBuffers().bufferSource().getBuffer(RenderType.entitySmoothCutout(TextureAtlas.LOCATION_BLOCKS));

SphereObject sphere = (SphereObject) obj;

Expand All @@ -35,8 +36,7 @@ public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEven
float blue = RenderUtil.getRed(sphere.color);

poseStack.translate(-view.x, -view.y, -view.z);
RenderUtil.drawSphere(poseStack, bufferBuilder, sphere.radius, sphere.x, sphere.y, sphere.z, sphere.xRot, sphere.yRot, sphere.zRot, red, green, blue, alpha, sphere.sectors, sphere.stacks);
BufferUploader.drawWithShader(bufferBuilder.end());
RenderUtil.drawSphere(poseStack, boxVertexConsumer, sphere.radius, sphere.x, sphere.y, sphere.z, sphere.xRot, sphere.yRot, sphere.zRot, red, green, blue, alpha, sphere.sectors, sphere.stacks);
onPostRender(obj);

poseStack.popPose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,63 +71,63 @@ public final void setColor(int color) {
}

@LuaFunction
public final void setMaxX(float maxX) {
this.maxX = maxX;
public final void setMaxX(double maxX) {
this.maxX = (float) maxX;
getModule().update(this);
}

@LuaFunction
public final float getMaxX() {
public final double getMaxX() {
return maxX;
}

@LuaFunction
public final void setMaxY(float maxY) {
this.maxY = maxY;
public final void setMaxY(double maxY) {
this.maxY = (float) maxY;
getModule().update(this);
}

@LuaFunction
public final float getMaxY() {
public final double getMaxY() {
return maxY;
}

@LuaFunction
public final void setMaxZ(float maxZ) {
this.maxZ = maxZ;
public final void setMaxZ(double maxZ) {
this.maxZ = (float) maxZ;
getModule().update(this);
}

@LuaFunction
public final float getMaxZ() {
public final double getMaxZ() {
return maxZ;
}

@LuaFunction
public final void setX(float x) {
this.x = x;
public final void setX(double x) {
this.x = (float) x;
getModule().update(this);
}

@LuaFunction
public final float getX() {
public final double getX() {
return x;
}

@LuaFunction
public final void setY(int y) {
this.y = y;
public final void setY(double y) {
this.y = (float) y;
getModule().update(this);
}

@LuaFunction
public final float getY() {
public final double getY() {
return y;
}

@LuaFunction
public final void setZ(float z) {
this.z = z;
public final void setZ(double z) {
this.z = (float) z;
getModule().update(this);
}

Expand Down

0 comments on commit c68ed10

Please sign in to comment.