Skip to content

Commit

Permalink
baked face: pre transform uv coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
Bixilon committed Oct 11, 2023
1 parent f61a1a8 commit e9bc701
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class BakedFaceTest {

val mesh = mesh()

face.render(floatArrayOf(0.0f, 0.0f, 0.0f), mesh, byteArrayOf(0, 0, 0, 0, 0, 0, 0), null, FloatArray(2))
face.render(floatArrayOf(0.0f, 0.0f, 0.0f), mesh, byteArrayOf(0, 0, 0, 0, 0, 0, 0), null)

val texture = 0.buffer()
val lightTint = 0xFFFFFF.buffer()
Expand All @@ -83,7 +83,7 @@ class BakedFaceTest {

val mesh = mesh()

face.render(floatArrayOf(0.0f, 0.0f, 0.0f), mesh, byteArrayOf(0, 0, 0, 0, 0, 0, 0), null, FloatArray(2))
face.render(floatArrayOf(0.0f, 0.0f, 0.0f), mesh, byteArrayOf(0, 0, 0, 0, 0, 0, 0), null)

val texture = 0.buffer()
val lightTint = 0xFFFFFF.buffer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class SignBlockEntityRenderer(
return rotation * 22.5f
}

override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?, temp: FloatArray): Boolean {
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?): Boolean {
val block = this.blockState.block
if (block is StandingSignBlock) {
renderStandingText(offset, mesh, light[SELF_LIGHT_INDEX].toInt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ class SingleChunkMesh(context: RenderContext, initialCacheSize: Int, onDemand: B
)
}

fun addVertex(x: Float, y: Float, z: Float, uv: FloatArray, texture: Texture, shaderTextureId: Float, lightTint: Float) {
fun addVertex(x: Float, y: Float, z: Float, u: Float, v: Float, shaderTextureId: Float, lightTint: Float) {
data.ensureSize(WorldMeshStruct.FLOATS_PER_VERTEX)
val transformedUV = texture.renderData.transformUV(uv)
data.add(x, y, z)
data.add(transformedUV)
data.add(shaderTextureId, lightTint)
data.add(
x, y, z,
u, v,
shaderTextureId, lightTint,
)
}

override fun compareTo(other: SingleChunkMesh): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class SolidSectionMesher(
val entities: MutableList<BlockEntityRenderer<*>> = ArrayList()

val position = BlockPosition()
val temp = FloatArray(2)
val neighbourBlocks: Array<BlockState?> = arrayOfNulls(Directions.SIZE)
val light = ByteArray(Directions.SIZE + 1) // last index (6) for the current block

Expand Down Expand Up @@ -148,10 +147,10 @@ class SolidSectionMesher(


val tints = tints.getAverageBlockTint(chunk, neighbourChunks, state, x, y, z)
var rendered = model.render(position, floatOffset, mesh, random, state, neighbourBlocks, light, tints, temp)
var rendered = model.render(position, floatOffset, mesh, random, state, neighbourBlocks, light, tints)

if (entity is BlockRender) {
rendered = entity.render(position, floatOffset, mesh, random, state, neighbourBlocks, light, tints, temp) || rendered
rendered = entity.render(position, floatOffset, mesh, random, state, neighbourBlocks, light, tints) || rendered
}

if (offset != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import de.bixilon.minosoft.gui.rendering.models.loader.legacy.ModelChooser
import java.util.*

class BlockModelPrototype(val model: DirectBlockModel) : BlockRender {
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?, temp: FloatArray) = prototype()
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?) = prototype()
override fun getParticleTexture(random: Random?, position: Vec3i) = prototype()
override fun getProperties(direction: Directions) = prototype()
override fun render(gui: GUIRenderer, offset: Vec2, consumer: GUIVertexConsumer, options: GUIVertexOptions?, size: Vec2, stack: ItemStack) = prototype()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,14 @@ data class SingleBlockStateApply(
uv = uv.pushRight(2, getTextureRotation(direction, rotatedX))
}

val vec = Vec2(0, uv)
for (ofs in 0 until 4) {
vec.ofs = ofs * 2
val transformed = texture.transformUV(vec)
uv[ofs * 2 + 0] = transformed.x
uv[ofs * 2 + 1] = transformed.y
}

val faceProperties = if (rotation == null && this@SingleBlockStateApply.rotation == null) positions.properties(rotatedXY, texture) else null
val bakedFace = BakedFace(positions, uv, this.shade, face.tintIndex, texture, rotatedXY, faceProperties)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import de.bixilon.minosoft.gui.rendering.models.block.element.FaceVertexData
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.Shades.Companion.shade
import de.bixilon.minosoft.gui.rendering.models.block.state.baked.cull.side.FaceProperties
import de.bixilon.minosoft.gui.rendering.system.base.MeshUtil.buffer
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureStates
import de.bixilon.minosoft.gui.rendering.system.base.texture.TextureTransparencies
import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.Texture
import de.bixilon.minosoft.gui.rendering.tint.TintUtil
Expand All @@ -36,6 +37,10 @@ class BakedFace(
) {
private val lightIndex = cull?.ordinal ?: SELF_LIGHT_INDEX

init {
check(texture.state == TextureStates.LOADED)
}


constructor(positions: FaceVertexData, uv: FaceVertexData, shade: Boolean, tintIndex: Int, texture: Texture, direction: Directions, properties: FaceProperties?) : this(positions, uv, if (shade) direction.shade else Shades.NONE, tintIndex, if (properties == null) null else direction, texture, properties)

Expand All @@ -44,28 +49,24 @@ class BakedFace(
return TintUtil.calculateTint(tint, shade)
}

fun render(offset: FloatArray, mesh: ChunkMesh, light: ByteArray, tints: IntArray?, temp: FloatArray) {
fun render(offset: FloatArray, mesh: ChunkMesh, light: ByteArray, tints: IntArray?) {
val tint = color(tints?.getOrNull(tintIndex) ?: 0)
val lightTint = ((light[lightIndex].toInt() shl 24) or tint).buffer()
val textureId = this.texture.shaderId.buffer()


val mesh = mesh.mesh(texture)

val uv = temp

var index = 0
val size = mesh.order.size
while (index < size) {
val vertexOffset = mesh.order[index] * 3
val uvOffset = mesh.order[index + 1] * 2
uv[0] = this.uv[uvOffset]
uv[1] = this.uv[uvOffset + 1]

mesh.addVertex(
x = offset[0] + positions[vertexOffset], y = offset[1] + positions[vertexOffset + 1], z = offset[2] + positions[vertexOffset + 2],
uv = uv,
texture = this.texture,
u = this.uv[uvOffset],
v = this.uv[uvOffset + 1],
shaderTextureId = textureId,
lightTint = lightTint,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class BakedModel(

override fun getParticleTexture(random: Random?, position: Vec3i) = particle

override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?, temp: FloatArray): Boolean {
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?): Boolean {
var rendered = false

for ((directionIndex, faces) in faces.withIndex()) {
Expand All @@ -56,7 +56,7 @@ class BakedModel(
if (FaceCulling.canCull(state, face.properties, direction, neighbour)) {
continue
}
face.render(offset, mesh, light, tints, temp)
face.render(offset, mesh, light, tints)

rendered = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ class BuiltModel(
val dynamic: Array<BlockRender>,
) : BlockRender {

override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?, temp: FloatArray): Boolean {
var rendered = model.render(position, offset, mesh, random, state, neighbours, light, tints, temp)
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?): Boolean {
var rendered = model.render(position, offset, mesh, random, state, neighbours, light, tints)

for (dynamic in this.dynamic) {
if (dynamic.render(position, offset, mesh, random, state, neighbours, light, tints, temp)) {
if (dynamic.render(position, offset, mesh, random, state, neighbours, light, tints)) {
rendered = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import java.util.*
interface BlockRender : ItemRender {
fun getParticleTexture(random: Random?, position: Vec3i): Texture? = null

fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?, temp: FloatArray): Boolean
fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?): Boolean

fun getProperties(direction: Directions): SideProperties? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ interface PickedBlockRender : BlockRender {
default?.render(gui, offset, consumer, options, size, stack)
}

override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?, temp: FloatArray): Boolean {
return pick(neighbours)?.render(position, offset, mesh, random, state, neighbours, light, tints, temp) ?: false
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?): Boolean {
return pick(neighbours)?.render(position, offset, mesh, random, state, neighbours, light, tints) ?: false
}

override fun getProperties(direction: Directions): SideProperties? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ class WeightedBlockRender(
return getModel(random, position).getParticleTexture(random, position)
}

override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?, temp: FloatArray): Boolean {
return getModel(random, position).render(position, offset, mesh, random, state, neighbours, light, tints, temp)
override fun render(position: BlockPosition, offset: FloatArray, mesh: ChunkMesh, random: Random?, state: BlockState, neighbours: Array<BlockState?>, light: ByteArray, tints: IntArray?): Boolean {
return getModel(random, position).render(position, offset, mesh, random, state, neighbours, light, tints)
}

override fun render(gui: GUIRenderer, offset: Vec2, consumer: GUIVertexConsumer, options: GUIVertexOptions?, size: Vec2, stack: ItemStack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ class FragmentedArrayFloatList(
invalidateOutput()
}

fun add(value1: Float, value2: Float, value3: Float, value4: Float, value5: Float, value6: Float, value7: Float) {
var buffer = grow(7)
buffer.put(value1); if (tryPush(buffer)) buffer = grow(6)
buffer.put(value2); if (tryPush(buffer)) buffer = grow(5)
buffer.put(value3); if (tryPush(buffer)) buffer = grow(4)
buffer.put(value4); if (tryPush(buffer)) buffer = grow(3)
buffer.put(value5); if (tryPush(buffer)) buffer = grow(2)
buffer.put(value6); if (tryPush(buffer)) buffer = grow(1)
buffer.put(value7)

size += 7
tryPush(buffer)
invalidateOutput()
}

private fun tryPush(fragment: FloatBuffer): Boolean {
if (fragment.position() != fragment.limit()) {
return false
Expand Down

0 comments on commit e9bc701

Please sign in to comment.