diff options
author | Soopyboo32 <49228220+Soopyboo32@users.noreply.github.com> | 2022-09-17 19:39:05 +0800 |
---|---|---|
committer | Soopyboo32 <49228220+Soopyboo32@users.noreply.github.com> | 2022-09-17 19:39:05 +0800 |
commit | 431e4fc9d1657a50ebc34b8ac24f9bfaea06417f (patch) | |
tree | 5987bb14f38d2999c682970429f34b41eb3e5826 /src/utils/renderLib2d.js | |
parent | e73f2efdf0f50aa775c540317394d46428e9704f (diff) | |
download | SoopyV2-431e4fc9d1657a50ebc34b8ac24f9bfaea06417f.tar.gz SoopyV2-431e4fc9d1657a50ebc34b8ac24f9bfaea06417f.tar.bz2 SoopyV2-431e4fc9d1657a50ebc34b8ac24f9bfaea06417f.zip |
Initial move to babel + change fetch to use async/await
Diffstat (limited to 'src/utils/renderLib2d.js')
-rw-r--r-- | src/utils/renderLib2d.js | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/src/utils/renderLib2d.js b/src/utils/renderLib2d.js new file mode 100644 index 0000000..30ac28f --- /dev/null +++ b/src/utils/renderLib2d.js @@ -0,0 +1,172 @@ + +//-------------------------------------------------------------------------- +// CODE BY DJtheRedstoner +// IM COPYING THIS BECAUSE THE UPLOADED VERSION IS FOR +// CT 2.0.0 ONLY +// +// Edit: iv added some features to this so might keep as is +//-------------------------------------------------------------------------- + +import { f, m } from "../../mappings/mappings"; + + +const BufferUtils = org.lwjgl.BufferUtils; +const Project = org.lwjgl.util.glu.Project; + +const modelViewMatrix = BufferUtils.createFloatBuffer(16); +const projectionMatrix = BufferUtils.createFloatBuffer(16); +const viewportDims = BufferUtils.createIntBuffer(16); + +if(!GlStateManager){ + var GL11 = Java.type("org.lwjgl.opengl.GL11"); //using var so it goes to global scope + var GlStateManager = Java.type("net.minecraft.client.renderer.GlStateManager"); +} + +const ScaledResolution = net.minecraft.client.gui.ScaledResolution; + +const AxisAlignedBB = Java.type("net.minecraft.util.AxisAlignedBB") + +register('renderWorld', () => { + GlStateManager[m.pushMatrix](); + + let x = Player.getX(); + let y = Player.getY(); + let z = Player.getZ(); + + Tessellator.translate(-x, -y, -z); + + GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelViewMatrix); + GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projectionMatrix); + + GlStateManager[m.popMatrix](); + + GL11.glGetInteger(GL11.GL_VIEWPORT, viewportDims); +}); + +export default class RenderLib2D { + // Utils + + // Original made by DJtheRedstoner + static projectPoint = (posX, posY, posZ) => { + const coords = BufferUtils.createFloatBuffer(3); + const success = Project.gluProject( + posX, + posY, + posZ, + modelViewMatrix, + projectionMatrix, + viewportDims, + coords + ); + + const z = coords.get(2); + if (!success || !(z > 0 && z < 1)) return null; + + const sr = new ScaledResolution(Client.getMinecraft()); + + const x = coords.get(0) / sr[m.getScaleFactor](); // getScaleFactor + let y = coords.get(1) / sr[m.getScaleFactor](); // getScaleFactor + // OpenGL starts at bottom left, mc starts at top left + y = sr[m.getScaledHeight]() - y; // getScaledHeight + + return { x, y, z }; + } + + static drawLine(x1, y1, z1, x2, y2, z2, color, thickness=1) { + let pos1 = RenderLib2D.projectPoint(x1, y1, z1); + let pos2 = RenderLib2D.projectPoint(x2, y2, z2); + + if(!pos1 || !pos2) return; + + let {x, y} = pos1 + let {x:ox, y:oy} = pos2 + + // console.log(x, y, ox, oy, thickness) + Renderer.drawLine(color, x, y, ox, oy, thickness); + } + + // Original made by DJtheRedstoner + static calculateBoundingBox = (box) => { + let vertices = RenderLib2D.getVertices(box); + + let x1 = java.lang.Float.MAX_VALUE; + let x2 = 0; + let y1 = java.lang.Float.MAX_VALUE; + let y2 = 0; + + vertices.forEach(vertex => { + let vec = RenderLib2D.projectPoint(vertex.x, vertex.y, vertex.z); + if (vec == null) return null; + + let x = vec.x; + let y = vec.y; + + if (x < x1) x1 = x; + if (x > x2) x2 = x; + if (y < y1) y1 = y; + if (y > y2) y2 = y; + }); + + return { x1, y1, x2, y2 }; + } + + static getVertices = (box) => { + let list = []; + + list.push({ x: box[f.minX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] }); + list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] }); + list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] }); + list.push({ x: box[f.minX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.minZ.AxisAlignedBB] }); + list.push({ x: box[f.minX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] }); + list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.minY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] }); + list.push({ x: box[f.maxX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] }); + list.push({ x: box[f.minX.AxisAlignedBB], y: box[f.maxY.AxisAlignedBB], z: box[f.maxZ.AxisAlignedBB] }); + + return list; + } + + // Rendering Functions + + static drawNameTag = (vec, string) => { + if (vec === null) return; + + Renderer.drawStringWithShadow(string, vec.x - Renderer.getStringWidth(string) / 2, vec.y); + } + + static draw2DESP = (aabb, color, thickness) => { + let bb = RenderLib2D.calculateBoundingBox(aabb); + + Renderer.drawLine(color, bb.x1, bb.y1, bb.x1, bb.y2, thickness); + Renderer.drawLine(color, bb.x1, bb.y1, bb.x2, bb.y1, thickness); + Renderer.drawLine(color, bb.x2, bb.y2, bb.x2, bb.y1, thickness); + Renderer.drawLine(color, bb.x2, bb.y2, bb.x1, bb.y2, thickness); + } + + static draw3DESP = (aabb, color, thickness) => { + let vertices = RenderLib2D.getVertices(aabb); + let projected = []; + + vertices.forEach(vertex => { + let vec = RenderLib2D.projectPoint(vertex.x, vertex.y, vertex.z); + if (vec == null) return null; + projected.push(vec); + }); + + if (projected[0] && projected[1]) Renderer.drawLine(color, projected[0].x, projected[0].y, projected[1].x, projected[1].y, thickness); + if (projected[0] && projected[4]) Renderer.drawLine(color, projected[0].x, projected[0].y, projected[4].x, projected[4].y, thickness); + if (projected[5] && projected[1]) Renderer.drawLine(color, projected[5].x, projected[5].y, projected[1].x, projected[1].y, thickness); + if (projected[5] && projected[4]) Renderer.drawLine(color, projected[5].x, projected[5].y, projected[4].x, projected[4].y, thickness); + if (projected[3] && projected[2]) Renderer.drawLine(color, projected[3].x, projected[3].y, projected[2].x, projected[2].y, thickness); + if (projected[3] && projected[7]) Renderer.drawLine(color, projected[3].x, projected[3].y, projected[7].x, projected[7].y, thickness); + if (projected[6] && projected[2]) Renderer.drawLine(color, projected[6].x, projected[6].y, projected[2].x, projected[2].y, thickness); + if (projected[6] && projected[7]) Renderer.drawLine(color, projected[6].x, projected[6].y, projected[7].x, projected[7].y, thickness); + if (projected[1] && projected[2]) Renderer.drawLine(color, projected[1].x, projected[1].y, projected[2].x, projected[2].y, thickness); + if (projected[0] && projected[3]) Renderer.drawLine(color, projected[0].x, projected[0].y, projected[3].x, projected[3].y, thickness); + if (projected[4] && projected[7]) Renderer.drawLine(color, projected[4].x, projected[4].y, projected[7].x, projected[7].y, thickness); + if (projected[5] && projected[6]) Renderer.drawLine(color, projected[5].x, projected[5].y, projected[6].x, projected[6].y, thickness); + } + + static getBlockAABB = (x, y, z) => { + return new AxisAlignedBB(x, y, z, x + 1, y + 1, z + 1); + } +} |