rendering: add support for uvlock

This commit is contained in:
Lukas 2021-03-01 22:03:28 +01:00
parent 07654ca1ab
commit a1cbac33ab
5 changed files with 34 additions and 17 deletions

View File

@ -18,6 +18,14 @@ enum class Axes {
Z;
companion object {
fun byDirection(direction: Directions): Axes {
return when (direction) {
Directions.EAST, Directions.WEST -> X
Directions.UP, Directions.DOWN -> Y
Directions.NORTH, Directions.SOUTH -> Z
}
}
val AXES = values()
}
}

View File

@ -46,7 +46,7 @@ open class BlockModelElement(data: JsonObject) {
data["rotation"]?.asJsonObject?.let {
val axis = Axes.valueOf(it["axis"].asString.toUpperCase())
val angle = glm.radians(it["angle"].asDouble)
val angle = glm.radians(it["angle"].asFloat)
val rescale = it["rescale"]?.asBoolean ?: false
rotatePositions(positions, axis, angle, VecUtil.jsonToVec3(it["origin"].asJsonArray), rescale)
}
@ -64,7 +64,6 @@ open class BlockModelElement(data: JsonObject) {
}
companion object {
const val BLOCK_RESOLUTION = 16f
val FACE_POSITION_MAP_TEMPLATE = arrayOf(
@ -76,9 +75,9 @@ open class BlockModelElement(data: JsonObject) {
intArrayOf(5, 1, 3, 7)
)
fun rotatePositions(positions: Array<Vec3>, axis: Axes, angle: Double, origin: Vec3, rescale: Boolean) {
fun rotatePositions(positions: Array<Vec3>, axis: Axes, angle: Float, origin: Vec3, rescale: Boolean) {
// TODO: optimize for 90deg, 180deg, 270deg rotations
if (angle == 0.0) {
if (angle == 0f) {
return
}
for ((i, position) in positions.withIndex()) {

View File

@ -86,8 +86,8 @@ class BlockModelFace {
return result
}
fun rotate(angle: Double) {
if (angle == 0.0) {
fun rotate(angle: Float) {
if (angle == 0f) {
return
}
val sin = glm.sin(angle)

View File

@ -41,7 +41,6 @@ class ElementRenderer(parent: BlockModelElement, val rotation: Vec3, uvLock: Boo
init {
rotatePositionsAxes(positions, rotation, rescale)
// TODO : uvLock
for (direction in Directions.DIRECTIONS) {
if (positions.containsAllVectors(FULL_TEST_POSITIONS[direction.ordinal], 0.0001f)) { // TODO: check if texture is transparent ==> && ! texture.isTransparent
fullFaceDirections.add(direction)
@ -51,6 +50,17 @@ class ElementRenderer(parent: BlockModelElement, val rotation: Vec3, uvLock: Boo
faces[direction] = BlockModelFace(it)
}
}
if (uvLock) {
for (direction in Directions.DIRECTIONS) {
val axes = Axes.byDirection(direction)
val angle = when (axes) {
Axes.X -> rotation.x
Axes.Y -> rotation.y
Axes.Z -> rotation.z
}
faces[direction]?.rotate(angle)
}
}
}
@ -145,18 +155,18 @@ class ElementRenderer(parent: BlockModelElement, val rotation: Vec3, uvLock: Boo
if (rotation == EMPTY_VECTOR) {
return direction
}
var rotatedDirectionVector = VecUtil.rotateVector(direction.directionVector, rotation.x.toDouble(), Axes.X)
rotatedDirectionVector = VecUtil.rotateVector(rotatedDirectionVector, rotation.y.toDouble(), Axes.Y)
return Directions.byDirection(VecUtil.rotateVector(rotatedDirectionVector, rotation.z.toDouble(), Axes.Z))
var rotatedDirectionVector = VecUtil.rotateVector(direction.directionVector, rotation.x, Axes.X)
rotatedDirectionVector = VecUtil.rotateVector(rotatedDirectionVector, rotation.y, Axes.Y)
return Directions.byDirection(VecUtil.rotateVector(rotatedDirectionVector, rotation.z, Axes.Z))
}
fun rotatePositionsAxes(positions: Array<Vec3>, angles: Vec3, rescale: Boolean) {
if (angles == EMPTY_VECTOR) {
return
}
BlockModelElement.rotatePositions(positions, Axes.X, angles.x.toDouble(), EMPTY_VECTOR, rescale)
BlockModelElement.rotatePositions(positions, Axes.Y, angles.y.toDouble(), EMPTY_VECTOR, rescale)
BlockModelElement.rotatePositions(positions, Axes.Z, angles.z.toDouble(), EMPTY_VECTOR, rescale)
BlockModelElement.rotatePositions(positions, Axes.X, angles.x, EMPTY_VECTOR, rescale)
BlockModelElement.rotatePositions(positions, Axes.Y, angles.y, EMPTY_VECTOR, rescale)
BlockModelElement.rotatePositions(positions, Axes.Z, angles.z, EMPTY_VECTOR, rescale)
}
private val POSITION_1 = Vec3(-0.5f, -0.5f, -0.5f)

View File

@ -31,12 +31,12 @@ object VecUtil {
}
}
fun getRotatedValues(x: Float, y: Float, sin: Double, cos: Double): Vec2 {
return Vec2((x * cos - y * sin).toFloat(), (x * sin + y * cos).toFloat())
fun getRotatedValues(x: Float, y: Float, sin: Float, cos: Float): Vec2 {
return Vec2(x * cos - y * sin, x * sin + y * cos)
}
fun rotateVector(original: Vec3, angle: Double, axis: Axes): Vec3 {
if (angle == 0.0) {
fun rotateVector(original: Vec3, angle: Float, axis: Axes): Vec3 {
if (angle == 0f) {
return original
}
return when (axis) {