Fix weird recursions in specific setups, Fix normal calculation being broken on mirrors placed with axis y

This commit is contained in:
grimmauld 2021-04-20 11:43:57 +02:00
parent 19129c321f
commit a2e2e2a313
4 changed files with 47 additions and 33 deletions

View File

@ -1,11 +1,5 @@
package com.simibubi.create.content.optics; package com.simibubi.create.content.optics;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
import javax.annotation.Nullable;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.foundation.utility.VecHelper; import com.simibubi.create.foundation.utility.VecHelper;
@ -15,6 +9,12 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.vector.Vector3d; import net.minecraft.util.math.vector.Vector3d;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Beam extends ArrayList<BeamSegment> { public class Beam extends ArrayList<BeamSegment> {
private final Set<ILightHandler<?>> lightEventListeners; private final Set<ILightHandler<?>> lightEventListeners;
@Nullable @Nullable

View File

@ -1,11 +1,5 @@
package com.simibubi.create.content.optics; package com.simibubi.create.content.optics;
import static com.simibubi.create.foundation.utility.VecHelper.UP;
import static net.minecraft.client.renderer.tileentity.BeaconTileEntityRenderer.TEXTURE_BEACON_BEAM;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.mojang.blaze3d.matrix.MatrixStack; import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.vertex.IVertexBuilder; import com.mojang.blaze3d.vertex.IVertexBuilder;
import com.simibubi.create.foundation.utility.MatrixStacker; import com.simibubi.create.foundation.utility.MatrixStacker;
@ -18,15 +12,17 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Direction; import net.minecraft.util.Direction;
import net.minecraft.util.LazyValue; import net.minecraft.util.LazyValue;
import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.vector.Matrix3f; import net.minecraft.util.math.vector.*;
import net.minecraft.util.math.vector.Matrix4f;
import net.minecraft.util.math.vector.Quaternion;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.util.math.vector.Vector3f;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import static com.simibubi.create.foundation.utility.VecHelper.UP;
import static net.minecraft.client.renderer.tileentity.BeaconTileEntityRenderer.TEXTURE_BEACON_BEAM;
public class BeamSegment { public class BeamSegment {
public final float[] colors; public final float[] colors;
private final Vector3d direction; private final Vector3d direction;
@ -120,7 +116,14 @@ public class BeamSegment {
beaconBeamModifier = Quaternion.IDENTITY; beaconBeamModifier = Quaternion.IDENTITY;
} else { } else {
Vector3f unitVec = axis.getUnitVector(); Vector3f unitVec = axis.getUnitVector();
beaconBeamModifier = unitVec.getRadialQuaternion((float) Math.acos(dotProd) * (new Vector3d(unitVec).dotProduct(getNormalized().crossProduct(UP)) > 0 ? -1 : 1)); beaconBeamModifier = unitVec.getRadialQuaternion((float) Math.acos(dotProd) * (new Vector3d(unitVec).dotProduct(getNormalized().crossProduct(UP)) > 0
^ axis.getAxis()
.isVertical() ? -1 : 1));
if (axis.getAxis()
.isVertical()) {
beaconBeamModifier.multiply(unitVec.getDegreesQuaternion(-90));
beaconBeamModifier.multiply(new Vector3f(getNormalized().crossProduct(Vector3d.of(axis.getDirectionVec()))).getDegreesQuaternion(90));
}
} }
} }
return beaconBeamModifier; return beaconBeamModifier;

View File

@ -1,16 +1,5 @@
package com.simibubi.create.content.optics.mirror; package com.simibubi.create.content.optics.mirror;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import com.google.common.collect.Iterators; import com.google.common.collect.Iterators;
import com.simibubi.create.content.contraptions.base.KineticTileEntity; import com.simibubi.create.content.contraptions.base.KineticTileEntity;
import com.simibubi.create.content.optics.Beam; import com.simibubi.create.content.optics.Beam;
@ -33,6 +22,13 @@ import net.minecraft.util.math.vector.Vector3d;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;
public class MirrorTileEntity extends KineticTileEntity implements ILightHandler<MirrorTileEntity> { public class MirrorTileEntity extends KineticTileEntity implements ILightHandler<MirrorTileEntity> {
protected float angle; protected float angle;
protected float clientAngleDiff; protected float clientAngleDiff;
@ -41,6 +37,7 @@ public class MirrorTileEntity extends KineticTileEntity implements ILightHandler
@Nullable @Nullable
private BeaconTileEntity beacon; private BeaconTileEntity beacon;
private Beam beaconBeam = null; private Beam beaconBeam = null;
private boolean isUpdating = false;
public MirrorTileEntity(TileEntityType<?> typeIn) { public MirrorTileEntity(TileEntityType<?> typeIn) {
super(typeIn); super(typeIn);
@ -122,8 +119,12 @@ public class MirrorTileEntity extends KineticTileEntity implements ILightHandler
@Override @Override
public void updateBeams() { public void updateBeams() {
if (isUpdating)
return;
isUpdating = true;
Map<Beam, Beam> newBeams = new HashMap<>(); Map<Beam, Beam> newBeams = new HashMap<>();
for (Map.Entry<Beam, Beam> entry : beams.entrySet()) { for (Map.Entry<Beam, Beam> entry : new HashSet<>(beams.entrySet())) {
entry.getValue() entry.getValue()
.onRemoved(); .onRemoved();
if (entry.getKey() if (entry.getKey()
@ -138,13 +139,22 @@ public class MirrorTileEntity extends KineticTileEntity implements ILightHandler
} }
} }
beams = newBeams; beams = newBeams;
isUpdating = false;
} }
private Vector3d getReflectionAngle(Vector3d inputAngle) { private Vector3d getReflectionAngle(Vector3d inputAngle) {
inputAngle = inputAngle.normalize(); inputAngle = inputAngle.normalize();
Vector3d normal = new Matrix3d().asIdentity() Direction.Axis axis = getBlockState().get(BlockStateProperties.AXIS);
.asAxisRotation(getBlockState().get(BlockStateProperties.AXIS), AngleHelper.rad(angle)) Vector3d normal;
.transform(VecHelper.UP); if (axis.isHorizontal())
normal = new Matrix3d().asIdentity()
.asAxisRotation(axis, AngleHelper.rad(angle))
.transform(VecHelper.UP);
else
normal = new Matrix3d().asIdentity()
.asAxisRotation(axis, AngleHelper.rad(-angle))
.transform(VecHelper.SOUTH);
return inputAngle.subtract(normal.scale(2 * inputAngle.dotProduct(normal))); return inputAngle.subtract(normal.scale(2 * inputAngle.dotProduct(normal)));
} }

View File

@ -23,6 +23,7 @@ public class VecHelper {
public static final Vector3d CENTER_OF_ORIGIN = new Vector3d(.5, .5, .5); public static final Vector3d CENTER_OF_ORIGIN = new Vector3d(.5, .5, .5);
public static final Vector3d UP = new Vector3d(0, 1, 0); public static final Vector3d UP = new Vector3d(0, 1, 0);
public static final Vector3d SOUTH = new Vector3d(1, 0, 0);
public static Vector3d rotate(Vector3d vec, Vector3d rotationVec) { public static Vector3d rotate(Vector3d vec, Vector3d rotationVec) {
return rotate(vec, rotationVec.x, rotationVec.y, rotationVec.z); return rotate(vec, rotationVec.x, rotationVec.y, rotationVec.z);