Skip to content

Commit

Permalink
wip: create support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rushmead committed Aug 4, 2024
1 parent 40aa711 commit ede5ad9
Show file tree
Hide file tree
Showing 23 changed files with 783 additions and 185 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.imabad.theatrical;

import dev.architectury.injectables.annotations.ExpectPlatform;
import dev.imabad.theatrical.net.compat.create.SendBEDataToContraption;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.entity.BlockEntityType;
Expand All @@ -24,4 +25,9 @@ public static String getModVersion() {
throw new AssertionError();
}

@ExpectPlatform
public static void handleBEDataForContraption(SendBEDataToContraption packet){
throw new AssertionError();
}

}
3 changes: 3 additions & 0 deletions common/src/main/java/dev/imabad/theatrical/api/Fixture.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,7 @@ public boolean hasBeam(){
}
public abstract float[] getTransforms(BlockState fixtureBlockState, BlockState supportBlockState);
public abstract List<DMXPersonality> getDMXPersonalities();
public boolean isUpsideDown(BlockState blockState) {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,6 @@ public interface DMXConsumer extends BelongsToNetwork {
int getActivePersonality();

UUID getNetworkId();

void setStartAddress(int startAddress);
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void update(byte[] data) {
if(level != null && level.getServer() != null) {
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if(dmxData != null) {
dmxData.getConsumersInRange(universe, getBlockPos(), TheatricalConfig.INSTANCE.COMMON.wirelessDMXRadius).forEach(dmxConsumer -> dmxConsumer.consume(data));
// dmxData.getConsumersInRange(universe, getBlockPos(), TheatricalConfig.INSTANCE.COMMON.wirelessDMXRadius).forEach(dmxConsumer -> dmxConsumer.consume(data));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ public UUID getNetworkId() {
return networkId;
}

@Override
public void setStartAddress(int startAddress) {
setChannelStartPoint(startAddress);
}

public void setNetworkId(UUID networkId) {
if(networkId == this.networkId){
return;
Expand Down Expand Up @@ -178,14 +183,14 @@ private void addConsumer(){
if(deviceId == null){
generateDeviceId();
}
dmxData.addConsumer(getBlockPos(), this);
dmxData.addConsumer(this);
}
}

private void removeConsumer(){
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
dmxData.removeConsumer(this, getBlockPos());
dmxData.removeConsumer(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ public void setChannelStartPoint(int channelStartPoint) {
setChanged();
level.sendBlockUpdated(getBlockPos(), getBlockState(), getBlockState(), Block.UPDATE_CLIENTS);
}

@Override
public void setStartAddress(int startAddress) {
setChannelStartPoint(startAddress);
}

private void updateConsumer(){
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
Expand All @@ -125,7 +131,7 @@ private void updateConsumer(){
private void removeConsumer(){
var dmxData = DMXNetworkData.getInstance(level.getServer().overworld()).getNetwork(networkId);
if (dmxData != null) {
dmxData.removeConsumer(this, getBlockPos());
dmxData.removeConsumer(this);
}
}
private void addConsumer(){
Expand All @@ -134,7 +140,7 @@ private void addConsumer(){
if(deviceId == null){
generateDeviceId();
}
dmxData.addConsumer(getBlockPos(), this);
dmxData.addConsumer(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public boolean emitsLight() {
}

@Override
public boolean isUpsideDown() {
public boolean isUpsideDown(){
return false;
}

Expand Down Expand Up @@ -182,6 +182,32 @@ public static <T extends BlockEntity> void tick(Level level, BlockPos pos, Block
// }
}

public static <T extends BlockEntity> void tickInContraption(Level level, T be, Vec3 position) {
BaseLightBlockEntity tile = (BaseLightBlockEntity) be;
// if(!level.isClientSide){
tile.tickTimer++;
if(tile.tickTimer >= 5){
// if(tile.storePrev()){
// level.sendBlockUpdated(pos, state, state, Block.UPDATE_CLIENTS);
// }

tile.tickTimer = 0;
}
if(tile.shouldTrace()){
tile.distance = tile.doRayTrace(position);
}
if (level.isClientSide() && LightManager.shouldUpdateDynamicLight()) {
if (tile.isRemoved()) {
tile.setDynamicLightEnabled(false);
} else {
tile.dynamicLightTick();
LightManager.updateTracking(tile);
}
}
// } else {
// }
}

public int getPan() {
return pan;
}
Expand Down Expand Up @@ -242,10 +268,10 @@ public int getPrevColor(){
return (getPrevRed() << 16) | (getPrevGreen() << 8) | getPrevBlue();
}

public Optional<BlockState> getSupportingStructure(){
if(getLevel() != null){
BlockState blockState = getLevel().getBlockState(getBlockPos()
.relative(getBlockState().getValue(HangableBlock.HANG_DIRECTION)));
public static Optional<BlockState> getSupportingStructure(Level level, BlockPos pos, BlockState state){
if(level != null){
BlockState blockState = level.getBlockState(pos
.relative(state.getValue(HangableBlock.HANG_DIRECTION)));
if(blockState.getBlock() instanceof Support) {
return Optional.of(blockState);
}
Expand All @@ -272,6 +298,14 @@ public int calculatePartialColour(float partialTicks){
return (r << 16) | (g << 8) | b;
}

public static int calculatePartialColour(int prevRed, int prevGreen, int prevBlue, int red, int green, int blue, float partialTicks){
int r = (int) (prevRed + ((red) - prevRed) * partialTicks);
int g = (int) (prevGreen + ((green) - prevGreen) * partialTicks);
int b = (int) (prevBlue + ((blue) - prevBlue) * partialTicks);
return (r << 16) | (g << 8) | b;
}


public static final Vec3 calculateViewVector(float xRot, float yRot) {
float f = xRot * 0.017453292F;
float g = -yRot * 0.017453292F;
Expand Down Expand Up @@ -337,17 +371,20 @@ public static Vec3 rayTraceDir(BaseLightBlockEntity be){
}
}

public double doRayTrace() {
public double doRayTrace(){
return doRayTrace(getBlockPos().getCenter());
}

public double doRayTrace(Vec3 position) {
Vec3 viewVector = BaseLightBlockEntity.rayTraceDir(this);
double distance = getMaxLightDistance();
Vec3 vec3 = getBlockPos().getCenter();
Vec3 vec33 = vec3.add(viewVector.x * distance, viewVector.y * distance, viewVector.z * distance);
ClipContext context = new ClipContext(vec3, vec33, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, null);
Vec3 vec33 = position.add(viewVector.x * distance, viewVector.y * distance, viewVector.z * distance);
ClipContext context = new ClipContext(position, vec33, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, null);
((ClipContextAccessor) context).setCollisionContext(new LightCollisionContext(getBlockPos()));
BlockHitResult result = this.level.clip(context);
BlockPos lightPos = result.getBlockPos();
if (result.getType() != HitResult.Type.MISS && !result.isInside()) {
distance = result.getLocation().distanceTo(vec3);
distance = result.getLocation().distanceTo(position);
if (!result.getBlockPos().equals(getBlockPos())) {
lightPos = result.getBlockPos().relative(result.getDirection(), 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import dev.imabad.theatrical.api.Fixture;
import dev.imabad.theatrical.blockentities.light.BaseLightBlockEntity;
import dev.imabad.theatrical.blocks.HangableBlock;
import dev.imabad.theatrical.blocks.light.MovingLightBlock;
Expand All @@ -10,19 +11,31 @@
import dev.imabad.theatrical.config.TheatricalConfig;
import net.minecraft.client.Camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.LevelRenderer;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.joml.Matrix3f;
import org.joml.Matrix4f;

import java.util.Optional;

public abstract class FixtureRenderer<T extends BaseLightBlockEntity> implements BlockEntityRenderer<T> {

public record FixtureRenderContext(Fixture fixtureType, Direction facing, boolean isFlipped, boolean isHanging,
int prevPan, int pan, int prevTilt, int tilt,
Optional<BlockState> supportingStructure, float intensity, float prevIntensity,
int prevRed, int red, int prevGreen, int green, int prevBlue, int blue,
BlockPos pos, int focus, double distance){}

private final Double beamOpacity = TheatricalConfig.INSTANCE.CLIENT.beamOpacity;

public FixtureRenderer(BlockEntityRendererProvider.Context context) {
Expand All @@ -35,21 +48,28 @@ public void render(T blockEntity, float partialTick, PoseStack poseStack, MultiB
boolean isFlipped = blockEntity.isUpsideDown();
boolean isHanging = ((HangableBlock) blockState.getBlock()).isHanging(blockEntity.getLevel(), blockEntity.getBlockPos());
Direction facing = blockState.getValue(MovingLightBlock.FACING);
renderModel(blockEntity, poseStack, vertexConsumer, facing, partialTick, isFlipped, blockState, isHanging, packedLight, packedOverlay);
beforeRenderBeam(blockEntity, poseStack, vertexConsumer, multiBufferSource, facing, partialTick, isFlipped, blockState, isHanging, packedLight, packedOverlay);
if(shouldRenderBeam(blockEntity)){
FixtureRenderContext fixtureRenderContext = new FixtureRenderContext(blockEntity.getFixture(), facing, isFlipped, isHanging, blockEntity.getPrevPan(),
blockEntity.getPan(), blockEntity.getPrevTilt(), blockEntity.getTilt(), BaseLightBlockEntity
.getSupportingStructure(blockEntity.getLevel(), blockEntity.getBlockPos(), blockState), blockEntity.getIntensity(),
blockEntity.getPrevIntensity(), blockEntity.getPrevRed(), blockEntity.getRed(), blockEntity.getPrevGreen(), blockEntity.getGreen(),
blockEntity.getPrevBlue(), blockEntity.getBlue(), blockEntity.getBlockPos(), blockEntity.getFocus(), blockEntity.getDistance());
renderModel(fixtureRenderContext, poseStack, vertexConsumer, partialTick, blockState, packedLight, packedOverlay);
beforeRenderBeam(fixtureRenderContext, poseStack, vertexConsumer, multiBufferSource, partialTick, blockState, packedLight, packedOverlay);
VertexConsumer linesB = multiBufferSource.getBuffer(RenderType.lines());
LevelRenderer.renderLineBox(poseStack, linesB, AABB.ofSize(new Vec3(0, 0, 0), .1d, .1d, .1d), 1, 0, 1, 1);
if(!shouldRenderBeam(fixtureRenderContext)){
LazyRenderers.addLazyRender(new LazyRenderers.LazyRenderer() {
@Override
public void render(MultiBufferSource.BufferSource bufferSource, PoseStack poseStack, Camera camera, float partialTick) {
poseStack.pushPose();
Vec3 offset = Vec3.atLowerCornerOf(blockEntity.getBlockPos()).subtract(camera.getPosition());
poseStack.translate(offset.x, offset.y, offset.z);
preparePoseStack(blockEntity, poseStack, facing, partialTick, isFlipped, blockState, isHanging);
preparePoseStack(fixtureRenderContext, poseStack, partialTick, blockState);
VertexConsumer beamConsumer = bufferSource.getBuffer(TheatricalRenderTypes.BEAM);
poseStack.translate(blockEntity.getFixture().getBeamStartPosition()[0], blockEntity.getFixture().getBeamStartPosition()[1], blockEntity.getFixture().getBeamStartPosition()[2]);
float intensity = (blockEntity.getPrevIntensity() + ((blockEntity.getIntensity()) - blockEntity.getPrevIntensity()) * partialTick);
int color = blockEntity.calculatePartialColour(partialTick);
renderLightBeam(beamConsumer, poseStack, blockEntity, partialTick, (float) ((intensity * beamOpacity) / 255f), blockEntity.getFixture().getBeamWidth(), (float) blockEntity.getDistance(), color);
renderLightBeam(beamConsumer, poseStack, fixtureRenderContext, partialTick, (float) ((intensity * beamOpacity) / 255f), blockEntity.getFixture().getBeamWidth(), (float) blockEntity.getDistance(), color);
poseStack.popPose();
}

Expand All @@ -62,31 +82,30 @@ public Vec3 getPos(float partialTick) {
poseStack.popPose();
}

public abstract void renderModel(T blockEntity, PoseStack poseStack, VertexConsumer vertexConsumer, Direction facing, float partialTicks, boolean isFlipped, BlockState blockState, boolean isHanging, int packedLight, int packedOverlay);
public abstract void renderModel(FixtureRenderContext fixtureRenderContext, PoseStack poseStack, VertexConsumer vertexConsumer, float partialTicks, BlockState blockState, int packedLight, int packedOverlay);

public abstract void preparePoseStack(T blockEntity, PoseStack poseStack, Direction facing, float partialTicks, boolean isFlipped, BlockState blockState, boolean isHanging);
public abstract void preparePoseStack(FixtureRenderContext fixtureRenderContext, PoseStack poseStack, float partialTicks, BlockState blockState);

public void beforeRenderBeam(T blockEntity, PoseStack poseStack, VertexConsumer vertexConsumer,
MultiBufferSource multiBufferSource, Direction facing, float partialTicks,
boolean isFlipped, BlockState blockstate, boolean isHanging, int packedLight,
public void beforeRenderBeam(FixtureRenderContext fixtureRenderContext, PoseStack poseStack, VertexConsumer vertexConsumer,
MultiBufferSource multiBufferSource, float partialTicks, BlockState blockstate, int packedLight,
int packedOverlay) {}

public boolean shouldRenderBeam(T blockEntity){
return blockEntity.getIntensity() > 0 && blockEntity.getFixture().hasBeam();
public static boolean shouldRenderBeam(FixtureRenderContext context){
return context.intensity() > 0 && context.fixtureType().hasBeam();
}

protected void minecraftRenderModel(PoseStack poseStack, VertexConsumer vertexConsumer, BlockState blockState, BakedModel model, int packedLight, int packedOverlay){
protected static void minecraftRenderModel(PoseStack poseStack, VertexConsumer vertexConsumer, BlockState blockState, BakedModel model, int packedLight, int packedOverlay){
Minecraft.getInstance().getBlockRenderer().getModelRenderer().renderModel(poseStack.last(), vertexConsumer, blockState, model, 1, 1, 1, packedLight, packedOverlay);
}

protected void renderLightBeam(VertexConsumer builder, PoseStack stack, T tileEntityFixture, float partialTicks, float alpha, float beamSize, float length, int color) {
public static void renderLightBeam(VertexConsumer builder, PoseStack stack, FixtureRenderContext context, float partialTicks, float alpha, float beamSize, float length, int color) {
int r = (color >> 16) & 0xFF;
int g = (color >> 8) & 0xFF;
int b = color & 0xFF;
int a = (int) (alpha * 255);
Matrix4f m = stack.last().pose();
Matrix3f normal = stack.last().normal();
float endMultiplier = beamSize * tileEntityFixture.getFocus();
float endMultiplier = beamSize * context.focus();
addVertex(builder, m, normal, r, g, b, 0, beamSize * endMultiplier, beamSize * endMultiplier, -length);
addVertex(builder, m, normal, r, g, b, a, beamSize, beamSize, 0);
addVertex(builder, m, normal, r, g, b, a, beamSize, -beamSize, 0);
Expand All @@ -108,7 +127,7 @@ protected void renderLightBeam(VertexConsumer builder, PoseStack stack, T tileEn
addVertex(builder, m, normal, r, g, b, 0, -beamSize * endMultiplier, -beamSize * endMultiplier, -length);
}

protected void addVertex(VertexConsumer builder, Matrix4f matrix4f, Matrix3f matrix3f, int r, int g, int b, int a, float x, float y, float z) {
protected static void addVertex(VertexConsumer builder, Matrix4f matrix4f, Matrix3f matrix3f, int r, int g, int b, int a, float x, float y, float z) {
builder.vertex(matrix4f, x, y, z).color(r, g, b, a).endVertex();
}

Expand Down
Loading

0 comments on commit ede5ad9

Please sign in to comment.