рендер шизы

(или жидкостях в танках-предметах)
#258
This commit is contained in:
YuRaNnNzZZ 2023-07-25 05:58:32 +03:00
parent 28801252fa
commit e518f2c732
Signed by: YuRaNnNzZZ
GPG Key ID: 5F71738C85A6006D
4 changed files with 148 additions and 52 deletions

View File

@ -32,7 +32,6 @@ fun addItemModels(provider: MatteryItemModelProvider) {
provider.block(MItems.PHANTOM_ATTRACTOR) provider.block(MItems.PHANTOM_ATTRACTOR)
provider.block(MItems.HOLO_SIGN) provider.block(MItems.HOLO_SIGN)
provider.generated(MItems.FLUID_CAPSULE) provider.generated(MItems.FLUID_CAPSULE)
provider.block(MItems.FLUID_TANK)
MRegistry.VENT.allItems.values.forEach(provider::block) MRegistry.VENT.allItems.values.forEach(provider::block)
MRegistry.VENT_ALTERNATIVE.allItems.values.forEach(provider::block) MRegistry.VENT_ALTERNATIVE.allItems.values.forEach(provider::block)

View File

@ -5,35 +5,85 @@ import com.mojang.math.Axis
import net.minecraft.client.renderer.* import net.minecraft.client.renderer.*
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer import net.minecraft.client.renderer.blockentity.BlockEntityRenderer
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider
import net.minecraft.client.renderer.entity.ItemRenderer
import net.minecraft.world.inventory.InventoryMenu import net.minecraft.world.inventory.InventoryMenu
import net.minecraft.world.item.BlockItem
import net.minecraft.world.item.ItemDisplayContext
import net.minecraft.world.item.ItemStack
import net.minecraft.world.level.block.HalfTransparentBlock
import net.minecraft.world.level.block.StainedGlassPaneBlock
import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions import net.minecraftforge.client.extensions.common.IClientFluidTypeExtensions
import net.minecraftforge.common.capabilities.ForgeCapabilities
import net.minecraftforge.fluids.FluidStack
import net.minecraftforge.fluids.capability.IFluidHandler
import ru.dbotthepony.mc.otm.block.entity.decorative.FluidTankBlockEntity import ru.dbotthepony.mc.otm.block.entity.decorative.FluidTankBlockEntity
import ru.dbotthepony.mc.otm.client.minecraft import ru.dbotthepony.mc.otm.client.minecraft
import ru.dbotthepony.mc.otm.core.ifPresentK
import ru.dbotthepony.mc.otm.core.math.RGBAColor import ru.dbotthepony.mc.otm.core.math.RGBAColor
import ru.dbotthepony.mc.otm.core.math.linearInterpolation import ru.dbotthepony.mc.otm.core.math.linearInterpolation
import ru.dbotthepony.mc.otm.registry.MBlocks
class FluidTankRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer<FluidTankBlockEntity> { class FluidTankRenderer(private val context: BlockEntityRendererProvider.Context) : BlockEntityRenderer<FluidTankBlockEntity> {
private val fluidTop = 13f / 16f
private val fluidBottom = 3f / 16f
private val fluidHeight = 10f / 16f
private val fluidWidth = 15.8f / 16f
private val fluidPadding = (1f - fluidWidth) * .5f
override fun render( override fun render(
tile: FluidTankBlockEntity, tile: FluidTankBlockEntity,
partialTick: Float, partialTick: Float,
poseStack: PoseStack, poseStack: PoseStack,
bufferSource: MultiBufferSource, bufferSource: MultiBufferSource,
packedLight: Int, packedLight: Int,
overlayLight: Int packedOverlay: Int
) { ) {
val fluid = tile.synchronizedFluid renderFluidInTank(tile.fluid, tile.synchronizedFluid, poseStack, bufferSource, packedLight, packedOverlay)
}
object FluidTankItemRenderer : BlockEntityWithoutLevelRenderer(minecraft.blockEntityRenderDispatcher, minecraft.entityModels) {
override fun renderByItem(
stack: ItemStack,
displayContext: ItemDisplayContext,
poseStack: PoseStack,
bufferSource: MultiBufferSource,
packedLight: Int,
packedOverlay: Int
) {
val model = minecraft.modelManager.blockModelShaper.getBlockModel(MBlocks.FLUID_TANK.defaultBlockState())
stack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).ifPresentK {
renderFluidInTank(it, it.getFluidInTank(0), poseStack, bufferSource, packedLight, packedOverlay)
}
val hasFoil = stack.hasFoil()
val fabulous = displayContext == ItemDisplayContext.GUI
|| displayContext.firstPerson()
|| (stack.item is BlockItem && ((stack.item as BlockItem).block is HalfTransparentBlock || (stack.item as BlockItem).block is StainedGlassPaneBlock))
for (renderPass in model.getRenderPasses(stack, fabulous)) {
for (renderType in renderPass.getRenderTypes(stack, fabulous)) {
val buffer = if (fabulous) ItemRenderer.getFoilBufferDirect(bufferSource, renderType, true, hasFoil) else ItemRenderer.getFoilBuffer(bufferSource, renderType, true, hasFoil)
minecraft.itemRenderer.renderModelLists(renderPass, stack, packedLight, packedOverlay, poseStack, buffer)
}
}
}
}
private companion object {
val fluidTop = 13f / 16f
val fluidBottom = 3f / 16f
val fluidHeight = 10f / 16f
val fluidWidth = 15.8f / 16f
val fluidPadding = (1f - fluidWidth) * .5f
fun renderFluidInTank(
fluidHandler: IFluidHandler,
fluid: FluidStack,
poseStack: PoseStack,
bufferSource: MultiBufferSource,
packedLight: Int,
packedOverlay: Int
) {
if (fluid.isEmpty) if (fluid.isEmpty)
return return
val fluidLevel = fluid.amount.toFloat() / tile.fluid.getTankCapacity(0).toFloat() val fluidLevel = fluid.amount.toFloat() / fluidHandler.getTankCapacity(0).toFloat()
if (fluidLevel < 0.01f) if (fluidLevel < 0.01f)
return return
@ -47,8 +97,6 @@ class FluidTankRenderer(private val context: BlockEntityRendererProvider.Context
val interp = linearInterpolation(fluidLevel, sprite.v1, sprite.v0) val interp = linearInterpolation(fluidLevel, sprite.v1, sprite.v0)
val v1 = linearInterpolation(fluidBottom, sprite.v1, sprite.v0) val v1 = linearInterpolation(fluidBottom, sprite.v1, sprite.v0)
// val lightLevel = fluid.fluid.fluidType.getLightLevel(fluid) // 0-15
val builder = bufferSource.getBuffer(Sheets.translucentCullBlockSheet()) val builder = bufferSource.getBuffer(Sheets.translucentCullBlockSheet())
poseStack.pushPose() poseStack.pushPose()
@ -63,10 +111,10 @@ class FluidTankRenderer(private val context: BlockEntityRendererProvider.Context
val matrix = poseStack.last().pose() val matrix = poseStack.last().pose()
builder.vertex(matrix, fluidPadding, 0f, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, v1).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding, 0f, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, v1).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding + fluidWidth, 0f, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, v1).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding + fluidWidth, 0f, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, v1).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding + fluidWidth, fluidLevel * fluidHeight, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, interp).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding + fluidWidth, fluidLevel * fluidHeight, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, interp).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding, fluidLevel * fluidHeight, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, interp).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding, fluidLevel * fluidHeight, 0f).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, interp).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
poseStack.popPose() poseStack.popPose()
} }
@ -74,19 +122,20 @@ class FluidTankRenderer(private val context: BlockEntityRendererProvider.Context
val matrix = poseStack.last().pose() val matrix = poseStack.last().pose()
if (!gas && fluidLevel < 1f) { // top if (!gas && fluidLevel < 1f) { // top
builder.vertex(matrix, fluidPadding, fluidLevel * fluidHeight, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v1).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding, fluidLevel * fluidHeight, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v1).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding + fluidWidth, fluidLevel * fluidHeight, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v1).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding + fluidWidth, fluidLevel * fluidHeight, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v1).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding + fluidWidth, fluidLevel * fluidHeight, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v0).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding + fluidWidth, fluidLevel * fluidHeight, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v0).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding, fluidLevel * fluidHeight, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v0).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding, fluidLevel * fluidHeight, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v0).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
} }
if (gas && fluidLevel < 1f) { // bottom if (gas && fluidLevel < 1f) { // bottom
builder.vertex(matrix, fluidPadding, 0f, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v0).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding, 0f, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v0).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding + fluidWidth, 0f, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v0).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding + fluidWidth, 0f, fluidPadding).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v0).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding + fluidWidth, 0f, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v1).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding + fluidWidth, 0f, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u1, sprite.v1).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
builder.vertex(matrix, fluidPadding, 0f, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v1).overlayCoords(overlayLight).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex() builder.vertex(matrix, fluidPadding, 0f, fluidPadding + fluidWidth).color(tint.red, tint.green, tint.blue, tint.alpha).uv(sprite.u0, sprite.v1).overlayCoords(packedOverlay).uv2(packedLight).normal(0.0f, 1.0f, 0.0f).endVertex()
} }
poseStack.popPose() poseStack.popPose()
} }
}
} }

View File

@ -1,5 +1,6 @@
package ru.dbotthepony.mc.otm.item package ru.dbotthepony.mc.otm.item
import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer
import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.CompoundTag
import net.minecraft.network.chat.Component import net.minecraft.network.chat.Component
import net.minecraft.world.InteractionResult import net.minecraft.world.InteractionResult
@ -8,14 +9,17 @@ import net.minecraft.world.item.ItemStack
import net.minecraft.world.item.TooltipFlag import net.minecraft.world.item.TooltipFlag
import net.minecraft.world.item.context.UseOnContext import net.minecraft.world.item.context.UseOnContext
import net.minecraft.world.level.Level import net.minecraft.world.level.Level
import net.minecraftforge.client.extensions.common.IClientItemExtensions
import net.minecraftforge.common.capabilities.ForgeCapabilities import net.minecraftforge.common.capabilities.ForgeCapabilities
import net.minecraftforge.common.capabilities.ICapabilityProvider import net.minecraftforge.common.capabilities.ICapabilityProvider
import ru.dbotthepony.mc.otm.block.decorative.FluidTankBlock import ru.dbotthepony.mc.otm.block.decorative.FluidTankBlock
import ru.dbotthepony.mc.otm.block.entity.decorative.FluidTankBlockEntity import ru.dbotthepony.mc.otm.block.entity.decorative.FluidTankBlockEntity
import ru.dbotthepony.mc.otm.capability.fluid.BlockMatteryFluidHandler import ru.dbotthepony.mc.otm.capability.fluid.BlockMatteryFluidHandler
import ru.dbotthepony.mc.otm.capability.fluidLevel import ru.dbotthepony.mc.otm.capability.fluidLevel
import ru.dbotthepony.mc.otm.client.render.blockentity.FluidTankRenderer
import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.ifPresentK import ru.dbotthepony.mc.otm.core.ifPresentK
import java.util.function.Consumer
import java.util.function.IntSupplier import java.util.function.IntSupplier
class FluidTankItem(block: FluidTankBlock, properties: Properties, val capacity: IntSupplier) : BlockItem(block, properties) { class FluidTankItem(block: FluidTankBlock, properties: Properties, val capacity: IntSupplier) : BlockItem(block, properties) {
@ -54,4 +58,14 @@ class FluidTankItem(block: FluidTankBlock, properties: Properties, val capacity:
it.fluidLevel(pTooltip) it.fluidLevel(pTooltip)
} }
} }
override fun initializeClient(consumer: Consumer<IClientItemExtensions>) {
super.initializeClient(consumer)
consumer.accept(object : IClientItemExtensions {
override fun getCustomRenderer(): BlockEntityWithoutLevelRenderer {
return FluidTankRenderer.FluidTankItemRenderer
}
})
}
} }

View File

@ -0,0 +1,34 @@
{
"parent": "minecraft:builtin/entity",
"display": {
"thirdperson_righthand": {
"rotation": [75, 45, 2],
"translation": [0, 2.75, 0],
"scale": [0.375, 0.375, 0.375]
},
"thirdperson_lefthand": {
"rotation": [75, 45, 0],
"translation": [0, 2.5, 0],
"scale": [0.375, 0.375, 0.375]
},
"firstperson_righthand": {
"rotation": [0, 45, 0],
"scale": [0.4, 0.4, 0.4]
},
"firstperson_lefthand": {
"rotation": [0, 225, 0],
"scale": [0.4, 0.4, 0.4]
},
"ground": {
"translation": [0, 3, 0],
"scale": [0.25, 0.25, 0.25]
},
"gui": {
"rotation": [30, 225, 0],
"scale": [0.625, 0.625, 0.625]
},
"fixed": {
"scale": [0.5, 0.5, 0.5]
}
}
}