world-renderer: mesh: calculate max positions (for frustum culling)

This commit is contained in:
Bixilon 2021-11-10 22:42:47 +01:00
parent 8bd1c5ff32
commit 821a849e25
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
7 changed files with 46 additions and 15 deletions

View File

@ -90,7 +90,7 @@ class WorldRenderer(
mesh = sectionPreparer.prepare(section) mesh = sectionPreparer.prepare(section)
// for (i in 0 until 1000) for (i in 0 until 1000)
mesh = sectionPreparer.prepare(section) mesh = sectionPreparer.prepare(section)
/* /*

View File

@ -13,9 +13,7 @@
package de.bixilon.minosoft.gui.rendering.block.mesh package de.bixilon.minosoft.gui.rendering.block.mesh
import de.bixilon.minosoft.data.registries.AABB
import de.bixilon.minosoft.gui.rendering.RenderWindow import de.bixilon.minosoft.gui.rendering.RenderWindow
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.EMPTY
import glm_.vec3.Vec3i import glm_.vec3.Vec3i
class ChunkSectionMeshes( class ChunkSectionMeshes(
@ -28,11 +26,9 @@ class ChunkSectionMeshes(
var transparentMesh: ChunkSectionMesh? = ChunkSectionMesh(renderWindow) var transparentMesh: ChunkSectionMesh? = ChunkSectionMesh(renderWindow)
private set private set
val minPosition = Vec3i.EMPTY // used for frustum culling
val maxPosition = Vec3i.EMPTY val minPosition = Vec3i(16)
val maxPosition = Vec3i(0)
lateinit var aabb: AABB
private set
@Synchronized @Synchronized
fun load() { fun load() {
@ -58,4 +54,26 @@ class ChunkSectionMeshes(
mesh.load() mesh.load()
} }
} }
fun addBlock(x: Int, y: Int, z: Int) {
if (x < minPosition.x) {
minPosition.x = x
}
if (y < minPosition.y) {
minPosition.y = y
}
if (z < minPosition.z) {
minPosition.z = z
}
if (x > maxPosition.x) {
maxPosition.x = x
}
if (y > maxPosition.y) {
maxPosition.y = y
}
if (z > maxPosition.z) {
maxPosition.z = z
}
}
} }

View File

@ -25,6 +25,7 @@ class CullSectionPreparer(
for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) { for (y in 0 until ProtocolDefinition.SECTION_HEIGHT_Y) {
for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) { for (z in 0 until ProtocolDefinition.SECTION_WIDTH_Z) {
block = section.blocks[ChunkSection.getIndex(x, y, z)] block = section.blocks[ChunkSection.getIndex(x, y, z)]
val model = block?.model ?: continue
// ToDo: Chunk borders // ToDo: Chunk borders
neighbours[Directions.DOWN.ordinal] = if (y == 0) { neighbours[Directions.DOWN.ordinal] = if (y == 0) {
@ -59,7 +60,10 @@ class CullSectionPreparer(
} }
random.setSeed(VecUtil.generatePositionHash(x, y, z)) random.setSeed(VecUtil.generatePositionHash(x, y, z))
block?.model?.singleRender(Vec3i(x, y, z), mesh, random, neighbours, 0xFF, floatArrayOf(1.0f, 1.0f, 1.0f, 1.0f)) val rendered = model.singleRender(Vec3i(x, y, z), mesh, random, neighbours, 0xFF, floatArrayOf(1.0f, 1.0f, 1.0f, 1.0f))
if (rendered) {
mesh.addBlock(x, y, z)
}
} }
} }
} }

View File

@ -35,9 +35,13 @@ class MultipartBakedModel(
return 0xFF return 0xFF
} }
override fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray) { override fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray): Boolean {
var rendered = false
for (model in models) { for (model in models) {
model.singleRender(position, mesh, random, neighbours, light, ambientLight) if (model.singleRender(position, mesh, random, neighbours, light, ambientLight) && !rendered) {
rendered = true
}
} }
return rendered
} }
} }

View File

@ -60,8 +60,8 @@ class WeightedBakedModel(
return getModel(random).getLight(position, random, side, lightAccessor) return getModel(random).getLight(position, random, side, lightAccessor)
} }
override fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray) { override fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray): Boolean {
getModel(random).singleRender(position, mesh, random, neighbours, light, ambientLight) return getModel(random).singleRender(position, mesh, random, neighbours, light, ambientLight)
} }
} }

View File

@ -27,7 +27,7 @@ interface BakedBlockModel : BakedModel {
fun getSize(random: Random, direction: Directions): Array<FaceSize> fun getSize(random: Random, direction: Directions): Array<FaceSize>
// ToDo: Tint // ToDo: Tint
fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray) fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray): Boolean
// ToDo: Get ambient light // ToDo: Get ambient light
fun getLight(position: Vec3i, random: Random, side: Directions, lightAccessor: LightAccessor): Int fun getLight(position: Vec3i, random: Random, side: Directions, lightAccessor: LightAccessor): Int

View File

@ -35,8 +35,9 @@ class BakedBlockStateModel(
return sizes[direction.ordinal] return sizes[direction.ordinal]
} }
override fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray) { override fun singleRender(position: Vec3i, mesh: ChunkSectionMeshes, random: Random, neighbours: Array<BlockState?>, light: Int, ambientLight: FloatArray): Boolean {
val floatPosition = position.toVec3().array val floatPosition = position.toVec3().array
var rendered = false
for ((index, faces) in faces.withIndex()) { for ((index, faces) in faces.withIndex()) {
val direction = Directions.VALUES[index] val direction = Directions.VALUES[index]
val neighbour = neighbours[index]?.model val neighbour = neighbours[index]?.model
@ -50,8 +51,12 @@ class BakedBlockStateModel(
continue continue
} }
face.singleRender(floatPosition, mesh, light, ambientLight) face.singleRender(floatPosition, mesh, light, ambientLight)
if (!rendered) {
rendered = true
}
} }
} }
return rendered
} }
override fun greedyRender(start: Vec3i, end: Vec3i, side: Directions, mesh: ChunkSectionMesh, light: Int) { override fun greedyRender(start: Vec3i, end: Vec3i, side: Directions, mesh: ChunkSectionMesh, light: Int) {