Skip to content

Commit edd9e18

Browse files
committed
Cache some values
1 parent 14d5130 commit edd9e18

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

src/progressed/graphics/draw3d/Perspective.java

+42-18
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,52 @@
11
package progressed.graphics.draw3d;
22

3+
import arc.*;
34
import arc.math.*;
45
import arc.math.geom.*;
6+
import mindustry.game.EventType.*;
57
import progressed.util.*;
68

79
import static arc.Core.*;
10+
import static mindustry.Vars.*;
811

912
public class Perspective{
1013
private static final Vec2 offsetPos = new Vec2();
1114
/** Viewport offset from the camera height in world units. */
1215
public static float viewportOffset = 8f;
1316
/** Field of View in degrees */
14-
public static float fov = settings.getInt("pm-fov", 60);
17+
public static float fov = -1f;
1518
public static float fadeDst = 1024f;
1619
public static float maxScale = 8f;
1720

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+
1839
/** @return If the z coordinate is below the viewport height. */
1940
public static boolean canDraw(float z){
20-
return z < cameraZ() - viewportOffset;
41+
return z < cameraZ - viewportOffset;
2142
}
2243

2344
/** @return Perspective projected coordinates to draw at. */
2445
public static Vec2 drawPos(float x, float y, float z){
2546
//viewport
26-
Vec2 v = viewportSize();
27-
float vw = v.x, vh = v.y;
47+
float vw = viewportSize.x, vh = viewportSize.y;
2848
float cx = camera.position.x, cy = camera.position.y;
29-
float cz = cameraZ();
49+
float cz = cameraZ;
3050

3151
x -= cx;
3252
y -= cy;
@@ -42,7 +62,7 @@ public static Vec2 drawPos(float x, float y, float z){
4262
/** Multiplicative size scale at a point. */
4363
public static float scale(float x, float y, float z){
4464
float cx = camera.position.x, cy = camera.position.y;
45-
float cz = cameraZ();
65+
float cz = cameraZ;
4666

4767
x -= cx;
4868
y -= cy;
@@ -63,7 +83,7 @@ public static float scale(float x, float y, float z){
6383
/** Fade out based on distance to viewport. */
6484
public static float alpha(float x, float y, float z){
6585
float cx = camera.position.x, cy = camera.position.y;
66-
float cz = cameraZ();
86+
float cz = cameraZ;
6787

6888
x -= cx;
6989
y -= cy;
@@ -85,32 +105,36 @@ public static float alpha(float x, float y, float z){
85105
}
86106
}
87107

88-
/**
89-
* Calculates the camera z coordinate based on FOV and the size of the vanilla camera.
90-
* @return camera z coordinate
91-
* */
92108
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;
96110
}
97111

98112
/**
99113
* @return viewport z coordinate
100114
*/
101115
public static float viewportZ(){
102-
return cameraZ() - viewportOffset;
116+
return cameraZ - viewportOffset;
103117
}
104118

105119
/** Calculates the size of the viewport. */
106-
public static Vec2 viewportSize(){
120+
public static void viewportSize(){
107121
float v1 = (float)(Math.tan(fov / 2f * Mathf.degRad) * viewportOffset * 2f);
108122
if(camera.width >= camera.height){
109123
float v2 = v1 * (camera.height / camera.width);
110-
return offsetPos.set(v1, v2);
124+
viewportSize.set(v1, v2);
111125
}else{
112126
float v2 = v1 * (camera.width / camera.height);
113-
return offsetPos.set(v2, v1);
127+
viewportSize.set(v2, v1);
114128
}
115129
}
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+
}
116140
}

src/progressed/util/PMSettings.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import mindustry.gen.*;
66
import mindustry.ui.dialogs.SettingsMenuDialog.*;
77
import mindustry.ui.dialogs.SettingsMenuDialog.SettingsTable.*;
8-
import progressed.graphics.draw3d.*;
98

109
import static arc.Core.*;
1110
import static mindustry.Vars.*;
@@ -16,10 +15,7 @@ public static void init(){
1615
t.pref(new Separator("pm-graphics-settings"));
1716
t.sliderPref("pm-sword-opacity", 100, 20, 100, 5, s -> s + "%");
1817
t.sliderPref("pm-zone-opacity", 100, 0, 100, 5, s -> s + "%");
19-
t.sliderPref("pm-fov", 60, 10, 120, 5, s -> {
20-
Perspective.fov = s; //Cursed but it works.
21-
return s + " deg";
22-
});
18+
t.sliderPref("pm-fov", 60, 10, 120, 5, s -> s + " deg");
2319
t.checkPref("pm-tesla-range", true);
2420
t.pref(new Separator("pm-other-settings"));
2521
t.checkPref("pm-farting", false, b -> Sounds.wind3.play(Interp.pow2In.apply(Core.settings.getInt("sfxvol") / 100f) * 5f));

0 commit comments

Comments
 (0)