Skip to content

Commit

Permalink
Sphere renderer, very cursed
Browse files Browse the repository at this point in the history
  • Loading branch information
SirEndii committed Nov 4, 2024
1 parent 07d14d8 commit 396e6b7
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 68 deletions.
205 changes: 141 additions & 64 deletions src/main/java/de/srendi/advancedperipherals/client/RenderUtil.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public static void renderLevelState(RenderLevelStageEvent event) {
((IThreeDObjectRenderer) batch.get(0).getRenderObject()).renderBatch(batch, event, posestack, view, bufferbuilder);
}

BlockPos blockPos = new BlockPos(2, 190, 0);
//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 @@ -85,7 +87,7 @@ public static void renderLevelState(RenderLevelStageEvent event) {
bufferbuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);
colors = EnumColor.LIGHT_PURPLE.getRgb();

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

VoxelShape shape = Block.box(0.0, 0.0, 0.0, 16.0, 18.0, 16.0);
Expand All @@ -97,6 +99,17 @@ public static void renderLevelState(RenderLevelStageEvent event) {
BufferUploader.drawWithShader(bufferbuilder.end());
posestack.popPose();

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

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, colors[0], colors[1], colors[2], 0.6f, 128, 48);

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

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,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.three_dim.SphereObject;
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 @@ -50,5 +51,6 @@ public static void registerDecodeObjects() {

ObjectDecodeRegistry.register(BoxObject.TYPE_ID, BoxObject::decode);
ObjectDecodeRegistry.register(BlockObject.TYPE_ID, BlockObject::decode);
ObjectDecodeRegistry.register(SphereObject.TYPE_ID, SphereObject::decode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
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 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;

Expand All @@ -23,7 +23,7 @@ public void renderBatch(List<ThreeDimensionalObject> batch, RenderLevelStageEven
for (ThreeDimensionalObject renderableObject : batch) {
poseStack.pushPose();
onPreRender(renderableObject);
bufferBuilder.begin(RenderType.translucent().mode(), DefaultVertexFormat.POSITION_COLOR_NORMAL);
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR_NORMAL);

RenderSystem.setShader(GameRenderer::getPositionColorShader);
float alpha = renderableObject.opacity;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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 com.mojang.blaze3d.vertex.VertexFormat;
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.renderer.GameRenderer;
import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.RenderLevelStageEvent;

import java.util.List;

public class SphereRenderer 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(VertexFormat.Mode.TRIANGLES, DefaultVertexFormat.POSITION_COLOR_NORMAL);

SphereObject sphere = (SphereObject) renderableObject;

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.drawSphere(poseStack, bufferBuilder, 1f, 0f, 0f, 0f, red, green, blue, alpha, sphere.sectors, sphere.stacks);
BufferUploader.drawWithShader(bufferBuilder.end());
onPostRender(renderableObject);

poseStack.popPose();
}


poseStack.popPose();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
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.three_dim.SphereObject;
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 @@ -73,6 +74,14 @@ public final MethodResult createBox(IArguments arguments) throws LuaException {
return MethodResult.of(object, "SUCCESS");
}

@LuaFunction
public final MethodResult createSphere(IArguments arguments) throws LuaException {
SphereObject block = new SphereObject(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
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package de.srendi.advancedperipherals.common.smartglasses.modules.overlay.objects.three_dim;

import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.LuaException;
import de.srendi.advancedperipherals.AdvancedPeripherals;
import de.srendi.advancedperipherals.client.smartglasses.objects.IObjectRenderer;
import de.srendi.advancedperipherals.client.smartglasses.objects.threedim.SphereRenderer;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.OverlayModule;
import de.srendi.advancedperipherals.common.smartglasses.modules.overlay.propertytypes.FixedPointNumberProperty;
import net.minecraft.network.FriendlyByteBuf;

import java.util.UUID;

public class SphereObject extends ThreeDimensionalObject {
public static final int TYPE_ID = 6;

private final IObjectRenderer renderer = new SphereRenderer();

@FixedPointNumberProperty(min = 1, max = 1024)
public int sectors = 16;

@FixedPointNumberProperty(min = 1, max = 1024)
public int stacks = 16;

public SphereObject(OverlayModule module, IArguments arguments) throws LuaException {
super(module, arguments);
reflectivelyMapProperties(arguments);
}

public SphereObject(UUID player) {
super(player);
}

@Override
public void encode(FriendlyByteBuf buffer) {
buffer.writeInt(TYPE_ID);
super.encode(buffer);
buffer.writeInt(sectors);
buffer.writeInt(stacks);
}

public static SphereObject decode(FriendlyByteBuf buffer) {
int objectId = buffer.readInt();
boolean hasValidUUID = buffer.readBoolean();
if (!hasValidUUID) {
AdvancedPeripherals.exception("Tried to decode a buffer for an OverlayObject but without a valid player as target.", new IllegalArgumentException());
return null;
}
UUID player = buffer.readUUID();
int color = buffer.readInt();
float opacity = buffer.readFloat();

int x = buffer.readInt();
int y = buffer.readInt();
int z = buffer.readInt();
int maxX = buffer.readInt();
int maxY = buffer.readInt();
int maxZ = buffer.readInt();

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

int sectors = buffer.readInt();
int stacks = buffer.readInt();

SphereObject clientObject = new SphereObject(player);
clientObject.setId(objectId);
clientObject.color = color;
clientObject.opacity = opacity;
clientObject.x = x;
clientObject.y = y;
clientObject.z = z;
clientObject.maxX = maxX;
clientObject.maxY = maxY;
clientObject.maxZ = maxZ;
clientObject.disableDepthTest = disableDepthTest;
clientObject.disableCulling = disableCulling;
clientObject.sectors = sectors;
clientObject.stacks = stacks;

return clientObject;
}

@Override
public IObjectRenderer getRenderObject() {
return renderer;
}
}

0 comments on commit 396e6b7

Please sign in to comment.