mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-12 08:58:02 -04:00
cleanup some Mesh code
This commit is contained in:
parent
5a816d1403
commit
206fcef5e1
@ -25,17 +25,11 @@ open class DummyGUIVertexConsumer : GUIVertexConsumer {
|
|||||||
override val order: RenderOrder get() = MeshOrder.QUAD
|
override val order: RenderOrder get() = MeshOrder.QUAD
|
||||||
var char = 0
|
var char = 0
|
||||||
|
|
||||||
override fun addCache(cache: GUIMeshCache) {
|
override fun addCache(cache: GUIMeshCache) = Broken()
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun ensureSize(size: Int) {
|
override fun ensureSize(size: Int) = Broken()
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addVertex(x: Float, y: Float, texture: ShaderTexture?, u: Float, v: Float, tint: RGBColor, options: GUIVertexOptions?) {
|
override fun addVertex(x: Float, y: Float, texture: ShaderTexture?, u: Float, v: Float, tint: RGBColor, options: GUIVertexOptions?) = Broken()
|
||||||
TODO("Not yet implemented")
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun addVertex(x: Float, y: Float, textureId: Float, u: Float, v: Float, tint: Int, options: GUIVertexOptions?) = Broken()
|
override fun addVertex(x: Float, y: Float, textureId: Float, u: Float, v: Float, tint: Int, options: GUIVertexOptions?) = Broken()
|
||||||
|
|
||||||
@ -43,7 +37,5 @@ open class DummyGUIVertexConsumer : GUIVertexConsumer {
|
|||||||
addChar(start, end, this.char++)
|
addChar(start, end, this.char++)
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun addChar(start: Vec2, end: Vec2, index: Int) {
|
open fun addChar(start: Vec2, end: Vec2, index: Int): Unit = Broken()
|
||||||
TODO("Abstract")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,7 @@ package de.bixilon.minosoft.gui.rendering.models.baked
|
|||||||
|
|
||||||
import de.bixilon.kutil.collections.primitive.floats.HeapArrayFloatList
|
import de.bixilon.kutil.collections.primitive.floats.HeapArrayFloatList
|
||||||
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
|
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
|
||||||
|
import de.bixilon.kutil.reflection.ReflectionUtil.getFieldOrNull
|
||||||
import de.bixilon.minosoft.gui.rendering.chunk.mesh.ChunkMesh
|
import de.bixilon.minosoft.gui.rendering.chunk.mesh.ChunkMesh
|
||||||
import de.bixilon.minosoft.gui.rendering.chunk.mesh.ChunkMeshes
|
import de.bixilon.minosoft.gui.rendering.chunk.mesh.ChunkMeshes
|
||||||
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakedFace
|
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.BakedFace
|
||||||
@ -39,10 +40,10 @@ class BakedFaceTest {
|
|||||||
|
|
||||||
private fun singleMesh(): ChunkMesh {
|
private fun singleMesh(): ChunkMesh {
|
||||||
val mesh = OBJENESIS.newInstance(ChunkMesh::class.java)
|
val mesh = OBJENESIS.newInstance(ChunkMesh::class.java)
|
||||||
mesh::quadType.forceSet(PrimitiveTypes.QUAD)
|
mesh::primitive.forceSet(PrimitiveTypes.QUAD)
|
||||||
mesh::order.forceSet(MeshOrder.QUAD)
|
mesh::order.forceSet(MeshOrder.QUAD)
|
||||||
|
|
||||||
mesh.data = HeapArrayFloatList(1000)
|
mesh::class.java.getFieldOrNull("_data")!!.forceSet(mesh, HeapArrayFloatList(1000))
|
||||||
|
|
||||||
mesh::initialCacheSize.forceSet(1000)
|
mesh::initialCacheSize.forceSet(1000)
|
||||||
|
|
||||||
|
@ -117,7 +117,7 @@ class WorldBorderRenderer(
|
|||||||
val mesh = this.borderMesh ?: return
|
val mesh = this.borderMesh ?: return
|
||||||
update()
|
update()
|
||||||
|
|
||||||
if (mesh.state == Mesh.MeshStates.PREPARING) {
|
if (mesh.state == Mesh.MeshStates.WAITING) {
|
||||||
mesh.load()
|
mesh.load()
|
||||||
}
|
}
|
||||||
mesh.draw()
|
mesh.draw()
|
||||||
|
@ -21,7 +21,7 @@ import de.bixilon.minosoft.gui.rendering.system.base.texture.shader.ShaderTextur
|
|||||||
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
|
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
|
||||||
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
||||||
|
|
||||||
class ChunkMesh(context: RenderContext, initialCacheSize: Int, onDemand: Boolean = false) : Mesh(context, ChunkMeshStruct, initialCacheSize = initialCacheSize, onDemand = onDemand), BlockVertexConsumer, Comparable<ChunkMesh> {
|
class ChunkMesh(context: RenderContext, initialCacheSize: Int) : Mesh(context, ChunkMeshStruct, initialCacheSize = initialCacheSize), BlockVertexConsumer, Comparable<ChunkMesh> {
|
||||||
var distance: Float = 0.0f // Used for sorting
|
var distance: Float = 0.0f // Used for sorting
|
||||||
|
|
||||||
override val order = context.system.quadOrder
|
override val order = context.system.quadOrder
|
||||||
|
@ -33,8 +33,8 @@ class ChunkMeshes(
|
|||||||
) : BlockVertexConsumer {
|
) : BlockVertexConsumer {
|
||||||
val center: Vec3 = Vec3(Vec3i.of(chunkPosition, sectionHeight, Vec3i(8, 8, 8)))
|
val center: Vec3 = Vec3(Vec3i.of(chunkPosition, sectionHeight, Vec3i(8, 8, 8)))
|
||||||
var opaqueMesh: ChunkMesh? = ChunkMesh(context, if (smallMesh) 3000 else 100000)
|
var opaqueMesh: ChunkMesh? = ChunkMesh(context, if (smallMesh) 3000 else 100000)
|
||||||
var translucentMesh: ChunkMesh? = ChunkMesh(context, if (smallMesh) 3000 else 10000, onDemand = true)
|
var translucentMesh: ChunkMesh? = ChunkMesh(context, if (smallMesh) 3000 else 10000)
|
||||||
var textMesh: ChunkMesh? = ChunkMesh(context, if (smallMesh) 5000 else 50000, onDemand = true)
|
var textMesh: ChunkMesh? = ChunkMesh(context, if (smallMesh) 5000 else 50000)
|
||||||
var blockEntities: ArrayList<BlockEntityRenderer<*>>? = null
|
var blockEntities: ArrayList<BlockEntityRenderer<*>>? = null
|
||||||
|
|
||||||
// used for frustum culling
|
// used for frustum culling
|
||||||
@ -42,9 +42,9 @@ class ChunkMeshes(
|
|||||||
val maxPosition = Vec3i(0)
|
val maxPosition = Vec3i(0)
|
||||||
|
|
||||||
fun finish() {
|
fun finish() {
|
||||||
this.opaqueMesh?.finish()
|
this.opaqueMesh?.preload()
|
||||||
this.translucentMesh?.finish()
|
this.translucentMesh?.preload()
|
||||||
this.textMesh?.finish()
|
this.textMesh?.preload()
|
||||||
}
|
}
|
||||||
|
|
||||||
@Synchronized
|
@Synchronized
|
||||||
|
@ -91,7 +91,7 @@ open class GUIMeshElement<T : Element>(
|
|||||||
if (this._mesh != null) throw MemoryLeakException("Mesh to unload is already set!")
|
if (this._mesh != null) throw MemoryLeakException("Mesh to unload is already set!")
|
||||||
this._mesh = this.mesh
|
this._mesh = this.mesh
|
||||||
this.mesh = GUIMesh(context, guiRenderer.halfSize, mesh.data)
|
this.mesh = GUIMesh(context, guiRenderer.halfSize, mesh.data)
|
||||||
this.mesh.finish()
|
this.mesh.preload()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun prepare() = Unit
|
fun prepare() = Unit
|
||||||
@ -112,7 +112,7 @@ open class GUIMeshElement<T : Element>(
|
|||||||
}
|
}
|
||||||
this._mesh = null
|
this._mesh = null
|
||||||
|
|
||||||
if (this.mesh.state == Mesh.MeshStates.FINISHED) {
|
if (this.mesh.state == Mesh.MeshStates.PRELOADED) {
|
||||||
mesh.load()
|
mesh.load()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,10 +27,12 @@ class GUIMesh(
|
|||||||
context: RenderContext,
|
context: RenderContext,
|
||||||
val halfSize: Vec2,
|
val halfSize: Vec2,
|
||||||
data: AbstractFloatList,
|
data: AbstractFloatList,
|
||||||
) : Mesh(context, GUIMeshStruct, initialCacheSize = 40000, clearOnLoad = false, data = data), GUIVertexConsumer {
|
) : Mesh(context, GUIMeshStruct, initialCacheSize = 40000, data = data), GUIVertexConsumer {
|
||||||
private val whiteTexture = context.textures.whiteTexture
|
private val whiteTexture = context.textures.whiteTexture
|
||||||
override val order = context.system.quadOrder
|
override val order = context.system.quadOrder
|
||||||
|
|
||||||
|
override fun clear() = Unit
|
||||||
|
|
||||||
|
|
||||||
override fun addVertex(x: Float, y: Float, texture: ShaderTexture?, u: Float, v: Float, tint: RGBColor, options: GUIVertexOptions?) {
|
override fun addVertex(x: Float, y: Float, texture: ShaderTexture?, u: Float, v: Float, tint: RGBColor, options: GUIVertexOptions?) {
|
||||||
addVertex(data, halfSize, x, y, texture ?: whiteTexture.texture, u, v, tint, options)
|
addVertex(data, halfSize, x, y, texture ?: whiteTexture.texture, u, v, tint, options)
|
||||||
|
@ -25,7 +25,9 @@ import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.Texture
|
|||||||
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
|
import de.bixilon.minosoft.gui.rendering.util.mesh.Mesh
|
||||||
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
import de.bixilon.minosoft.gui.rendering.util.mesh.MeshStruct
|
||||||
|
|
||||||
class ParticleMesh(context: RenderContext, data: AbstractFloatList) : Mesh(context, ParticleMeshStruct, PrimitiveTypes.POINT, -1, clearOnLoad = false, data = data) {
|
class ParticleMesh(context: RenderContext, data: AbstractFloatList) : Mesh(context, ParticleMeshStruct, PrimitiveTypes.POINT, -1, data = data) {
|
||||||
|
|
||||||
|
override fun clear() = Unit
|
||||||
|
|
||||||
fun addVertex(position: Vec3d, scale: Float, texture: Texture, tintColor: RGBColor, uvMin: FloatArray? = null, uvMax: FloatArray? = null, light: Int) {
|
fun addVertex(position: Vec3d, scale: Float, texture: Texture, tintColor: RGBColor, uvMin: FloatArray? = null, uvMax: FloatArray? = null, light: Int) {
|
||||||
val minTransformedUV = if (uvMin == null) {
|
val minTransformedUV = if (uvMin == null) {
|
||||||
|
@ -28,68 +28,66 @@ import de.bixilon.minosoft.util.collections.floats.FloatListUtil
|
|||||||
abstract class Mesh(
|
abstract class Mesh(
|
||||||
val context: RenderContext,
|
val context: RenderContext,
|
||||||
private val struct: MeshStruct,
|
private val struct: MeshStruct,
|
||||||
val quadType: PrimitiveTypes = context.system.quadType,
|
val primitive: PrimitiveTypes = context.system.quadType,
|
||||||
var initialCacheSize: Int = 10000,
|
var initialCacheSize: Int = 10000,
|
||||||
val clearOnLoad: Boolean = true,
|
|
||||||
data: AbstractFloatList? = null,
|
data: AbstractFloatList? = null,
|
||||||
val onDemand: Boolean = false,
|
|
||||||
) : AbstractVertexConsumer {
|
) : AbstractVertexConsumer {
|
||||||
override val order = context.system.legacyQuadOrder
|
override val order = context.system.legacyQuadOrder
|
||||||
private var _data: AbstractFloatList? = data ?: if (onDemand) null else FloatListUtil.direct(initialCacheSize)
|
private var _data = data
|
||||||
var data: AbstractFloatList
|
val data: AbstractFloatList
|
||||||
get() {
|
get() {
|
||||||
if (_data == null && onDemand) {
|
if (_data == null) {
|
||||||
_data = FloatListUtil.direct(initialCacheSize)
|
_data = FloatListUtil.direct(initialCacheSize)
|
||||||
}
|
}
|
||||||
return _data.unsafeCast()
|
return _data.unsafeCast()
|
||||||
}
|
}
|
||||||
set(value) {
|
|
||||||
_data = value
|
|
||||||
}
|
|
||||||
|
|
||||||
protected lateinit var buffer: FloatVertexBuffer
|
protected lateinit var buffer: FloatVertexBuffer
|
||||||
|
|
||||||
var vertices: Int = -1
|
var vertices: Int = -1
|
||||||
protected set
|
protected set
|
||||||
|
|
||||||
var state = MeshStates.PREPARING
|
var state = MeshStates.WAITING
|
||||||
protected set
|
protected set
|
||||||
|
|
||||||
|
|
||||||
fun finish() {
|
fun preload() {
|
||||||
if (state != MeshStates.PREPARING) throw IllegalStateException("Mesh is not preparing: $state")
|
if (state != MeshStates.WAITING) throw InvalidMeshState(state)
|
||||||
val data = this.data
|
val data = this.data
|
||||||
buffer = context.system.createVertexBuffer(struct, data, quadType)
|
buffer = context.system.createVertexBuffer(struct, data, primitive)
|
||||||
state = MeshStates.FINISHED
|
state = MeshStates.PRELOADED
|
||||||
}
|
}
|
||||||
|
|
||||||
fun load() {
|
fun load() {
|
||||||
if (state == MeshStates.PREPARING) {
|
if (state == MeshStates.WAITING) {
|
||||||
finish()
|
preload()
|
||||||
}
|
}
|
||||||
if (state != MeshStates.FINISHED) throw IllegalStateException("Mesh is not finished: $state")
|
if (state != MeshStates.PRELOADED) throw InvalidMeshState(state)
|
||||||
buffer.init()
|
buffer.init()
|
||||||
if (clearOnLoad) {
|
clear()
|
||||||
|
|
||||||
|
vertices = buffer.vertices
|
||||||
|
state = MeshStates.LOADED
|
||||||
|
}
|
||||||
|
|
||||||
|
protected open fun clear() {
|
||||||
val data = data
|
val data = data
|
||||||
if (data is DirectArrayFloatList) {
|
if (data is DirectArrayFloatList) {
|
||||||
data.unload()
|
data.unload()
|
||||||
}
|
}
|
||||||
_data = null
|
_data = null
|
||||||
}
|
}
|
||||||
vertices = buffer.vertices
|
|
||||||
state = MeshStates.LOADED
|
|
||||||
}
|
|
||||||
|
|
||||||
fun draw() {
|
fun draw() {
|
||||||
check(state == MeshStates.LOADED) { "Can not draw $state mesh!" }
|
if (state != MeshStates.LOADED) throw InvalidMeshState(state)
|
||||||
buffer.draw()
|
buffer.draw()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun unload() {
|
fun unload() {
|
||||||
when (state) {
|
when (state) {
|
||||||
MeshStates.LOADED -> buffer.unload()
|
MeshStates.LOADED -> buffer.unload()
|
||||||
MeshStates.PREPARING, MeshStates.FINISHED -> _data?.nullCast<DirectArrayFloatList>()?.unload()
|
MeshStates.WAITING, MeshStates.PRELOADED -> _data?.nullCast<DirectArrayFloatList>()?.unload()
|
||||||
else -> throw IllegalStateException("Mesh is already unloaded")
|
MeshStates.UNLOADED -> throw InvalidMeshState(state)
|
||||||
}
|
}
|
||||||
state = MeshStates.UNLOADED
|
state = MeshStates.UNLOADED
|
||||||
}
|
}
|
||||||
@ -140,9 +138,12 @@ abstract class Mesh(
|
|||||||
}
|
}
|
||||||
|
|
||||||
enum class MeshStates {
|
enum class MeshStates {
|
||||||
PREPARING,
|
WAITING,
|
||||||
FINISHED,
|
PRELOADED,
|
||||||
LOADED,
|
LOADED,
|
||||||
UNLOADED,
|
UNLOADED,
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class InvalidMeshState(state: MeshStates) : Exception("Invalid mesh state: $state")
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user