From b77461a3e0e363355f2c1013c97ef62b8ce687de Mon Sep 17 00:00:00 2001 From: Max Weber Date: Mon, 9 Sep 2019 10:33:49 -0600 Subject: [PATCH] Perspective: Add modelToCanvas This has significantly less overhead than repeatedly calling localToCanvas, and may be vectorized in the future by a smarter jit --- .../java/net/runelite/api/Perspective.java | 67 +++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/runelite-api/src/main/java/net/runelite/api/Perspective.java b/runelite-api/src/main/java/net/runelite/api/Perspective.java index 45469cc00c..e414e89c0d 100644 --- a/runelite-api/src/main/java/net/runelite/api/Perspective.java +++ b/runelite-api/src/main/java/net/runelite/api/Perspective.java @@ -148,7 +148,74 @@ public class Perspective } return null; + } + /** + * Translates a model's vertices into 2d space + */ + public static void modelToCanvas(Client client, int end, int x3dCenter, int y3dCenter, int z3dCenter, int rotate, int[] x3d, int[] y3d, int[] z3d, int[] x2d, int[] y2d) + { + final int + cameraPitch = client.getCameraPitch(), + cameraYaw = client.getCameraYaw(), + + pitchSin = SINE[cameraPitch], + pitchCos = COSINE[cameraPitch], + yawSin = SINE[cameraYaw], + yawCos = COSINE[cameraYaw], + rotateSin = SINE[rotate], + rotateCos = COSINE[rotate], + + cx = x3dCenter - client.getCameraX(), + cy = y3dCenter - client.getCameraY(), + cz = z3dCenter - client.getCameraZ(), + + viewportXMiddle = client.getViewportWidth() / 2, + viewportYMiddle = client.getViewportHeight() / 2, + viewportXOffset = client.getViewportXOffset(), + viewportYOffset = client.getViewportYOffset(), + + zoom3d = client.getScale(); + + for (int i = 0; i < end; i++) + { + int x = x3d[i]; + int y = y3d[i]; + int z = z3d[i]; + + if (rotate != 0) + { + int x0 = x; + x = x0 * rotateCos + y * rotateSin >> 16; + y = y * rotateCos - x0 * rotateSin >> 16; + } + + x += cx; + y += cy; + z += cz; + + final int + x1 = x * yawCos + y * yawSin >> 16, + y1 = y * yawCos - x * yawSin >> 16, + y2 = z * pitchCos - y1 * pitchSin >> 16, + z1 = y1 * pitchCos + z * pitchSin >> 16; + + int viewX, viewY; + + if (z1 < 50) + { + viewX = Integer.MIN_VALUE; + viewY = Integer.MIN_VALUE; + } + else + { + viewX = (viewportXMiddle + x1 * zoom3d / z1) + viewportXOffset; + viewY = (viewportYMiddle + y2 * zoom3d / z1) + viewportYOffset; + } + + x2d[i] = viewX; + y2d[i] = viewY; + } } /**