improve world border mesh code

This commit is contained in:
Bixilon 2023-05-25 19:30:24 +02:00
parent 17374f7756
commit 802dc48a07
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 41 additions and 45 deletions

View File

@ -32,69 +32,65 @@ class WorldBorderMesh(
val radius: Double, val radius: Double,
) : Mesh(context, WorldBorderMeshStruct, initialCacheSize = 6 * 2 * 3 * WorldBorderMeshStruct.FLOATS_PER_VERTEX) { ) : Mesh(context, WorldBorderMeshStruct, initialCacheSize = 6 * 2 * 3 * WorldBorderMeshStruct.FLOATS_PER_VERTEX) {
private fun x() {
val width = minOf(radius.toFloat(), World.MAX_RENDER_DISTANCE.toFloat() * ProtocolDefinition.SECTION_WIDTH_X)
val left = (center.y - width).toFloat()
val right = (center.y + width).toFloat()
val positions = arrayOf( private fun width(): Float {
return minOf(radius.toFloat(), World.MAX_RENDER_DISTANCE.toFloat() * ProtocolDefinition.SECTION_WIDTH_X)
}
private fun positions(width: Float, center: Double): Array<Vec2> {
val left = (center - width).toFloat()
val right = (center + width).toFloat()
return arrayOf(
Vec2(left, -1.0f), Vec2(left, -1.0f),
Vec2(left, +1.0f), Vec2(left, +1.0f),
Vec2(right, +1.0f), Vec2(right, +1.0f),
Vec2(right, -1.0f), Vec2(right, -1.0f),
) )
}
val x = (maxOf(-WorldBorder.MAX_RADIUS, center.x - radius) - offset.x).toFloat() private fun textureIndex(index: Int): Int {
for ((position, texture) in order) { return when (index) {
val (z, y) = positions[position] 1 -> 2
addVertex(x, y, z, texture, width) 2 -> 1
} 3 -> 0
else -> 3
val x1 = (minOf(WorldBorder.MAX_RADIUS, center.x + radius) - offset.x).toFloat()
for ((position, texture) in order) {
val (z, y) = positions[position]
addVertex(x1, y, z, when (texture) {
1 -> 2
2 -> 1
3 -> 0
else -> 3
}, width)
} }
} }
private fun z() { private fun addVertexX(x: Float, width: Float, positions: Array<Vec2>, index: Boolean) {
val width = minOf(radius.toFloat(), World.MAX_RENDER_DISTANCE.toFloat() * ProtocolDefinition.SECTION_WIDTH_X) for ((position, texture) in order) {
val left = (center.x - width).toFloat() val (z, y) = positions[position]
val right = (center.x + width).toFloat() val texture = if (index) textureIndex(texture) else texture
addVertex(x, y, z, textureIndex(texture), width)
}
}
val positions = arrayOf( private fun x(width: Float) {
Vec2(left, -1.0f), val positions = positions(width, center.y)
Vec2(left, +1.0f), addVertexX((maxOf(-WorldBorder.MAX_RADIUS, center.x - radius) - offset.x).toFloat(), width, positions, false)
Vec2(right, +1.0f), addVertexX((minOf(WorldBorder.MAX_RADIUS, center.x + radius) - offset.x).toFloat(), width, positions, true)
Vec2(right, -1.0f), }
)
val z = (maxOf(-WorldBorder.MAX_RADIUS, center.y - radius) - offset.z).toFloat() private fun addVertexZ(z: Float, width: Float, positions: Array<Vec2>, index: Boolean) {
for ((position, texture) in order) { for ((position, texture) in order) {
val (x, y) = positions[position] val (x, y) = positions[position]
addVertex(x, y, z, when (texture) { val texture = if (index) textureIndex(texture) else texture
1 -> 2 addVertex(x, y, z, textureIndex(texture), width)
2 -> 1
3 -> 0
else -> 3
}, width)
} }
}
val z1 = (minOf(WorldBorder.MAX_RADIUS, center.y + radius) - offset.z).toFloat() private fun z(width: Float) {
for ((position, texture) in order) { val positions = positions(width, center.x)
val (x, y) = positions[position]
addVertex(x, y, z1, texture, width) addVertexZ((maxOf(-WorldBorder.MAX_RADIUS, center.y - radius) - offset.z).toFloat(), width, positions, true)
} addVertexZ((minOf(WorldBorder.MAX_RADIUS, center.y + radius) - offset.z).toFloat(), width, positions, false)
} }
fun build() { fun build() {
x() val width = width()
z() x(width)
z(width)
} }
private fun addVertex(x: Float, y: Float, z: Float, uvIndex: Int, width: Float) { private fun addVertex(x: Float, y: Float, z: Float, uvIndex: Int, width: Float) {

View File

@ -103,7 +103,7 @@ class WorldBorderRenderer(
val radius = getRadius() val radius = getRadius()
val previous = this.borderMesh val previous = this.borderMesh
// if (previous != null && !reload && center == previous.center && radius == previous.radius) return if (previous != null && !reload && center == previous.center && radius == previous.radius) return
context.queue += { previous?.unload() } context.queue += { previous?.unload() }