Convert Cable block model to code

This commit is contained in:
Shadowfacts 2021-03-14 14:07:56 -04:00
parent a86058f8bd
commit f536bf72a9
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
14 changed files with 300 additions and 251 deletions

View File

@ -4,8 +4,10 @@ import net.fabricmc.fabric.api.client.model.ModelProviderContext
import net.fabricmc.fabric.api.client.model.ModelResourceProvider
import net.minecraft.client.render.model.UnbakedModel
import net.minecraft.resource.ResourceManager
import net.minecraft.util.DyeColor
import net.minecraft.util.Identifier
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.client.model.ColoredCableModel
import net.shadowfacts.phycon.client.model.RedstoneControllerModel
import net.shadowfacts.phycon.client.model.SimpleFaceDeviceModel
@ -23,6 +25,7 @@ class PhyModelProvider(resourceManager: ResourceManager) : ModelResourceProvider
val EXTRACTOR_SIDE = Identifier(PhysicalConnectivity.MODID, "block/extractor_side")
val INSERTER = Identifier(PhysicalConnectivity.MODID, "block/inserter")
val INSERTER_SIDE = Identifier(PhysicalConnectivity.MODID, "block/inserter_side")
val CABLE_BLUE = Identifier(PhysicalConnectivity.MODID, "block/cable/blue")
}
override fun loadModelResource(resourceId: Identifier, context: ModelProviderContext): UnbakedModel? {
@ -32,6 +35,7 @@ class PhyModelProvider(resourceManager: ResourceManager) : ModelResourceProvider
REDSTONE_EMITTER -> SimpleFaceDeviceModel(REDSTONE_EMITTER_SIDE)
EXTRACTOR -> SimpleFaceDeviceModel(EXTRACTOR_SIDE)
INSERTER -> SimpleFaceDeviceModel(INSERTER_SIDE)
CABLE_BLUE -> ColoredCableModel(DyeColor.BLUE)
else -> null
}
}

View File

@ -0,0 +1,282 @@
package net.shadowfacts.phycon.client.model
import com.mojang.datafixers.util.Pair
import net.minecraft.block.BlockState
import net.minecraft.client.render.model.*
import net.minecraft.client.render.model.json.ModelOverrideList
import net.minecraft.client.render.model.json.ModelTransformation
import net.minecraft.client.texture.Sprite
import net.minecraft.client.texture.SpriteAtlasTexture
import net.minecraft.client.util.SpriteIdentifier
import net.minecraft.util.DyeColor
import net.minecraft.util.Identifier
import net.minecraft.util.math.Direction
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.block.cable.CableBlock
import net.shadowfacts.phycon.util.CableConnection
import java.util.Random
import java.util.function.Function
/**
* @author shadowfacts
*/
class ColoredCableModel(
val color: DyeColor,
): UnbakedModel, BakedModel {
companion object {
val SIDE = Identifier(PhysicalConnectivity.MODID, "block/cable_side")
val CENTER = Identifier(PhysicalConnectivity.MODID, "block/cable_center")
val DIAG_CORNER = Identifier(PhysicalConnectivity.MODID, "block/cable_diag_corner")
val DIAG_CORNER_CONT = Identifier(PhysicalConnectivity.MODID, "block/cable_diag_corner_cont")
val DIAG_CORNER_XZ = Identifier(PhysicalConnectivity.MODID, "block/cable_diag_corner_xz")
val DIAG_CORNER_XZ_CONT = Identifier(PhysicalConnectivity.MODID, "block/cable_diag_corner_xz_cont")
}
private var centerSprite: Sprite? = null
private var side: Array<BakedModel?> = Array(6) { null }
private var center: Array<BakedModel?> = Array(6) { null }
private var diagCorner = mutableMapOf<ModelRotation, BakedModel>()
private var diagCornerCont = mutableMapOf<ModelRotation, BakedModel>()
private var diagCornerXZ = mutableMapOf<ModelRotation, BakedModel>()
private var diagCornerXZCont = mutableMapOf<ModelRotation, BakedModel>()
private val sideRotations = listOf(
Direction.DOWN to ModelRotation.X0_Y0,
Direction.UP to ModelRotation.X180_Y0,
Direction.NORTH to ModelRotation.X270_Y0,
Direction.SOUTH to ModelRotation.X90_Y0,
Direction.WEST to ModelRotation.X90_Y90,
Direction.EAST to ModelRotation.X90_Y270,
)
override fun getModelDependencies(): Collection<Identifier> {
return setOf(
SIDE,
CENTER,
DIAG_CORNER,
DIAG_CORNER_CONT,
DIAG_CORNER_XZ,
DIAG_CORNER_XZ_CONT,
)
}
override fun getTextureDependencies(
unbakedModelGetter: Function<Identifier, UnbakedModel>,
unresolvedTextureDependencies: MutableSet<Pair<String, String>>
): Collection<SpriteIdentifier> {
val newSet = mutableSetOf<Pair<String, String>>()
val res = modelDependencies.map(unbakedModelGetter::apply).flatMap {
it.getTextureDependencies(unbakedModelGetter, newSet)
}
unresolvedTextureDependencies.addAll(newSet)
return res.map {
if (it.textureId.namespace == PhysicalConnectivity.MODID && it.textureId.path.startsWith("block/cable/color/")) {
val newPath = it.textureId.path.replace("block/cable/color/", "block/cable/${color.getName()}/")
SpriteIdentifier(it.atlasId, Identifier(PhysicalConnectivity.MODID, newPath))
} else {
it
}
}
}
override fun bake(
loader: ModelLoader,
textureGetter: Function<SpriteIdentifier, Sprite>,
rotationContainer: ModelBakeSettings,
modelId: Identifier
): BakedModel {
centerSprite = textureGetter.apply(SpriteIdentifier(SpriteAtlasTexture.BLOCK_ATLAS_TEXTURE, Identifier(PhysicalConnectivity.MODID, "block/cable/${color.getName()}/straight")))
sideRotations.forEach { (side, rot) ->
this.side[side.ordinal] = loader.bakeRetextured(SIDE, textureGetter, rot)
this.center[side.ordinal] = loader.bakeRetextured(CENTER, textureGetter, rot)
}
diagCorner.clear()
diagCornerCont.clear()
listOf(
ModelRotation.X0_Y0,
ModelRotation.X0_Y180,
ModelRotation.X180_Y180,
ModelRotation.X180_Y0,
ModelRotation.X0_Y270,
ModelRotation.X0_Y90,
ModelRotation.X180_Y90,
ModelRotation.X180_Y270,
).forEach { rot ->
diagCorner[rot] = loader.bakeRetextured(DIAG_CORNER, textureGetter, rot)
diagCornerCont[rot] = loader.bakeRetextured(DIAG_CORNER_CONT, textureGetter, rot)
}
diagCornerXZ.clear()
diagCornerXZCont.clear()
listOf(
ModelRotation.X0_Y0,
ModelRotation.X0_Y90,
ModelRotation.X0_Y180,
ModelRotation.X0_Y270,
).forEach { rot ->
diagCornerXZ[rot] = loader.bakeRetextured(DIAG_CORNER_XZ, textureGetter, rot)
diagCornerXZCont[rot] = loader.bakeRetextured(DIAG_CORNER_XZ_CONT, textureGetter, rot)
}
return this
}
private fun ModelLoader.bakeRetextured(id: Identifier, textureGetter: Function<SpriteIdentifier, Sprite>, rot: ModelRotation): BakedModel {
val unbaked = getOrLoadModel(id)
val wrappedTextureGetter: (SpriteIdentifier) -> Sprite = {
var newId = it
if (it.textureId.namespace == PhysicalConnectivity.MODID && it.textureId.path.startsWith("block/cable/color/")) {
val newPath = it.textureId.path.replace("block/cable/color/", "block/cable/${color.getName()}/")
newId = SpriteIdentifier(it.atlasId, Identifier(PhysicalConnectivity.MODID, newPath))
}
textureGetter.apply(newId)
}
return unbaked.bake(this, wrappedTextureGetter, rot, id)!!
}
override fun getQuads(state: BlockState?, face: Direction?, random: Random): List<BakedQuad> {
if (state == null) {
return center.flatMap {
it?.getQuads(state, face, random) ?: listOf()
}
}
val quads = mutableListOf<BakedQuad>()
fun addModel(model: BakedModel?) {
if (model == null) return
quads.addAll(model.getQuads(state, face, random))
}
for ((side, prop) in CableBlock.CONNECTIONS) {
if (state[prop] == CableConnection.ON) {
addModel(this.side[side.ordinal])
} else {
addModel(this.center[side.ordinal])
}
}
val down = state[CableBlock.CONNECTIONS[Direction.DOWN]] == CableConnection.ON
val up = state[CableBlock.CONNECTIONS[Direction.UP]] == CableConnection.ON
val north = state[CableBlock.CONNECTIONS[Direction.NORTH]] == CableConnection.ON
val south = state[CableBlock.CONNECTIONS[Direction.SOUTH]] == CableConnection.ON
val west = state[CableBlock.CONNECTIONS[Direction.WEST]] == CableConnection.ON
val east = state[CableBlock.CONNECTIONS[Direction.EAST]] == CableConnection.ON
if (!down && !north) {
if (up && south && !east && !west) {
addModel(diagCornerCont[ModelRotation.X0_Y0])
} else {
addModel(diagCorner[ModelRotation.X0_Y0])
}
}
if (!down && !south) {
if (up && north && !east && !west) {
addModel(diagCornerCont[ModelRotation.X0_Y180])
} else {
addModel(diagCorner[ModelRotation.X0_Y180])
}
}
if (!up && !north) {
if (down && south && !east && !west) {
addModel(diagCornerCont[ModelRotation.X180_Y180])
} else {
addModel(diagCorner[ModelRotation.X180_Y180])
}
}
if (!up && !south) {
if (down && north && !east && !west) {
addModel(diagCornerCont[ModelRotation.X180_Y0])
} else {
addModel(diagCorner[ModelRotation.X180_Y0])
}
}
if (!down && !west) {
if (up && east && !north && !south) {
addModel(diagCornerCont[ModelRotation.X0_Y270])
} else {
addModel(diagCorner[ModelRotation.X0_Y270])
}
}
if (!down && !east) {
if (up && west && !north && !south) {
addModel(diagCornerCont[ModelRotation.X0_Y90])
} else {
addModel(diagCorner[ModelRotation.X0_Y90])
}
}
if (!up && !west) {
if (down && east && !north && !south) {
addModel(diagCornerCont[ModelRotation.X180_Y90])
} else {
addModel(diagCorner[ModelRotation.X180_Y90])
}
}
if (!up && !east) {
if (down && west && !north && !south) {
addModel(diagCornerCont[ModelRotation.X180_Y270])
} else {
addModel(diagCorner[ModelRotation.X180_Y270])
}
}
if (!north && !west) {
if (south && east && !down && !up) {
addModel(diagCornerXZCont[ModelRotation.X0_Y0])
} else {
addModel(diagCornerXZ[ModelRotation.X0_Y0])
}
}
if (!north && !east) {
if (south && west && !down && !up) {
addModel(diagCornerXZCont[ModelRotation.X0_Y90])
} else {
addModel(diagCornerXZ[ModelRotation.X0_Y90])
}
}
if (!south && !east) {
if (north && west && !down && !up) {
addModel(diagCornerXZCont[ModelRotation.X0_Y180])
} else {
addModel(diagCornerXZ[ModelRotation.X0_Y180])
}
}
if (!south && !west) {
if (north && east && !down && !up) {
addModel(diagCornerXZCont[ModelRotation.X0_Y270])
} else {
addModel(diagCornerXZ[ModelRotation.X0_Y270])
}
}
return quads
}
override fun useAmbientOcclusion() = true
override fun hasDepth() = false
override fun isSideLit() = false
override fun isBuiltin() = false
override fun getSprite() = centerSprite
override fun getTransformation() = null
override fun getOverrides() = null
}

View File

@ -89,7 +89,7 @@ abstract class FaceDeviceModel: UnbakedModel, BakedModel {
}
override fun getQuads(state: BlockState?, face: Direction?, random: Random): List<BakedQuad> {
if (state == null) return listOf()
if (state == null) return listOf()
val facing = state[FaceDeviceBlock.FACING]
val connection = state[FaceDeviceBlock.CABLE_CONNECTION]

View File

@ -1,244 +1,7 @@
{
"multipart": [
{
"when": { "down": "on" },
"apply": { "model": "phycon:block/cable_side" }
},
{
"when": { "up": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 180 }
},
{
"when": { "north": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 270 }
},
{
"when": { "south": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 90 }
},
{
"when": { "west": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 90, "y": 90 }
},
{
"when": { "east": "on" },
"apply": { "model": "phycon:block/cable_side", "x": 90, "y": 270 }
},
{
"when": { "down": "off|disabled" },
"apply": { "model": "phycon:block/cable_center" }
},
{
"when": { "up": "off|disabled" },
"apply": { "model": "phycon:block/cable_center", "x": 180 }
},
{
"when": { "north": "off|disabled" },
"apply": { "model": "phycon:block/cable_center", "x": 270 }
},
{
"when": { "south": "off|disabled" },
"apply": { "model": "phycon:block/cable_center", "x": 90 }
},
{
"when": { "west": "off|disabled" },
"apply": { "model": "phycon:block/cable_center", "x": 90, "y": 90 }
},
{
"when": { "east": "off|disabled" },
"apply": { "model": "phycon:block/cable_center", "x": 90, "y": 270 }
},
{
"when": {
"OR": [
{ "down": "off|disabled", "north": "off|disabled", "up": "off|disabled" },
{ "down": "off|disabled", "north": "off|disabled", "south": "off|disabled" },
{ "down": "off|disabled", "north": "off|disabled", "east": "on" },
{ "down": "off|disabled", "north": "off|disabled", "west": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner" }
},
{
"when": { "down": "off|disabled", "north": "off|disabled", "up": "on", "south": "on", "east": "off|disabled", "west": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont" }
},
{
"when": {
"OR": [
{ "down": "off|disabled", "south": "off|disabled", "up": "off|disabled" },
{ "down": "off|disabled", "south": "off|disabled", "north": "off|disabled" },
{ "down": "off|disabled", "south": "off|disabled", "east": "on" },
{ "down": "off|disabled", "south": "off|disabled", "west": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner", "y": 180 }
},
{
"when": { "down": "off|disabled", "south": "off|disabled", "up": "on", "north": "on", "east": "off|disabled", "west": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont", "y": 180 }
},
{
"when": {
"OR": [
{ "up": "off|disabled", "north": "off|disabled", "down": "off|disabled" },
{ "up": "off|disabled", "north": "off|disabled", "south": "off|disabled" },
{ "up": "off|disabled", "north": "off|disabled", "east": "on" },
{ "up": "off|disabled", "north": "off|disabled", "west": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner", "x": 180, "y": 180 }
},
{
"when": { "up": "off|disabled", "north": "off|disabled", "down": "on", "south": "on", "east": "off|disabled", "west": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont", "x": 180, "y": 180 }
},
{
"when": {
"OR": [
{ "up": "off|disabled", "south": "off|disabled", "down": "off|disabled" },
{ "up": "off|disabled", "south": "off|disabled", "north": "off|disabled" },
{ "up": "off|disabled", "south": "off|disabled", "east": "on" },
{ "up": "off|disabled", "south": "off|disabled", "west": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner", "x": 180 }
},
{
"when": { "up": "off|disabled", "south": "off|disabled", "down": "on", "north": "on", "east": "off|disabled", "west": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont", "x": 180}
},
{
"when": {
"OR": [
{ "down": "off|disabled", "west": "off|disabled", "up": "off|disabled" },
{ "down": "off|disabled", "west": "off|disabled", "east": "off|disabled" },
{ "down": "off|disabled", "west": "off|disabled", "north": "on" },
{ "down": "off|disabled", "west": "off|disabled", "south": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner", "y": 270 }
},
{
"when": { "down": "off|disabled", "west": "off|disabled", "up": "on", "east": "on", "north": "off|disabled", "south": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont", "y": 270 }
},
{
"when": {
"OR": [
{ "down": "off|disabled", "east": "off|disabled", "up": "off|disabled" },
{ "down": "off|disabled", "east": "off|disabled", "west": "off|disabled" },
{ "down": "off|disabled", "east": "off|disabled", "north": "on" },
{ "down": "off|disabled", "east": "off|disabled", "south": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner", "y": 90 }
},
{
"when": { "down": "off|disabled", "east": "off|disabled", "up": "on", "west": "on", "north": "off|disabled", "south": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont", "y": 90 }
},
{
"when": {
"OR": [
{ "up": "off|disabled", "west": "off|disabled", "down": "off|disabled" },
{ "up": "off|disabled", "west": "off|disabled", "east": "off|disabled" },
{ "up": "off|disabled", "west": "off|disabled", "north": "on" },
{ "up": "off|disabled", "west": "off|disabled", "south": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner", "x": 180, "y": 90 }
},
{
"when": { "up": "off|disabled", "west": "off|disabled", "down": "on", "east": "on", "north": "off|disabled", "south": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont", "x": 180, "y": 90 }
},
{
"when": {
"OR": [
{ "up": "off|disabled", "east": "off|disabled", "down": "off|disabled" },
{ "up": "off|disabled", "east": "off|disabled", "west": "off|disabled" },
{ "up": "off|disabled", "east": "off|disabled", "north": "on" },
{ "up": "off|disabled", "east": "off|disabled", "south": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner", "x": 180, "y": 270 }
},
{
"when": { "up": "off|disabled", "east": "off|disabled", "down": "on", "west": "on", "north": "off|disabled", "south": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_cont", "x": 180, "y": 270 }
},
{
"when": {
"OR": [
{ "north": "off|disabled", "west": "off|disabled", "south": "off|disabled" },
{ "north": "off|disabled", "west": "off|disabled", "east": "off|disabled" },
{ "north": "off|disabled", "west": "off|disabled", "down": "on" },
{ "north": "off|disabled", "west": "off|disabled", "up": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner_xz" }
},
{
"when": { "north": "off|disabled", "west": "off|disabled", "south": "on", "east": "on", "down": "off|disabled", "up": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_xz_cont" }
},
{
"when": {
"OR": [
{ "north": "off|disabled", "east": "off|disabled", "south": "off|disabled" },
{ "north": "off|disabled", "east": "off|disabled", "west": "off|disabled" },
{ "north": "off|disabled", "east": "off|disabled", "down": "on" },
{ "north": "off|disabled", "east": "off|disabled", "up": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner_xz", "y": 90 }
},
{
"when": { "north": "off|disabled", "east": "off|disabled", "south": "on", "west": "on", "down": "off|disabled", "up": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_xz_cont", "y": 90 }
},
{
"when": {
"OR": [
{ "south": "off|disabled", "east": "off|disabled", "north": "off|disabled" },
{ "south": "off|disabled", "east": "off|disabled", "west": "off|disabled" },
{ "south": "off|disabled", "east": "off|disabled", "down": "on" },
{ "south": "off|disabled", "east": "off|disabled", "up": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner_xz", "y": 180 }
},
{
"when": { "south": "off|disabled", "east": "off|disabled", "north": "on", "west": "on", "down": "off|disabled", "up": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_xz_cont", "y": 180 }
},
{
"when": {
"OR": [
{ "south": "off|disabled", "west": "off|disabled", "north": "off|disabled" },
{ "south": "off|disabled", "west": "off|disabled", "east": "off|disabled" },
{ "south": "off|disabled", "west": "off|disabled", "down": "on" },
{ "south": "off|disabled", "west": "off|disabled", "up": "on" }
]
},
"apply": { "model": "phycon:block/cable_diag_corner_xz", "y": 270 }
},
{
"when": { "south": "off|disabled", "west": "off|disabled", "north": "on", "east": "on", "down": "off|disabled", "up": "off|disabled" },
"apply": { "model": "phycon:block/cable_diag_corner_xz_cont", "y": 270 }
"apply": { "model": "phycon:block/cable/blue" }
}
]
}

View File

@ -1,6 +1,6 @@
{
"textures": {
"center": "phycon:block/cable_cap_end",
"center": "phycon:block/cable/color/cap_end",
"particle": "#center"
},
"elements": [

View File

@ -1,6 +1,6 @@
{
"textures": {
"corner": "phycon:block/cable_cap_end"
"corner": "phycon:block/cable/color/cap_end"
},
"elements": [
{
@ -12,4 +12,4 @@
}
}
]
}
}

View File

@ -1,6 +1,6 @@
{
"textures": {
"corner": "phycon:block/cable_straight"
"corner": "phycon:block/cable/color/straight"
},
"elements": [
{
@ -12,4 +12,4 @@
}
}
]
}
}

View File

@ -1,6 +1,6 @@
{
"textures": {
"corner": "phycon:block/cable_cap_end"
"corner": "phycon:block/cable/color/cap_end"
},
"elements": [
{
@ -12,4 +12,4 @@
}
}
]
}
}

View File

@ -1,6 +1,6 @@
{
"textures": {
"corner": "phycon:block/cable_straight_rotated"
"corner": "phycon:block/cable/color/straight_rotated"
},
"elements": [
{
@ -12,4 +12,4 @@
}
}
]
}
}

View File

@ -1,7 +1,7 @@
{
"parent": "block/block",
"textures": {
"side": "phycon:block/cable_straight",
"side": "phycon:block/cable/color/straight",
"particle": "#side"
},
"elements": [

View File

@ -1,7 +1,7 @@
{
"parent": "minecraft:block/block",
"textures": {
"center": "phycon:block/cable_cap_end"
"center": "phycon:block/cable/blue/cap_end"
},
"elements": [
{
@ -17,4 +17,4 @@
}
}
]
}
}