Add programatic model for interface block

This commit is contained in:
Shadowfacts 2021-03-06 14:23:41 -05:00
parent 7cc96d78ad
commit 5542f088f9
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
5 changed files with 189 additions and 175 deletions

View File

@ -1,12 +1,14 @@
package net.shadowfacts.phycon
import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.model.ModelLoadingRegistry
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
import net.fabricmc.fabric.api.client.screenhandler.v1.ScreenRegistry
import net.shadowfacts.phycon.block.inserter.InserterScreen
import net.shadowfacts.phycon.block.redstone_emitter.RedstoneEmitterScreen
import net.shadowfacts.phycon.init.PhyScreens
import net.shadowfacts.phycon.block.terminal.TerminalScreen
import net.shadowfacts.phycon.client.PhyModelProvider
import net.shadowfacts.phycon.networking.ClientReceiver
import net.shadowfacts.phycon.networking.S2CTerminalUpdateDisplayedItems
@ -16,6 +18,8 @@ import net.shadowfacts.phycon.networking.S2CTerminalUpdateDisplayedItems
object PhysicalConnectivityClient: ClientModInitializer {
override fun onInitializeClient() {
ModelLoadingRegistry.INSTANCE.registerResourceProvider(::PhyModelProvider)
ScreenRegistry.register(PhyScreens.TERMINAL, ::TerminalScreen)
ScreenRegistry.register(PhyScreens.INSERTER, ::InserterScreen)
ScreenRegistry.register(PhyScreens.REDSTONE_EMITTER, ::RedstoneEmitterScreen)

View File

@ -0,0 +1,25 @@
package net.shadowfacts.phycon.client
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.Identifier
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.client.model.InterfaceModel
/**
* @author shadowfacts
*/
class PhyModelProvider(resourceManager: ResourceManager) : ModelResourceProvider {
companion object {
val INTERFACE = Identifier(PhysicalConnectivity.MODID, "block/network_interface")
}
override fun loadModelResource(resourceId: Identifier, context: ModelProviderContext): UnbakedModel? {
return when (resourceId) {
INTERFACE -> InterfaceModel
else -> null
}
}
}

View File

@ -0,0 +1,158 @@
package net.shadowfacts.phycon.client.model
import net.minecraft.block.BlockState
import net.minecraft.client.render.model.*
import net.minecraft.client.texture.Sprite
import net.minecraft.client.util.SpriteIdentifier
import net.minecraft.util.Identifier
import net.minecraft.util.math.Direction
import net.shadowfacts.phycon.PhysicalConnectivity
import net.shadowfacts.phycon.block.FaceDeviceBlock
import java.util.Random
import java.util.function.Function
/**
* @author shadowfacts
*/
object InterfaceModel: UnbakedModel, BakedModel {
private val interfaceSideID = Identifier(PhysicalConnectivity.MODID, "block/interface_side")
private val interfaceCableStraightID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_straight")
private val interfaceCableCornerID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_corner")
private val interfaceCableCorner2ID = Identifier(PhysicalConnectivity.MODID, "block/interface_cable_corner_2")
private var interfaceSides = Array<BakedModel?>(6) { null }
private var interfaceCableStraight = Array<BakedModel?>(6) { null }
private var interfaceCableCorner = mutableMapOf<ModelRotation, BakedModel>()
private var interfaceCableCorner2 = mutableMapOf<ModelRotation, BakedModel>()
override fun getModelDependencies(): Collection<Identifier> {
return listOf(
interfaceSideID,
interfaceCableStraightID,
interfaceCableCornerID,
interfaceCableCorner2ID,
)
}
override fun getTextureDependencies(
unbakedModelGetter: Function<Identifier, UnbakedModel>,
unresolvedTextureReferences: MutableSet<com.mojang.datafixers.util.Pair<String, String>>
): Collection<SpriteIdentifier> {
return listOf()
}
override fun bake(loader: ModelLoader, textureGetter: Function<SpriteIdentifier, Sprite>, rotationContainer: ModelBakeSettings, modelId: Identifier): BakedModel {
listOf(
ModelRotation.X0_Y0,
ModelRotation.X180_Y0,
ModelRotation.X270_Y0,
ModelRotation.X90_Y0,
ModelRotation.X90_Y90,
ModelRotation.X90_Y270,
).forEachIndexed { i, rot ->
interfaceSides[i] = loader.bake(interfaceSideID, rot)
interfaceCableStraight[i] = loader.bake(interfaceCableStraightID, rot)
}
mapOf(
interfaceCableCorner to interfaceCableCornerID to ModelRotation.values().toList(),
interfaceCableCorner2 to interfaceCableCorner2ID to listOf(
ModelRotation.X0_Y0,
ModelRotation.X0_Y90,
ModelRotation.X0_Y180,
ModelRotation.X0_Y270,
ModelRotation.X180_Y0,
ModelRotation.X180_Y90,
ModelRotation.X180_Y180,
ModelRotation.X180_Y270,
),
).forEach { (k, rotations) ->
val (map, id) = k
map.clear()
rotations.forEach { rot ->
val model = loader.bake(id, rot)
if (model == null) map.remove(rot)
else map[rot] = model
}
}
return this
}
override fun getQuads(state: BlockState?, face: Direction?, random: Random): List<BakedQuad> {
if (state == null) return listOf()
val facing = state[FaceDeviceBlock.FACING]
val connection = state[FaceDeviceBlock.CABLE_CONNECTION]
val sideQuads = interfaceSides[facing.ordinal]?.getQuads(state, face, random) ?: listOf()
val cableQuads = if (connection == facing.opposite) {
interfaceCableStraight[facing.ordinal]?.getQuads(state, face, random) ?: listOf()
} else {
val model = when (facing) {
Direction.DOWN -> when (connection) {
Direction.NORTH -> interfaceCableCorner[ModelRotation.X0_Y0]
Direction.EAST -> interfaceCableCorner[ModelRotation.X0_Y90]
Direction.SOUTH -> interfaceCableCorner[ModelRotation.X0_Y180]
Direction.WEST -> interfaceCableCorner[ModelRotation.X0_Y270]
else -> null
}
Direction.UP -> when (connection) {
Direction.NORTH -> interfaceCableCorner[ModelRotation.X180_Y180]
Direction.EAST -> interfaceCableCorner[ModelRotation.X180_Y270]
Direction.SOUTH -> interfaceCableCorner[ModelRotation.X180_Y0]
Direction.WEST -> interfaceCableCorner[ModelRotation.X180_Y90]
else -> null
}
Direction.NORTH -> when (connection) {
Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y0]
Direction.EAST -> interfaceCableCorner2[ModelRotation.X180_Y180]
Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y180]
Direction.WEST -> interfaceCableCorner2[ModelRotation.X0_Y0]
else -> null
}
Direction.SOUTH -> when (connection) {
Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y180]
Direction.WEST -> interfaceCableCorner2[ModelRotation.X180_Y0]
Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y0]
Direction.EAST -> interfaceCableCorner2[ModelRotation.X0_Y180]
else -> null
}
Direction.WEST -> when (connection) {
Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y270]
Direction.NORTH -> interfaceCableCorner2[ModelRotation.X180_Y90]
Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y90]
Direction.SOUTH -> interfaceCableCorner2[ModelRotation.X0_Y270]
else -> null
}
Direction.EAST -> when (connection) {
Direction.UP -> interfaceCableCorner[ModelRotation.X270_Y90]
Direction.SOUTH -> interfaceCableCorner2[ModelRotation.X180_Y270]
Direction.DOWN -> interfaceCableCorner[ModelRotation.X90_Y270]
Direction.NORTH -> interfaceCableCorner2[ModelRotation.X0_Y90]
else -> null
}
else -> null
}
model?.getQuads(state, face, random) ?: listOf()
}
return sideQuads + cableQuads
}
override fun useAmbientOcclusion() = true
override fun hasDepth() = false
override fun isSideLit() = false
override fun isBuiltin() = false
override fun getSprite(): Sprite {
return interfaceSides.first()!!.sprite
}
override fun getTransformation() = null
override fun getOverrides() = null
}

View File

@ -1,159 +1,8 @@
{
"multipart": [
{
"apply": { "model": "phycon:block/cable_center" }
},
{
"when": { "facing": "down" },
"apply": { "model": "phycon:block/interface_side" }
},
{
"when": { "facing": "up" },
"apply": { "model": "phycon:block/interface_side", "x": 180 }
},
{
"when": { "facing": "north" },
"apply": { "model": "phycon:block/interface_side", "x": 270 }
},
{
"when": { "facing": "south" },
"apply": { "model": "phycon:block/interface_side", "x": 90 }
},
{
"when": { "facing": "west" },
"apply": { "model": "phycon:block/interface_side", "x": 90, "y": 90 }
},
{
"when": { "facing": "east" },
"apply": { "model": "phycon:block/interface_side", "x": 90, "y": 270 }
},
{
"when": { "cable_connection": "up", "facing": "down" },
"apply": { "model": "phycon:block/interface_cable_straight" }
},
{
"when": { "cable_connection": "down", "facing": "up" },
"apply": { "model": "phycon:block/interface_cable_straight", "x": 180 }
},
{
"when": { "cable_connection": "north", "facing": "south" },
"apply": { "model": "phycon:block/interface_cable_straight", "x": 90 }
},
{
"when": { "cable_connection": "south", "facing": "north" },
"apply": { "model": "phycon:block/interface_cable_straight", "x": 270 }
},
{
"when": { "cable_connection": "west", "facing": "east" },
"apply": { "model": "phycon:block/interface_cable_straight", "x": 90, "y": 270 }
},
{
"when": { "cable_connection": "east", "facing": "west" },
"apply": { "model": "phycon:block/interface_cable_straight", "x": 90, "y": 90 }
},
{
"when": {"cable_connection": "north", "facing": "down"},
"apply": { "model": "phycon:block/interface_cable_corner" }
},
{
"when": {"cable_connection": "east", "facing": "down"},
"apply": { "model": "phycon:block/interface_cable_corner", "y": 90 }
},
{
"when": {"cable_connection": "south", "facing": "down"},
"apply": { "model": "phycon:block/interface_cable_corner", "y": 180 }
},
{
"when": {"cable_connection": "west", "facing": "down"},
"apply": { "model": "phycon:block/interface_cable_corner", "y": 270 }
},
{
"when": {"cable_connection": "north", "facing": "up"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 180, "y": 180 }
},
{
"when": {"cable_connection": "east", "facing": "up"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 180, "y": 270 }
},
{
"when": {"cable_connection": "south", "facing": "up"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 180 }
},
{
"when": {"cable_connection": "west", "facing": "up"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 180, "y": 90 }
},
{
"when": {"cable_connection": "down", "facing": "north"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 90, "y": 180 }
},
{
"when": {"cable_connection": "up", "facing": "north"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 270 }
},
{
"when": {"cable_connection": "west", "facing": "north"},
"apply": { "model": "phycon:block/interface_cable_corner_2" }
},
{
"when": {"cable_connection": "east", "facing": "north"},
"apply": { "model": "phycon:block/interface_cable_corner_2", "x": 180, "y": 180 }
},
{
"when": {"cable_connection": "down", "facing": "south"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 90 }
},
{
"when": {"cable_connection": "up", "facing": "south"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 270, "y": 180 }
},
{
"when": {"cable_connection": "west", "facing": "south"},
"apply": { "model": "phycon:block/interface_cable_corner_3" }
},
{
"when": {"cable_connection": "east", "facing": "south"},
"apply": { "model": "phycon:block/interface_cable_corner_3", "x": 180, "y": 180 }
},
{
"when": {"cable_connection": "down", "facing": "west"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 90, "y": 90 }
},
{
"when": {"cable_connection": "up", "facing": "west"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 270, "y": 270 }
},
{
"when": {"cable_connection": "north", "facing": "west"},
"apply": { "model": "phycon:block/interface_cable_corner_3", "y": 90 }
},
{
"when": {"cable_connection": "south", "facing": "west"},
"apply": { "model": "phycon:block/interface_cable_corner_3", "x": 180, "y": 270 }
},
{
"when": {"cable_connection": "down", "facing": "east"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 90, "y": 270 }
},
{
"when": {"cable_connection": "up", "facing": "east"},
"apply": { "model": "phycon:block/interface_cable_corner", "x": 270, "y": 90 }
},
{
"when": {"cable_connection": "north", "facing": "east"},
"apply": { "model": "phycon:block/interface_cable_corner_2", "y": 90 }
},
{
"when": {"cable_connection": "south", "facing": "east"},
"apply": { "model": "phycon:block/interface_cable_corner_2", "x": 180, "y": 270 }
"_comment": "see InterfaceModel",
"apply": { "model": "phycon:block/network_interface" }
}
]
}

View File

@ -1,22 +0,0 @@
{
"textures": {
"straight": "phycon:block/cable_straight_rotated",
"cap": "phycon:block/cable_cap_end",
"corner_down": "phycon:block/interface_cable_corner_l_up",
"corner_up": "phycon:block/interface_cable_corner_r"
},
"elements": [
{
"from": [0, 6, 6],
"to": [10, 10, 10],
"faces": {
"down": {"texture": "#corner_down"},
"up": {"texture": "#corner_up"},
"north": {"texture": "#straight"},
"south": {"texture": "#straight"},
"west": {"texture": "#cap", "cullface": "west"},
"east": {"texture": "#straight"}
}
}
]
}