From 4761eb266f33b9e79afdf6f1481933e84fd24445 Mon Sep 17 00:00:00 2001 From: octarine-noise Date: Sat, 12 Aug 2017 15:59:06 +0200 Subject: [PATCH] add support for block variants and multiple wood log bark textures --- .../client/integration/ForestryIntegration.kt | 26 +++--- .../client/integration/RubberIntegration.kt | 67 +++++++------- .../client/render/AbstractRenderColumn.kt | 17 ++-- .../client/render/EntityFallingLeavesFX.kt | 3 +- .../client/render/RenderCactus.kt | 19 ++-- .../betterfoliage/client/render/RenderLog.kt | 27 +++--- .../client/texture/GrassRegistry.kt | 38 ++++---- .../client/texture/LeafRegistry.kt | 38 ++++---- src/main/kotlin/mods/octarinecore/Utils.kt | 5 ++ .../render/AbstractBlockRenderingHandler.kt | 7 +- .../client/resource/ModelProcessor.kt | 89 ++++++++++++++----- 11 files changed, 203 insertions(+), 133 deletions(-) diff --git a/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt b/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt index 5e823eb..e40eb53 100644 --- a/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt +++ b/src/main/kotlin/mods/betterfoliage/client/integration/ForestryIntegration.kt @@ -7,9 +7,9 @@ import mods.betterfoliage.client.render.* import mods.betterfoliage.client.texture.ILeafRegistry import mods.betterfoliage.client.texture.LeafInfo import mods.betterfoliage.client.texture.LeafRegistry -import mods.betterfoliage.client.texture.StandardLeafSupport import mods.betterfoliage.loader.Refs import mods.octarinecore.client.resource.ModelProcessor +import mods.octarinecore.client.resource.ModelVariant import mods.octarinecore.client.resource.get import mods.octarinecore.metaprog.ClassRef import mods.octarinecore.metaprog.FieldRef @@ -19,7 +19,6 @@ import mods.octarinecore.tryDefault import net.minecraft.block.state.IBlockState import net.minecraft.client.Minecraft import net.minecraft.client.renderer.block.model.ModelResourceLocation -import net.minecraft.client.renderer.texture.TextureAtlasSprite import net.minecraft.client.renderer.texture.TextureMap import net.minecraft.util.EnumFacing import net.minecraft.util.ResourceLocation @@ -103,9 +102,9 @@ object ForestryLeavesSupport : ILeafRegistry { textureToValue[textureLocation] = LeafInfo(generated, LeafRegistry.getParticleType(texture, atlas)) } - override fun get(state: IBlockState) = null + override fun get(state: IBlockState, rand: Int) = null - override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing): LeafInfo? { + override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing, rand: Int): LeafInfo? { // check variant property (used in decorative leaves) state.properties.entries.find { ForestryIntegration.PropertyTreeType.isInstance(it.key) && ForestryIntegration.TreeDefinition.isInstance(it.value) @@ -127,21 +126,22 @@ object ForestryLeavesSupport : ILeafRegistry { @SideOnly(Side.CLIENT) object ForestryLogSupport : ModelProcessor, IColumnTextureInfo>, IColumnRegistry { - override var stateToKey = mutableMapOf>() - override var stateToValue = mapOf() + override var variants = mutableMapOf>() + override var variantToKey = mutableMapOf>() + override var variantToValue = mapOf() override val logger = BetterFoliageMod.logDetail init { MinecraftForge.EVENT_BUS.register(this) } - override fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): List? { + override fun processModelLoad1(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel) { // respect class list to avoid triggering on fences, stairs, etc. - if (!Config.blocks.logClasses.matchesClass(state.block)) return null + if (!Config.blocks.logClasses.matchesClass(state.block)) return // find wood type property val woodType = state.properties.entries.find { ForestryIntegration.PropertyWoodType.isInstance(it.key) && ForestryIntegration.IWoodType.isInstance(it.value) - } ?: return null + } ?: return logger.log(Level.DEBUG, "ForestryLogSupport: block state ${state.toString()}") logger.log(Level.DEBUG, "ForestryLogSupport: variant ${woodType.value.toString()}") @@ -151,14 +151,14 @@ object ForestryLogSupport : ModelProcessor, IColumnTextureInfo>, IC val heart = ForestryIntegration.heartTex.invoke(woodType.value) as String? logger.log(Level.DEBUG, "ForestryLogSupport: textures [heart=$heart, bark=$bark]") - return if (bark != null && heart != null) listOf(heart, bark) else null + if (bark != null && heart != null) putKeySingle(state, listOf(heart, bark)) } - override fun processStitch(state: IBlockState, key: List, atlas: TextureMap): IColumnTextureInfo? { + override fun processStitch(variant: ModelVariant, key: List, atlas: TextureMap): IColumnTextureInfo? { val heart = atlas[key[0]] ?: return null val bark = atlas[key[1]] ?: return null - return StaticColumnInfo(StandardLogSupport.getAxis(state), heart, heart, bark) + return StaticColumnInfo(StandardLogSupport.getAxis(variant.state), heart, heart, listOf(bark)) } - override fun get(state: IBlockState) = stateToValue[state] + override fun get(state: IBlockState, rand: Int) = variants[state]?.let { variantToValue[it[0]] } } \ No newline at end of file diff --git a/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt b/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt index 2bb3c70..bd637c8 100644 --- a/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt +++ b/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt @@ -7,22 +7,16 @@ import mods.betterfoliage.client.render.IColumnRegistry import mods.betterfoliage.client.render.IColumnTextureInfo import mods.betterfoliage.client.render.LogRegistry import mods.betterfoliage.client.render.StaticColumnInfo -import mods.betterfoliage.client.texture.LeafRegistry import mods.betterfoliage.client.texture.StandardLeafSupport import mods.betterfoliage.loader.Refs import mods.octarinecore.client.render.Quad import mods.octarinecore.client.render.ShadingContext import mods.octarinecore.client.render.blockContext -import mods.octarinecore.client.resource.ModelProcessor -import mods.octarinecore.client.resource.derivesFrom -import mods.octarinecore.client.resource.get -import mods.octarinecore.client.resource.modelBlockAndLoc +import mods.octarinecore.client.resource.* import mods.octarinecore.common.rotate import mods.octarinecore.metaprog.ClassRef import mods.octarinecore.metaprog.MethodRef import mods.octarinecore.metaprog.allAvailable -import mods.octarinecore.tryDefault -import net.minecraft.block.properties.PropertyDirection import net.minecraft.block.state.IBlockState import net.minecraft.client.renderer.block.model.ModelResourceLocation import net.minecraft.client.renderer.texture.TextureAtlasSprite @@ -106,36 +100,40 @@ data class RubberLogColumnInfo(override val axis: EnumFacing.Axis?, @SideOnly(Side.CLIENT) abstract class RubberLogSupportBase : ModelProcessor, IColumnRegistry { - override var stateToKey = mutableMapOf() - override var stateToValue = mapOf() + override var variants = mutableMapOf>() + override var variantToKey = mutableMapOf() + override var variantToValue = mapOf() override val logger = BetterFoliageMod.logDetail init { MinecraftForge.EVENT_BUS.register(this) } - override fun processStitch(state: IBlockState, key: RubberLogModelInfo, atlas: TextureMap): IColumnTextureInfo? { + override fun processStitch(variant: ModelVariant, key: RubberLogModelInfo, atlas: TextureMap): IColumnTextureInfo? { val topTex = atlas[key.textures[0]] ?: return null val bottomTex = atlas[key.textures[1]] ?: return null val sideTex = atlas[key.textures[2]] ?: return null if (key.spotDir == null) - return StaticColumnInfo(key.axis, topTex, bottomTex, sideTex) + return StaticColumnInfo(key.axis, topTex, bottomTex, listOf(sideTex)) else { val spotTex = atlas[key.textures[3]] ?: return null return RubberLogColumnInfo(key.axis, key.spotDir, topTex, bottomTex, sideTex, spotTex) } } - override fun get(state: IBlockState) = stateToValue[state] + override fun get(state: IBlockState, rand: Int): IColumnTextureInfo? { + val variant = getVariant(state, rand) ?: return null + return variantToValue[variant] + } } @SideOnly(Side.CLIENT) object IC2LogSupport : RubberLogSupportBase() { - override fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): RubberLogModelInfo? { + override fun processModelLoad1(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel) { // check for proper block class, existence of ModelBlock, and "state" blockstate property - if (!IC2Integration.BlockRubWood.isInstance(state.block)) return null - val blockLoc = model.modelBlockAndLoc.firstOrNull() ?: return null - val type = state.properties.entries.find { it.key.getName() == "state" }?.value?.toString() ?: return null + if (!IC2Integration.BlockRubWood.isInstance(state.block)) return + val blockLoc = model.modelBlockAndLoc.firstOrNull() ?: return + val type = state.properties.entries.find { it.key.getName() == "state" }?.value?.toString() ?: return // logs with no rubber spot if (blockLoc.derivesFrom(ResourceLocation("block/cube_column"))) { @@ -148,7 +146,10 @@ object IC2LogSupport : RubberLogSupportBase() { val textureNames = listOf("end", "end", "side").map { blockLoc.first.resolveTextureName(it) } logger.log(Level.DEBUG, "IC2LogSupport: block state ${state.toString()}") logger.log(Level.DEBUG, "IC2LogSupport: axis=$axis, end=${textureNames[0]}, side=${textureNames[2]}") - return if (textureNames.all { it != "missingno" }) RubberLogModelInfo(axis, null, textureNames) else null + if (textureNames.all { it != "missingno" }) { + putKeySingle(state, RubberLogModelInfo(axis, null, textureNames)) + } + return } // logs with rubber spot @@ -162,19 +163,21 @@ object IC2LogSupport : RubberLogSupportBase() { val textureNames = listOf("up", "down", "south", "north").map { blockLoc.first.resolveTextureName(it) } logger.log(Level.DEBUG, "IC2LogSupport: block state ${state.toString()}") logger.log(Level.DEBUG, "IC2LogSupport: spotDir=$spotDir, up=${textureNames[0]}, down=${textureNames[1]}, side=${textureNames[2]}, spot=${textureNames[3]}") - return if (textureNames.all { it != "missingno" }) RubberLogModelInfo(EnumFacing.Axis.Y, spotDir, textureNames) else null + if (textureNames.all { it != "missingno" }) { + putKeySingle(state, RubberLogModelInfo(EnumFacing.Axis.Y, spotDir, textureNames)) + } } } @SideOnly(Side.CLIENT) object TechRebornLogSupport : RubberLogSupportBase() { - override fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): RubberLogModelInfo? { + override fun processModelLoad1(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel) { // check for proper block class, existence of ModelBlock - if (!TechRebornIntegration.BlockRubberLog.isInstance(state.block)) return null + if (!TechRebornIntegration.BlockRubberLog.isInstance(state.block)) return - val hasSap = state.properties.entries.find { it.key.getName() == "hassap" }?.value as? Boolean ?: return null - val sapSide = state.properties.entries.find { it.key.getName() == "sapside" }?.value as? EnumFacing ?: return null + val hasSap = state.properties.entries.find { it.key.getName() == "hassap" }?.value as? Boolean ?: return + val sapSide = state.properties.entries.find { it.key.getName() == "sapside" }?.value as? EnumFacing ?: return logger.log(Level.DEBUG, "TechRebornLogSupport: block state ${state.toString()}") if (hasSap) { @@ -182,13 +185,17 @@ object TechRebornLogSupport : RubberLogSupportBase() { TechRebornIntegration.getTextureNameFromState.invoke(state.block, state, it) as String } logger.log(Level.DEBUG, "TechRebornLogSupport: spotDir=$sapSide, up=${textureNames[0]}, down=${textureNames[1]}, side=${textureNames[2]}, spot=${textureNames[3]}") - return if (textureNames.all { it != "missingno" }) RubberLogModelInfo(EnumFacing.Axis.Y, sapSide, textureNames) else null + if (textureNames.all { it != "missingno" }) { + putKeySingle(state, RubberLogModelInfo(EnumFacing.Axis.Y, sapSide, textureNames)) + } } else { val textureNames = listOf(EnumFacing.UP, EnumFacing.DOWN, sapSide).map { TechRebornIntegration.getTextureNameFromState.invoke(state.block, state, it) as String } logger.log(Level.DEBUG, "TechRebornLogSupport: up=${textureNames[0]}, down=${textureNames[1]}, side=${textureNames[2]}") - return if (textureNames.all { it != "missingno" }) RubberLogModelInfo(EnumFacing.Axis.Y, null, textureNames) else null + if (textureNames.all { it != "missingno" }) { + putKeySingle(state, RubberLogModelInfo(EnumFacing.Axis.Y, null, textureNames)) + } } } } @@ -198,21 +205,21 @@ object TechRebornLeafSupport : ModelProcessor { init { MinecraftForge.EVENT_BUS.register(this) } - override var stateToKey = mutableMapOf() - override var stateToValue = mapOf() + override var variants = mutableMapOf>() + override var variantToKey = mutableMapOf() + override var variantToValue = mapOf() override val logger: Logger get() = BetterFoliageMod.logDetail - override fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): Nothing? { + override fun processModelLoad1(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel) { if (Config.blocks.leavesClasses.matchesClass(state.block) && TechRebornIntegration.ITexturedBlock.isInstance(state.block)) { val textureName = TechRebornIntegration.getTextureNameFromState.invoke(state.block, state, EnumFacing.UP) as String logger.log(Level.DEBUG, "TechRebornLeafSupport: block state ${state.toString()}") logger.log(Level.DEBUG, "TechRebornLeafSupport: texture=$textureName") // register directly into StandardLeafSupport for the sake of simplicity - StandardLeafSupport.stateToKey[state] = listOf(textureName) + StandardLeafSupport.putKeySingle(state, listOf(textureName)) } - return null } // no-op - override fun processStitch(state: IBlockState, key: Nothing, atlas: TextureMap) = null + override fun processStitch(variant: ModelVariant, key: Nothing, atlas: TextureMap) = null } \ No newline at end of file diff --git a/src/main/kotlin/mods/betterfoliage/client/render/AbstractRenderColumn.kt b/src/main/kotlin/mods/betterfoliage/client/render/AbstractRenderColumn.kt index a1fb9f5..e3aff68 100644 --- a/src/main/kotlin/mods/betterfoliage/client/render/AbstractRenderColumn.kt +++ b/src/main/kotlin/mods/betterfoliage/client/render/AbstractRenderColumn.kt @@ -13,7 +13,6 @@ import net.minecraft.client.renderer.BlockRendererDispatcher import net.minecraft.client.renderer.VertexBuffer import net.minecraft.client.renderer.texture.TextureAtlasSprite import net.minecraft.util.BlockRenderLayer -import net.minecraft.util.EnumFacing import net.minecraft.util.EnumFacing.* import net.minecraftforge.fml.relauncher.Side import net.minecraftforge.fml.relauncher.SideOnly @@ -28,14 +27,18 @@ interface IColumnTextureInfo { @SideOnly(Side.CLIENT) interface IColumnRegistry { - operator fun get(state: IBlockState): IColumnTextureInfo? + operator fun get(state: IBlockState, rand: Int): IColumnTextureInfo? } @SideOnly(Side.CLIENT) data class StaticColumnInfo(override val axis: Axis?, val topTexture: TextureAtlasSprite, val bottomTexture: TextureAtlasSprite, - val sideTexture: TextureAtlasSprite) : IColumnTextureInfo { + val sideTextures: List) : IColumnTextureInfo { + + // index offsets for EnumFacings, to make it less likely for neighboring faces to get the same bark texture + val dirToIdx = arrayOf(0, 1, 2, 4, 3, 5) + override val top: QuadIconResolver = { ctx, _, _ -> OptifineCTM.override(topTexture, blockContext, UP.rotate(ctx.rotation)) } @@ -43,7 +46,9 @@ data class StaticColumnInfo(override val axis: Axis?, OptifineCTM.override(bottomTexture, blockContext, DOWN.rotate(ctx.rotation)) } override val side: QuadIconResolver = { ctx, idx, _ -> - OptifineCTM.override(sideTexture, blockContext, (if ((idx and 1) == 0) SOUTH else EAST).rotate(ctx.rotation)) + val worldFace = (if ((idx and 1) == 0) SOUTH else EAST).rotate(ctx.rotation) + val baseTexture = sideTextures[(blockContext.random(1) + dirToIdx[worldFace.ordinal]) % sideTextures.size] + OptifineCTM.override(baseTexture, blockContext, worldFace) } } @@ -141,7 +146,7 @@ abstract class AbstractRenderColumn(modId: String) : AbstractBlockRenderingHandl override fun render(ctx: BlockContext, dispatcher: BlockRendererDispatcher, renderer: VertexBuffer, layer: BlockRenderLayer): Boolean { if (ctx.isSurroundedBy(surroundPredicate) ) return false - val columnTextures = registry[ctx.blockState(Int3.zero)] + val columnTextures = registry[ctx.blockState(Int3.zero), ctx.random(0)] if (columnTextures == null) { Client.logRenderError(ctx.blockState(Int3.zero), ctx.pos) return renderWorldBlockBase(ctx, dispatcher, renderer, null) @@ -346,7 +351,7 @@ abstract class AbstractRenderColumn(modId: String) : AbstractBlockRenderingHandl return if (!blockPredicate(state)) { if (state.isOpaqueCube) SOLID else NONSOLID } else { - (registry[state]?.axis ?: if (Config.roundLogs.defaultY) Axis.Y else null)?.let { + (registry[state, random(0)]?.axis ?: if (Config.roundLogs.defaultY) Axis.Y else null)?.let { if (it == axis) PARALLEL else PERPENDICULAR } ?: SOLID } diff --git a/src/main/kotlin/mods/betterfoliage/client/render/EntityFallingLeavesFX.kt b/src/main/kotlin/mods/betterfoliage/client/render/EntityFallingLeavesFX.kt index a75602c..089568c 100644 --- a/src/main/kotlin/mods/betterfoliage/client/render/EntityFallingLeavesFX.kt +++ b/src/main/kotlin/mods/betterfoliage/client/render/EntityFallingLeavesFX.kt @@ -9,6 +9,7 @@ import mods.octarinecore.client.render.HSB import mods.octarinecore.common.Double3 import mods.octarinecore.minmax import mods.octarinecore.random +import mods.octarinecore.semiRandom import net.minecraft.client.Minecraft import net.minecraft.client.renderer.VertexBuffer import net.minecraft.util.EnumFacing.DOWN @@ -45,7 +46,7 @@ AbstractEntityFX(world, pos.x.toDouble() + 0.5, pos.y.toDouble(), pos.z.toDouble val state = world.getBlockState(pos) val blockColor = Minecraft.getMinecraft().blockColors.colorMultiplier(state, world, pos, 0) - val leafInfo = LeafRegistry.get(state, world, pos, DOWN) + val leafInfo = LeafRegistry.get(state, world, pos, DOWN, semiRandom(pos.x, pos.y, pos.z, 0)) if (leafInfo != null) { particleTexture = leafInfo.particleTextures?.get(rand.nextInt(1024)) calculateParticleColor(leafInfo.averageColor, blockColor) diff --git a/src/main/kotlin/mods/betterfoliage/client/render/RenderCactus.kt b/src/main/kotlin/mods/betterfoliage/client/render/RenderCactus.kt index 0dd00c2..81e60de 100644 --- a/src/main/kotlin/mods/betterfoliage/client/render/RenderCactus.kt +++ b/src/main/kotlin/mods/betterfoliage/client/render/RenderCactus.kt @@ -4,12 +4,11 @@ import mods.betterfoliage.BetterFoliageMod import mods.betterfoliage.client.Client import mods.betterfoliage.client.config.Config import mods.octarinecore.client.render.* +import mods.octarinecore.client.resource.ModelVariant import mods.octarinecore.client.resource.TextureListModelProcessor import mods.octarinecore.client.resource.get import mods.octarinecore.common.Int3 import mods.octarinecore.common.Rotation -import mods.octarinecore.common.config.ConfigurableBlockMatcher -import mods.octarinecore.common.config.ModelTextureList import mods.octarinecore.common.config.SimpleBlockMatcher import mods.octarinecore.common.config.modelTextures import net.minecraft.block.BlockCactus @@ -37,8 +36,9 @@ class RenderCactus : AbstractBlockRenderingHandler(BetterFoliageMod.MOD_ID) { init { MinecraftForge.EVENT_BUS.register(this) } - override var stateToKey = mutableMapOf>() - override var stateToValue = mapOf() + override var variants = mutableMapOf>() + override var variantToKey = mutableMapOf>() + override var variantToValue = mapOf() override val logger = BetterFoliageMod.logDetail override val logName = "CactusTextures" @@ -47,14 +47,17 @@ class RenderCactus : AbstractBlockRenderingHandler(BetterFoliageMod.MOD_ID) { modelTextures("block/cactus", "top", "bottom", "side") ) - override fun processStitch(state: IBlockState, key: List, atlas: TextureMap): IColumnTextureInfo? { + override fun processStitch(variant: ModelVariant, key: List, atlas: TextureMap): IColumnTextureInfo? { val topTex = atlas[key[0]] ?: return null val bottomTex = atlas[key[1]] ?: return null val sideTex = atlas[key[2]] ?: return null - return StaticColumnInfo(Axis.Y, topTex, bottomTex, sideTex) + return StaticColumnInfo(Axis.Y, topTex, bottomTex, listOf(sideTex)) } - override fun get(state: IBlockState) = stateToValue[state] + override fun get(state: IBlockState, rand: Int): IColumnTextureInfo? { + val variant = getVariant(state, rand) ?: return null + return variantToValue[variant] + } } val modelStem = model { @@ -100,7 +103,7 @@ class RenderCactus : AbstractBlockRenderingHandler(BetterFoliageMod.MOD_ID) { // get AO data modelRenderer.updateShading(Int3.zero, allFaces) - val icons = cactusTextures[ctx.blockState(Int3.zero)] ?: return renderWorldBlockBase(ctx, dispatcher, renderer, null) + val icons = cactusTextures[ctx.blockState(Int3.zero), ctx.random(0)] ?: return renderWorldBlockBase(ctx, dispatcher, renderer, null) modelRenderer.render( renderer, diff --git a/src/main/kotlin/mods/betterfoliage/client/render/RenderLog.kt b/src/main/kotlin/mods/betterfoliage/client/render/RenderLog.kt index bda3dc8..6095a00 100644 --- a/src/main/kotlin/mods/betterfoliage/client/render/RenderLog.kt +++ b/src/main/kotlin/mods/betterfoliage/client/render/RenderLog.kt @@ -2,27 +2,21 @@ package mods.betterfoliage.client.render import mods.betterfoliage.BetterFoliageMod import mods.betterfoliage.client.config.Config -import mods.betterfoliage.client.texture.GrassRegistry -import mods.betterfoliage.client.texture.IGrassRegistry -import mods.betterfoliage.client.texture.StandardGrassSupport import mods.octarinecore.client.render.BlockContext +import mods.octarinecore.client.resource.ModelVariant import mods.octarinecore.client.resource.TextureListModelProcessor import mods.octarinecore.client.resource.get -import mods.octarinecore.common.Int3 import mods.octarinecore.common.config.ConfigurableBlockMatcher import mods.octarinecore.common.config.ModelTextureList import mods.octarinecore.findFirst import mods.octarinecore.tryDefault import net.minecraft.block.BlockLog import net.minecraft.block.state.IBlockState -import net.minecraft.client.renderer.texture.TextureAtlasSprite import net.minecraft.client.renderer.texture.TextureMap import net.minecraft.util.EnumFacing.Axis import net.minecraftforge.common.MinecraftForge import net.minecraftforge.fml.relauncher.Side import net.minecraftforge.fml.relauncher.SideOnly -import org.apache.logging.log4j.Level -import org.apache.logging.log4j.Logger class RenderLog : AbstractRenderColumn(BetterFoliageMod.MOD_ID) { @@ -50,7 +44,7 @@ class RenderLog : AbstractRenderColumn(BetterFoliageMod.MOD_ID) { @SideOnly(Side.CLIENT) object LogRegistry : IColumnRegistry { val subRegistries: MutableList = mutableListOf() - override fun get(state: IBlockState) = subRegistries.findFirst { it[state] } + override fun get(state: IBlockState, rand: Int) = subRegistries.findFirst { it[state, rand] } } @SideOnly(Side.CLIENT) @@ -61,22 +55,27 @@ object StandardLogSupport : TextureListModelProcessor, IColu MinecraftForge.EVENT_BUS.register(this) } - override var stateToKey = mutableMapOf>() - override var stateToValue = mapOf() + override var variants = mutableMapOf>() + override var variantToKey = mutableMapOf>() + override var variantToValue = mapOf() override val logger = BetterFoliageMod.logDetail override val logName = "StandardLogSupport" override val matchClasses: ConfigurableBlockMatcher get() = Config.blocks.logClasses override val modelTextures: List get() = Config.blocks.logModels.list - override fun processStitch(state: IBlockState, key: List, atlas: TextureMap): IColumnTextureInfo? { + override fun processStitch(variant: ModelVariant, key: List, atlas: TextureMap): IColumnTextureInfo? { val topTex = atlas[key[0]] ?: return null val bottomTex = atlas[key[1]] ?: return null - val sideTex = atlas[key[2]] ?: return null - return StaticColumnInfo(getAxis(state), topTex, bottomTex, sideTex) + val sideTexList = key.drop(2).map { atlas[it] }.filterNotNull() + if (sideTexList.isEmpty()) return null + return StaticColumnInfo(getAxis(variant.state), topTex, bottomTex, sideTexList) } - override fun get(state: IBlockState) = stateToValue[state] + override fun get(state: IBlockState, rand: Int): IColumnTextureInfo? { + val variant = getVariant(state, rand) ?: return null + return variantToValue[variant] + } fun getAxis(state: IBlockState): Axis? { val axis = tryDefault(null) { state.getValue(BlockLog.LOG_AXIS).toString() } ?: diff --git a/src/main/kotlin/mods/betterfoliage/client/texture/GrassRegistry.kt b/src/main/kotlin/mods/betterfoliage/client/texture/GrassRegistry.kt index 509e809..c6fd49c 100644 --- a/src/main/kotlin/mods/betterfoliage/client/texture/GrassRegistry.kt +++ b/src/main/kotlin/mods/betterfoliage/client/texture/GrassRegistry.kt @@ -5,10 +5,7 @@ import mods.betterfoliage.client.config.Config import mods.betterfoliage.client.integration.OptifineCTM import mods.octarinecore.client.render.BlockContext import mods.octarinecore.client.render.HSB -import mods.octarinecore.client.resource.TextureListModelProcessor -import mods.octarinecore.client.resource.TextureMediatedRegistry -import mods.octarinecore.client.resource.averageColor -import mods.octarinecore.client.resource.get +import mods.octarinecore.client.resource.* import mods.octarinecore.common.Int3 import mods.octarinecore.common.config.ConfigurableBlockMatcher import mods.octarinecore.common.config.ModelTextureList @@ -42,8 +39,8 @@ class GrassInfo( ) interface IGrassRegistry { - fun get(state: IBlockState): GrassInfo? - fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing): GrassInfo? + operator fun get(state: IBlockState, rand: Int): GrassInfo? + operator fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing, rand: Int): GrassInfo? } /** Collects and manages rendering-related information for grass blocks. */ @@ -51,12 +48,12 @@ interface IGrassRegistry { object GrassRegistry : IGrassRegistry { val subRegistries: MutableList = mutableListOf(StandardGrassSupport) - override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing) = - subRegistries.findFirst { it.get(state, world, pos, face) } + override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing, rand: Int) = + subRegistries.findFirst { it.get(state, world, pos, face, rand) } - operator fun get(ctx: BlockContext, face: EnumFacing) = get(ctx.blockState(Int3.zero), ctx.world!!, ctx.pos, face) + operator fun get(ctx: BlockContext, face: EnumFacing) = get(ctx.blockState(Int3.zero), ctx.world!!, ctx.pos, face, ctx.random(0)) - override fun get(state: IBlockState) = subRegistries.findFirst { it.get(state) } + override fun get(state: IBlockState, rand: Int) = subRegistries.findFirst { it[state, rand] } } object StandardGrassSupport : @@ -66,8 +63,9 @@ object StandardGrassSupport : { init { MinecraftForge.EVENT_BUS.register(this) } - override var stateToKey = mutableMapOf>() - override var stateToValue = mapOf() + override var variants = mutableMapOf>() + override var variantToKey = mutableMapOf>() + override var variantToValue = mapOf() override var textureToValue = mutableMapOf() override val logger = BetterFoliageMod.logDetail @@ -75,20 +73,22 @@ object StandardGrassSupport : override val matchClasses: ConfigurableBlockMatcher get() = Config.blocks.grassClasses override val modelTextures: List get() = Config.blocks.grassModels.list - override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing): GrassInfo? { - val baseTexture = stateToValue[state] ?: return null + override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing, rand: Int): GrassInfo? { + val variant = getVariant(state, rand) ?: return null + val baseTexture = variantToValue[variant] ?: return null return textureToValue[OptifineCTM.override(baseTexture, world, pos, face)] ?: textureToValue[baseTexture] } - override fun get(state: IBlockState) = StandardLeafSupport.stateToValue[state].let { - if (it == null) null else textureToValue[it] + override fun get(state: IBlockState, rand: Int): GrassInfo? { + val variant = getVariant(state, rand) ?: return null + return variantToValue[variant].let { if (it == null) null else textureToValue[it] } } - override fun processStitch(state: IBlockState, key: List, atlas: TextureMap) = atlas[key[0]] + override fun processStitch(variant: ModelVariant, key: List, atlas: TextureMap) = atlas[key[0]] - override fun processTexture(states: List, texture: TextureAtlasSprite, atlas: TextureMap) { + override fun processTexture(variants: List, texture: TextureAtlasSprite, atlas: TextureMap) { registerGrass(texture, atlas) - OptifineCTM.getAllCTM(states, texture).forEach { + OptifineCTM.getAllCTM(variants.map { it.state }, texture).forEach { registerGrass(it, atlas) } } diff --git a/src/main/kotlin/mods/betterfoliage/client/texture/LeafRegistry.kt b/src/main/kotlin/mods/betterfoliage/client/texture/LeafRegistry.kt index 55512d1..d81b5cd 100644 --- a/src/main/kotlin/mods/betterfoliage/client/texture/LeafRegistry.kt +++ b/src/main/kotlin/mods/betterfoliage/client/texture/LeafRegistry.kt @@ -44,8 +44,8 @@ class LeafInfo( } interface ILeafRegistry { - operator fun get(state: IBlockState): LeafInfo? - operator fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing): LeafInfo? + operator fun get(state: IBlockState, rand: Int): LeafInfo? + operator fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing, rand: Int): LeafInfo? } /** Collects and manages rendering-related information for grass blocks. */ @@ -62,12 +62,12 @@ object LeafRegistry : ILeafRegistry { typeMappings.loadMappings(ResourceLocation(BetterFoliageMod.DOMAIN, "leafTextureMappings.cfg")) } - override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing) = - subRegistries.findFirst { it.get(state, world, pos, face) } + override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing, rand: Int) = + subRegistries.findFirst { it.get(state, world, pos, face, rand) } - operator fun get(ctx: BlockContext, face: EnumFacing) = get(ctx.blockState(Int3.zero), ctx.world!!, ctx.pos, face) + operator fun get(ctx: BlockContext, face: EnumFacing) = get(ctx.blockState(Int3.zero), ctx.world!!, ctx.pos, face, ctx.random(0)) - override fun get(state: IBlockState) = subRegistries.findFirst { it.get(state) } + override fun get(state: IBlockState, rand: Int) = subRegistries.findFirst { it[state, rand] } fun getParticleType(texture: TextureAtlasSprite, atlas: TextureMap): String { var leafType = typeMappings.getType(texture) ?: "default" @@ -100,26 +100,30 @@ object StandardLeafSupport : override val modelTextures: List get() = Config.blocks.leavesModels.list override val logger: Logger? get() = BetterFoliageMod.logDetail - override var stateToKey = mutableMapOf>() - override var stateToValue = mapOf() + override var variants = mutableMapOf>() + override var variantToKey = mutableMapOf>() + override var variantToValue = mapOf() override var textureToValue = mutableMapOf() - override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing): LeafInfo? { - val baseTexture = stateToValue[state] ?: return null + override fun get(state: IBlockState, world: IBlockAccess, pos: BlockPos, face: EnumFacing, rand: Int): LeafInfo? { + val variant = getVariant(state, rand) ?: return null + val baseTexture = variantToValue[variant] ?: return null return textureToValue[OptifineCTM.override(baseTexture, world, pos, face)] ?: textureToValue[baseTexture] } - override fun get(state: IBlockState) = stateToValue[state].let { - if (it == null) null else textureToValue[it] + override fun get(state: IBlockState, rand: Int): LeafInfo? { + val variant = getVariant(state, rand) ?: return null + return variantToValue[variant].let { if (it == null) null else textureToValue[it] } } - override fun processStitch(state: IBlockState, key: List, atlas: TextureMap) = atlas[key[0]] + override fun processStitch(variant: ModelVariant, key: List, atlas: TextureMap) = atlas[key[0]] - override fun processTexture(states: List, texture: TextureAtlasSprite, atlas: TextureMap) { - logger?.log(Level.DEBUG, "$logName: leaf texture ${texture.iconName}") - logger?.log(Level.DEBUG, "$logName: #states ${states.size}") + override fun processTexture(variants: List, texture: TextureAtlasSprite, atlas: TextureMap) { + logger?.log(Level.DEBUG, "$logName: leaf texture ${texture.iconName}") + logger?.log(Level.DEBUG, "$logName: #variants ${variants.size}") + logger?.log(Level.DEBUG, "$logName: #states ${variants.distinctBy { it.state }.size}") registerLeaf(texture, atlas) - OptifineCTM.getAllCTM(states, texture).forEach { + OptifineCTM.getAllCTM(variants.map { it.state }, texture).forEach { logger?.log(Level.DEBUG, "$logName: CTM ${texture.iconName}") registerLeaf(it, atlas) } diff --git a/src/main/kotlin/mods/octarinecore/Utils.kt b/src/main/kotlin/mods/octarinecore/Utils.kt index dfef9a8..19e2a34 100644 --- a/src/main/kotlin/mods/octarinecore/Utils.kt +++ b/src/main/kotlin/mods/octarinecore/Utils.kt @@ -73,6 +73,11 @@ fun tryDefault(default: T, work: ()->T) = try { work() } catch (e: Throwable /** Return a random [Double] value between the given two limits (inclusive min, exclusive max). */ fun random(min: Double, max: Double) = Math.random().let { min + (max - min) * it } +fun semiRandom(x: Int, y: Int, z: Int, seed: Int): Int { + var value = (x * x + y * y + z * z + x * y + y * z + z * x + (seed * seed)) and 63 + value = (3 * x * value + 5 * y * value + 7 * z * value + (11 * seed)) and 63 + return value +} /** * Return this [Double] value if it lies between the two limits. If outside, return the * minimum/maximum value correspondingly. diff --git a/src/main/kotlin/mods/octarinecore/client/render/AbstractBlockRenderingHandler.kt b/src/main/kotlin/mods/octarinecore/client/render/AbstractBlockRenderingHandler.kt index 6dacb77..cded593 100644 --- a/src/main/kotlin/mods/octarinecore/client/render/AbstractBlockRenderingHandler.kt +++ b/src/main/kotlin/mods/octarinecore/client/render/AbstractBlockRenderingHandler.kt @@ -10,6 +10,7 @@ import mods.octarinecore.common.Double3 import mods.octarinecore.common.Int3 import mods.octarinecore.common.forgeDirOffsets import mods.octarinecore.common.plus +import mods.octarinecore.semiRandom import net.minecraft.block.Block import net.minecraft.block.state.IBlockState import net.minecraft.client.Minecraft @@ -105,11 +106,7 @@ class BlockContext { fun isSurroundedBy(predicate: (IBlockState)->Boolean) = forgeDirOffsets.all { predicate(blockState(it)) } /** Get a semi-random value based on the block coordinate and the given seed. */ - fun random(seed: Int): Int { - var value = (pos.x * pos.x + pos.y * pos.y + pos.z * pos.z + pos.x * pos.y + pos.y * pos.z + pos.z * pos.x + (seed * seed)) and 63 - value = (3 * pos.x * value + 5 * pos.y * value + 7 * pos.z * value + (11 * seed)) and 63 - return value - } + fun random(seed: Int) = semiRandom(pos.x, pos.y, pos.z, seed) /** Get an array of semi-random values based on the block coordinate. */ fun semiRandomArray(num: Int): Array = Array(num) { random(it) } diff --git a/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt b/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt index 5527f09..53e2949 100644 --- a/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt +++ b/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt @@ -2,17 +2,18 @@ package mods.octarinecore.client.resource import com.google.common.base.Joiner import mods.betterfoliage.loader.Refs -import mods.octarinecore.common.config.ConfigurableBlockMatcher import mods.octarinecore.common.config.IBlockMatcher import mods.octarinecore.common.config.ModelTextureList import mods.octarinecore.filterValuesNotNull import net.minecraft.block.Block import net.minecraft.block.state.IBlockState +import net.minecraft.client.renderer.block.model.ModelBlock import net.minecraft.client.renderer.block.model.ModelResourceLocation import net.minecraft.client.renderer.block.statemap.DefaultStateMapper import net.minecraft.client.renderer.block.statemap.IStateMapper import net.minecraft.client.renderer.texture.TextureAtlasSprite import net.minecraft.client.renderer.texture.TextureMap +import net.minecraft.util.ResourceLocation import net.minecraftforge.client.event.TextureStitchEvent import net.minecraftforge.client.model.IModel import net.minecraftforge.client.model.ModelLoader @@ -24,20 +25,38 @@ import org.apache.logging.log4j.Logger class LoadModelDataEvent(val loader: ModelLoader) : Event() +data class ModelVariant( + val state: IBlockState, + var modelBlock: ModelBlock?, + val modelLocation: ResourceLocation?, + val weight: Int +) + interface ModelProcessor { val logger: Logger? - var stateToKey: MutableMap - var stateToValue: Map + var variants: MutableMap> + var variantToKey: MutableMap + var variantToValue: Map + + fun addVariant(state: IBlockState, variant: ModelVariant) { variants.getOrPut(state) { mutableListOf() }.add(variant) } + fun getVariant(state: IBlockState, rand: Int) = variants[state]?.let { it[rand % it.size] } + fun putKeySingle(state: IBlockState, key: T1) { + val variant = ModelVariant(state, null, null, 1) + variants[state] = mutableListOf(variant) + variantToKey[variant] = key + } fun onPostLoad() { } fun onPreStitch() { } - fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): T1? - fun processStitch(state: IBlockState, key: T1, atlas: TextureMap): T2? + fun processModelLoad1(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel) + + fun processStitch(variant: ModelVariant, key: T1, atlas: TextureMap): T2? @SubscribeEvent(priority = EventPriority.HIGHEST) fun clearBeforeLoadModelData(event: LoadModelDataEvent) { - stateToKey.clear() + variants.clear() + variantToKey.clear() } @SubscribeEvent @@ -52,7 +71,7 @@ interface ModelProcessor { stateMappings.forEach { mapping -> if (mapping.key.block != null) stateModels[mapping.value]?.let { model -> - processModelLoad(mapping.key, mapping.value, model)?.let { key -> stateToKey.put(mapping.key, key) } + processModelLoad1(mapping.key, mapping.value, model) } } } @@ -61,7 +80,7 @@ interface ModelProcessor { @SubscribeEvent(priority = EventPriority.LOW) fun handlePreStitch(event: TextureStitchEvent.Pre) { onPreStitch() - stateToValue = stateToKey.mapValues { processStitch(it.key, it.value, event.map) }.filterValuesNotNull() + variantToValue = variantToKey.mapValues { processStitch(it.key, it.value, event.map) }.filterValuesNotNull() } } @@ -70,30 +89,60 @@ interface TextureListModelProcessor : ModelProcessor, T2> { val matchClasses: IBlockMatcher val modelTextures: List - override fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): List? { - val matchClass = matchClasses.matchingClass(state.block) ?: return null + override fun processModelLoad1(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel) { + val matchClass = matchClasses.matchingClass(state.block) ?: return logger?.log(Level.DEBUG, "$logName: block state ${state.toString()}") logger?.log(Level.DEBUG, "$logName: class ${state.block.javaClass.name} matches ${matchClass.name}") - val allModels = model.modelBlockAndLoc + val allModels = model.modelBlockAndLoc.distinctBy { it.second } if (allModels.isEmpty()) { logger?.log(Level.DEBUG, "$logName: no models found") - return null + return } + allModels.forEach { blockLoc -> - modelTextures.firstOrNull { blockLoc.derivesFrom(it.modelLocation) }?.let{ modelMatch -> - logger?.log(Level.DEBUG, "$logName: model ${blockLoc.second} matches ${modelMatch.modelLocation.toString()}") + val modelMatch = modelTextures.firstOrNull { blockLoc.derivesFrom(it.modelLocation) } + if (modelMatch != null) { + logger?.log(Level.DEBUG, "$logName: model ${blockLoc.second} matches ${modelMatch.modelLocation}") val textures = modelMatch.textureNames.map { it to blockLoc.first.resolveTextureName(it) } val texMapString = Joiner.on(", ").join(textures.map { "${it.first}=${it.second}" }) logger?.log(Level.DEBUG, "$logName: textures [$texMapString]") - return if (textures.all { it.second != "missingno" }) textures.map { it.second } else null + if (textures.all { it.second != "missingno" }) { + // found a valid variant (all required textures exist) + val variant = ModelVariant(state, blockLoc.first, blockLoc.second, 1) + addVariant(state, variant) + variantToKey[variant] = textures.map { it.second } + } } } - logger?.log(Level.DEBUG, "$logName: no matching models found") - return null } + +// override fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): List? { +// val matchClass = matchClasses.matchingClass(state.block) ?: return null +// logger?.log(Level.DEBUG, "$logName: block state ${state.toString()}") +// logger?.log(Level.DEBUG, "$logName: class ${state.block.javaClass.name} matches ${matchClass.name}") +// +// val allModels = model.modelBlockAndLoc +// if (allModels.isEmpty()) { +// logger?.log(Level.DEBUG, "$logName: no models found") +// return null +// } +// allModels.forEach { blockLoc -> +// modelTextures.firstOrNull { blockLoc.derivesFrom(it.modelLocation) }?.let{ modelMatch -> +// logger?.log(Level.DEBUG, "$logName: model ${blockLoc.second} matches ${modelMatch.modelLocation.toString()}") +// +// val textures = modelMatch.textureNames.map { it to blockLoc.first.resolveTextureName(it) } +// val texMapString = Joiner.on(", ").join(textures.map { "${it.first}=${it.second}" }) +// logger?.log(Level.DEBUG, "$logName: textures [$texMapString]") +// +// return if (textures.all { it.second != "missingno" }) textures.map { it.second } else null +// } +// } +// logger?.log(Level.DEBUG, "$logName: no matching models found") +// return null +// } } interface TextureMediatedRegistry : ModelProcessor { @@ -105,9 +154,9 @@ interface TextureMediatedRegistry : ModelProcessor, texture: TextureAtlasSprite, atlas: TextureMap) + fun processTexture(states: List, texture: TextureAtlasSprite, atlas: TextureMap) } \ No newline at end of file