particle: cache textures

That speeds up texture retrieving
This commit is contained in:
Moritz Zwerger 2023-12-18 18:05:37 +01:00
parent 47ee3d58df
commit 6032ed0bdc
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
3 changed files with 13 additions and 8 deletions

View File

@ -22,6 +22,7 @@ import de.bixilon.minosoft.data.registries.registries.registry.codec.IdentifierC
import de.bixilon.minosoft.gui.rendering.particle.DefaultParticleFactory
import de.bixilon.minosoft.gui.rendering.particle.ParticleFactory
import de.bixilon.minosoft.gui.rendering.particle.types.Particle
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.Texture
import de.bixilon.minosoft.gui.rendering.textures.TextureUtil.texture
import de.bixilon.minosoft.util.KUtil.toResourceLocation
import de.bixilon.minosoft.util.nbt.tag.NBTUtil.listCast
@ -32,6 +33,7 @@ data class ParticleType(
val overrideLimiter: Boolean = false,
val factory: ParticleFactory<out Particle>? = null,
) : RegistryItem() {
var loadedTextures: Array<Texture> = emptyArray()
override fun toString(): String {
return identifier.toString()

View File

@ -14,6 +14,7 @@
package de.bixilon.minosoft.gui.rendering.particle
import de.bixilon.kotlinglm.vec3.Vec3
import de.bixilon.kutil.array.ArrayUtil.cast
import de.bixilon.kutil.latch.AbstractLatch
import de.bixilon.kutil.observer.DataObserver.Companion.observe
import de.bixilon.minosoft.data.registries.identified.Namespaces.minosoft
@ -29,6 +30,7 @@ import de.bixilon.minosoft.gui.rendering.system.base.RenderSystem
import de.bixilon.minosoft.gui.rendering.system.base.layer.OpaqueLayer
import de.bixilon.minosoft.gui.rendering.system.base.layer.TranslucentLayer
import de.bixilon.minosoft.gui.rendering.system.base.phases.SkipAll
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.Texture
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener.Companion.listen
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.packets.s2c.play.block.chunk.ChunkUtil.isInViewDistance
@ -87,8 +89,9 @@ class ParticleRenderer(
private fun loadTextures() {
for (particle in connection.registries.particleType) {
for (file in particle.textures) {
context.textures.static.create(file)
val loaded: Array<Texture> = arrayOfNulls<Texture?>(particle.textures.size).cast()
for ((index, texture) in particle.textures.withIndex()) {
loaded[index] = context.textures.static.create(texture)
}
}
}

View File

@ -21,7 +21,7 @@ import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.file.FileTe
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
abstract class SimpleTextureParticle(connection: PlayConnection, position: Vec3d, velocity: Vec3d, data: ParticleData? = null) : TextureParticle(connection, position, velocity, data) {
override var texture = this.data.type.textures.getOrNull(0)?.let { connection.rendering?.context?.textures?.static?.get(it) }
override var texture = this.data.type.loadedTextures.getOrNull(0)
var spriteDisabled = false
@ -35,17 +35,17 @@ abstract class SimpleTextureParticle(connection: PlayConnection, position: Vec3d
return
}
// calculate next texture
val nextTextureResourceLocation = data.type.textures[age / (maxAge / totalTextures + 1)]
if (texture?.nullCast<FileTexture>()?.file == nextTextureResourceLocation) {
val next = data.type.loadedTextures[age / (maxAge / totalTextures + 1)]
if (texture?.nullCast<FileTexture>()?.file == next) {
return
}
texture = connection.rendering?.context?.textures?.static?.get(nextTextureResourceLocation)
texture = next
}
fun setRandomSprite() {
val textures = data.type.textures
val textures = data.type.loadedTextures
if (textures.isEmpty()) return
texture = connection.rendering?.context?.textures?.static?.get(textures.random())
texture = textures.random()
}
override fun tick() {