| 1 | package net.minecraft.client;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 4 | import java.util.Arrays;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.entity.ClientAvatarState;
|
| 8 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 9 | import net.minecraft.client.player.AbstractClientPlayer;
|
| 10 | import net.minecraft.client.player.LocalPlayer;
|
| 11 | import net.minecraft.client.renderer.Projection;
|
| 12 | import net.minecraft.client.renderer.culling.Frustum;
|
| 13 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 14 | import net.minecraft.core.BlockPos;
|
| 15 | import net.minecraft.core.Direction;
|
| 16 | import net.minecraft.core.BlockPos.MutableBlockPos;
|
| 17 | import net.minecraft.tags.FluidTags;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.world.attribute.EnvironmentAttributeProbe;
|
| 20 | import net.minecraft.world.effect.MobEffects;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 | import net.minecraft.world.entity.LivingEntity;
|
| 23 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 24 | import net.minecraft.world.entity.vehicle.minecart.Minecart;
|
| 25 | import net.minecraft.world.entity.vehicle.minecart.NewMinecartBehavior;
|
| 26 | import net.minecraft.world.level.ClipContext;
|
| 27 | import net.minecraft.world.level.Level;
|
| 28 | import net.minecraft.world.level.ClipContext.Block;
|
| 29 | import net.minecraft.world.level.ClipContext.Fluid;
|
| 30 | import net.minecraft.world.level.block.Blocks;
|
| 31 | import net.minecraft.world.level.block.state.BlockState;
|
| 32 | import net.minecraft.world.level.material.FluidState;
|
| 33 | import net.minecraft.world.level.material.FogType;
|
| 34 | import net.minecraft.world.phys.HitResult;
|
| 35 | import net.minecraft.world.phys.Vec3;
|
| 36 | import net.minecraft.world.phys.HitResult.Type;
|
| 37 | import org.joml.Matrix4f;
|
| 38 | import org.joml.Matrix4fc;
|
| 39 | import org.joml.Quaternionf;
|
| 40 | import org.joml.Vector3f;
|
| 41 | import org.joml.Vector3fc;
|
| 42 | import org.jspecify.annotations.Nullable;
|
| 43 |
|
| 44 | @Environment(EnvType.CLIENT)
|
| 45 | public class Camera implements net.minecraft.world.waypoints.TrackedWaypoint.Camera {
|
| 46 | public static final float PROJECTION_Z_NEAR = 0.05F;
|
| 47 | private static final float DEFAULT_CAMERA_DISTANCE = 4.0F;
|
| 48 | private static final Vector3fc FORWARDS = new Vector3f(0.0F, 0.0F, -1.0F);
|
| 49 | private static final Vector3fc UP = new Vector3f(0.0F, 1.0F, 0.0F);
|
| 50 | private static final Vector3fc LEFT = new Vector3f(-1.0F, 0.0F, 0.0F);
|
| 51 | public static final float BASE_HUD_FOV = 70.0F;
|
| 52 | private boolean initialized;
|
| 53 | private @Nullable Level level;
|
| 54 | private @Nullable Entity entity;
|
| 55 | private Vec3 position = Vec3.ZERO;
|
| 56 | private final MutableBlockPos blockPosition = new MutableBlockPos();
|
| 57 | private final Vector3f forwards = new Vector3f(FORWARDS);
|
| 58 | private final Vector3f panoramicForwards = new Vector3f(FORWARDS);
|
| 59 | private final Vector3f up = new Vector3f(UP);
|
| 60 | private final Vector3f left = new Vector3f(LEFT);
|
| 61 | private float xRot;
|
| 62 | private float yRot;
|
| 63 | private final Quaternionf rotation = new Quaternionf();
|
| 64 | private boolean detached;
|
| 65 | private float eyeHeight;
|
| 66 | private float eyeHeightOld;
|
| 67 | private final Projection projection = new Projection();
|
| 68 | private Frustum cullFrustum = new Frustum(new Matrix4f(), new Matrix4f());
|
| 69 | private @Nullable Frustum capturedFrustum;
|
| 70 | private boolean captureFrustum;
|
| 71 | private final Matrix4f cachedViewRotMatrix = new Matrix4f();
|
| 72 | private final Matrix4f cachedViewRotProjMatrix = new Matrix4f();
|
| 73 | private long lastProjectionVersion = -1L;
|
| 74 | private int matrixPropertiesDirty = -1;
|
| 75 | private static final int DIRTY_VIEW_ROT = 1;
|
| 76 | private static final int DIRTY_VIEW_ROT_PROJ = 2;
|
| 77 | private float fovModifier;
|
| 78 | private float oldFovModifier;
|
| 79 | private float fov;
|
| 80 | private float hudFov;
|
| 81 | private float depthFar;
|
| 82 | private boolean isPanoramicMode;
|
| 83 | private final EnvironmentAttributeProbe attributeProbe = new EnvironmentAttributeProbe();
|
| 84 | private final Minecraft minecraft = Minecraft.getInstance();
|
| 85 |
|
| 86 | public void tick() {
|
| 87 | if (this.level != null && this.entity != null) {
|
| 88 | this.eyeHeightOld = this.eyeHeight;
|
| 89 | this.eyeHeight = this.eyeHeight + (this.entity.getEyeHeight() - this.eyeHeight) * 0.5F;
|
| 90 | this.attributeProbe.tick(this.level, this.position);
|
| 91 | }
|
| 92 |
|
| 93 | this.tickFov();
|
| 94 | }
|
| 95 |
|
| 96 | public void update(final DeltaTracker deltaTracker) {
|
| 97 | float renderDistance = this.minecraft.options.getEffectiveRenderDistance() * 16;
|
| 98 | this.depthFar = Math.max(renderDistance * 4.0F, this.minecraft.options.cloudRange().get() * 16);
|
| 99 | LocalPlayer player = this.minecraft.player;
|
| 100 | if (player != null && this.level != null) {
|
| 101 | if (this.entity == null) {
|
| 102 | this.setEntity(player);
|
| 103 | }
|
| 104 |
|
| 105 | float partialTicks = this.getCameraEntityPartialTicks(deltaTracker);
|
| 106 | this.alignWithEntity(partialTicks);
|
| 107 | this.fov = this.calculateFov(partialTicks);
|
| 108 | this.hudFov = this.calculateHudFov(partialTicks);
|
| 109 | this.prepareCullFrustum(this.getViewRotationMatrix(this.cachedViewRotMatrix), this.createProjectionMatrixForCulling(), this.position);
|
| 110 | float windowWidth = this.minecraft.getWindow().getWidth();
|
| 111 | float windowHeight = this.minecraft.getWindow().getHeight();
|
| 112 | this.setupPerspective(0.05F, this.depthFar, this.fov, windowWidth, windowHeight);
|
| 113 | this.initialized = true;
|
| 114 | }
|
| 115 | }
|
| 116 |
|
| 117 | public float getCameraEntityPartialTicks(final DeltaTracker deltaTracker) {
|
| 118 | return this.level.tickRateManager().isEntityFrozen(this.entity) ? 1.0F : deltaTracker.getGameTimeDeltaPartialTick(true);
|
| 119 | }
|
| 120 |
|
| 121 | public void extractRenderState(final CameraRenderState cameraState, final DeltaTracker deltaTracker) {
|
| 122 | cameraState.cameraEntityPartialTicks = this.getCameraEntityPartialTicks(deltaTracker);
|
| 123 | cameraState.initialized = this.isInitialized();
|
| 124 | cameraState.isPanoramicMode = this.isPanoramicMode;
|
| 125 | cameraState.isFrustumCaptured = this.capturedFrustum != null;
|
| 126 | cameraState.isFirstPerson = this.minecraft.options.getCameraType().isFirstPerson();
|
| 127 | cameraState.smartCull = this.minecraft.smartCull;
|
| 128 | if (this.minecraft.player.isSpectator() && this.level.getBlockState(this.blockPosition).isSolidRender()) {
|
| 129 | cameraState.smartCull = false;
|
| 130 | }
|
| 131 |
|
| 132 | cameraState.pos = this.position();
|
| 133 | cameraState.xRot = this.xRot;
|
| 134 | cameraState.yRot = this.yRot;
|
| 135 | cameraState.blockPos = this.blockPosition();
|
| 136 | cameraState.orientation.set(this.rotation());
|
| 137 | cameraState.cullFrustum.set(this.cullFrustum);
|
| 138 | cameraState.depthFar = this.depthFar;
|
| 139 | this.projection.getMatrix(cameraState.projectionMatrix);
|
| 140 | this.getViewRotationMatrix(cameraState.viewRotationMatrix);
|
| 141 | if (this.entity instanceof LivingEntity livingEntity) {
|
| 142 | cameraState.entityRenderState.isLiving = true;
|
| 143 | cameraState.entityRenderState.isSleeping = livingEntity.isSleeping();
|
| 144 | cameraState.entityRenderState.doesMobEffectBlockSky = livingEntity.hasEffect(MobEffects.BLINDNESS) || livingEntity.hasEffect(MobEffects.DARKNESS);
|
| 145 | cameraState.entityRenderState.isDeadOrDying = livingEntity.isDeadOrDying();
|
| 146 | cameraState.entityRenderState.hurtDir = livingEntity.getHurtDir();
|
| 147 | cameraState.entityRenderState.hurtTime = livingEntity.hurtTime - cameraState.cameraEntityPartialTicks;
|
| 148 | cameraState.entityRenderState.deathTime = livingEntity.deathTime + cameraState.cameraEntityPartialTicks;
|
| 149 | cameraState.entityRenderState.hurtDuration = livingEntity.hurtDuration;
|
| 150 | } else {
|
| 151 | cameraState.entityRenderState.isLiving = false;
|
| 152 | cameraState.entityRenderState.isSleeping = false;
|
| 153 | cameraState.entityRenderState.doesMobEffectBlockSky = false;
|
| 154 | }
|
| 155 |
|
| 156 | if (this.entity instanceof AbstractClientPlayer player) {
|
| 157 | cameraState.entityRenderState.isPlayer = true;
|
| 158 | ClientAvatarState avatarState = player.avatarState();
|
| 159 | cameraState.entityRenderState.backwardsInterpolatedWalkDistance = avatarState.getBackwardsInterpolatedWalkDistance(cameraState.cameraEntityPartialTicks);
|
| 160 | cameraState.entityRenderState.bob = avatarState.getInterpolatedBob(cameraState.cameraEntityPartialTicks);
|
| 161 | } else {
|
| 162 | cameraState.entityRenderState.isPlayer = false;
|
| 163 | }
|
| 164 |
|
| 165 | cameraState.hudFov = this.hudFov;
|
| 166 | }
|
| 167 |
|
| 168 | private void tickFov() {
|
| 169 | float targetFovModifier;
|
| 170 | if (this.minecraft.getCameraEntity() instanceof AbstractClientPlayer player) {
|
| 171 | Options options = this.minecraft.options;
|
| 172 | boolean firstPerson = options.getCameraType().isFirstPerson();
|
| 173 | float effectScale = options.fovEffectScale().get().floatValue();
|
| 174 | targetFovModifier = player.getFieldOfViewModifier(firstPerson, effectScale);
|
| 175 | } else {
|
| 176 | targetFovModifier = 1.0F;
|
| 177 | }
|
| 178 |
|
| 179 | this.oldFovModifier = this.fovModifier;
|
| 180 | this.fovModifier = this.fovModifier + (targetFovModifier - this.fovModifier) * 0.5F;
|
| 181 | this.fovModifier = Mth.clamp(this.fovModifier, 0.1F, 1.5F);
|
| 182 | }
|
| 183 |
|
| 184 | private Matrix4f createProjectionMatrixForCulling() {
|
| 185 | float fovForCulling = Math.max(this.fov, this.minecraft.options.fov().get().intValue());
|
| 186 | Matrix4f projection = new Matrix4f();
|
| 187 | return projection.perspective(
|
| 188 | fovForCulling * (float) (Math.PI / 180.0),
|
| 189 | (float)this.minecraft.getWindow().getWidth() / this.minecraft.getWindow().getHeight(),
|
| 190 | 0.05F,
|
| 191 | this.depthFar,
|
| 192 | RenderSystem.getDevice().getDeviceInfo().isZZeroToOne()
|
| 193 | );
|
| 194 | }
|
| 195 |
|
| 196 | public Frustum getCullFrustum() {
|
| 197 | return this.cullFrustum;
|
| 198 | }
|
| 199 |
|
| 200 | private void prepareCullFrustum(final Matrix4fc modelViewMatrix, final Matrix4f projectionMatrixForCulling, final Vec3 cameraPos) {
|
| 201 | if (this.capturedFrustum != null && !this.captureFrustum) {
|
| 202 | this.cullFrustum = this.capturedFrustum;
|
| 203 | } else {
|
| 204 | this.cullFrustum = new Frustum(modelViewMatrix, projectionMatrixForCulling);
|
| 205 | this.cullFrustum.prepare(cameraPos.x(), cameraPos.y(), cameraPos.z());
|
| 206 | }
|
| 207 |
|
| 208 | if (this.captureFrustum) {
|
| 209 | this.capturedFrustum = this.cullFrustum;
|
| 210 | this.captureFrustum = false;
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | public @Nullable Frustum getCapturedFrustum() {
|
| 215 | return this.capturedFrustum;
|
| 216 | }
|
| 217 |
|
| 218 | public void captureFrustum() {
|
| 219 | this.captureFrustum = true;
|
| 220 | }
|
| 221 |
|
| 222 | public void killFrustum() {
|
| 223 | this.capturedFrustum = null;
|
| 224 | }
|
| 225 |
|
| 226 | private float calculateFov(final float partialTicks) {
|
| 227 | if (this.isPanoramicMode) {
|
| 228 | return 90.0F;
|
| 229 | }
|
| 230 |
|
| 231 | float fov = this.minecraft.options.fov().get().intValue() * Mth.lerp(partialTicks, this.oldFovModifier, this.fovModifier);
|
| 232 | return this.modifyFovBasedOnDeathOrFluid(partialTicks, fov);
|
| 233 | }
|
| 234 |
|
| 235 | private float calculateHudFov(final float partialTicks) {
|
| 236 | return this.modifyFovBasedOnDeathOrFluid(partialTicks, 70.0F);
|
| 237 | }
|
| 238 |
|
| 239 | private float modifyFovBasedOnDeathOrFluid(final float partialTicks, float fov) {
|
| 240 | if (this.entity instanceof LivingEntity cameraEntity && cameraEntity.isDeadOrDying()) {
|
| 241 | float duration = Math.min(cameraEntity.deathTime + partialTicks, 20.0F);
|
| 242 | fov /= (1.0F - 500.0F / (duration + 500.0F)) * 2.0F + 1.0F;
|
| 243 | }
|
| 244 |
|
| 245 | FogType state = this.getFluidInCamera();
|
| 246 | if (state == FogType.LAVA || state == FogType.WATER) {
|
| 247 | float effectScale = this.minecraft.options.fovEffectScale().get().floatValue();
|
| 248 | fov *= Mth.lerp(effectScale, 1.0F, 0.85714287F);
|
| 249 | }
|
| 250 |
|
| 251 | return fov;
|
| 252 | }
|
| 253 |
|
| 254 | private void alignWithEntity(final float partialTicks) {
|
| 255 | if (this.entity.isPassenger()
|
| 256 | && this.entity.getVehicle() instanceof Minecart minecart
|
| 257 | && minecart.getBehavior() instanceof NewMinecartBehavior behavior
|
| 258 | && behavior.cartHasPosRotLerp()) {
|
| 259 | Vec3 positionOffset = minecart.getPassengerRidingPosition(this.entity)
|
| 260 | .subtract(minecart.position())
|
| 261 | .subtract(this.entity.getVehicleAttachmentPoint(minecart))
|
| 262 | .add(new Vec3(0.0, Mth.lerp(partialTicks, this.eyeHeightOld, this.eyeHeight), 0.0));
|
| 263 | this.setRotation(this.entity.getViewYRot(partialTicks), this.entity.getViewXRot(partialTicks));
|
| 264 | this.setPosition(behavior.getCartLerpPosition(partialTicks).add(positionOffset));
|
| 265 | } else {
|
| 266 | this.setRotation(this.entity.getViewYRot(partialTicks), this.entity.getViewXRot(partialTicks));
|
| 267 | this.setPosition(
|
| 268 | Mth.lerp(partialTicks, this.entity.xo, this.entity.getX()),
|
| 269 | Mth.lerp(partialTicks, this.entity.yo, this.entity.getY()) + Mth.lerp(partialTicks, this.eyeHeightOld, this.eyeHeight),
|
| 270 | Mth.lerp(partialTicks, this.entity.zo, this.entity.getZ())
|
| 271 | );
|
| 272 | }
|
| 273 |
|
| 274 | this.detached = !this.minecraft.options.getCameraType().isFirstPerson();
|
| 275 | if (this.detached) {
|
| 276 | if (this.minecraft.options.getCameraType().isMirrored()) {
|
| 277 | this.setRotation(this.yRot + 180.0F, -this.xRot);
|
| 278 | }
|
| 279 |
|
| 280 | float cameraDistance = 4.0F;
|
| 281 | float cameraScale = 1.0F;
|
| 282 | if (this.entity instanceof LivingEntity living) {
|
| 283 | cameraScale = living.getScale();
|
| 284 | cameraDistance = (float)living.getAttributeValue(Attributes.CAMERA_DISTANCE);
|
| 285 | }
|
| 286 |
|
| 287 | float mountScale = cameraScale;
|
| 288 | float mountDistance = cameraDistance;
|
| 289 | if (this.entity.isPassenger() && this.entity.getVehicle() instanceof LivingEntity mount) {
|
| 290 | mountScale = mount.getScale();
|
| 291 | mountDistance = (float)mount.getAttributeValue(Attributes.CAMERA_DISTANCE);
|
| 292 | }
|
| 293 |
|
| 294 | this.move(-this.getMaxZoom(Math.max(cameraScale * cameraDistance, mountScale * mountDistance)), 0.0F, 0.0F);
|
| 295 | } else if (this.entity instanceof LivingEntity && ((LivingEntity)this.entity).isSleeping()) {
|
| 296 | Direction bedOrientation = ((LivingEntity)this.entity).getBedOrientation();
|
| 297 | this.setRotation(bedOrientation != null ? bedOrientation.toYRot() - 180.0F : 0.0F, 0.0F);
|
| 298 | this.move(0.0F, 0.15F, 0.0F);
|
| 299 | }
|
| 300 | }
|
| 301 |
|
| 302 | private float getMaxZoom(float cameraDist) {
|
| 303 | float jitterScale = 0.1F;
|
| 304 |
|
| 305 | for (int i = 0; i < 8; i++) {
|
| 306 | float offsetX = (i & 1) * 2 - 1;
|
| 307 | float offsetY = (i >> 1 & 1) * 2 - 1;
|
| 308 | float offsetZ = (i >> 2 & 1) * 2 - 1;
|
| 309 | Vec3 from = this.position.add(offsetX * 0.1F, offsetY * 0.1F, offsetZ * 0.1F);
|
| 310 | Vec3 to = from.add(new Vec3(this.forwards).scale(-cameraDist));
|
| 311 | HitResult hitResult = this.level.clip(new ClipContext(from, to, Block.VISUAL, Fluid.NONE, this.entity));
|
| 312 | if (hitResult.getType() != Type.MISS) {
|
| 313 | float distSq = (float)hitResult.getLocation().distanceToSqr(this.position);
|
| 314 | if (distSq < Mth.square(cameraDist)) {
|
| 315 | cameraDist = Mth.sqrt(distSq);
|
| 316 | }
|
| 317 | }
|
| 318 | }
|
| 319 |
|
| 320 | return cameraDist;
|
| 321 | }
|
| 322 |
|
| 323 | public boolean isPanoramicMode() {
|
| 324 | return this.isPanoramicMode;
|
| 325 | }
|
| 326 |
|
| 327 | public float getFov() {
|
| 328 | return this.fov;
|
| 329 | }
|
| 330 |
|
| 331 | private void setupPerspective(final float zNear, final float zFar, final float fov, final float width, final float height) {
|
| 332 | this.projection.setupPerspective(zNear, zFar, fov, width, height);
|
| 333 | }
|
| 334 |
|
| 335 | private void setupOrtho(final float zNear, final float zFar, final float width, final float height, final boolean invertY) {
|
| 336 | this.projection.setupOrtho(zNear, zFar, width, height, invertY);
|
| 337 | }
|
| 338 |
|
| 339 | protected void move(final float forwards, final float up, final float right) {
|
| 340 | Vector3f offset = new Vector3f(right, up, -forwards).rotate(this.rotation);
|
| 341 | this.setPosition(new Vec3(this.position.x + offset.x, this.position.y + offset.y, this.position.z + offset.z));
|
| 342 | }
|
| 343 |
|
| 344 | protected void setRotation(final float yRot, final float xRot) {
|
| 345 | this.xRot = xRot;
|
| 346 | this.yRot = yRot;
|
| 347 | this.rotation.rotationYXZ((float) Math.PI - yRot * (float) (Math.PI / 180.0), -xRot * (float) (Math.PI / 180.0), 0.0F);
|
| 348 | FORWARDS.rotate(this.rotation, this.forwards);
|
| 349 | UP.rotate(this.rotation, this.up);
|
| 350 | LEFT.rotate(this.rotation, this.left);
|
| 351 | this.matrixPropertiesDirty |= 3;
|
| 352 | }
|
| 353 |
|
| 354 | protected void setPosition(final double x, final double y, final double z) {
|
| 355 | this.setPosition(new Vec3(x, y, z));
|
| 356 | }
|
| 357 |
|
| 358 | protected void setPosition(final Vec3 position) {
|
| 359 | this.position = position;
|
| 360 | this.blockPosition.set(position.x, position.y, position.z);
|
| 361 | }
|
| 362 |
|
| 363 | @Override
|
| 364 | public Vec3 position() {
|
| 365 | return this.position;
|
| 366 | }
|
| 367 |
|
| 368 | public BlockPos blockPosition() {
|
| 369 | return this.blockPosition;
|
| 370 | }
|
| 371 |
|
| 372 | public float xRot() {
|
| 373 | return this.xRot;
|
| 374 | }
|
| 375 |
|
| 376 | public float yRot() {
|
| 377 | return this.yRot;
|
| 378 | }
|
| 379 |
|
| 380 | @Override
|
| 381 | public float yaw() {
|
| 382 | return Mth.wrapDegrees(this.yRot());
|
| 383 | }
|
| 384 |
|
| 385 | public Quaternionf rotation() {
|
| 386 | return this.rotation;
|
| 387 | }
|
| 388 |
|
| 389 | public Matrix4f getViewRotationMatrix(final Matrix4f dest) {
|
| 390 | if ((this.matrixPropertiesDirty & 1) != 0) {
|
| 391 | Quaternionf inverseRotation = this.rotation().conjugate(new Quaternionf());
|
| 392 | this.cachedViewRotMatrix.rotation(inverseRotation);
|
| 393 | this.matrixPropertiesDirty &= -2;
|
| 394 | }
|
| 395 |
|
| 396 | return dest.set(this.cachedViewRotMatrix);
|
| 397 | }
|
| 398 |
|
| 399 | public Matrix4f getViewRotationProjectionMatrix(final Matrix4f dest) {
|
| 400 | long projectionVersion = this.projection.getMatrixVersion();
|
| 401 | if ((this.matrixPropertiesDirty & 2) != 0 || this.lastProjectionVersion != this.projection.getMatrixVersion()) {
|
| 402 | this.getViewRotationMatrix(this.cachedViewRotMatrix);
|
| 403 | this.projection.getMatrix(this.cachedViewRotProjMatrix);
|
| 404 | this.cachedViewRotProjMatrix.mul(this.cachedViewRotMatrix);
|
| 405 | this.matrixPropertiesDirty &= -3;
|
| 406 | this.lastProjectionVersion = projectionVersion;
|
| 407 | }
|
| 408 |
|
| 409 | return dest.set(this.cachedViewRotProjMatrix);
|
| 410 | }
|
| 411 |
|
| 412 | public @Nullable Entity entity() {
|
| 413 | return this.entity;
|
| 414 | }
|
| 415 |
|
| 416 | public void setEntity(final Entity entity) {
|
| 417 | this.entity = entity;
|
| 418 | }
|
| 419 |
|
| 420 | public boolean isInitialized() {
|
| 421 | return this.initialized;
|
| 422 | }
|
| 423 |
|
| 424 | public boolean isDetached() {
|
| 425 | return this.detached;
|
| 426 | }
|
| 427 |
|
| 428 | public EnvironmentAttributeProbe attributeProbe() {
|
| 429 | return this.attributeProbe;
|
| 430 | }
|
| 431 |
|
| 432 | public Camera.NearPlane getNearPlane(final float fov) {
|
| 433 | double aspectRatio = (double)this.projection.width() / this.projection.height();
|
| 434 | double planeHeight = Math.tan(fov * (float) (Math.PI / 180.0) / 2.0) * this.projection.zNear();
|
| 435 | double planeWidth = planeHeight * aspectRatio;
|
| 436 | Vec3 forwardsVec3 = new Vec3(this.forwards).scale(this.projection.zNear());
|
| 437 | Vec3 leftVec3 = new Vec3(this.left).scale(planeWidth);
|
| 438 | Vec3 upVec3 = new Vec3(this.up).scale(planeHeight);
|
| 439 | return new Camera.NearPlane(forwardsVec3, leftVec3, upVec3);
|
| 440 | }
|
| 441 |
|
| 442 | public FogType getFluidInCamera() {
|
| 443 | if (!this.initialized) {
|
| 444 | return FogType.NONE;
|
| 445 | }
|
| 446 |
|
| 447 | FluidState fluidState1 = this.level.getFluidState(this.blockPosition);
|
| 448 | if (fluidState1.is(FluidTags.WATER) && this.position.y < this.blockPosition.getY() + fluidState1.getHeightForCamera(this.level, this.blockPosition)) {
|
| 449 | return FogType.WATER;
|
| 450 | }
|
| 451 |
|
| 452 | Camera.NearPlane plane = this.getNearPlane(this.minecraft.options.fov().get().intValue());
|
| 453 |
|
| 454 | for (Vec3 point : Arrays.asList(plane.forward, plane.getTopLeft(), plane.getTopRight(), plane.getBottomLeft(), plane.getBottomRight())) {
|
| 455 | Vec3 offsetPos = this.position.add(point);
|
| 456 | BlockPos checkPos = BlockPos.containing(offsetPos);
|
| 457 | FluidState fluidState = this.level.getFluidState(checkPos);
|
| 458 | if (fluidState.is(FluidTags.LAVA)) {
|
| 459 | if (offsetPos.y <= fluidState.getHeightForCamera(this.level, checkPos) + checkPos.getY()) {
|
| 460 | return FogType.LAVA;
|
| 461 | }
|
| 462 | } else {
|
| 463 | BlockState state = this.level.getBlockState(checkPos);
|
| 464 | if (state.is(Blocks.POWDER_SNOW)) {
|
| 465 | return FogType.POWDER_SNOW;
|
| 466 | }
|
| 467 | }
|
| 468 | }
|
| 469 |
|
| 470 | return FogType.NONE;
|
| 471 | }
|
| 472 |
|
| 473 | public Vector3fc forwardVector() {
|
| 474 | return this.forwards;
|
| 475 | }
|
| 476 |
|
| 477 | public Vector3fc panoramicForwards() {
|
| 478 | return this.panoramicForwards;
|
| 479 | }
|
| 480 |
|
| 481 | public Vector3fc upVector() {
|
| 482 | return this.up;
|
| 483 | }
|
| 484 |
|
| 485 | public Vector3fc leftVector() {
|
| 486 | return this.left;
|
| 487 | }
|
| 488 |
|
| 489 | public void reset() {
|
| 490 | this.level = null;
|
| 491 | this.entity = null;
|
| 492 | this.attributeProbe.reset();
|
| 493 | this.initialized = false;
|
| 494 | }
|
| 495 |
|
| 496 | public void setLevel(final @Nullable ClientLevel level) {
|
| 497 | this.level = level;
|
| 498 | }
|
| 499 |
|
| 500 | public void enablePanoramicMode() {
|
| 501 | this.isPanoramicMode = true;
|
| 502 | this.panoramicForwards.set(this.forwards);
|
| 503 | }
|
| 504 |
|
| 505 | public void disablePanoramicMode() {
|
| 506 | this.isPanoramicMode = false;
|
| 507 | }
|
| 508 |
|
| 509 | @Environment(EnvType.CLIENT)
|
| 510 | public static class NearPlane {
|
| 511 | private final Vec3 forward;
|
| 512 | private final Vec3 left;
|
| 513 | private final Vec3 up;
|
| 514 |
|
| 515 | private NearPlane(final Vec3 forward, final Vec3 left, final Vec3 up) {
|
| 516 | this.forward = forward;
|
| 517 | this.left = left;
|
| 518 | this.up = up;
|
| 519 | }
|
| 520 |
|
| 521 | public Vec3 getTopLeft() {
|
| 522 | return this.forward.add(this.up).add(this.left);
|
| 523 | }
|
| 524 |
|
| 525 | public Vec3 getTopRight() {
|
| 526 | return this.forward.add(this.up).subtract(this.left);
|
| 527 | }
|
| 528 |
|
| 529 | public Vec3 getBottomLeft() {
|
| 530 | return this.forward.subtract(this.up).add(this.left);
|
| 531 | }
|
| 532 |
|
| 533 | public Vec3 getBottomRight() {
|
| 534 | return this.forward.subtract(this.up).subtract(this.left);
|
| 535 | }
|
| 536 |
|
| 537 | public Vec3 getPointOnPlane(final float x, final float y) {
|
| 538 | return this.forward.add(this.up.scale(y)).subtract(this.left.scale(x));
|
| 539 | }
|
| 540 | }
|
| 541 | }
|
| 542 |
|