improve hitbox performance

This commit is contained in:
Bixilon 2023-05-31 21:55:45 +02:00
parent 11f207fc54
commit e328f36c48
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 23 additions and 22 deletions

View File

@ -174,12 +174,8 @@ abstract class Entity(
val ticksFrozen: Int
get() = data.get(TICKS_FROZEN_DATA, 0)
open val hitboxColor: RGBColor?
get() = when {
isInvisible -> ChatColors.GREEN
else -> ChatColors.WHITE
}
get() = ChatColors.WHITE
fun forceTick(time: Long = millis()) {

View File

@ -25,6 +25,7 @@ import de.bixilon.minosoft.data.entities.entities.properties.StatusEffectPropert
import de.bixilon.minosoft.data.registries.effects.attributes.EntityAttributes
import de.bixilon.minosoft.data.registries.effects.attributes.MinecraftAttributes
import de.bixilon.minosoft.data.registries.entities.EntityType
import de.bixilon.minosoft.data.text.formatting.color.ChatColors
import de.bixilon.minosoft.data.text.formatting.color.RGBColor
import de.bixilon.minosoft.data.text.formatting.color.RGBColor.Companion.asRGBColor
import de.bixilon.minosoft.gui.rendering.particle.types.render.texture.simple.spell.AmbientEntityEffectParticle
@ -90,6 +91,13 @@ abstract class LivingEntity(connection: PlayConnection, entityType: EntityType,
open val isSleeping: Boolean
get() = bedPosition != null
override val hitboxColor: RGBColor?
get() = when {
isInvisible -> ChatColors.GREEN
else -> super.hitboxColor
}
override fun createPhysics(): LivingEntityPhysics<*> {
return LivingEntityPhysics(this)
}

View File

@ -120,7 +120,7 @@ class AABB {
}
fun grow(size: Float): AABB {
return AABB(min - size, max + size)
return grow(size.toDouble())
}
private fun Double.isIn(min: Double, max: Double): Boolean {

View File

@ -72,9 +72,10 @@ class EntityHitbox(
return
}
val lazy = model.renderer.profile.hitbox.lazy
val shrunk = aabb.shrink(0.01f)
val mesh = LineMesh(model.context)
if (model.renderer.profile.hitbox.lazy) {
val mesh = LineMesh(model.context, if (lazy) 1488 else 2496)
if (lazy) {
mesh.drawLazyAABB(shrunk, color = data.color)
} else {
mesh.drawAABB(aabb = shrunk, color = data.color, margin = 0.1f)

View File

@ -20,19 +20,15 @@ import de.bixilon.minosoft.gui.rendering.RenderContext
import de.bixilon.minosoft.gui.rendering.system.base.MeshUtil.buffer
import de.bixilon.minosoft.gui.rendering.system.base.buffer.vertex.PrimitiveTypes
open class GenericColorMesh(context: RenderContext, primitiveType: PrimitiveTypes = context.renderSystem.preferredPrimitiveType) : Mesh(context, GenericColorMeshStruct, primitiveType) {
open class GenericColorMesh(context: RenderContext, primitiveType: PrimitiveTypes = context.renderSystem.preferredPrimitiveType, initialCacheSize: Int = 1000) : Mesh(context, GenericColorMeshStruct, primitiveType, initialCacheSize) {
fun addVertex(position: Vec3, color: RGBColor?) {
data.add(position.x)
data.add(position.y)
data.add(position.z)
data.add(position.array)
data.add((color ?: ChatColors.WHITE).rgba.buffer())
}
fun addVertex(position: Vec3, color: Float) {
data.add(position.x)
data.add(position.y)
data.add(position.z)
data.add(position.array)
data.add(color)
}

View File

@ -24,19 +24,19 @@ import de.bixilon.minosoft.gui.rendering.RenderContext
import de.bixilon.minosoft.gui.rendering.system.base.MeshUtil.buffer
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3Util.EMPTY
open class LineMesh(context: RenderContext) : GenericColorMesh(context) {
open class LineMesh(context: RenderContext, initialCacheSize: Int = 1000) : GenericColorMesh(context, initialCacheSize = initialCacheSize) {
fun drawLine(start: Vec3, end: Vec3, lineWidth: Float = RenderConstants.DEFAULT_LINE_WIDTH, color: RGBColor) {
data.ensureSize(4 * order.size * GenericColorMeshStruct.FLOATS_PER_VERTEX)
val direction = (end - start).normalize()
val direction = (end - start).normalizeAssign()
val normal1 = Vec3(direction.z, direction.z, direction.x - direction.y)
if (normal1 == Vec3.EMPTY) {
normal1.x = normal1.z
normal1.z = direction.z
}
normal1.normalizeAssign()
val normal2 = (direction cross normal1).normalize()
val normal2 = (direction cross normal1).normalizeAssign()
val halfLineWidth = lineWidth / 2
val directionWidth = direction * halfLineWidth
@ -64,10 +64,10 @@ open class LineMesh(context: RenderContext) : GenericColorMesh(context) {
private fun drawLineQuad(start: Vec3, end: Vec3, normal1: Vec3, normal2: Vec3, directionWidth: Vec3, color: Float) {
val positions = arrayOf(
start + normal2 - directionWidth,
start + normal1 - directionWidth,
end + normal1 + directionWidth,
end + normal2 + directionWidth,
Vec3(start.x + normal2.x - directionWidth.x, start.y + normal2.y - directionWidth.y, start.z + normal2.z - directionWidth.z),
Vec3(start.x + normal1.x - directionWidth.x, start.y + normal1.y - directionWidth.y, start.z + normal1.z - directionWidth.z),
Vec3(end.x + normal1.x + directionWidth.x, end.y + normal1.y + directionWidth.y, end.z + normal1.z + directionWidth.z),
Vec3(end.x + normal2.x + directionWidth.x, end.y + normal2.y + directionWidth.y, end.z + normal2.z + directionWidth.z),
)
for ((positionIndex, _) in order) {
addVertex(positions[positionIndex], color)