Lasers Render Correctly
All checks were successful
EnergonRelics/pipeline/head This commit looks good

This commit is contained in:
TheBrokenRail 2020-07-16 14:50:18 -04:00
parent 4983948e3b
commit 5cc87d537c
9 changed files with 53 additions and 246 deletions

View File

@ -13,47 +13,38 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.block.entity.BlockEntityRenderer;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.sound.BlockSoundGroup;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.DirectionProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.BlockMirror;
import net.minecraft.util.BlockRotation;
import net.minecraft.util.math.Direction;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.BlockView;
import java.util.function.Function;
@SuppressWarnings("deprecation")
public class DefensiveLaserBlock extends EnergyProviderBlock {
public static final BooleanProperty POWERED = Properties.POWERED;
public static final DirectionProperty FACING = Properties.FACING;
public DefensiveLaserBlock() {
super(FabricBlockSettings.of(Material.GLASS).sounds(BlockSoundGroup.GLASS).nonOpaque().lightLevel(state -> state.get(POWERED) ? 15 : 0).strength(0.3f));
setDefaultState(getDefaultState().with(POWERED, false).with(FACING, Direction.NORTH));
super(FabricBlockSettings.of(Material.GLASS).sounds(BlockSoundGroup.GLASS).nonOpaque().strength(0.3f).allowsSpawning((state, world, pos, type) -> false).solidBlock((state, world, pos) -> false).suffocates((state, world, pos) -> false));
setDefaultState(getDefaultState().with(POWERED, false));
}
@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(POWERED);
builder.add(FACING);
}
@Override
public BlockState rotate(BlockState state, BlockRotation rotation) {
return state.with(FACING, rotation.rotate(state.get(FACING)));
@SuppressWarnings("deprecation")
@Environment(EnvType.CLIENT)
public float getAmbientOcclusionLightLevel(BlockState state, BlockView world, BlockPos pos) {
return 1.0F;
}
@Override
public BlockState mirror(BlockState state, BlockMirror mirror) {
return state.rotate(mirror.getRotation(state.get(FACING)));
}
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
return getDefaultState().with(FACING, ctx.getSide().getOpposite());
public boolean isTranslucent(BlockState state, BlockView world, BlockPos pos) {
return true;
}
@Override

View File

@ -5,6 +5,7 @@ import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.DefensiveLaserBlock;
import com.thebrokenrail.energonrelics.energy.core.EnergyReceiverBlockEntity;
import com.thebrokenrail.energonrelics.energy.core.util.Action;
import com.thebrokenrail.energonrelics.util.MissingCaseException;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.entity.LivingEntity;
@ -24,8 +25,8 @@ import java.util.Optional;
import java.util.function.Predicate;
public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
private static final float MAX_PITCH = 60;
private static final float MIN_PITCH = -20;
private static final float MAX_PITCH = 0;
private static final float MIN_PITCH = -70;
private static final float ROTATION_INCREMENT = 2;
@ -59,86 +60,6 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
}
}
private interface SwapFunction {
class SwapData {
private final float yaw;
private final float pitch;
public SwapData(float yaw, float pitch) {
this.yaw = yaw;
this.pitch = pitch;
}
}
Vec3d swap(SwapData data);
SwapData line(Vec3d data);
static SwapFunction upDown(boolean up) {
return new SwapFunction() {
@Override
public Vec3d swap(SwapData data) {
return new Vec3d(-data.yaw, data.pitch, 0);
}
private SwapData reverseSwap(Vec3d data) {
return new SwapData((float) -data.getX(), (float) data.getY());
}
@Override
public SwapData line(Vec3d data) {
SwapData reverse = reverseSwap(data);
return new SwapData(-reverse.yaw, reverse.pitch);
}
};
}
static SwapFunction northSouth(boolean north) {
return new SwapFunction() {
@Override
public Vec3d swap(SwapData data) {
if (north) {
return new Vec3d(0, data.pitch, data.yaw);
} else {
return new Vec3d(0, -data.pitch, data.yaw);
}
}
private SwapData reverseSwap(Vec3d data) {
if (north) {
return new SwapData((float) data.getZ(), (float) data.getY());
} else {
return new SwapData((float) data.getZ(), (float) -data.getY());
}
}
@Override
public SwapData line(Vec3d data) {
SwapData reverse = reverseSwap(data);
if (north) {
return new SwapData(reverse.pitch + 90, reverse.yaw + 90);
} else {
return new SwapData(reverse.pitch - 180, reverse.yaw - 90);
}
}
};
}
SwapFunction SWAP_PITCH_ROLL = new SwapFunction() {
@Override
public Vec3d swap(SwapData data) {
return new Vec3d(data.yaw, data.pitch, 0);
}
private SwapData reverseSwap(Vec3d data) {
return new SwapData((float) data.getX(), (float) data.getY());
}
@Override
public SwapData line(Vec3d data) {
SwapData reverse = reverseSwap(data);
return new SwapData(-reverse.pitch, reverse.yaw);
}
};
}
public class Rotation {
private float yaw = 0;
private float pitch = 0;
@ -161,11 +82,6 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
tag.put(key, rotation);
}
private void change(float yaw, float pitch) {
setRaw(this.yaw + yaw, this.pitch + pitch);
markDirty();
}
private void setRaw(float yaw, float pitch) {
setYaw(yaw);
setPitch(pitch);
@ -176,82 +92,25 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
}
private void setPitch(float pitch) {
this.pitch = Math.max(MIN_PITCH, Math.min(MAX_PITCH, pitch % 360));
this.pitch = pitch % 360;
}
public float getYaw(boolean display) {
return (float) getBaseVector(display).getX();
public float getYaw() {
return yaw;
}
public float getPitch(boolean display) {
return (float) getBaseVector(display).getY();
public float getPitch() {
return pitch;
}
public float getRoll(boolean display) {
return (float) getBaseVector(display).getZ();
}
private Vec3d getBaseModifier() {
Direction facing = getCachedState().get(DefensiveLaserBlock.FACING);
switch (facing) {
case DOWN: {
return new Vec3d(0, 0, 0);
}
case UP: {
return new Vec3d(0, 180, 0);
}
case NORTH: {
return new Vec3d(0, 0, 90);
}
case SOUTH: {
return new Vec3d(180, 90, 90);
}
case EAST: {
return new Vec3d(180, 0, 90);
}
case WEST: {
return new Vec3d(0, 0, -90);
}
default: {
throw new UnsupportedOperationException();
}
}
}
public SwapFunction getSwapFunction() {
Direction facing = getCachedState().get(DefensiveLaserBlock.FACING);
switch (facing) {
case DOWN: {
return SwapFunction.upDown(false);
}
case UP: {
return SwapFunction.upDown(true);
}
case EAST:
case WEST: {
return SwapFunction.SWAP_PITCH_ROLL;
}
case NORTH: {
return SwapFunction.northSouth(true);
}
case SOUTH: {
return SwapFunction.northSouth(false);
}
default: {
throw new UnsupportedOperationException();
}
}
}
private Vec3d getBaseVector(boolean display) {
Vec3d modifier = getBaseModifier();
return getSwapFunction().swap(new SwapFunction.SwapData(yaw, -(pitch - (display ? 45 : 0)))).add(modifier);
private void change(float diffYaw, float diffPitch) {
setRaw(yaw + diffYaw, pitch + diffPitch);
markDirty();
}
private Vec3d getRayVector() {
SwapFunction.SwapData data = getSwapFunction().line(getBaseVector(false));
float pitch = data.pitch * (float) DEG2RAD;
float yaw = data.yaw * (float) DEG2RAD;
float pitch = this.pitch * (float) DEG2RAD;
float yaw = this.yaw * (float) DEG2RAD;
float cosYaw = MathHelper.cos(yaw);
float sinYaw = MathHelper.sin(yaw);
float cosPitch = MathHelper.cos(pitch);
@ -265,11 +124,8 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
}
private void target(float targetYaw, float targetPitch) {
SwapFunction function = getSwapFunction();
SwapFunction.SwapData data = function.line(getBaseVector(false));
Vec3d vec = new Vec3d(targetYaw, targetPitch, 0);
float yawChange = getAngleChange(data.yaw, (float) vec.getX());
float pitchChange = getAngleChange(data.pitch, (float) vec.getY());
float yawChange = getAngleChange(yaw, targetYaw);
float pitchChange = getAngleChange(pitch, targetPitch);
change(yawChange, pitchChange);
}
}
@ -303,7 +159,6 @@ public class DefensiveLaserBlockEntity extends EnergyReceiverBlockEntity {
Vec3d vec = rotation.getRayVector().add(getPosVec());
((ServerWorld) getWorld()).spawnParticles(ParticleTypes.END_ROD, vec.getX(), vec.getY(), vec.getZ(), 1, 0, 0, 0, 0);
addAction(Action.createBlockStatePropertyAction(Config.DEFENSIVE_LASER_IDLE_ENERGY_REQUIRED, DefensiveLaserBlock.POWERED, true, false));
rotation.target(45, 45);
if (getCachedState().get(DefensiveLaserBlock.POWERED)) {
if (countdown > 0) {
countdown--;

View File

@ -1,14 +1,18 @@
package com.thebrokenrail.energonrelics.client;
import com.thebrokenrail.energonrelics.EnergonRelics;
import com.thebrokenrail.energonrelics.block.util.EnergyProviderBlock;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.blockrenderlayer.v1.BlockRenderLayerMap;
import net.minecraft.client.render.RenderLayer;
@Environment(EnvType.CLIENT)
public class EnergonRelicsClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
EnergyProviderBlock.initRenderer();
BlockRenderLayerMap.INSTANCE.putBlock(EnergonRelics.DEFENSIVE_LASER_BLOCK, RenderLayer.getCutout());
}
}

View File

@ -6,6 +6,8 @@ import net.minecraft.block.entity.BlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.block.entity.BlockEntityRenderDispatcher;
import net.minecraft.client.render.entity.feature.HeadFeatureRenderer;
import net.minecraft.client.render.entity.feature.ShulkerHeadFeatureRenderer;
import net.minecraft.client.render.model.json.ModelTransformation;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.util.math.Vector3f;
@ -18,28 +20,27 @@ public class DefensiveLaserBlockEntityRenderer extends HighlightBlockEntityRende
@Override
public void render(BlockEntity entity, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay) {
super.render(entity, tickDelta, matrices, vertexConsumers, light, overlay);
matrices.push();
matrices.translate(0.5d, 0.5d, 0.5d);
matrices.scale(0.6f, 0.6f, 0.6f);
if (entity instanceof DefensiveLaserBlockEntity) {
DefensiveLaserBlockEntity laser = (DefensiveLaserBlockEntity) entity;
float roll = (float) (laser.rotation.getRoll(true) * DefensiveLaserBlockEntity.DEG2RAD);
if (roll != 0) {
matrices.multiply(Vector3f.POSITIVE_Z.getRadialQuaternion(roll));
}
float yaw = (float) (laser.rotation.getYaw(true) * DefensiveLaserBlockEntity.DEG2RAD);
float yaw = (float) ((laser.rotation.getYaw()) * DefensiveLaserBlockEntity.DEG2RAD);
if (yaw != 0) {
matrices.multiply(Vector3f.POSITIVE_Y.getRadialQuaternion(yaw));
}
float pitch = (float) (laser.rotation.getPitch(true) * DefensiveLaserBlockEntity.DEG2RAD);
float pitch = (float) ((laser.rotation.getPitch() + 45) * DefensiveLaserBlockEntity.DEG2RAD);
if (pitch != 0) {
matrices.multiply(Vector3f.POSITIVE_X.getRadialQuaternion(pitch));
}
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(90));
MinecraftClient.getInstance().getItemRenderer().renderItem(new ItemStack(EnergonRelics.DEFENSIVE_LASER_CORE_ITEM), ModelTransformation.Mode.GROUND, light, overlay, matrices, vertexConsumers);
matrices.multiply(Vector3f.POSITIVE_Y.getDegreesQuaternion(-90));
MinecraftClient.getInstance().getItemRenderer().renderItem(new ItemStack(EnergonRelics.DEFENSIVE_LASER_CORE_ITEM), ModelTransformation.Mode.FIXED, light, overlay, matrices, vertexConsumers);
}
matrices.pop();
super.render(entity, tickDelta, matrices, vertexConsumers, light, overlay);
}
}

View File

@ -0,0 +1,7 @@
package com.thebrokenrail.energonrelics.util;
public class MissingCaseException extends RuntimeException {
public MissingCaseException(Enum<?> value) {
super(value.name());
}
}

View File

@ -1,31 +1,7 @@
{
"variants": {
"facing=down": {
"": {
"model": "energonrelics:block/defensive_laser"
},
"facing=east": {
"model": "energonrelics:block/defensive_laser",
"x": 90,
"y": 270
},
"facing=north": {
"model": "energonrelics:block/defensive_laser",
"x": 90,
"y": 180
},
"facing=south": {
"model": "energonrelics:block/defensive_laser",
"y": 180,
"x": 270
},
"facing=up": {
"model": "energonrelics:block/defensive_laser",
"x": 180
},
"facing=west": {
"model": "energonrelics:block/defensive_laser",
"y": 90,
"x": 90
}
}
}
}

View File

@ -1,33 +1,6 @@
{
"parent": "minecraft:block/block",
"parent": "minecraft:block/cube_all",
"textures": {
"0": "energonrelics:block/defensive_laser_base",
"particle": "energonrelics:block/defensive_laser_base"
},
"elements": [
{
"from": [0, 0, 0],
"to": [16, 4, 16],
"faces": {
"north": {"uv": [0, 0, 16, 4], "texture": "#0"},
"east": {"uv": [0, 0, 16, 4], "texture": "#0"},
"south": {"uv": [0, 0, 16, 4], "texture": "#0"},
"west": {"uv": [0, 0, 16, 4], "texture": "#0"},
"up": {"uv": [0, 0, 16, 16], "texture": "#0"},
"down": {"uv": [0, 0, 16, 16], "texture": "#0"}
}
},
{
"from": [7, 4, 7],
"to": [9, 8, 9],
"faces": {
"north": {"uv": [7, 6, 9, 10], "texture": "#0"},
"east": {"uv": [7, 6, 9, 10], "texture": "#0"},
"south": {"uv": [7, 6, 9, 10], "texture": "#0"},
"west": {"uv": [7, 6, 9, 10], "texture": "#0"},
"up": {"uv": [7, 7, 9, 9], "texture": "#0"},
"down": {"uv": [7, 7, 9, 9], "texture": "#0"}
}
}
]
}
"all": "energonrelics:block/defensive_laser_base"
}
}

View File

@ -1,3 +1,3 @@
{
"parent": "minecraft:builtin/entity"
"parent": "energonrelics:block/defensive_laser"
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 665 B

After

Width:  |  Height:  |  Size: 719 B