improve item rendering: durability, enchanted

This commit is contained in:
Bixilon 2022-02-26 18:40:19 +01:00
parent 5bc3c5f4e3
commit 173b28b6e6
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
5 changed files with 43 additions and 5 deletions

View File

@ -30,6 +30,7 @@ import de.bixilon.minosoft.gui.rendering.gui.input.mouse.MouseActions
import de.bixilon.minosoft.gui.rendering.gui.input.mouse.MouseButtons
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions.Companion.copy
import de.bixilon.minosoft.gui.rendering.system.window.CursorShapes
import de.bixilon.minosoft.gui.rendering.system.window.KeyChangeTypes
import glm_.vec2.Vec2i
@ -57,7 +58,7 @@ class ItemElement(
override fun forceRender(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?) {
var options = options
if (hovered) {
options = (options?.copy(alpha = options.alpha * 0.7f)) ?: GUIVertexOptions(null, 0.7f)
options = options.copy(alpha = 0.7f)
}
raw.render(offset, consumer, options)
}

View File

@ -93,7 +93,7 @@ class RawItemElement(
(element ?: ColorElement(guiRenderer, textureSize, color)).render(offset, consumer, options)
} else {
model.render2d(offset, consumer, options, textureSize, stack)
model.render2d(guiRenderer, offset, consumer, options, textureSize, stack)
}
val countSize = countText.size

View File

@ -23,6 +23,7 @@ import de.bixilon.minosoft.gui.rendering.gui.elements.HorizontalAlignments
import de.bixilon.minosoft.gui.rendering.gui.elements.Pollable
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions.Companion.copy
import de.bixilon.minosoft.gui.rendering.util.vec.vec2.Vec2iUtil.EMPTY
import glm_.vec2.Vec2i

View File

@ -15,7 +15,17 @@ package de.bixilon.minosoft.gui.rendering.gui.mesh
import de.bixilon.minosoft.data.text.RGBColor
data class GUIVertexOptions(
class GUIVertexOptions(
val tintColor: RGBColor? = null,
val alpha: Float = 1.0f,
)
) {
companion object {
fun GUIVertexOptions?.copy(tintColor: RGBColor? = null, alpha: Float = 1.0f): GUIVertexOptions {
var outColor = this?.tintColor
if (tintColor != null) {
outColor = outColor?.mix(tintColor) ?: tintColor
}
return GUIVertexOptions(outColor, alpha = (this?.alpha ?: 1.0f) * alpha)
}
}
}

View File

@ -15,8 +15,11 @@ package de.bixilon.minosoft.gui.rendering.models.baked.item
import de.bixilon.minosoft.data.container.stack.ItemStack
import de.bixilon.minosoft.data.text.ChatColors
import de.bixilon.minosoft.data.text.RGBColor
import de.bixilon.minosoft.gui.rendering.gui.GUIRenderer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexConsumer
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions
import de.bixilon.minosoft.gui.rendering.gui.mesh.GUIVertexOptions.Companion.copy
import de.bixilon.minosoft.gui.rendering.models.baked.BakedModel
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture
import glm_.vec2.Vec2i
@ -26,8 +29,31 @@ class BakedItemModel(
val texture: AbstractTexture?,
) : BakedModel {
fun render2d(offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?, size: Vec2i, stack: ItemStack) {
private fun renderDurability(guiRenderer: GUIRenderer, offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?, size: Vec2i, stack: ItemStack) {
val durability = stack._durability?._durability
val maxDurability = stack.item.item.maxDurability
if (durability == null || durability < 0 || durability == stack.item.item.maxDurability) {
return
}
val percent = (durability / maxDurability.toFloat())
val width = size.x
val fillWidth = width * percent
consumer.addQuad(offset + Vec2i(2, size.y - 3), offset + Vec2i(size.x, size.y - 1), guiRenderer.renderWindow.WHITE_TEXTURE, tint = ChatColors.BLACK, options = options)
val color = RGBColor(1.0f - percent, percent, 0.0f) // ToDo: Color transition, something like https://gist.github.com/mlocati/7210513
consumer.addQuad(offset + Vec2i(1, size.y - 3), offset + Vec2i(fillWidth - 1, size.y - 2), guiRenderer.renderWindow.WHITE_TEXTURE, tint = color, options = options)
}
fun render2d(guiRenderer: GUIRenderer, offset: Vec2i, consumer: GUIVertexConsumer, options: GUIVertexOptions?, size: Vec2i, stack: ItemStack) {
val texture = texture ?: return
consumer.addQuad(offset, offset + size, texture, tint = ChatColors.WHITE, options = options)
renderDurability(guiRenderer, offset, consumer, options, size, stack)
if (stack._enchanting?.enchantments?.isNotEmpty() == true) {
consumer.addQuad(offset, offset + size, guiRenderer.renderWindow.WHITE_TEXTURE, tint = ChatColors.BLUE, options = options.copy(alpha = 0.5f))
}
}
}