1
1
package progressed .graphics .draw3d ;
2
2
3
+ import arc .*;
3
4
import arc .math .*;
4
5
import arc .math .geom .*;
6
+ import mindustry .game .EventType .*;
5
7
import progressed .util .*;
6
8
7
9
import static arc .Core .*;
10
+ import static mindustry .Vars .*;
8
11
9
12
public class Perspective {
10
13
private static final Vec2 offsetPos = new Vec2 ();
11
14
/** Viewport offset from the camera height in world units. */
12
15
public static float viewportOffset = 8f ;
13
16
/** Field of View in degrees */
14
- public static float fov = settings . getInt ( "pm-fov" , 60 ) ;
17
+ public static float fov = - 1f ;
15
18
public static float fadeDst = 1024f ;
16
19
public static float maxScale = 8f ;
17
20
21
+ private static float lastScale ;
22
+ private static float cameraZ ;
23
+ private static Vec2 viewportSize = new Vec2 ();
24
+
25
+ static {
26
+ if (!headless ){
27
+ Events .run (Trigger .preDraw , () -> {
28
+ int newFov = settings .getInt ("pm-fov" , 60 );
29
+ if (renderer .getDisplayScale () != lastScale || newFov != fov ){
30
+ lastScale = renderer .getDisplayScale ();
31
+ fov = newFov ;
32
+ cameraZ = calcCameraZ ();
33
+ viewportSize ();
34
+ }
35
+ });
36
+ }
37
+ }
38
+
18
39
/** @return If the z coordinate is below the viewport height. */
19
40
public static boolean canDraw (float z ){
20
- return z < cameraZ () - viewportOffset ;
41
+ return z < cameraZ - viewportOffset ;
21
42
}
22
43
23
44
/** @return Perspective projected coordinates to draw at. */
24
45
public static Vec2 drawPos (float x , float y , float z ){
25
46
//viewport
26
- Vec2 v = viewportSize ();
27
- float vw = v .x , vh = v .y ;
47
+ float vw = viewportSize .x , vh = viewportSize .y ;
28
48
float cx = camera .position .x , cy = camera .position .y ;
29
- float cz = cameraZ () ;
49
+ float cz = cameraZ ;
30
50
31
51
x -= cx ;
32
52
y -= cy ;
@@ -42,7 +62,7 @@ public static Vec2 drawPos(float x, float y, float z){
42
62
/** Multiplicative size scale at a point. */
43
63
public static float scale (float x , float y , float z ){
44
64
float cx = camera .position .x , cy = camera .position .y ;
45
- float cz = cameraZ () ;
65
+ float cz = cameraZ ;
46
66
47
67
x -= cx ;
48
68
y -= cy ;
@@ -63,7 +83,7 @@ public static float scale(float x, float y, float z){
63
83
/** Fade out based on distance to viewport. */
64
84
public static float alpha (float x , float y , float z ){
65
85
float cx = camera .position .x , cy = camera .position .y ;
66
- float cz = cameraZ () ;
86
+ float cz = cameraZ ;
67
87
68
88
x -= cx ;
69
89
y -= cy ;
@@ -85,32 +105,36 @@ public static float alpha(float x, float y, float z){
85
105
}
86
106
}
87
107
88
- /**
89
- * Calculates the camera z coordinate based on FOV and the size of the vanilla camera.
90
- * @return camera z coordinate
91
- * */
92
108
public static float cameraZ (){
93
- float width = Math .max (camera .width , camera .height ) / 2f ;
94
- //TOA
95
- return (float )(width / Math .tan (fov / 2f * Mathf .degRad ));
109
+ return cameraZ ;
96
110
}
97
111
98
112
/**
99
113
* @return viewport z coordinate
100
114
*/
101
115
public static float viewportZ (){
102
- return cameraZ () - viewportOffset ;
116
+ return cameraZ - viewportOffset ;
103
117
}
104
118
105
119
/** Calculates the size of the viewport. */
106
- public static Vec2 viewportSize (){
120
+ public static void viewportSize (){
107
121
float v1 = (float )(Math .tan (fov / 2f * Mathf .degRad ) * viewportOffset * 2f );
108
122
if (camera .width >= camera .height ){
109
123
float v2 = v1 * (camera .height / camera .width );
110
- return offsetPos .set (v1 , v2 );
124
+ viewportSize .set (v1 , v2 );
111
125
}else {
112
126
float v2 = v1 * (camera .width / camera .height );
113
- return offsetPos .set (v2 , v1 );
127
+ viewportSize .set (v2 , v1 );
114
128
}
115
129
}
130
+
131
+ /**
132
+ * Calculates the camera z coordinate based on FOV and the size of the vanilla camera.
133
+ * @return camera z coordinate
134
+ * */
135
+ private static float calcCameraZ (){
136
+ float width = Math .max (camera .width , camera .height ) / 2f ;
137
+ //TOA
138
+ return (float )(width / Math .tan (fov / 2f * Mathf .degRad ));
139
+ }
116
140
}
0 commit comments