Skip to content

Commit 808b7f4

Browse files
committed
refactor: add precalculated sine/cosine to CameraRotation
1 parent ba1c781 commit 808b7f4

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

src/main/java/org/runejs/client/scene/Scene.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ public void computeTileVisibilityMaps(int minHeight, int maxHeight, int width, i
146146
for (int pitch = 128; pitch <= 384; pitch += 32) {
147147
// Iterating over different camera yaw angles (from 0 to 2048)
148148
for (int yaw = 0; yaw < 2048; yaw += 64) {
149-
currentPitchSine = Model.SINE[pitch];
150-
currentPitchCosine = Model.COSINE[pitch];
151-
currentYawSine = Model.SINE[yaw];
152-
currentYawCosine = Model.COSINE[yaw];
149+
CameraRotation camera = new CameraRotation(yaw, pitch);
153150

154151
int pitchIndex = (pitch - 128) / 32;
155152
int yawIndex = yaw / 64;
@@ -163,7 +160,7 @@ public void computeTileVisibilityMaps(int minHeight, int maxHeight, int width, i
163160

164161
// Checking visibility at different heights
165162
for (int h = -minHeight; h <= maxHeight; h += 128) {
166-
if (isPointVisibleOnScreen(absoluteTileX, arg0[pitchIndex] + h, absoluteTileY)) {
163+
if (isPointVisibleOnScreen(absoluteTileX, arg0[pitchIndex] + h, absoluteTileY, camera)) {
167164
isVisible = true;
168165
break;
169166
}
@@ -234,14 +231,14 @@ public static int adjustLightness(int hsl, int lightness) {
234231
* @param z The z-coordinate of the point in 3D space.
235232
* @return Returns true if the projected point falls within the screen boundaries; otherwise false.
236233
*/
237-
public boolean isPointVisibleOnScreen(int x, int y, int z) {
234+
public boolean isPointVisibleOnScreen(int x, int y, int z, CameraRotation cameraRotation) {
238235
// Rotate around the X axis
239-
int rotatedX = z * currentYawSine + x * currentYawCosine >> 16;
240-
int rotatedZ = z * currentYawCosine - x * currentYawSine >> 16;
236+
int rotatedX = z * cameraRotation.yawSine + x * cameraRotation.yawCosine >> 16;
237+
int rotatedZ = z * cameraRotation.yawCosine - x * cameraRotation.yawSine >> 16;
241238

242239
// Rotate around the Y axis
243-
int rotatedY = y * currentPitchSine + rotatedZ * currentPitchCosine >> 16;
244-
int finalZ = y * currentPitchCosine - rotatedZ * currentPitchSine >> 16;
240+
int rotatedY = y * cameraRotation.pitchSine + rotatedZ * cameraRotation.pitchCosine >> 16;
241+
int finalZ = y * cameraRotation.pitchCosine - rotatedZ * cameraRotation.pitchSine >> 16;
245242

246243
// Check if the point is behind the near clipping plane (too close to the camera)
247244
if (rotatedY < 50/* || rotatedY > 3500*/) {

src/main/java/org/runejs/client/scene/camera/CameraRotation.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package org.runejs.client.scene.camera;
22

3+
import org.runejs.client.media.renderable.Model;
4+
35
public class CameraRotation {
46
public final int yaw;
7+
public final int yawSine;
8+
public final int yawCosine;
59
public final int pitch;
10+
public final int pitchSine;
11+
public final int pitchCosine;
612

713
public CameraRotation(int yaw, int pitch) {
814
this.yaw = yaw & 0x7ff;
915
this.pitch = pitch;
16+
17+
pitchSine = Model.SINE[pitch];
18+
pitchCosine = Model.COSINE[pitch];
19+
yawSine = Model.SINE[yaw];
20+
yawCosine = Model.COSINE[yaw];
1021
}
1122

1223
public CameraRotation add(CameraRotation other) {

0 commit comments

Comments
 (0)