improve transparency: post drawing

This commit is contained in:
Bixilon 2021-06-19 22:29:30 +02:00
parent 5a991b45b8
commit 7ff391f703
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 53 additions and 26 deletions

View File

@ -333,9 +333,15 @@ class RenderWindow(
textures.animator.draw() textures.animator.draw()
for (renderer in rendererMap.values) {
renderer.update()
}
for (renderer in rendererMap.values) { for (renderer in rendererMap.values) {
renderer.draw() renderer.draw()
} }
for (renderer in rendererMap.values) {
renderer.postDraw()
}
renderStats.endDraw() renderStats.endDraw()

View File

@ -14,7 +14,14 @@
package de.bixilon.minosoft.gui.rendering package de.bixilon.minosoft.gui.rendering
interface Renderer { interface Renderer {
fun init() {} fun init() {}
fun postInit() {} fun postInit() {}
fun update() {}
fun draw() {} fun draw() {}
fun postDraw() {}
} }

View File

@ -64,6 +64,7 @@ class WorldRenderer(
val allChunkSections: SynchronizedMap<Vec2i, SynchronizedMap<Int, ChunkMeshCollection>> = synchronizedMapOf() val allChunkSections: SynchronizedMap<Vec2i, SynchronizedMap<Int, ChunkMeshCollection>> = synchronizedMapOf()
val visibleChunks: SynchronizedMap<Vec2i, SynchronizedMap<Int, ChunkMeshCollection>> = synchronizedMapOf() val visibleChunks: SynchronizedMap<Vec2i, SynchronizedMap<Int, ChunkMeshCollection>> = synchronizedMapOf()
private var lastVisibleChunks: SynchronizedMap<Vec2i, SynchronizedMap<Int, ChunkMeshCollection>> = synchronizedMapOf()
val queuedChunks: MutableSet<Vec2i> = synchronizedSetOf() val queuedChunks: MutableSet<Vec2i> = synchronizedSetOf()
private val preparationTasks: SynchronizedMap<Vec2i, SynchronizedMap<Int, ThreadPoolRunnable>> = synchronizedMapOf() private val preparationTasks: SynchronizedMap<Vec2i, SynchronizedMap<Int, ThreadPoolRunnable>> = synchronizedMapOf()
@ -194,19 +195,26 @@ class WorldRenderer(
allBlocks = null allBlocks = null
} }
override fun update() {
lastVisibleChunks = visibleChunks.toSynchronizedMap()
}
override fun draw() { override fun draw() {
renderWindow.renderSystem.reset() renderWindow.renderSystem.reset()
chunkShader.use() chunkShader.use()
val visibleChunks = visibleChunks.toSynchronizedMap()
for (map in visibleChunks.values) { for (map in lastVisibleChunks.values) {
for (mesh in map.values) { for (mesh in map.values) {
mesh.opaqueSectionArrayMesh.draw() mesh.opaqueSectionArrayMesh.draw()
} }
} }
}
renderWindow.renderSystem.depthMask = false override fun postDraw() {
for (map in visibleChunks.values) { renderWindow.renderSystem.reset(depthMask = false)
chunkShader.use()
for (map in lastVisibleChunks.values) {
for (mesh in map.values) { for (mesh in map.values) {
mesh.transparentSectionArrayMesh?.draw() mesh.transparentSectionArrayMesh?.draw()
} }

View File

@ -150,7 +150,7 @@ class HUDRenderer(val connection: PlayConnection, val renderWindow: RenderWindow
} }
} }
override fun draw() { override fun postDraw() {
if (!RenderConstants.RENDER_HUD) { if (!RenderConstants.RENDER_HUD) {
return return
} }

View File

@ -135,12 +135,14 @@ class Camera(
private fun onPositionChange() { private fun onPositionChange() {
recalculateViewProjectionMatrix() recalculateViewProjectionMatrix()
// recalculate sky color for current biome
val skyRenderer = renderWindow[SkyRenderer.Companion] ?: return
skyRenderer.baseColor = connection.world.getBiome(entity.positionInfo.blockPosition)?.skyColor ?: RenderConstants.DEFAULT_SKY_COLOR
frustum.recalculate() frustum.recalculate()
connection.fireEvent(FrustumChangeEvent(renderWindow, frustum)) connection.fireEvent(FrustumChangeEvent(renderWindow, frustum))
connection.fireEvent(CameraPositionChangeEvent(renderWindow, entity.eyePosition))
// recalculate sky color for current biome
renderWindow[SkyRenderer.Companion]?.let { skyRenderer ->
skyRenderer.baseColor = connection.world.getBiome(entity.positionInfo.blockPosition)?.skyColor ?: RenderConstants.DEFAULT_SKY_COLOR
connection.world.dimension?.hasSkyLight?.let { connection.world.dimension?.hasSkyLight?.let {
if (it) { if (it) {
@ -149,7 +151,7 @@ class Camera(
skyRenderer.baseColor = RenderConstants.BLACK_COLOR skyRenderer.baseColor = RenderConstants.BLACK_COLOR
} }
} ?: let { skyRenderer.baseColor = RenderConstants.DEFAULT_SKY_COLOR } } ?: let { skyRenderer.baseColor = RenderConstants.DEFAULT_SKY_COLOR }
connection.fireEvent(CameraPositionChangeEvent(renderWindow, entity.eyePosition)) }
} }
private fun calculateProjectionMatrix(screenDimensions: Vec2): Mat4d { private fun calculateProjectionMatrix(screenDimensions: Vec2): Mat4d {

View File

@ -80,10 +80,7 @@ class ParticleRenderer(
add(particle) add(particle)
} }
override fun draw() { override fun update() {
renderWindow.renderSystem.reset()
particleShader.use()
particleMesh.unload() particleMesh.unload()
transparentParticleMesh.unload() transparentParticleMesh.unload()
particleMesh = ParticleMesh() particleMesh = ParticleMesh()
@ -101,10 +98,17 @@ class ParticleRenderer(
particleMesh.load() particleMesh.load()
transparentParticleMesh.load() transparentParticleMesh.load()
}
override fun draw() {
renderWindow.renderSystem.reset()
particleShader.use()
particleMesh.draw() particleMesh.draw()
}
renderWindow.renderSystem.depthMask = false override fun postDraw() {
renderWindow.renderSystem.reset(depthMask = false)
particleShader.use()
transparentParticleMesh.draw() transparentParticleMesh.draw()
} }

View File

@ -79,7 +79,7 @@ class SkyRenderer(
} }
private fun setSunMatrix(projectionViewMatrix: Mat4d) { private fun setSunMatrix(projectionViewMatrix: Mat4d) {
val timeAngle = ((getSkyAngle(connection.world.time).toDouble() * 360.0) + 180.0).rad val timeAngle = ((getSkyAngle(connection.world.time) * 360.0) + 180.0).rad
val rotatedMatrix = if (timeAngle == 0.0) { val rotatedMatrix = if (timeAngle == 0.0) {
projectionViewMatrix projectionViewMatrix
} else { } else {

View File

@ -24,10 +24,10 @@ interface RenderSystem {
depthTest: Boolean = true, depthTest: Boolean = true,
blending: Boolean = true, blending: Boolean = true,
faceCulling: Boolean = true, faceCulling: Boolean = true,
depthMask: Boolean = true,
sourceAlpha: BlendingFunctions = BlendingFunctions.SOURCE_ALPHA, sourceAlpha: BlendingFunctions = BlendingFunctions.SOURCE_ALPHA,
destinationAlpha: BlendingFunctions = BlendingFunctions.ONE_MINUS_SOURCE_ALPHA, destinationAlpha: BlendingFunctions = BlendingFunctions.ONE_MINUS_SOURCE_ALPHA,
depth: DepthFunctions = DepthFunctions.LESS, depth: DepthFunctions = DepthFunctions.LESS,
depthMask: Boolean = true,
) { ) {
this[RenderingCapabilities.DEPTH_TEST] = depthTest this[RenderingCapabilities.DEPTH_TEST] = depthTest
this[RenderingCapabilities.BLENDING] = blending this[RenderingCapabilities.BLENDING] = blending

View File

@ -81,8 +81,8 @@ object KUtil {
return ret return ret
} }
fun <K, V> Map<K, V>.toSynchronizedMap(): MutableMap<K, V> { fun <K, V> Map<K, V>.toSynchronizedMap(): SynchronizedMap<K, V> {
return synchronizedCopy { Collections.synchronizedMap(this.toMutableMap()) } return synchronizedCopy { SynchronizedMap(this.toMutableMap()) }
} }
fun <V> Collection<V>.toSynchronizedList(): MutableList<V> { fun <V> Collection<V>.toSynchronizedList(): MutableList<V> {