Skip to content

Commit

Permalink
✨ Working outline
Browse files Browse the repository at this point in the history
  • Loading branch information
PriestOfFerns committed Feb 20, 2024
1 parent adc473e commit 2562b5d
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.priestoffern.vs_ship_assembler.mixin;

import com.mojang.blaze3d.vertex.PoseStack;
import io.github.priestoffern.vs_ship_assembler.rendering.Renderer;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GameRenderer;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import static io.github.priestoffern.vs_ship_assembler.rendering.RendererKt.renderData;


@Mixin(GameRenderer.class)
public abstract class GameRendererMixin {
@Shadow @Final private Camera mainCamera;
@Shadow @Final private Minecraft minecraft;

@Inject(method = "renderLevel", at = @At(value = "FIELD", target = "Lnet/minecraft/client/renderer/GameRenderer;renderHand:Z", opcode = Opcodes.GETFIELD, ordinal = 0 ))
void vsshipassembler_postWorldRender(float partialTicks, long finishTimeNano, PoseStack matrixStack, CallbackInfo ci) {
minecraft.getProfiler().push("vsshipassembler_rendering_phase");
renderData(matrixStack, mainCamera);
minecraft.getProfiler().pop();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import dev.architectury.registry.registries.DeferredRegister
import dev.architectury.registry.registries.RegistrySupplier
import io.github.priestoffern.vs_ship_assembler.Items.ShipAssemblerItem
import io.github.priestoffern.vs_ship_assembler.items.ShipAssemblerItem

object VsShipAssemblerItems {
val ITEMS = DeferredRegister.create(VsShipAssemblerMod.MOD_ID, Registry.ITEM_REGISTRY)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
package io.github.priestoffern.vs_ship_assembler.Items
package io.github.priestoffern.vs_ship_assembler.items

import de.m_marvin.univec.impl.Vec3d
import io.github.priestoffern.vs_ship_assembler.VsShipAssemblerTags
import io.github.priestoffern.vs_ship_assembler.rendering.Renderer
import io.github.priestoffern.vs_ship_assembler.rendering.RenderingData
import io.github.priestoffern.vs_ship_assembler.rendering.SelectionZoneRenderer
import io.github.priestoffern.vs_ship_assembler.util.PhysicUtility
import net.minecraft.Util
import net.minecraft.client.resources.sounds.Sound
import net.minecraft.core.BlockPos
import net.minecraft.network.chat.TextComponent
import net.minecraft.util.Mth
import net.minecraft.world.InteractionHand
import net.minecraft.world.InteractionResultHolder
import net.minecraft.world.entity.Entity
import net.minecraft.world.entity.player.Player
import net.minecraft.world.item.Item
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.ClipContext
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.SoundType
import net.minecraft.world.phys.BlockHitResult
import org.joml.Vector3d
import org.valkyrienskies.mod.common.getShipObjectManagingPos
import org.valkyrienskies.mod.common.util.toJOML
import org.valkyrienskies.mod.common.util.toMinecraft
import java.awt.Color
import java.lang.Math.*

class ShipAssemblerItem(properties: Properties): Item(properties) {

var firstPosition: BlockPos? = null
var secondPosition: BlockPos? = null
var SelectionZone: RenderingData? = null

override fun use(level: Level, player: Player, interactionHand: InteractionHand): InteractionResultHolder<ItemStack> {
val clipResult = level.clip(
Expand Down Expand Up @@ -54,10 +62,11 @@ class ShipAssemblerItem(properties: Properties): Item(properties) {
fun makeSelection(level: Level, player: Player, interactionHand: InteractionHand, pos: BlockPos){

if (!level.isClientSide) {
println(level.getBlockState(pos))
if (player.isShiftKeyDown and (level.getBlockState(pos).isAir)) {
firstPosition = null
secondPosition = null
if (SelectionZone!=null) Renderer.removeRender(SelectionZone!!)
SelectionZone = null;
player.sendMessage(TextComponent("Selection reset"), Util.NIL_UUID)
} else if (firstPosition == null) {
if (level.getShipObjectManagingPos(pos) == null) {
Expand All @@ -68,8 +77,16 @@ class ShipAssemblerItem(properties: Properties): Item(properties) {
}
} else if (secondPosition == null) {
if (level.getShipObjectManagingPos(pos) == null) {


if (SelectionZone!=null) Renderer.removeRender(SelectionZone!!)
SelectionZone = null;

secondPosition = pos
player.sendMessage(TextComponent("Second pos selected"), Util.NIL_UUID)
val SZ = SelectionZoneRenderer(Vec3d(firstPosition!!.x.toDouble(),
firstPosition!!.y.toDouble(), firstPosition!!.z.toDouble()),Vec3d(pos.x.toDouble(),pos.y.toDouble(),pos.z.toDouble()), Color.GREEN);
SelectionZone = Renderer.addRender(SZ)
} else {
player.sendMessage(TextComponent("Selected position is on a ship!"), Util.NIL_UUID)
}
Expand All @@ -93,11 +110,44 @@ class ShipAssemblerItem(properties: Properties): Item(properties) {
player.sendMessage(TextComponent("Failed to Assemble: Empty ship"), Util.NIL_UUID)
}


if (SelectionZone!=null) Renderer.removeRender(SelectionZone!!)
SelectionZone = null;
firstPosition = null
secondPosition = null
}
}
}


@Override
override fun inventoryTick(stack: ItemStack, level: Level, entity: Entity, slotId: Int, isSelected: Boolean) {
super.inventoryTick(stack, level, entity, slotId, isSelected)

if (isSelected && firstPosition!=null && secondPosition == null) {
if (SelectionZone!=null) Renderer.removeRender(SelectionZone!!)
SelectionZone = null;

val res = raycast(level,entity as Player,ClipContext.Fluid.NONE)
if (res!=null) {
val SZ = SelectionZoneRenderer(Vec3d(firstPosition!!.x.toDouble(),
firstPosition!!.y.toDouble(), firstPosition!!.z.toDouble()),Vec3d(res.blockPos.x.toDouble(),res.blockPos.y.toDouble(),res.blockPos.z.toDouble()), Color.GREEN);
SelectionZone = Renderer.addRender(SZ)
}
}
}

protected fun raycast(level: Level, player: Player, fluidMode: ClipContext.Fluid?): BlockHitResult? {
val f = player.xRot
val g = player.yRot
val vec3 = player.eyePosition
val h = Mth.cos(-g * 0.017453292f - 3.1415927f)
val i = Mth.sin(-g * 0.017453292f - 3.1415927f)
val j = -Mth.cos(-f * 0.017453292f)
val k = Mth.sin(-f * 0.017453292f)
val l = i * j
val n = h * j
val d = 5.0
val vec32 = vec3.add(l.toDouble() * 5.0, k.toDouble() * 5.0, n.toDouble() * 5.0)
return level.clip(ClipContext(vec3, vec32, ClipContext.Block.OUTLINE, fluidMode, player))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package io.github.priestoffern.vs_ship_assembler.rendering

import com.google.common.base.Supplier
import com.mojang.blaze3d.vertex.PoseStack

import net.minecraft.client.Camera
import net.minecraft.client.Minecraft

import org.valkyrienskies.mod.common.shipObjectWorld


fun renderData(poseStack: PoseStack, camera: Camera) {

for (data in Renderer.toRender) {
data.renderData(poseStack, camera)
}
}

object Renderer {
var CurrentId:Long = 0;
val toRender = mutableListOf<RenderingData>()

fun addRender(renderData: RenderingData): RenderingData {
renderData.Id = CurrentId;
CurrentId++

toRender.add(renderData);
return renderData;
}

fun removeRender(renderData: RenderingData) {
toRender.remove(renderData);
}
}


interface RenderingData {
var Id: Long;
fun renderData(poseStack: PoseStack, camera: Camera)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package io.github.priestoffern.vs_ship_assembler.rendering

import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.*
import com.mojang.math.Matrix4f
import de.m_marvin.univec.impl.Vec3d
import net.fabricmc.loader.impl.lib.sat4j.core.Vec
import net.minecraft.client.Camera
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.GameRenderer
import net.minecraft.client.renderer.RenderType
import org.joml.Vector3d
import org.lwjgl.opengl.GL11
import java.awt.Color
class SelectionZoneRenderer() : RenderingData {
var point0 = Vec3d();
var point1 = Vec3d();
var color: Color = Color(0)
override var Id: Long = 0;
constructor(
point0: Vec3d,
point1: Vec3d,
color: Color,
): this() {
this.point0 = point0
this.point1 = point1
this.color = color
}

override fun renderData(poseStack: PoseStack, camera: Camera) {


val tesselator = Tesselator.getInstance()
val vBuffer = tesselator.builder

RenderSystem.enableDepthTest()
RenderSystem.depthFunc(GL11.GL_LEQUAL)
RenderSystem.depthMask(true)
RenderSystem.setShader(GameRenderer::getPositionColorShader)

// val light = LightTexture.pack(level!!.getBrightness(LightLayer.BLOCK, point1.toBlockPos()), level!!.getBrightness(LightLayer.SKY, point1.toBlockPos()))

val light = Int.MAX_VALUE

vBuffer.begin(VertexFormat.Mode.DEBUG_LINES, DefaultVertexFormat.POSITION_COLOR_LIGHTMAP)

poseStack.pushPose()

val cameraPos = camera.position

val Add1 = Vec3d(if (point0.x>=point1.x) 1.0 else 0.0, if (point0.y>=point1.y) 1.0 else 0.0, if (point0.z>=point1.z) 1.0 else 0.0)
val Add2 = Vec3d(if (point1.x>point0.x) 1.0 else 0.0, if (point1.y>point0.y) 1.0 else 0.0, if (point1.z>point0.z) 1.0 else 0.0)

val tpos1 = Vec3d(point0.x-cameraPos.x,point0.y-cameraPos.y,point0.z-cameraPos.z).add(Add1)
val tpos2 = Vec3d(point1.x-cameraPos.x,point1.y-cameraPos.y,point1.z-cameraPos.z).add(Add2)

val matrix = poseStack.last().pose()
makeBox(
vBuffer, matrix,
color.red, color.green, color.blue, color.alpha, light,
tpos1, tpos2
)

tesselator.end()

poseStack.popPose()


}

fun tof(n: Double) = n.toFloat()
fun makeBox(buf: VertexConsumer, matrix: Matrix4f,
r: Int, g: Int, b: Int, a: Int, lightmapUV: Int,
A: Vec3d, B:Vec3d
) {
buf.vertex(matrix, tof(A.x), tof(A.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(A.x), tof(A.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(A.x), tof(A.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(A.x), tof(B.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(A.x), tof(A.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(A.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()


buf.vertex(matrix, tof(A.x), tof(A.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(A.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(A.x), tof(A.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(A.x), tof(B.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(A.x), tof(B.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(B.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()


buf.vertex(matrix, tof(A.x), tof(B.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(A.x), tof(B.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(A.x), tof(B.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(B.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(B.x), tof(B.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(B.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()


buf.vertex(matrix, tof(B.x), tof(A.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(B.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(B.x), tof(A.y), tof(A.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(A.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()

buf.vertex(matrix, tof(B.x), tof(A.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
buf.vertex(matrix, tof(B.x), tof(B.y), tof(B.z)).color(r, g, b, a).uv2(lightmapUV).endVertex()
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"package": "io.github.priestoffern.vs_ship_assembler.mixin",
"compatibilityLevel": "JAVA_17",
"mixins": [
"GameRendererMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 2562b5d

Please sign in to comment.