Skip to content

Commit

Permalink
better fuel tank rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-s168 committed Aug 3, 2024
1 parent db1b1ec commit 3a2283d
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.valkyrienskies.tournament.mixin.client;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import org.valkyrienskies.tournament.util.block.WithExRenderInfo;

@Mixin(Block.class)
public class MixinBlockClient {
@Inject(at = @At("HEAD"), method = "shouldRenderFace", cancellable = true)
private static void shouldRenderFace(BlockState state, BlockGetter level, BlockPos offset, Direction face, BlockPos pos, CallbackInfoReturnable<Boolean> cir) {
var blockState = level.getBlockState(pos);
if (blockState.getBlock() instanceof WithExRenderInfo blockEx) {
var info = blockEx.getFaceRenderType(blockState, level, pos, face);

if (info == WithExRenderInfo.FaceRenderType.FORCE_RENDER) {
cir.setReturnValue(true);
return;
}

if (info == WithExRenderInfo.FaceRenderType.FORCE_NOT_RENDER) {
cir.setReturnValue(false);
return;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.valkyrienskies.tournament.util.block;

import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.block.state.BlockState;

public interface WithExRenderInfo {
FaceRenderType getFaceRenderType(BlockState state, BlockGetter level, BlockPos pos, Direction face);

enum FaceRenderType {
NORMAL,
FORCE_RENDER,
FORCE_NOT_RENDER,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ object TournamentModels {
}

data class Model(
val resourceLocation: ResourceLocation
val resourceLocation: ResourceLocation,
val checkSides: Boolean = true,
val useAO: Boolean = false,
) {
val bakedModel: BakedModel by lazy {
getModel(resourceLocation)
Expand All @@ -53,14 +55,16 @@ object TournamentModels {
) {
val level = blockEntity.level ?: return

Minecraft.getInstance().blockRenderer.modelRenderer.tesselateWithoutAO(
val modRend = Minecraft.getInstance().blockRenderer.modelRenderer
val fn = if (useAO) modRend::tesselateWithAO else modRend::tesselateWithoutAO
fn(
level,
bakedModel,
blockEntity.blockState,
blockEntity.blockPos,
matrixStack,
bufferSource.getBuffer(RenderType.cutout()),
true,
checkSides,
level.random,
42L, // Used in ModelBlockRenderer.class in renderModel, not sure what the right number is but this seems to work
packedOverlay
Expand All @@ -69,18 +73,21 @@ object TournamentModels {
}
}

private fun model(name: String): Model {
private fun model(name: String, checkSides: Boolean = true, useAO: Boolean = false): Model {
val rl = ResourceLocation(TournamentMod.MOD_ID, name)

MODELS += rl

return Model(rl)
return Model(rl, checkSides, useAO)
}

val PROP_BIG = model("block/prop_big_prop")
val PROP_SMALL = model("block/prop_small_prop")
val SOLID_FUEL = model("block/solid_fuel")
val ROTATOR_ROTARY = model("block/rotator_rotary")
val FUEL_TANK_FULL_TRANSPARENT = model("block/fuel_tank_full_transparent")
val FUEL_TANK_FULL_TRANSPARENT = model(
"block/fuel_tank_full_transparent",
useAO = true
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@ import blitz.collections.remove
import net.minecraft.core.BlockPos
import net.minecraft.resources.ResourceLocation
import org.valkyrienskies.core.api.ships.properties.ShipId
import org.valkyrienskies.core.impl.game.ships.ShipObjectServerWorld
import org.valkyrienskies.core.impl.networking.simple.SimplePacket
import org.valkyrienskies.core.impl.networking.simple.register
import org.valkyrienskies.core.impl.networking.simple.registerClientHandler
import org.valkyrienskies.core.impl.networking.simple.sendToAllClients
import org.valkyrienskies.mod.common.vsCore
import org.valkyrienskies.tournament.ship.TournamentShips
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

object TournamentNetworking {
@OptIn(ExperimentalContracts::class)
private fun <R: Any> runIfServer(fn: () -> R): R? {
contract {
callsInPlace(fn, InvocationKind.AT_MOST_ONCE)
}

return if (vsCore.dummyShipWorldServer is ShipObjectServerWorld) {
fn()
} else null
}

data class ShipFuelTypeChange(
val ship: ShipId,
val fuel: String?,
Expand All @@ -27,10 +43,12 @@ object TournamentNetworking {
fuelKey()?.let(TournamentFuelManager.fuels::get)

fun send() {
// TODO after vs update
// with(vsCore.simplePacketNetworking) {
this.sendToAllClients()
// }
runIfServer {
// TODO after vs update
// with(vsCore.simplePacketNetworking) {
this.sendToAllClients()
// }
}
}
}

Expand All @@ -45,10 +63,12 @@ object TournamentNetworking {
throttle < 0.0f

fun send() {
// TODO after vs update
// with(vsCore.simplePacketNetworking) {
this.sendToAllClients()
// }
runIfServer {
// TODO after vs update
// with(vsCore.simplePacketNetworking) {
this.sendToAllClients()
// }
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.valkyrienskies.tournament.blockentity

import blitz.caching
import net.minecraft.core.BlockPos
import net.minecraft.core.Direction
import net.minecraft.nbt.CompoundTag
Expand All @@ -17,6 +18,7 @@ import org.valkyrienskies.tournament.TournamentConfig
import org.valkyrienskies.tournament.ship.TournamentShips
import org.valkyrienskies.tournament.tournamentFuel
import org.valkyrienskies.tournament.util.extension.void
import java.util.BitSet
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract
Expand All @@ -27,16 +29,14 @@ import kotlin.math.min
class FuelTankBlockEntity(
pos: BlockPos,
state: BlockState,
val capf: Float,
var capf: Float,
src: () -> BlockEntityType<FuelTankBlockEntity>
): BlockEntity(
src(),
pos,
state
) {

// TODO: do using custom networking instead

@Volatile
var wholeShipFillLevelSynced = 0.0f
private set
Expand All @@ -51,15 +51,21 @@ class FuelTankBlockEntity(

override fun saveAdditional(tag: CompoundTag) {
tag.putFloat("ship_fill_synced", wholeShipFillLevelSynced)
tag.putByteArray("neighbors", neighborsTransparent.toByteArray())
super.saveAdditional(tag)
}

override fun load(tag: CompoundTag) {
wholeShipFillLevelSynced = tag.getFloat("ship_fill_synced")

neighborsTransparent = if (tag.contains("neighbors"))
BitSet.valueOf(tag.getByteArray("neighbors"))
else BitSet(6)

super.load(tag)
}

private fun update() {
fun update() {
level?.sendBlockUpdated(blockPos, blockState, blockState, Block.UPDATE_ALL_IMMEDIATE)
}

Expand All @@ -70,7 +76,9 @@ class FuelTankBlockEntity(
}
}

val cap = ceil(TournamentConfig.SERVER.fuelContainerCap * capf).toInt()
val cap by caching(::capf) {
ceil(TournamentConfig.SERVER.fuelContainerCap * capf).toInt()
}

@OptIn(ExperimentalContracts::class)
fun <R> ship(fn: (TournamentShips) -> R): R? {
Expand All @@ -92,6 +100,15 @@ class FuelTankBlockEntity(
}
}

fun updateCapf(new: Float) {
val old = cap
capf = new
val diff = cap - old
ship {
it.fuelCap += diff
}
}

fun getContainer(): CustomContainer {
return CustomContainer(this)
}
Expand All @@ -111,6 +128,11 @@ class FuelTankBlockEntity(
it.fuelCount += count
}.void()

var neighborsTransparent = BitSet(6)

fun isNeighborTransparent(direction: Direction) =
neighborsTransparent[direction.ordinal]

class CustomContainer(
val be: FuelTankBlockEntity
): WorldlyContainer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.mojang.blaze3d.systems.RenderSystem
import com.mojang.blaze3d.vertex.PoseStack
import net.minecraft.client.renderer.MultiBufferSource
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer
import net.minecraft.core.Direction
import org.valkyrienskies.tournament.TournamentModels
import org.valkyrienskies.tournament.blockentity.FuelTankBlockEntity
import org.valkyrienskies.tournament.util.extension.pose
Expand All @@ -20,12 +21,34 @@ class TransparentFuelTankBlockEntityRender:
packedLight: Int,
packedOverlay: Int
) {
// TODO: back faces of fuel tank not visible
RenderSystem.disableCull()

if (be.wholeShipFillLevelSynced > 0.05f) {
val byDir = List(Direction.entries.size) { index ->
if (be.neighborsTransparent[index]) {
0.0
} else {
0.01
}
}

val x = byDir[Direction.WEST.ordinal]
val y = byDir[Direction.DOWN.ordinal]
val z = byDir[Direction.NORTH.ordinal]

val nx = byDir[Direction.EAST.ordinal]
val ny = byDir[Direction.UP.ordinal]
val nz = byDir[Direction.SOUTH.ordinal]

pose.pose {
translate(0.1, 0.1, 0.1)
scale(0.8f, 0.8f * be.wholeShipFillLevelSynced, 0.8f)
translate(x, y, z)

scale(
1.0f - (x + nx).toFloat(),
(1.0f - (y + ny).toFloat()) * be.wholeShipFillLevelSynced,
1.0f - (z + nz).toFloat()
)

TournamentModels.SOLID_FUEL.renderer.render(
pose,
be,
Expand All @@ -43,6 +66,8 @@ class TransparentFuelTankBlockEntityRender:
packedLight,
packedOverlay
)

RenderSystem.enableCull()
}

}
Loading

0 comments on commit 3a2283d

Please sign in to comment.