Skip to content

Commit

Permalink
Scale trails to viewport + Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MEEPofFaith committed Nov 21, 2024
1 parent 01fc17c commit 67746bb
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
22 changes: 11 additions & 11 deletions src/progressed/graphics/draw3d/Perspective.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

public class Perspective{
private static final Vec2 offsetPos = new Vec2();
private static final Vec3 scalingPos = new Vec3();
/** Viewport offset from the camera height in world units. */
public static float viewportOffset = 16f;
/** Field of View in degrees */
Expand Down Expand Up @@ -46,16 +47,9 @@ public static Vec2 drawPos(float x, float y, float z){
//viewport
float vw = viewportSize.x, vh = viewportSize.y;
float cx = camera.position.x, cy = camera.position.y;
float cz = cameraZ;

x -= cx;
y -= cy;
z = cz - z;

float vx = x / z * viewportOffset,
vy = y / z * viewportOffset;
Vec3 scaled = scaleToViewport(x, y, z);

offsetPos.set(vx / vw * camera.width, vy / vh * camera.height).add(cx, cy);
offsetPos.set(scaled.x / vw * camera.width, scaled.y / vh * camera.height).add(cx, cy);
return offsetPos;
}

Expand Down Expand Up @@ -122,7 +116,7 @@ public static void viewportSize(){
}
}

public static float dstToViewport(float x, float y, float z){
public static Vec3 scaleToViewport(float x, float y, float z){
float cx = camera.position.x, cy = camera.position.y;
float cz = cameraZ;

Expand All @@ -134,7 +128,13 @@ public static float dstToViewport(float x, float y, float z){
vy = y / zz * viewportOffset;
float vz = viewportZ();

return Math3D.dst(x, y, z, vx, vy, vz); //Distance between viewport and pos
return scalingPos.set(vx, vy, vz);
}

public static float dstToViewport(float x, float y, float z){
Vec3 scaled = scaleToViewport(x, y, z);
float cx = camera.position.x, cy = camera.position.y;
return scaled.dst(x - cx, y - cy, z);
}

/**
Expand Down
43 changes: 31 additions & 12 deletions src/progressed/graphics/trails/ZTrail.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,35 +73,53 @@ public void draw(Color color, float width, boolean fade){
float lastAngle = 0;
float[] items = points.items;
float size = width / (int)(points.size / 4);
float vz = Perspective.viewportZ();
boolean calcLast = true;

for(int i = 0; i < points.size; i += 4){
float px1 = items[i], py1 = items[i + 1], z1 = items[i + 3];
float z2 = i < points.size - 4 ? items[i + 4 + 3] : lastZ;

if(z1 > vz && z2 > vz){
calcLast = true;
continue;
}

if(z1 > vz){ //Scale to near plane
Vec3 pos = Perspective.scaleToViewport(px1, py1, z1);
px1 = pos.x;
py1 = pos.y;
z1 = pos.z;
}

Vec2 pos1 = Perspective.drawPos(px1, py1, z1);
float x1 = pos1.x, y1 = pos1.y, w1 = Perspective.scale(items[i], items[i + 1], z1);
float x2, y2, px2, py2, w2, z2;
float px2, py2, x2, y2, w2;

//last position is always lastX/Y/W
if(i < points.size - 4){
px2 = items[i + 4];
py2 = items[i + 4 + 1];
z2 = items[i + 4 + 3];
Vec2 pos2 = Perspective.drawPos(px2, py2, z2);
x2 = pos2.x;
y2 = pos2.y;
w2 = Perspective.scale(items[i + 4], items[i + 4 + 1], z2);
}else{
px2 = lastX;
py2 = lastY;
z2 = lastZ;
Vec2 pos2 = Perspective.drawPos(px2, py2, z2);
x2 = pos2.x;
y2 = pos2.y;
w2 = Perspective.scale(lastX, lastY, z2);
}

if(z2 > vz){ //Scale to near plane
Vec3 pos = Perspective.scaleToViewport(px2, py2, z2);
px2 = pos.x;
py2 = pos.y;
z2 = pos.z;
}

Vec2 pos2 = Perspective.drawPos(px2, py2, z2);
x2 = pos2.x;
y2 = pos2.y;
w2 = Perspective.scale(px2, py2, z2);

float a2 = -Angles.angleRad(x1, y1, x2, y2);
//end of the trail (i = 0) has the same angle as the next.
float a1 = i == 0 ? a2 : lastAngle;
float a1 = calcLast ? a2 : lastAngle;
if(w1 <= 0.001f || w2 <= 0.001f) continue;

float
Expand All @@ -125,6 +143,7 @@ public void draw(Color color, float width, boolean fade){
);

lastAngle = a2;
calcLast = false;
}

Draw.color();
Expand Down

0 comments on commit 67746bb

Please sign in to comment.