Skip to content

Commit

Permalink
Basic Box and Block renderer with depth test and culling configurable
Browse files Browse the repository at this point in the history
We couldn't use batching for this. We want to change the color, opacity and depthtest/culling for every object and one render call would not support different settings for each object
  • Loading branch information
SirEndii committed Oct 25, 2024
1 parent c7ede95 commit 0b2fcde
Show file tree
Hide file tree
Showing 22 changed files with 297 additions and 74 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static void drawVoxelShape(PoseStack pPoseStack, VertexConsumer pConsumer
}

public static void drawShape(PoseStack pPoseStack, VertexConsumer pConsumer, VoxelShape pShape, double pX, double pY, double pZ, float r, float g, float b, float a) {
PoseStack.Pose posestack$pose = pPoseStack.last();
PoseStack.Pose pose = pPoseStack.last();
pShape.forAllEdges((minX, minY, minZ, maxX, maxY, maxZ) -> {
float f = (float) (maxX - minX);
float f1 = (float) (maxY - minY);
Expand All @@ -138,8 +138,8 @@ public static void drawShape(PoseStack pPoseStack, VertexConsumer pConsumer, Vox
f /= f3;
f1 /= f3;
f2 /= f3;
pConsumer.vertex(posestack$pose.pose(), (float) (minX + pX), (float) (minY + pY), (float) (minZ + pZ)).color(r, g, b, a).normal(posestack$pose.normal(), f, f1, f2).endVertex();
pConsumer.vertex(posestack$pose.pose(), (float) (maxX + pX), (float) (maxY + pY), (float) (maxZ + pZ)).color(r, g, b, a).normal(posestack$pose.normal(), f, f1, f2).endVertex();
pConsumer.vertex(pose.pose(), (float) (minX + pX), (float) (minY + pY), (float) (minZ + pZ)).color(r, g, b, a).normal(pose.normal(), f, f1, f2).endVertex();
pConsumer.vertex(pose.pose(), (float) (maxX + pX), (float) (maxY + pY), (float) (maxZ + pZ)).color(r, g, b, a).normal(pose.normal(), f, f1, f2).endVertex();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.client.smartglasses.objects.threedim.IThreeDObjectRenderer;
import de.srendi.advancedperipherals.client.smartglasses.objects.twodim.ITwoDObjectRenderer;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RenderableObject;
import de.srendi.advancedperipherals.common.util.EnumColor;
import net.minecraft.client.Minecraft;
Expand All @@ -19,7 +19,6 @@
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.phys.shapes.VoxelShape;
import net.minecraftforge.api.distmarker.Dist;
Expand All @@ -42,46 +41,33 @@ public static void renderLevelState(RenderLevelStageEvent event) {

BufferBuilder bufferbuilder = Tesselator.getInstance().getBuilder();
if (event.getStage() == RenderLevelStageEvent.Stage.AFTER_TRANSLUCENT_BLOCKS) {
Map<Class<? extends RenderableObject>, List<RenderableObject>> batches = new HashMap<>();
Map<Class<? extends RenderableObject>, List<ThreeDimensionalObject>> batches = new HashMap<>();

for (RenderableObject object : OverlayObjectHolder.getObjects()) {
if (!object.isEnabled() || object.getRenderObject() instanceof ITwoDObjectRenderer)
if (!object.isEnabled() || !(object.getRenderObject() instanceof IThreeDObjectRenderer))
continue;

Class<? extends RenderableObject> objectClass = object.getClass();
ThreeDimensionalObject threeDimObject = (ThreeDimensionalObject) object;

Class<? extends ThreeDimensionalObject> objectClass = threeDimObject.getClass();

if (batches.containsKey(objectClass)) {
batches.get(objectClass).add(object);
batches.get(objectClass).add(threeDimObject);
continue;
}

List<RenderableObject> newBatchArray = new ArrayList<>();
newBatchArray.add(object);
List<ThreeDimensionalObject> newBatchArray = new ArrayList<>();
newBatchArray.add(threeDimObject);
batches.put(objectClass, newBatchArray);
}

for (List<RenderableObject> batch : batches.values()) {
for (List<ThreeDimensionalObject> batch : batches.values()) {
((IThreeDObjectRenderer) batch.get(0).getRenderObject()).renderBatch(batch, event, posestack, view, bufferbuilder);
}

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

bufferbuilder.begin(RenderType.solid().mode(), DefaultVertexFormat.BLOCK);
BlockPos blockPos = new BlockPos(0, 190, 0);

posestack.pushPose();
posestack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ());
float[] colors = EnumColor.GREEN.getRgb();
RenderSystem.setShaderColor(colors[0], colors[1], colors[2], 0.8f);

Minecraft.getInstance().getBlockRenderer().renderBatched(Blocks.STONE.defaultBlockState(), blockPos, event.getCamera().getEntity().level, posestack, bufferbuilder, false, event.getCamera().getEntity().level.random);
BufferUploader.drawWithShader(bufferbuilder.end());
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);

posestack.popPose();

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

colors = EnumColor.DARK_PURPLE.getRgb();
float[] colors = EnumColor.DARK_PURPLE.getRgb();

RenderSystem.setShader(GameRenderer::getPositionColorShader);
bufferbuilder.begin(RenderType.translucent().mode(), DefaultVertexFormat.POSITION_COLOR_NORMAL);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.srendi.advancedperipherals.client.smartglasses;

import com.mojang.blaze3d.vertex.PoseStack;
import de.srendi.advancedperipherals.client.smartglasses.objects.threedim.IThreeDObjectRenderer;
import de.srendi.advancedperipherals.client.smartglasses.objects.twodim.ITwoDObjectRenderer;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RenderableObject;
import net.minecraftforge.client.gui.overlay.ForgeGui;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.ObjectDecodeRegistry;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BoxObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RectangleObject;
Expand Down Expand Up @@ -47,6 +48,7 @@ public static void registerDecodeObjects() {
ObjectDecodeRegistry.register(TextObject.TYPE_ID, TextObject::decode);
ObjectDecodeRegistry.register(ItemObject.TYPE_ID, ItemObject::decode);

ObjectDecodeRegistry.register(BoxObject.TYPE_ID, BoxObject::decode);
ObjectDecodeRegistry.register(BlockObject.TYPE_ID, BlockObject::decode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,58 @@
import com.mojang.blaze3d.vertex.BufferUploader;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.blaze3d.vertex.PoseStack;
import de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import de.srendi.advancedperipherals.common.util.RegistryUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.List;

public class BlockRenderer implements IThreeDObjectRenderer {

@Override
public void renderBatch(List<RenderableObject> objects, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) {
public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) {
poseStack.pushPose();
bufferBuilder.begin(RenderType.translucent().mode(), DefaultVertexFormat.POSITION_COLOR_NORMAL);

for (RenderableObject renderableObject : objects) {
for (ThreeDimensionalObject renderableObject : batch) {
poseStack.pushPose();
RenderSystem.setShader(GameRenderer::getPositionColorShader);
onPreRender(renderableObject);

bufferBuilder.begin(RenderType.solid().mode(), DefaultVertexFormat.BLOCK);

BlockObject block = (BlockObject) renderableObject;

BlockPos blockPos = new BlockPos(renderableObject.getX(), renderableObject.getY(), renderableObject.getZ());

poseStack.translate(-view.x + blockPos.getX(), -view.y + blockPos.getY(), -view.z + blockPos.getZ());
float alpha = renderableObject.opacity;
float red = (float) (renderableObject.color >> 16 & 255) / 255.0F;
float green = (float) (renderableObject.color >> 8 & 255) / 255.0F;
float blue = (float) (renderableObject.color & 255) / 255.0F;

poseStack.translate(-view.x +renderableObject.getX(), -view.y + renderableObject.getY(), -view.z + renderableObject.getZ());
RenderSystem.setShader(GameRenderer::getBlockShader);
RenderSystem.setShaderColor(red, green, blue, alpha);

Block blockToRender = RegistryUtil.getRegistryEntry(block.block, ForgeRegistries.BLOCKS);

if (blockToRender != null)
Minecraft.getInstance().getBlockRenderer().renderBatched(blockToRender.defaultBlockState(), blockPos, event.getCamera().getEntity().level, poseStack, bufferBuilder, false, event.getCamera().getEntity().level.random);

RenderUtil.drawBox(poseStack, bufferBuilder, red, green, blue, alpha, renderableObject.getMaxX(), renderableObject.getMaxY(), renderableObject.getMaxX());
poseStack.popPose();
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(renderableObject);
RenderSystem.setShaderColor(1f, 1f, 1f, 1f);
}
BufferUploader.drawWithShader(bufferBuilder.end());


poseStack.popPose();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package de.srendi.advancedperipherals.client.smartglasses.objects.threedim;

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 de.srendi.advancedperipherals.client.RenderUtil;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;

import java.util.List;

public class BoxRenderer implements IThreeDObjectRenderer {

@Override
public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder) {
poseStack.pushPose();

for (ThreeDimensionalObject renderableObject : batch) {
poseStack.pushPose();
onPreRender(renderableObject);
bufferBuilder.begin(RenderType.translucent().mode(), DefaultVertexFormat.POSITION_COLOR_NORMAL);

RenderSystem.setShader(GameRenderer::getPositionColorShader);
float alpha = renderableObject.opacity;
float red = (float) (renderableObject.color >> 16 & 255) / 255.0F;
float green = (float) (renderableObject.color >> 8 & 255) / 255.0F;
float blue = (float) (renderableObject.color & 255) / 255.0F;

poseStack.translate(-view.x + renderableObject.getX(), -view.y + renderableObject.getY(), -view.z + renderableObject.getZ());
RenderUtil.drawBox(poseStack, bufferBuilder, red, green, blue, alpha, renderableObject.getMaxX(), renderableObject.getMaxY(), renderableObject.getMaxX());
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(renderableObject);

poseStack.popPose();
}

poseStack.popPose();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package de.srendi.advancedperipherals.client.smartglasses.objects.threedim;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.BufferBuilder;
import com.mojang.blaze3d.vertex.PoseStack;
import de.srendi.advancedperipherals.client.smartglasses.objects.IObjectRenderer;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RenderableObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.ThreeDimensionalObject;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;
import net.minecraftforge.client.gui.overlay.ForgeGui;

import java.util.List;

public interface IThreeDObjectRenderer extends IObjectRenderer {

void renderBatch(List<RenderableObject> object, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder);
void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEvent event, PoseStack poseStack, Vec3 view, BufferBuilder bufferBuilder);

default void onPostRender(ThreeDimensionalObject object) {
if (object.disableCulling)
RenderSystem.enableCull();
if (object.disableDepthTest)
RenderSystem.enableDepthTest();
}

default void onPreRender(ThreeDimensionalObject object) {
if (object.disableCulling)
RenderSystem.disableCull();
if (object.disableDepthTest)
RenderSystem.disableDepthTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.mojang.blaze3d.vertex.PoseStack;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RenderableObject;
import de.srendi.advancedperipherals.common.util.inventory.ItemUtil;
import de.srendi.advancedperipherals.common.util.RegistryUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -19,7 +19,7 @@ public void renderBatch(List<RenderableObject> objects, ForgeGui gui, PoseStack
Minecraft minecraft = Minecraft.getInstance();

for (RenderableObject object : objects) {
Item renderItem = ItemUtil.getRegistryEntry(((ItemObject) object).item, ForgeRegistries.ITEMS);
Item renderItem = RegistryUtil.getRegistryEntry(((ItemObject) object).item, ForgeRegistries.ITEMS);
if (renderItem == null)
continue;
minecraft.getItemRenderer().renderGuiItem(new ItemStack(renderItem), object.x, object.y);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import de.srendi.advancedperipherals.common.smartglasses.SmartGlassesAccess;
import de.srendi.advancedperipherals.common.smartglasses.modules.IModuleFunctions;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BlockObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim.BoxObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.CircleObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.ItemObject;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.two_dim.RectangleObject;
Expand Down Expand Up @@ -64,6 +65,14 @@ public final MethodResult createBlock(IArguments arguments) throws LuaException
return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult createBox(IArguments arguments) throws LuaException {
BoxObject block = new BoxObject(overlayModule, arguments);
RenderableObject object = overlayModule.addObject(block);

return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult getObject(IArguments arguments) throws LuaException {
int id = arguments.getInt(0);
Expand Down Expand Up @@ -91,6 +100,11 @@ public final MethodResult getSize() {
return MethodResult.of(Minecraft.getInstance().getWindow().getWidth(), Minecraft.getInstance().getWindow().getHeight());
}

@LuaFunction
public final MethodResult getCoords() {
return MethodResult.of(access.getEntity().position().x, access.getEntity().position().y, access.getEntity().position().z);
}

@LuaFunction
public final MethodResult update() {
return MethodResult.of(overlayModule.bulkUpdate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.smartglasses.objects.IObjectRenderer;
import de.srendi.advancedperipherals.client.smartglasses.objects.threedim.BlockRenderer;
import de.srendi.advancedperipherals.client.smartglasses.objects.twodim.ITwoDObjectRenderer;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.OverlayModule;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertytypes.StringProperty;
import net.minecraft.network.FriendlyByteBuf;

import java.util.UUID;

public class BlockObject extends ThreeDimensionalObject {
public static final int TYPE_ID = 4;
public static final int TYPE_ID = 5;

private final IObjectRenderer renderer = new BlockRenderer();

Expand All @@ -31,7 +30,7 @@ public BlockObject(UUID player) {
}

@LuaFunction
public void setBlock(String block) {
public final void setBlock(String block) {
this.block = block;
getModule().update(this);
}
Expand All @@ -41,6 +40,7 @@ public final String getBlock() {
return block;
}


@Override
public void encode(FriendlyByteBuf buffer) {
buffer.writeInt(TYPE_ID);
Expand All @@ -64,6 +64,11 @@ public static BlockObject decode(FriendlyByteBuf buffer) {
int z = buffer.readInt();
int maxX = buffer.readInt();
int maxY = buffer.readInt();
int maxZ = buffer.readInt();

boolean disableDepthTest = buffer.readBoolean();
boolean disableCulling = buffer.readBoolean();

String block = buffer.readUtf();

BlockObject clientObject = new BlockObject(player);
Expand All @@ -75,6 +80,9 @@ public static BlockObject decode(FriendlyByteBuf buffer) {
clientObject.z = z;
clientObject.maxX = maxX;
clientObject.maxY = maxY;
clientObject.maxZ = maxZ;
clientObject.disableDepthTest = disableDepthTest;
clientObject.disableCulling = disableCulling;
clientObject.block = block;

return clientObject;
Expand Down
Loading

0 comments on commit 0b2fcde

Please sign in to comment.