Skip to content

Commit

Permalink
Proper slantTube draw
Browse files Browse the repository at this point in the history
  • Loading branch information
MEEPofFaith committed Nov 21, 2024
1 parent 21f2d67 commit 99c5889
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

public class SkyBeamBulletType extends BulletType{
public float z = 50f * tilesize;
public float offset = 0.25f;
public float radius = tilesize;
public boolean bloom = true;
public Color baseColor = PMPal.nexusLaserDark;
Expand Down Expand Up @@ -41,6 +40,6 @@ public void init(){
public void draw(Bullet b){
super.draw(b);

Draw3D.highBloom(bloom, () -> Fill3D.slantTube(b.x, b.y, b.originX, b.originY, z, radius, baseColor, topColor, offset));
Draw3D.highBloom(bloom, () -> Fill3D.slantTube(b.x, b.y, b.originX, b.originY, z, radius, baseColor, topColor));
}
}
1 change: 1 addition & 0 deletions src/progressed/graphics/draw3d/Draw3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public static void drawDiskDebug(float x1, float y1, float x2, float y2, float z
Lines.stroke(3f);
Draw.color(Color.white);
int vertCount = Lines.circleVertices(rad * Perspective.scale(x2, y2, z2));
if(vertCount < 0) return;
float[] verts = Fill3D.diskVertices(x2, y2, z2, rotation, 0f, tilt, rad, vertCount);
for(int i = 0; i <= vertCount; i += 3){
float px2, py2, pz2;
Expand Down
89 changes: 55 additions & 34 deletions src/progressed/graphics/draw3d/Fill3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import progressed.graphics.*;

import static arc.math.Mathf.*;

Expand All @@ -15,6 +14,35 @@ public class Fill3D{
private static final Vec3 axis = new Vec3();
private static final Vec3 rim = new Vec3();

public static void quad(float x1, float y1, float z1, Color c1,
float x2, float y2, float z2, Color c2,
float x3, float y3, float z3, Color c3,
float x4, float y4, float z4, Color c4){
Vec2 pos = Perspective.drawPos(x1, y1, z1);
float dx1 = pos.x, dy1 = pos.y, a1 = Perspective.alpha(x1, y1, z1);
pos = Perspective.drawPos(x2, y2, z2);
float dx2 = pos.x, dy2 = pos.y, a2 = Perspective.alpha(x2, y2, z2);
pos = Perspective.drawPos(x3, y3, z3);
float dx3 = pos.x, dy3 = pos.y, a3 = Perspective.alpha(x3, y3, z3);
pos = Perspective.drawPos(x4, y4, z4);
float dx4 = pos.x, dy4 = pos.y, a4 = Perspective.alpha(x4, y4, z4);

Fill.quad(
dx1, dy1, tmpCol.set(c1).mulA(a1).toFloatBits(),
dx2, dy2, tmpCol.set(c2).mulA(a2).toFloatBits(),
dx3, dy3, tmpCol.set(c3).mulA(a3).toFloatBits(),
dx4, dy4, tmpCol.set(c4).mulA(a4).toFloatBits()
);
}

public static void quad(float x1, float y1, float z1,
float x2, float y2, float z2,
float x3, float y3, float z3,
float x4, float y4, float z4){
Color color = Draw.getColor();
quad(x1, y1, z1, color, x2, y2, z2, color, x3, y3, z3, color, x4, y4, z4, color);
}

public static void tube(float x, float y, float rad, float z2, Color baseColorLight, Color baseColorDark, Color topColorLight, Color topColorDark){
int vert = Lines.circleVertices(rad);
float space = 360f / vert;
Expand Down Expand Up @@ -51,50 +79,43 @@ public static void tube(float x, float y, float rad, float z2, Color baseColor,
tube(x, y, rad, z2, baseColor, baseColor, topColor, topColor);
}

public static void slantTube(float x1, float y1, float x2, float y2, float z2, float rad, Color baseColor, Color topColor, float offset){
public static void slantTube(float x1, float y1, float x2, float y2, float z2, float rad, Color baseColor, Color topColor){
//Draw
float scl = Perspective.scale(x2, y2, z2);
int verts = Lines.circleVertices(rad * scl);
if(scl < 0) return;

float rotation = Angles.angle(x2, y2, x1, y1);
float tilt = 90f - Angles.angle(Mathf.dst(x1, y1, x2, y2), z2);
Vec2 pos = Perspective.drawPos(x2, y2, z2);
float startAngle = tubeStartAngle(pos.x, pos.y, x1, y1, rad * scl, rad);

int verts = Lines.circleVertices(rad * scl);
float[] castVerts = castVertices(x1, y1, rotation, startAngle, tilt, rad, verts);
float[] diskVerts = diskVertices(x2, y2, z2, rotation, startAngle, tilt, rad, verts);
float hAlpha = Perspective.alpha(x2, y2, z2 * offset);
float baseCol = Tmp.c1.set(baseColor).mulA(hAlpha).toFloatBits();
float topCol = Tmp.c1.set(topColor).mulA(hAlpha).toFloatBits();
for(int i = 0; i < verts - 1; i++){
int i2 = i + 1;
float bx1 = castVerts[i * 2],
by1 = castVerts[i * 2 + 1],
bx2 = castVerts[i2 * 2],
by2 = castVerts[i2 * 2 + 1];
Tmp.v1.set(Perspective.drawPos(diskVerts[i * 3], diskVerts[i * 3 + 1], diskVerts[i * 3 + 2]));
Tmp.v2.set(Perspective.drawPos(diskVerts[i2 * 3], diskVerts[i2 * 3 + 1], diskVerts[i2 * 3 + 2]));
if(offset > 0f){
Tmp.v1.lerp(bx1, by1, offset);
Tmp.v2.lerp(bx2, by2, offset);
}
int segments = Lines3D.linePointCount(x1, y1, 0, x2, y2, z2);

Fill.quad(
bx1, by1, baseCol,
bx2, by2, baseCol,
Tmp.v2.x, Tmp.v2.y, topCol,
Tmp.v1.x, Tmp.v1.y, topCol
for(int i = 0; i < verts; i++){
float[] v1 = Lines3D.linePoints(
castVerts[i * 2], castVerts[i * 2 + 1], 0,
diskVerts[i * 3], diskVerts[i * 3 + 1], diskVerts[i * 3 + 2], segments
);
int i2 = i == verts - 1 ? 0 : i + 1;
float[] v2 = Lines3D.linePoints(
castVerts[i2 * 2], castVerts[i2 * 2 + 1], 0,
diskVerts[i2 * 3], diskVerts[i2 * 3 + 1], diskVerts[i2 * 3 + 2], segments
);
for(int j = 0; j < segments - 3; j++){
int jj = j * 3;
Color c1 = Tmp.c1.set(baseColor).lerp(topColor, (float)jj / segments);
Color c2 = Tmp.c2.set(baseColor).lerp(topColor, (float)(jj + 3) / segments);
quad(
v1[jj], v1[jj + 1], v1[jj + 2], c1,
v2[jj], v2[jj + 1], v2[jj + 2], c1,
v2[jj + 3], v2[jj + 3 + 1], v2[jj + 3 + 2], c2,
v1[jj + 3], v1[jj + 3 + 1], v1[jj + 3 + 2], c2
);
}
}
//Debug
Draw.z(PMLayer.skyBloom + 10);
Lines.stroke(4);
Draw.color(Color.black);
Lines.line(x1, y1, castVerts[0], castVerts[1]);
Lines3D.line(x2, y2, z2, diskVerts[0], diskVerts[1], diskVerts[2]);
Lines3D.line(castVerts[0], castVerts[1], 0, diskVerts[0], diskVerts[1], diskVerts[2]);
}

public static void slantTube(float x1, float y1, float x2, float y2, float z, float rad, Color baseColor, Color topColor){
slantTube(x1, y1, x2, y2, z, rad, baseColor, topColor, 0f);
}

public static float[] diskVertices(float x, float y, float z, float rotation, float startAngle, float tilt, float rad, int verts){
Expand Down
7 changes: 5 additions & 2 deletions src/progressed/graphics/draw3d/Lines3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import arc.util.*;
import progressed.util.*;

import static mindustry.Vars.tilesize;
import static mindustry.Vars.*;

public class Lines3D{
public static void line(float x1, float y1, float z1, float x2, float y2, float z2, int pointCount, boolean scale){
Expand Down Expand Up @@ -83,7 +83,6 @@ public static float[] linePoints(float x1, float y1, float z1, float x2, float y
x2 = x1 + (x2 - x1) * scl;
y2 = y1 + (y2 - y1) * scl;
z2 = vz;
pointCount = Mathf.ceil(pointCount * scl);
}

float[] points = new float[pointCount * 3];
Expand All @@ -99,4 +98,8 @@ public static float[] linePoints(float x1, float y1, float z1, float x2, float y

return points;
}

public static float[] linePoints(float x1, float y1, float z1, float x2, float y2, float z2){
return linePoints(x1, y1, z1, x2, y2, z2, linePointCount(x1, y1, z1, x2, y2, z2));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package progressed.world.blocks.defence.turret.testing;

import progressed.entities.bullet.pseudo3d.*;
import progressed.graphics.draw3d.*;

import static mindustry.Vars.*;

Expand All @@ -15,7 +14,7 @@ public SkyLaserTestTurret(String name){
lifetime = bRange / speed;
radius = 1.5f * tilesize;
drag = -0.01f;
z = 1200;
z = 2000;
}};
reload = 45f;
shootY = 0f;
Expand All @@ -27,8 +26,8 @@ public void draw(){
super.draw();

SkyBeamBulletType type = (SkyBeamBulletType)shootType;
Draw3D.drawLineDebug(x, y, type.z, targetPos.x, targetPos.y, 0);
Draw3D.drawDiskDebug(targetPos.x, targetPos.y, x, y, type.z, type.radius);
//Draw3D.drawLineDebug(x, y, type.z, targetPos.x, targetPos.y, 0);
//Draw3D.drawDiskDebug(targetPos.x, targetPos.y, x, y, type.z, type.radius);
}
}
}

0 comments on commit 99c5889

Please sign in to comment.