Skip to content

Commit

Permalink
Better scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
MEEPofFaith committed Nov 19, 2024
1 parent 12ccdbf commit 21f2d67
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/progressed/graphics/draw3d/Lines3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static int linePointCount(float x1, float y1, float z1, float x2, float y
}

public static float[] linePoints(float x1, float y1, float z1, float x2, float y2, float z2, int pointCount){
if(z1 > z2){ //Always return from bottom to top
if(z1 > z2){ //Always order from bottom to top. Needed for viewport check.
float tx = x1, ty = y1, tz = z1;
x1 = x2;
y1 = y2;
Expand All @@ -78,7 +78,7 @@ public static float[] linePoints(float x1, float y1, float z1, float x2, float y
}

float vz = Perspective.viewportZ();
if(z2 > vz){ //If line goes above viewport height, scale to viewport height.
if(z2 > vz){ //If line goes above viewport, scale to viewport z.
float scl = vz / (z2 - z1);
x2 = x1 + (x2 - x1) * scl;
y2 = y1 + (y2 - y1) * scl;
Expand Down
25 changes: 15 additions & 10 deletions src/progressed/graphics/draw3d/Perspective.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public class Perspective{
public static float viewportOffset = 8f;
/** Field of View in degrees */
public static float fov = settings.getInt("pm-fov", 60);
public static float fadeDst = 128f;
public static float maxScale = 8f;

/** @return If the z coordinate is below the viewport height. */
public static boolean canDraw(float z){
Expand Down Expand Up @@ -44,23 +46,26 @@ public static float scale(float x, float y, float z){

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

float px = x / zz * cz; //Position scaled to far plane.
float py = y / zz * cz;

float px = x / z * cz;
float py = y / z * cz;
float vx = x / zz * viewportOffset; //Position scaled to near plane.
float vy = y / zz * viewportOffset;

float c1 = Mathf.dst(x, y), c2 = Mathf.dst(px, py);
float d1 = Mathf.dst(z, c1), d2 = Mathf.dst(cz, c2);
float d1 = Math3D.dst(vx, vy, cz - viewportOffset, x, y, z);
float d2 = Math3D.dst(vx, vy, cz - viewportOffset, px, py, 0);

return d2 / d1;
return 1f + (1f - d1 / d2) * (maxScale - 1f);
}

/** Fade out based on distance to viewport. */
public static float alpha(float x, float y, float z){
float cx = camera.position.x, cy = camera.position.y;
float cz = cameraZ();

float d1 = Math3D.dst(x, y, z, cx, cy, cz); //Distance between camera point
float d1 = Math3D.dst(x, y, z, cx, cy, cz); //Distance between camera and far point

x -= cx;
y -= cy;
Expand All @@ -72,14 +77,14 @@ public static float alpha(float x, float y, float z){
float d2 = Math3D.dst(vx, vy, z); //Distance between camera and viewport pos

float dst = d1 - d2;
float fadeDst = (cz - viewportOffset) / 8f;
float fade = Math.min(fadeDst, cz - viewportOffset);

if(dst > fadeDst){
if(dst > fade){
return 1f;
}else if(!canDraw(z)){ //Behind viewport, should be 0
return 0f;
}else{
return Mathf.clamp(dst / fadeDst);
return Mathf.clamp(dst / fade);
}
}

Expand Down

0 comments on commit 21f2d67

Please sign in to comment.