Skip to content

Commit

Permalink
Cached speck verts on client
Browse files Browse the repository at this point in the history
  • Loading branch information
miyucomics committed Jul 10, 2024
1 parent 78266c5 commit 886c692
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 68 deletions.
25 changes: 0 additions & 25 deletions src/main/java/miyucomics/hexical/entities/MeshRenderer.kt
Original file line number Diff line number Diff line change
@@ -1,41 +1,16 @@
package miyucomics.hexical.entities

import com.mojang.blaze3d.systems.RenderSystem
import miyucomics.hexical.utils.RenderUtils
import net.minecraft.client.MinecraftClient
import net.minecraft.client.render.Frustum
import net.minecraft.client.render.GameRenderer
import net.minecraft.client.render.VertexConsumerProvider
import net.minecraft.client.render.entity.EntityRenderer
import net.minecraft.client.render.entity.EntityRendererFactory
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.util.Identifier
import net.minecraft.util.math.Vec3f

class MeshRenderer(ctx: EntityRendererFactory.Context) : EntityRenderer<MeshEntity>(ctx) {
override fun getTexture(entity: MeshEntity?): Identifier? = null
override fun shouldRender(entity: MeshEntity?, frustum: Frustum?, x: Double, y: Double, z: Double) = true
override fun render(entity: MeshEntity?, yaw: Float, tickDelta: Float, matrices: MatrixStack, vertexConsumers: VertexConsumerProvider, light: Int) {
val oldShader = RenderSystem.getShader()
RenderSystem.setShader(GameRenderer::getPositionColorShader)
RenderSystem.enableDepthTest()
matrices.push()

val pos = MinecraftClient.getInstance().player!!.pos
if (entity!!.yaw != 0.0f)
matrices.multiply(Vec3f.POSITIVE_Y.getDegreesQuaternion(-entity.yaw))
if (entity.pitch != 0.0f)
matrices.multiply(Vec3f.POSITIVE_X.getDegreesQuaternion(entity.pitch))
if (entity.clientRoll != 0.0f)
matrices.multiply(Vec3f.POSITIVE_Z.getDegreesQuaternion(entity.clientRoll))
matrices.scale(entity!!.clientSize, entity.clientSize, entity.clientSize)
matrices.translate(entity.x - pos.x, entity.y - pos.y, entity.z - pos.z)

RenderSystem.disableCull()
RenderUtils.sentinelLike(matrices, entity.clientVertices, 5f, entity.clientPigment)
RenderSystem.enableCull()

matrices.pop()
RenderSystem.setShader { oldShader }
}
}
6 changes: 4 additions & 2 deletions src/main/java/miyucomics/hexical/entities/SpeckEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import at.petrak.hexcasting.api.spell.math.HexDir
import at.petrak.hexcasting.api.spell.math.HexPattern
import at.petrak.hexcasting.api.utils.putCompound
import miyucomics.hexical.interfaces.Specklike
import miyucomics.hexical.utils.RenderUtils
import net.minecraft.entity.Entity
import net.minecraft.entity.EntityDimensions
import net.minecraft.entity.EntityPose
Expand All @@ -17,6 +18,7 @@ import net.minecraft.entity.data.TrackedDataHandlerRegistry
import net.minecraft.nbt.NbtCompound
import net.minecraft.network.packet.s2c.play.EntitySpawnS2CPacket
import net.minecraft.text.Text
import net.minecraft.util.math.Vec2f
import net.minecraft.world.World

class SpeckEntity(entityType: EntityType<SpeckEntity?>?, world: World?) : Entity(entityType, world), Specklike {
Expand All @@ -33,7 +35,7 @@ class SpeckEntity(entityType: EntityType<SpeckEntity?>?, world: World?) : Entity
// client-only
var clientIsText = false
var clientText: Text = Text.empty()
var clientPattern: HexPattern = HexPattern.fromAngles("", HexDir.EAST)
var clientVerts: List<Vec2f> = listOf()
var clientPigment: FrozenColorizer = FrozenColorizer.DEFAULT.get()
var clientSize = 1f
var clientThickness = 1f
Expand Down Expand Up @@ -66,7 +68,7 @@ class SpeckEntity(entityType: EntityType<SpeckEntity?>?, world: World?) : Entity
this.clientText = Text.Serializer.fromJson(raw.getString("text"))!!
} else {
this.clientIsText = false
this.clientPattern = HexPattern.fromNBT(raw)
this.clientVerts = RenderUtils.getNormalizedStrokes(HexPattern.fromNBT(raw))
}
}
pigmentDataTracker -> this.clientPigment = FrozenColorizer.fromNBT(dataTracker.get(pigmentDataTracker))
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/miyucomics/hexical/entities/SpeckRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class SpeckRenderer(ctx: EntityRendererFactory.Context) : EntityRenderer<SpeckEn
matrices.scale(0.025f, -0.025f, 0.025f)
textRenderer.draw(matrices, entity.clientText, height, -textRenderer.fontHeight.toFloat() / 2f, entity.clientPigment.getColor(0f, entity.pos))
} else {
RenderUtils.drawFigure(matrices.peek().positionMatrix, RenderUtils.getNormalizedStrokes(entity.clientPattern), entity.clientThickness * 0.05f, entity.clientPigment, entity.pos)
RenderUtils.drawFigure(matrices.peek().positionMatrix, entity.clientVerts, entity.clientThickness * 0.05f / entity.clientSize, entity.clientPigment, entity.pos)
}
RenderSystem.enableCull()

Expand Down
42 changes: 2 additions & 40 deletions src/main/java/miyucomics/hexical/utils/RenderUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ package miyucomics.hexical.utils
import at.petrak.hexcasting.api.misc.FrozenColorizer
import at.petrak.hexcasting.api.spell.math.HexPattern
import at.petrak.hexcasting.client.rotate
import com.mojang.blaze3d.systems.RenderSystem
import net.minecraft.client.render.GameRenderer
import net.minecraft.client.render.Tessellator
import net.minecraft.client.render.VertexFormat
import net.minecraft.client.render.VertexFormats
import net.minecraft.client.util.math.MatrixStack
import net.minecraft.util.math.MathHelper
import net.minecraft.util.math.Matrix4f
import net.minecraft.util.math.Vec2f
import net.minecraft.util.math.Vec3d
import net.minecraft.client.render.*
import net.minecraft.util.math.*
import kotlin.math.abs
import kotlin.math.atan2
import kotlin.math.ceil
Expand All @@ -32,36 +24,6 @@ object RenderUtils {
return lines.toList()
}

// this rendering function draws some points similar to a sentinel: it will always be the same width regardless of distance
fun sentinelLike(matrices: MatrixStack, points: List<Vec3d>, width: Float, colorizer: FrozenColorizer) {
if (points.size <= 1) return
val tessellator = Tessellator.getInstance()
val buf = tessellator.buffer
RenderSystem.setShader(GameRenderer::getRenderTypeLinesShader)
RenderSystem.lineWidth(width)
val matrix = matrices.peek().positionMatrix
fun vertex(a: Vec3d, b: Vec3d) {
val aColor = colorizer.getColor(0f, a)
val bColor = colorizer.getColor(0f, b)
val normal = a.subtract(b).normalize()
buf.vertex(matrix, a.x.toFloat(), a.y.toFloat(), a.z.toFloat())
.color(aColor)
.normal(matrices.peek().normalMatrix, normal.x.toFloat(), normal.y.toFloat(), normal.z.toFloat())
.next()
buf.vertex(matrix, b.x.toFloat(), b.y.toFloat(), b.z.toFloat())
.color(bColor)
.normal(matrices.peek().normalMatrix, -normal.x.toFloat(), -normal.y.toFloat(), -normal.z.toFloat())
.next()
}
buf.begin(VertexFormat.DrawMode.LINES, VertexFormats.LINES)
for (i in 1 until points.size) {
val a = points[i - 1]
val b = points[i]
vertex(Vec3d(a.x, a.y, a.z), Vec3d(b.x, b.y, b.z))
}
tessellator.draw()
}

fun drawFigure(mat: Matrix4f, points: List<Vec2f>, width: Float, colorizer: FrozenColorizer, colorizerOffset: Vec3d) {
val pointCount = points.size
if (pointCount <= 1)
Expand Down

0 comments on commit 886c692

Please sign in to comment.