Fixed DebugDraw line drawing method
Now lines should always face the camera.
This commit is contained in:
parent
7f3a119d26
commit
afdc54bc35
|
@ -3,10 +3,10 @@
|
|||
in vec3 pos;
|
||||
in vec3 col;
|
||||
|
||||
uniform mat4 VP;
|
||||
uniform mat4 ViewProjection;
|
||||
out vec3 color;
|
||||
|
||||
void main() {
|
||||
color = col;
|
||||
gl_Position = VP * vec4(pos, 1.0);
|
||||
gl_Position = ViewProjection * vec4(pos, 1.0);
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ import kha.graphics4.CompareMode;
|
|||
import kha.graphics4.CullMode;
|
||||
import iron.math.Vec4;
|
||||
import iron.math.Mat4;
|
||||
using armory.object.TransformExtension;
|
||||
|
||||
class DebugDraw {
|
||||
|
||||
|
@ -54,7 +55,7 @@ class DebugDraw {
|
|||
pipeline.depthMode = CompareMode.Less;
|
||||
pipeline.cullMode = CullMode.None;
|
||||
pipeline.compile();
|
||||
vpID = pipeline.getConstantLocation("VP");
|
||||
vpID = pipeline.getConstantLocation("ViewProjection");
|
||||
vp = Mat4.identity();
|
||||
|
||||
vertexBuffer = new VertexBuffer(maxVertices, structure, Usage.DynamicUsage);
|
||||
|
@ -73,19 +74,19 @@ class DebugDraw {
|
|||
});
|
||||
}
|
||||
|
||||
static var v: Vec4;
|
||||
static var objPosition: Vec4;
|
||||
static var vx = new Vec4();
|
||||
static var vy = new Vec4();
|
||||
static var vz = new Vec4();
|
||||
public function bounds(t: iron.object.Transform) {
|
||||
v = t.world.getLoc();
|
||||
var dx = t.dim.x / 2;
|
||||
var dy = t.dim.y / 2;
|
||||
var dz = t.dim.z / 2;
|
||||
public function bounds(transform: iron.object.Transform) {
|
||||
objPosition = transform.getWorldPosition();
|
||||
var dx = transform.dim.x / 2;
|
||||
var dy = transform.dim.y / 2;
|
||||
var dz = transform.dim.z / 2;
|
||||
|
||||
var up = t.world.up();
|
||||
var look = t.world.look();
|
||||
var right = t.world.right();
|
||||
var up = transform.world.up();
|
||||
var look = transform.world.look();
|
||||
var right = transform.world.right();
|
||||
up.normalize();
|
||||
look.normalize();
|
||||
right.normalize();
|
||||
|
@ -117,12 +118,12 @@ class DebugDraw {
|
|||
static var v2 = new Vec4();
|
||||
static var t = new Vec4();
|
||||
function lineb(a: Int, b: Int, c: Int, d: Int, e: Int, f: Int) {
|
||||
v1.setFrom(v);
|
||||
v1.setFrom(objPosition);
|
||||
t.setFrom(vx); t.mult(a); v1.add(t);
|
||||
t.setFrom(vy); t.mult(b); v1.add(t);
|
||||
t.setFrom(vz); t.mult(c); v1.add(t);
|
||||
|
||||
v2.setFrom(v);
|
||||
v2.setFrom(objPosition);
|
||||
t.setFrom(vx); t.mult(d); v2.add(t);
|
||||
t.setFrom(vy); t.mult(e); v2.add(t);
|
||||
t.setFrom(vz); t.mult(f); v2.add(t);
|
||||
|
@ -134,42 +135,44 @@ class DebugDraw {
|
|||
line(v1.x, v1.y, v1.z, v2.x, v2.y, v2.z);
|
||||
}
|
||||
|
||||
static var midPoint = new Vec4();
|
||||
static var midLine = new Vec4();
|
||||
static var corner1 = new Vec4();
|
||||
static var corner2 = new Vec4();
|
||||
static var corner3 = new Vec4();
|
||||
static var corner4 = new Vec4();
|
||||
static var cameraLook = new Vec4();
|
||||
public function line(x1: Float, y1: Float, z1: Float, x2: Float, y2: Float, z2: Float) {
|
||||
|
||||
if (lines >= maxLines) { end(); begin(); }
|
||||
|
||||
midPoint.set(x1 + x2, y1 + y2, z1 + z2);
|
||||
midPoint.mult(0.5);
|
||||
|
||||
midLine.set(x1, y1, z1);
|
||||
midLine.sub(midPoint);
|
||||
|
||||
var camera = iron.Scene.active.camera;
|
||||
var l = camera.right();
|
||||
l.add(camera.up());
|
||||
cameraLook = camera.transform.getWorldPosition();
|
||||
cameraLook.sub(midPoint);
|
||||
|
||||
var lineWidth = cameraLook.cross(midLine);
|
||||
lineWidth.normalize();
|
||||
lineWidth.mult(strength);
|
||||
|
||||
corner1.set(x1, y1, z1).add(lineWidth);
|
||||
corner2.set(x1, y1, z1).sub(lineWidth);
|
||||
corner3.set(x2, y2, z2).sub(lineWidth);
|
||||
corner4.set(x2, y2, z2).add(lineWidth);
|
||||
|
||||
var i = lines * 24; // 4 * 6 (structure len)
|
||||
vbData.set(i , x1);
|
||||
vbData.set(i + 1, y1);
|
||||
vbData.set(i + 2, z1);
|
||||
vbData.set(i + 3, color.R);
|
||||
vbData.set(i + 4, color.G);
|
||||
vbData.set(i + 5, color.B);
|
||||
|
||||
vbData.set(i + 6, x2);
|
||||
vbData.set(i + 7, y2);
|
||||
vbData.set(i + 8, z2);
|
||||
vbData.set(i + 9, color.R);
|
||||
vbData.set(i + 10, color.G);
|
||||
vbData.set(i + 11, color.B);
|
||||
|
||||
vbData.set(i + 12, x2 + strength * l.x);
|
||||
vbData.set(i + 13, y2 + strength * l.y);
|
||||
vbData.set(i + 14, z2 + strength * l.z);
|
||||
vbData.set(i + 15, color.R);
|
||||
vbData.set(i + 16, color.G);
|
||||
vbData.set(i + 17, color.B);
|
||||
|
||||
vbData.set(i + 18, x1 + strength * l.x);
|
||||
vbData.set(i + 19, y1 + strength * l.y);
|
||||
vbData.set(i + 20, z1 + strength * l.z);
|
||||
vbData.set(i + 21, color.R);
|
||||
vbData.set(i + 22, color.G);
|
||||
vbData.set(i + 23, color.B);
|
||||
addVbData(i, [corner1.x, corner1.y, corner1.z, color.R, color.G, color.B]);
|
||||
i += 6;
|
||||
addVbData(i, [corner2.x, corner2.y, corner2.z, color.R, color.G, color.B]);
|
||||
i += 6;
|
||||
addVbData(i, [corner3.x, corner3.y, corner3.z, color.R, color.G, color.B]);
|
||||
i += 6;
|
||||
addVbData(i, [corner4.x, corner4.y, corner4.z, color.R, color.G, color.B]);
|
||||
|
||||
i = lines * 6;
|
||||
ibData[i ] = lines * 4;
|
||||
|
@ -201,6 +204,12 @@ class DebugDraw {
|
|||
g.setMatrix(vpID, vp.self);
|
||||
g.drawIndexedVertices(0, lines * 6);
|
||||
}
|
||||
|
||||
inline function addVbData(i: Int, data: Array<Float>) {
|
||||
for (offset in 0...6) {
|
||||
vbData.set(i + offset, data[offset]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#end
|
||||
|
|
Loading…
Reference in New Issue