| 1 | package net.minecraft.client.renderer;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.ProjectionType;
|
| 4 | import com.mojang.blaze3d.systems.RenderSystem;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import org.joml.Matrix4f;
|
| 8 |
|
| 9 | @Environment(EnvType.CLIENT)
|
| 10 | public class Projection {
|
| 11 | private ProjectionType projectionType = ProjectionType.PERSPECTIVE;
|
| 12 | private float zNear;
|
| 13 | private float zFar;
|
| 14 | private float perspectiveFov;
|
| 15 | private float width;
|
| 16 | private float height;
|
| 17 | private boolean orthoInvertY;
|
| 18 | private boolean isMatrixDirty;
|
| 19 | private final Matrix4f matrix = new Matrix4f();
|
| 20 | private long matrixVersion = -1L;
|
| 21 |
|
| 22 | public void setupPerspective(final float zNear, final float zFar, final float fov, final float width, final float height) {
|
| 23 | if (this.projectionType != ProjectionType.PERSPECTIVE
|
| 24 | || this.zNear != zNear
|
| 25 | || this.zFar != zFar
|
| 26 | || this.perspectiveFov != fov
|
| 27 | || this.width != width
|
| 28 | || this.height != height) {
|
| 29 | this.isMatrixDirty = true;
|
| 30 | this.projectionType = ProjectionType.PERSPECTIVE;
|
| 31 | this.zNear = zNear;
|
| 32 | this.zFar = zFar;
|
| 33 | this.perspectiveFov = fov;
|
| 34 | this.width = width;
|
| 35 | this.height = height;
|
| 36 | }
|
| 37 | }
|
| 38 |
|
| 39 | public void setupOrtho(final float zNear, final float zFar, final float width, final float height, final boolean invertY) {
|
| 40 | if (this.projectionType != ProjectionType.ORTHOGRAPHIC
|
| 41 | || this.zNear != zNear
|
| 42 | || this.zFar != zFar
|
| 43 | || this.width != width
|
| 44 | || this.height != height
|
| 45 | || this.orthoInvertY != invertY) {
|
| 46 | this.isMatrixDirty = true;
|
| 47 | this.projectionType = ProjectionType.ORTHOGRAPHIC;
|
| 48 | this.zNear = zNear;
|
| 49 | this.zFar = zFar;
|
| 50 | this.perspectiveFov = 0.0F;
|
| 51 | this.width = width;
|
| 52 | this.height = height;
|
| 53 | this.orthoInvertY = invertY;
|
| 54 | }
|
| 55 | }
|
| 56 |
|
| 57 | public void setSize(final float width, final float height) {
|
| 58 | this.isMatrixDirty = true;
|
| 59 | this.width = width;
|
| 60 | this.height = height;
|
| 61 | }
|
| 62 |
|
| 63 | public Matrix4f getMatrix(final Matrix4f dest) {
|
| 64 | if (!this.isMatrixDirty) {
|
| 65 | return dest.set(this.matrix);
|
| 66 | }
|
| 67 |
|
| 68 | this.isMatrixDirty = false;
|
| 69 | this.matrixVersion++;
|
| 70 | float near = this.zFar;
|
| 71 | float far = this.zNear;
|
| 72 | boolean zZeroToOne = RenderSystem.getDevice().getDeviceInfo().isZZeroToOne();
|
| 73 | return this.projectionType == ProjectionType.PERSPECTIVE
|
| 74 | ? dest.set(this.matrix.setPerspective(this.perspectiveFov * (float) (Math.PI / 180.0), this.width / this.height, near, far, zZeroToOne))
|
| 75 | : dest.set(this.matrix.setOrtho(0.0F, this.width, this.orthoInvertY ? this.height : 0.0F, this.orthoInvertY ? 0.0F : this.height, near, far, zZeroToOne));
|
| 76 | }
|
| 77 |
|
| 78 | public long getMatrixVersion() {
|
| 79 | return this.isMatrixDirty ? this.matrixVersion + 1L : this.matrixVersion;
|
| 80 | }
|
| 81 |
|
| 82 | public float zNear() {
|
| 83 | return this.zNear;
|
| 84 | }
|
| 85 |
|
| 86 | public float zFar() {
|
| 87 | return this.zFar;
|
| 88 | }
|
| 89 |
|
| 90 | public float width() {
|
| 91 | return this.width;
|
| 92 | }
|
| 93 |
|
| 94 | public float height() {
|
| 95 | return this.height;
|
| 96 | }
|
| 97 |
|
| 98 | public float fov() {
|
| 99 | return this.perspectiveFov;
|
| 100 | }
|
| 101 |
|
| 102 | public boolean invertY() {
|
| 103 | return this.orthoInvertY;
|
| 104 | }
|
| 105 | }
|
| 106 |
|