Skip to content

Commit

Permalink
Fix more portal issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Nov 19, 2024
1 parent 6215252 commit 7e0272b
Show file tree
Hide file tree
Showing 11 changed files with 157 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
import java.util.UUID;

import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector3f;
import org.joml.Vector4f;

import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.ability.magic.Caster;
Expand All @@ -22,6 +26,7 @@
import com.minelittlepony.unicopia.util.shape.*;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
Expand All @@ -31,6 +36,7 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction.Axis;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.WorldEvents;
Expand Down Expand Up @@ -139,21 +145,28 @@ public boolean tick(Caster<?> source, Situation situation) {
});
}
} else {
targetPortalPitch.set(targetEntry.getPitch());
targetPortalYaw.set(targetEntry.getYaw());

tickActive(source, targetEntry);
}
}
}

var entry = Ether.get(source.asWorld()).getOrCreate(this, source);
entry.setPitch(pitch.get());
entry.setYaw(yaw.get());
ownEntry.setPitch(getPitch());
ownEntry.setYaw(getYaw());
}
}

return !isDead();
}

private void tickActive(Caster<?> source, Ether.Entry<?> destination) {
destination.entity.getTarget().ifPresent(target -> {
Quaternionf rotationChange = getOrientationChange();
var matrix = getPositionMatrix(source, source.asEntity().getPos(), rotationChange, new Matrix4f());

float yawDifference = getYawDifference();

source.findAllEntitiesInRange(1).forEach(entity -> {
if (!entity.hasPortalCooldown()) {

Expand All @@ -162,13 +175,16 @@ private void tickActive(Caster<?> source, Ether.Entry<?> destination) {
return;
}

Vec3d offset = entity.getPos().subtract(source.asEntity().getPos())
.add(new Vec3d(0, 0, -0.7F).rotateY(-getTargetYaw() * MathHelper.RADIANS_PER_DEGREE));
float yawDifference = getYawDifference();
Vec3d dest = target.pos().add(offset.rotateY(yawDifference * MathHelper.RADIANS_PER_DEGREE)).add(0, 0.1, 0);
var dest4f = matrix.transform(new Vector4f(entity.getPos().toVector3f(), 1));
Vec3d dest = new Vec3d(dest4f.x, dest4f.y - 0.5, dest4f.z).add(new Vec3d(0, 0, -0.7F).rotateY(-getTargetYaw() * MathHelper.RADIANS_PER_DEGREE));

if (entity.getWorld().isTopSolid(BlockPos.ofFloored(dest).up(), entity)) {
dest = dest.add(0, 1, 0);
for (int i = 0; i < 2; i++) {
BlockPos destBlock = BlockPos.ofFloored(dest);
BlockState state = entity.getWorld().getBlockState(destBlock);
if (entity.getWorld().isTopSolid(destBlock, entity)) {
double maxY = state.getCollisionShape(entity.getWorld(), destBlock).getMax(Axis.Y);
dest = new Vec3d(dest.x, destBlock.getY() + maxY, dest.z);
}
}

entity.resetPortalCooldown();
Expand All @@ -193,6 +209,24 @@ private void tickActive(Caster<?> source, Ether.Entry<?> destination) {
});
}

public Matrix4f getPositionMatrix(Caster<?> source, Vec3d pos, Quaternionf orientationChange, Matrix4f matrix) {
getDestinationReference().getTarget().ifPresent(destEntity -> {
Vector3f destPos = destEntity.pos().toVector3f();
Vector3f sourcePos = pos.toVector3f();

matrix.rotateAround(orientationChange.conjugate(), destPos.x, destPos.y, destPos.z);
matrix.translate(destPos.sub(sourcePos));
});
return matrix;
}

public Quaternionf getOrientationChange() {
return new Quaternionf().rotateTo(
Vec3d.fromPolar(getPitch(), getYaw()).toVector3f(),
Vec3d.fromPolar(getTargetPitch(), getTargetYaw()).toVector3f()
);
}

@Override
public void setOrientation(Caster<?> caster, float pitch, float yaw) {
this.pitch.set(90 - pitch);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@
import com.minelittlepony.unicopia.client.render.spell.SpellRenderer;
import com.minelittlepony.unicopia.entity.mob.CastSpellEntity;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.EntityRenderer;
import net.minecraft.client.render.entity.EntityRendererFactory;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.util.Colors;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.ColorHelper.Argb;
import net.minecraft.util.math.RotationAxis;
Expand All @@ -41,6 +44,10 @@ public Identifier getTexture(CastSpellEntity entity) {

@Override
public void render(CastSpellEntity entity, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertices, int light) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.cameraEntity instanceof CastSpellEntity) {
return;
}
matrices.push();
matrices.translate(0, 0.001, 0);
final float height = entity.getHeight();
Expand All @@ -52,7 +59,6 @@ public void render(CastSpellEntity entity, float yaw, float tickDelta, MatrixSta
float animationProgress = getAnimationProgress(entity, tickDelta);
renderAmbientEffects(matrices, vertices, entity, entity.getSpellSlot().get().orElse(null), light, animationProgress, tickDelta);
SpellEffectsRenderDispatcher.INSTANCE.render(matrices, vertices, light, entity, entity.getScale(tickDelta), 0, tickDelta, animationProgress, yaw, pitch);

matrices.pop();
}

Expand All @@ -69,7 +75,7 @@ protected void renderAmbientEffects(MatrixStack matrices, VertexConsumerProvider

float angle = (animationProgress / 9F) % 360;

int color = spell == null ? 0 : spell.getTypeAndTraits().type().getColor();
int color = spell == null ? Colors.WHITE : spell.getTypeAndTraits().type().getColor();

@Nullable
SpellRenderer<?> renderer = spell == null ? null : SpellEffectsRenderDispatcher.INSTANCE.getRenderer(spell);
Expand All @@ -87,7 +93,7 @@ protected void renderAmbientEffects(MatrixStack matrices, VertexConsumerProvider
matrices.multiply(RotationAxis.POSITIVE_Z.rotationDegrees(angle * ringSpeed));
matrices.multiply(RotationAxis.POSITIVE_Y.rotationDegrees(angle * ringSpeed * dim));
matrices.multiply(RotationAxis.POSITIVE_X.rotationDegrees(angle * ringSpeed * dim));
PlaneModel.INSTANCE.render(matrices, buffer, light, 0, 1, Argb.withAlpha(color, (int)(255 * (scale / ((float)(dim * 3) + 1)))));
PlaneModel.INSTANCE.render(matrices, buffer, light, OverlayTexture.DEFAULT_UV, 1, Argb.withAlpha(color, (int)(255 * (scale / ((float)(dim * 3) + 1)))));
matrices.pop();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.joml.Quaternionf;
import org.joml.Vector4f;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
Expand Down Expand Up @@ -42,6 +43,7 @@
import net.minecraft.entity.Entity;
import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.util.Colors;
import net.minecraft.util.math.ChunkSectionPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;

Expand Down Expand Up @@ -92,14 +94,10 @@ public void draw(MatrixStack matrices, VertexConsumerProvider vertices) {

if (!(closed || framebuffer == null)) {
Tessellator tessellator = RenderSystem.renderThreadTesselator();
float uScale = (float)framebuffer.viewportWidth / (float)framebuffer.textureWidth;
float vScale = (float)framebuffer.viewportHeight / (float)framebuffer.textureHeight;
RenderSystem.setShader(UShaders.RENDER_TYPE_PORTAL_SURFACE);
RenderSystem._setShaderTexture(0, framebuffer.getColorAttachment());
BufferBuilder buffer = tessellator.begin(VertexFormat.DrawMode.QUADS, VertexFormats.POSITION_TEXTURE_COLOR);
SphereModel.DISK.scaleUV(uScale, vScale);

RenderSystem.setTextureMatrix(SphereModel.DISK.getTextureMatrix());
SphereModel.DISK.render(matrices, buffer, 1, 2F, Colors.WHITE);
BufferRenderer.drawWithGlobalProgram(buffer.end());

Expand All @@ -115,6 +113,7 @@ public void draw(MatrixStack matrices, VertexConsumerProvider vertices) {
}

public void build(PortalSpell spell, Caster<?> caster, EntityReference.EntityValues<Entity> target) {
closed = false;

long refreshRate = Unicopia.getConfig().fancyPortalRefreshRate.get();
if (refreshRate > 0 && framebuffer != null && System.currentTimeMillis() % refreshRate != 0) {
Expand Down Expand Up @@ -146,6 +145,8 @@ private void innerBuild(PortalSpell spell, Caster<?> caster, EntityReference.Ent
}
recursionCount++;

Entity globalCameraEntity = client.cameraEntity;

try {
if (closed || client.interactionManager == null) {
close();
Expand All @@ -155,17 +156,21 @@ private void innerBuild(PortalSpell spell, Caster<?> caster, EntityReference.Ent
Camera camera = client.gameRenderer.getCamera();

Entity cameraEntity = UEntities.CAST_SPELL.create(caster.asWorld());
Vec3d offset = new Vec3d(0, 1, -0.1F).rotateY(-spell.getTargetYaw() * MathHelper.RADIANS_PER_DEGREE);

float yaw = spell.getTargetYaw() + camera.getYaw() - spell.getYaw();
float pitch = spell.getTargetPitch();//MathHelper.clamp(spell.getTargetPitch() + (camera.getPitch() - spell.getPitch()) * 0.65F, -90, 90) + 90;
Vec3d pos = target.pos();

Quaternionf orientationChange = spell.getOrientationChange();
Matrix4f positionMatrix = spell.getPositionMatrix(caster, target.pos(), orientationChange, new Matrix4f());

cameraEntity.setPosition(target.pos().add(offset));
cameraEntity.setPitch(pitch % 180);
cameraEntity.setYaw((yaw + 180) % 360);
Vector4f transformedPos = positionMatrix.transform(new Vector4f(pos.toVector3f(), 1));
cameraEntity.setPosition(transformedPos.x, transformedPos.y + 0.5F, transformedPos.z);
cameraEntity.setPitch(MathHelper.clamp(camera.getPitch() - spell.getTargetPitch() + spell.getPitch(), -90, 90));
cameraEntity.setYaw(MathHelper.wrapDegrees(camera.getYaw() + spell.getYawDifference()));

client.cameraEntity = cameraEntity;
drawWorld(cameraEntity, 400, 400);
} finally {
client.cameraEntity = globalCameraEntity;
recursionCount--;
}
}
Expand Down Expand Up @@ -198,44 +203,38 @@ private void drawWorld(Entity cameraEntity, int width, int height) {
BackgroundRenderer.clearFog();
RenderSystem.enableCull();

if (renderer == null) {
renderer = new WorldRenderer(client, client.getEntityRenderDispatcher(), client.getBlockEntityRenderDispatcher(), client.getBufferBuilders());
}
if (cameraEntity.getWorld() != world) {
world = (ClientWorld)cameraEntity.getWorld();
renderer.setWorld(world);
}
//((MixinMinecraftClient)client).setWorldRenderer(renderer);

var tickCounter = client.getRenderTickCounter();
if (renderer == null) {
renderer = new WorldRenderer(client, client.getEntityRenderDispatcher(), client.getBlockEntityRenderDispatcher(), client.getBufferBuilders());
renderer.setWorld(world);
renderer.scheduleBlockRenders(
ChunkSectionPos.getSectionCoord((int)cameraEntity.getX()),
ChunkSectionPos.getSectionCoord((int)cameraEntity.getY()),
ChunkSectionPos.getSectionCoord((int)cameraEntity.getZ())
);
}

camera.update(world, cameraEntity, false, false, 1);

double fov = 110;
double fov = 120;
Matrix4f projectionMatrix = client.gameRenderer.getBasicProjectionMatrix(fov);
Matrix4f cameraTransform = new Matrix4f().rotation(camera.getRotation().conjugate(new Quaternionf()));

client.gameRenderer.loadProjectionMatrix(projectionMatrix);
/*renderer.scheduleBlockRenders(
ChunkSectionPos.getSectionCoord((int)cameraEntity.getX()),
ChunkSectionPos.getSectionCoord((int)cameraEntity.getY()),
ChunkSectionPos.getSectionCoord((int)cameraEntity.getZ())
);*/

renderer.setupFrustum(
camera.getPos(),
cameraTransform,
client.gameRenderer.getBasicProjectionMatrix(Math.max(fov, this.client.options.getFov().getValue().intValue()))
client.gameRenderer.getBasicProjectionMatrix(Math.max(fov, client.options.getFov().getValue().intValue()))
);
renderer.render(client.getRenderTickCounter(), false, camera, client.gameRenderer,
client.gameRenderer.getLightmapTextureManager(),
cameraTransform,
projectionMatrix
);
try {
renderer.render(tickCounter, false, camera, client.gameRenderer,
client.gameRenderer.getLightmapTextureManager(),
cameraTransform,
projectionMatrix
);
} catch (Throwable t) {
close();
}

// Strip transparency
RenderSystem.colorMask(false, false, false, true);
RenderSystem.clearColor(1, 1, 1, 1);
Expand All @@ -246,6 +245,7 @@ private void drawWorld(Entity cameraEntity, int width, int height) {
} finally {
client.getFramebuffer().beginWrite(true);
client.gameRenderer.loadProjectionMatrix(proj);
client.getBlockEntityRenderDispatcher().setWorld(client.world);

window.setFramebufferWidth(globalFramebufferWidth);
window.setFramebufferHeight(globalFramebufferHeight);
Expand All @@ -264,6 +264,7 @@ public void close() {
if (renderer != null) {
renderer.getChunkBuilder().stop();
renderer.close();
renderer = null;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.minelittlepony.unicopia.client.render.RenderLayers;
import com.minelittlepony.unicopia.client.render.model.SphereModel;
import com.minelittlepony.unicopia.entity.EntityReference;
import com.minelittlepony.unicopia.entity.mob.CastSpellEntity;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
Expand Down Expand Up @@ -42,11 +44,14 @@ public void render(MatrixStack matrices, VertexConsumerProvider vertices, Portal
return;
}

if (caster.asEntity().distanceTo(client.cameraEntity) > 50) {
return; // don't bother rendering if too far away
}
if (client.cameraEntity == caster.asEntity()) {
return;
if (client.cameraEntity instanceof CastSpellEntity) {
double distance = caster.asEntity().distanceTo(client.cameraEntity);
if (distance > 50) {
return; // don't bother rendering if too far away
}
if (distance < 2) {
return; // don't render ourselves
}
}

matrices.push();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -788,8 +788,8 @@ private void applyTurbulance(MutableVector velocity) {
Vec3d airflow = WeatherConditions.getAirflow(entity.getBlockPos(), entity.getWorld())
.multiply(0.04F * effectStrength)
.add(Vec3d.fromPolar(
(entity.getPitch() + (float)gust.getY()) * MathHelper.RADIANS_PER_DEGREE,
(entity.getYaw() + (float)gust.getZ()) * MathHelper.RADIANS_PER_DEGREE
(entity.getPitch() + (float)gust.getY()),
(entity.getYaw() + (float)gust.getZ())
).multiply(effectStrength * (float)gust.getX() / weight));

windStrength.update((float)airflow.length(), airflow.length() > windStrength.getValue() ? 1000 : 500);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@
import java.util.Queue;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.WorldRenderer;

@Mixin(MinecraftClient.class)
public interface MixinMinecraftClient {
@Accessor("renderTaskQueue")
Queue<Runnable> getRenderTaskQueue();

@Mutable
@Accessor("worldRenderer")
void setWorldRenderer(WorldRenderer worldRenderer);
}
Loading

0 comments on commit 7e0272b

Please sign in to comment.