Skip to content

Workable ray tracing for SchemaWidget #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions src/main/java/com/cleanroommc/modularui/test/TestTile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cleanroommc.modularui.api.IGuiHolder;
import com.cleanroommc.modularui.api.IPanelHandler;
import com.cleanroommc.modularui.api.drawable.IKey;
import com.cleanroommc.modularui.api.widget.IWidget;
import com.cleanroommc.modularui.drawable.Circle;
import com.cleanroommc.modularui.drawable.GuiTextures;
import com.cleanroommc.modularui.drawable.ItemDrawable;
Expand All @@ -16,6 +17,10 @@
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.utils.Alignment;
import com.cleanroommc.modularui.utils.Color;
import com.cleanroommc.modularui.utils.fakeworld.ArraySchema;
import com.cleanroommc.modularui.utils.fakeworld.BlockInfo;
import com.cleanroommc.modularui.utils.fakeworld.MapSchema;
import com.cleanroommc.modularui.utils.fakeworld.SchemaRenderer;
import com.cleanroommc.modularui.utils.Interpolation;
import com.cleanroommc.modularui.value.BoolValue;
import com.cleanroommc.modularui.value.IntValue;
Expand All @@ -32,20 +37,33 @@
import com.cleanroommc.modularui.widgets.slot.*;
import com.cleanroommc.modularui.widgets.textfield.TextFieldWidget;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;

import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.vertex.DefaultVertexFormats;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fml.common.registry.ForgeRegistries;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemStackHandler;

import org.jetbrains.annotations.NotNull;
import org.lwjgl.opengl.GL11;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -117,7 +135,10 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager,
.child(new PageButton(1, tabController)
.tab(GuiTextures.TAB_TOP, 0))
.child(new PageButton(2, tabController)
.tab(GuiTextures.TAB_TOP, 0)))
.tab(GuiTextures.TAB_TOP, 0))
.child(new PageButton(3, tabController)
.tab(GuiTextures.TAB_TOP, 0)
.overlay(new ItemDrawable(Items.ENDER_EYE).asIcon())))
.child(new Expandable()
.debugName("expandable")
.top(0)
Expand Down Expand Up @@ -368,7 +389,7 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager,
.size(14, 14))
.child(IKey.lang("bogosort.gui.enabled").asWidget()
.height(14)))))
))
.addPage(createSchemaPage())))
.child(SlotGroupWidget.playerInventory(false))
);
/*panel.child(new ButtonWidget<>()
Expand All @@ -384,6 +405,57 @@ public ModularPanel buildUI(PosGuiData guiData, PanelSyncManager guiSyncManager,
return panel;
}

private IWidget createSchemaPage() {
ParentWidget page = new ParentWidget<>();
page.debugName("page 5 schema");
page.sizeRel(1f);
page.child(IKey.str("schema").asWidget());
if (world.isRemote)
page.child(new SchemaWidget(new SchemaRenderer(new ArraySchema(collectBlocksAround())).rayTracing(true).afterRender((__, schema) -> {
RayTraceResult trace = schema.getBlockUnderMouse();
if (trace != null && trace.typeOfHit == RayTraceResult.Type.BLOCK) {
GlStateManager.disableTexture2D();
BufferBuilder buffer = Tessellator.getInstance().getBuffer();
buffer.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
for (Vec3d v : sideVertices.get(trace.sideHit)) {
buffer.pos(
trace.getBlockPos().getX() + v.x,
trace.getBlockPos().getY() + v.y,
trace.getBlockPos().getZ() + v.z
)
.color(128, 255, 128, 100)
.endVertex();
}
Tessellator.getInstance().draw();
}
})).pos(20,20).size(100,100));
return page;
}


double one = 1.001;
double zero = -0.001;
Map<EnumFacing, List<Vec3d>> sideVertices = ImmutableMap.<EnumFacing, List<Vec3d>>builder()
.put(EnumFacing.DOWN, ImmutableList.of(new Vec3d(one, zero, zero), new Vec3d(one, zero, one), new Vec3d(zero, zero, one), new Vec3d(zero, zero, zero)))
.put(EnumFacing.UP, ImmutableList.of(new Vec3d(zero, one, zero), new Vec3d(zero, one, one), new Vec3d(one, one, one), new Vec3d(one, one, zero)))
.put(EnumFacing.SOUTH, ImmutableList.of(new Vec3d(zero, zero, one), new Vec3d(one, zero, one), new Vec3d(one, one, one), new Vec3d(zero, one, one)))
.put(EnumFacing.NORTH, ImmutableList.of(new Vec3d(zero, one, zero), new Vec3d(one, one, zero), new Vec3d(one, zero, zero), new Vec3d(zero, zero, zero)))
.put(EnumFacing.EAST, ImmutableList.of(new Vec3d(one, one, zero), new Vec3d(one, one, one), new Vec3d(one, zero, one), new Vec3d(one, zero, zero)))
.put(EnumFacing.WEST, ImmutableList.of(new Vec3d(zero, zero, zero), new Vec3d(zero, zero, one), new Vec3d(zero, one, one), new Vec3d(zero, one, zero)))
.build();

public BlockInfo[][][] collectBlocksAround(){
BlockInfo[][][] blocks = new BlockInfo[11][11][11];
for (int x = -5; x <= 5; x++) {
for (int y = -5; y <= 5; y++) {
for (int z = -5; z <= 5; z++) {
blocks[x+5][y+5][z+5]=BlockInfo.of(world,pos.add(x,y,z));
}
}
}
return blocks;
}

public ModularPanel openSecondWindow(PanelSyncManager syncManager, IPanelHandler syncHandler) {
ModularPanel panel = new Dialog<>("second_window", null)
.setDisablePanelsBelow(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Builder builder() {

private final World world;
private final BlockInfo[][][] blocks;
private BiPredicate<BlockPos, BlockInfo> renderFilter;
private BiPredicate<BlockPos, BlockInfo> renderFilter = (__,___) -> true;
private final Vec3d center;

public ArraySchema(BlockInfo[][][] blocks) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
Expand All @@ -29,11 +30,24 @@ public static BlockInfo of(IBlockAccess world, BlockPos pos) {
}
TileEntity tile = null;
if (blockState.getBlock().hasTileEntity(blockState)) {
tile = world.getTileEntity(pos);
TileEntity realTile = world.getTileEntity(pos);
tile = fixRealTileWorldCorrupting(realTile, blockState);
}
return new BlockInfo(blockState, tile);
}

@Nullable
private static TileEntity fixRealTileWorldCorrupting(TileEntity realTile, IBlockState blockState) {
TileEntity fakeTile = null;
if(realTile != null){
fakeTile = blockState.getBlock().createTileEntity(null, blockState);
if(fakeTile != null){
fakeTile.deserializeNBT(realTile.serializeNBT());
}
}
return fakeTile;
}

private IBlockState blockState;
private TileEntity tileEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class Camera {

private final Vector3f pos;
private final Vector3f lookAt;
private double yaw, pitch;

public Camera(Vector3f pos, Vector3f lookAt) {
this.pos = pos;
Expand Down Expand Up @@ -44,6 +45,8 @@ public Camera setLookAt(float lookAtX, float lookAtY, float lookAtZ, double radi
pos.y += (float) (Math.tan(pitch) * pos.length());
pos.normalise().scale((float) radius);
this.pos.set(pos.translate(lookAtX, lookAtY, lookAtZ));
this.yaw = yaw;
this.pitch = pitch;
return this;
}

Expand All @@ -54,4 +57,12 @@ public Vector3f getPos() {
public Vector3f getLookAt() {
return lookAt;
}

public double getYaw() {
return yaw;
}

public double getPitch() {
return pitch;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.MinecraftForgeClient;

import org.jetbrains.annotations.Nullable;
import org.lwjgl.opengl.EXTFramebufferObject;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
Expand All @@ -50,11 +51,12 @@ public class SchemaRenderer implements IDrawable {
private boolean cameraSetup = false;
private DoubleSupplier scale;
private BooleanSupplier disableTESR;
private Consumer<IRayTracer> onRayTrace;
private Consumer<Projection> afterRender;
private BiConsumer<Projection, SchemaRenderer> afterRender;
private BiConsumer<Camera, ISchema> cameraFunc;
private int clearColor = 0;
private boolean isometric = false;
private boolean rayTracing = false;
private RayTraceResult lastRayTrace = null;

public SchemaRenderer(ISchema schema, Framebuffer framebuffer) {
this.schema = schema;
Expand All @@ -71,12 +73,7 @@ public SchemaRenderer cameraFunc(BiConsumer<Camera, ISchema> camera) {
return this;
}

public SchemaRenderer onRayTrace(Consumer<IRayTracer> consumer) {
this.onRayTrace = consumer;
return this;
}

public SchemaRenderer afterRender(Consumer<Projection> consumer) {
public SchemaRenderer afterRender(BiConsumer<Projection, SchemaRenderer> consumer) {
this.afterRender = consumer;
return this;
}
Expand Down Expand Up @@ -104,6 +101,16 @@ public SchemaRenderer disableTESR(BooleanSupplier disable) {
return this;
}

public SchemaRenderer rayTracing(boolean rayTracing){
this.rayTracing = rayTracing;
return this;
}

@Nullable
public RayTraceResult getBlockUnderMouse(){
return lastRayTrace;
}

@Override
public void draw(GuiContext context, int x, int y, int width, int height, WidgetTheme widgetTheme) {
render(x, y, width, height, context.getMouseX(), context.getMouseY());
Expand All @@ -124,18 +131,12 @@ public void render(int x, int y, int width, int height, int mouseX, int mouseY)
int lastFbo = bindFBO();
setupCamera(this.framebuffer.framebufferWidth, this.framebuffer.framebufferHeight);
renderWorld();
if (this.onRayTrace != null && Area.isInside(x, y, width, height, mouseX, mouseY)) {
this.onRayTrace.accept(new IRayTracer() {
@Override
public RayTraceResult rayTrace(int screenX, int screenY) {
return SchemaRenderer.this.rayTrace(Projection.INSTANCE.unProject(screenX, screenY));
}

@Override
public RayTraceResult rayTraceMousePos() {
return rayTrace(mouseX, mouseY);
}
});
if (this.rayTracing) {
if (Area.isInside(x, y, width, height, mouseX, mouseY)) {
this.lastRayTrace = rayTrace(mouseX, mouseY, width, height);
} else {
this.lastRayTrace = null;
}
}
resetCamera();
unbindFBO(lastFbo);
Expand All @@ -157,6 +158,21 @@ public RayTraceResult rayTraceMousePos() {
GlStateManager.bindTexture(lastFbo);
}

protected RayTraceResult rayTrace(int mouseX, int mouseY, int width, int height) {
final float halfPI = (float) (Math.PI / 2);
Vec3d cameraPos = new Vec3d(camera.getPos().x, camera.getPos().y, camera.getPos().z);
float yaw = (float) camera.getYaw();
float pitch = (float) camera.getPitch();

Vec3d mouseXShift = new Vec3d(1, 0, 0).rotatePitch(pitch).rotateYaw(-yaw + halfPI).scale(mouseX - width / 2d).scale(1 / 32d);
Vec3d mouseYShift = new Vec3d(0, -1, 0).rotatePitch(pitch).rotateYaw(-yaw + halfPI).scale(mouseY - height / 2d).scale(1 / 32d);
Vec3d mousePos = cameraPos.add(mouseXShift).add(mouseYShift);
Vec3d focus = new Vec3d(camera.getLookAt().x, camera.getLookAt().y, camera.getLookAt().z);
double perspectiveCompensation = isometric? 1: cameraPos.distanceTo(focus) / 3 * width/100;
Vec3d underMousePos = focus.add(mouseXShift.scale(perspectiveCompensation)).add(mouseYShift.scale(perspectiveCompensation));
return schema.getWorld().rayTraceBlocks(mousePos, underMousePos);
}

private void renderWorld() {
Minecraft mc = Minecraft.getMinecraft();
GlStateManager.enableCull();
Expand Down Expand Up @@ -215,7 +231,7 @@ private void renderWorld() {
GlStateManager.disableBlend();
GlStateManager.depthMask(true);
if (this.afterRender != null) {
this.afterRender.accept(Projection.INSTANCE);
this.afterRender.accept(Projection.INSTANCE, this);
}
}

Expand Down Expand Up @@ -313,24 +329,10 @@ private void unbindFBO(int lastID) {
OpenGlHelper.glBindFramebuffer(OpenGlHelper.GL_FRAMEBUFFER, lastID);
}

private RayTraceResult rayTrace(Vector3f hitPos) {
Vec3d startPos = new Vec3d(this.camera.getPos().x, this.camera.getPos().y, this.camera.getPos().z);
hitPos.scale(2); // Double view range to ensure pos can be seen.
Vec3d endPos = new Vec3d((hitPos.x - startPos.x), (hitPos.y - startPos.y), (hitPos.z - startPos.z));
return this.schema.getWorld().rayTraceBlocks(startPos, endPos);
}

public boolean isCameraSetup() {
return cameraSetup;
}

public interface IRayTracer {

RayTraceResult rayTrace(int screenX, int screenY);

RayTraceResult rayTraceMousePos();
}

public interface ICamera {

void setupCamera(Vector3f cameraPos, Vector3f lookAt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.cleanroommc.modularui.widget.Widget;

import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.RayTraceResult;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -138,6 +139,10 @@ public SchemaWidget enableAllInteraction(boolean enable) {
return schema;
}

public RayTraceResult getBlockUnderMouse() {
return schema.getBlockUnderMouse();
}

public static class LayerButton extends ButtonWidget<LayerButton> {

private final int minLayer;
Expand Down