Skip to content

Commit

Permalink
DebugDraw: Line thickness
Browse files Browse the repository at this point in the history
  • Loading branch information
magcius committed Nov 20, 2024
1 parent 20b840f commit 831ba43
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/SuperMarioGalaxy/RailRider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,13 +591,23 @@ export class RailRider {
const totalLength = this.getTotalLength();
const speed = totalLength / nPoints;

if (this.bezierRail.isClosed)
nPoints++;

this.bezierRail.calcPos(scratchVec3b, 0);
debugDraw.beginBatchLine(nPoints);
for (let i = 0; i < nPoints; i++) {
this.bezierRail.calcPos(scratchVec3c, i * speed);
debugDraw.drawLine(scratchVec3b, scratchVec3c, Magenta);
vec3.copy(scratchVec3b, scratchVec3c);
}

if (this.bezierRail.isClosed) {
this.bezierRail.calcPos(scratchVec3b, totalLength);
this.bezierRail.calcPos(scratchVec3c, 0);
debugDraw.drawLine(scratchVec3b, scratchVec3c, Magenta);
}

debugDraw.endBatch();
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/ZeldaWindWaker/d_a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3841,8 +3841,8 @@ class d_a_oship extends fopAc_ac_c implements ModeFuncExec<d_a_oship_mode> {

const angleY = cLib_targetAngleY(this.pos, this.targetPos);
// TODO(jstpierre): Figure out the bad aim system.
// this.targetPos[0] -= badAimRadius * Math.sin(cM__Short2Rad(angleY));
// this.targetPos[2] -= badAimRadius * Math.cos(cM__Short2Rad(angleY));
// this.targetPos[0] -= badAimRadius * Math.sin(cM_s2rad(angleY));
// this.targetPos[2] -= badAimRadius * Math.cos(cM_s2rad(angleY));
}

private attackCannon(globals: dGlobals): boolean {
Expand Down
30 changes: 21 additions & 9 deletions src/gfx/helpers/DebugDraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,19 @@ import { assert } from "../platform/GfxPlatformUtil.js";
// - Integrate text renderer?
// - More primitive types
// - Support view-space and screen-space primitives
// - Line width emulation?

interface DebugDrawOptions {
flags?: DebugDrawFlags;
};

export const enum DebugDrawFlags {
Default = 0,

WorldSpace = 0,
ViewSpace = 1 << 0,
ScreenSpace = 1 << 1,
BillboardSpace = 1 << 2,

DepthTint = 1 << 3,

Default = DepthTint,
};

const bindingLayouts: GfxBindingLayoutDescriptor[] = [
Expand Down Expand Up @@ -63,6 +61,16 @@ flat out uint v_Flags;
void main() {
uint t_Flags = uint(a_Color.a);
gl_Position = Mul(u_ClipFromView, Mul(_Mat4x4(u_ViewFromWorld), vec4(a_Position.xyz, 1.0)));
if (gl_InstanceID >= 1) {
uint t_LineIndex = uint(gl_InstanceID - 1);
vec2 t_PosOffs;
t_PosOffs.x = (t_LineIndex & 1u) != 0u ? 1.0f : -1.0f;
t_PosOffs.y = (t_LineIndex & 2u) != 0u ? 1.0f : -1.0f;
t_PosOffs.xy *= float((t_LineIndex / 4u) + 1u);
gl_Position.xy += (t_PosOffs / u_ScreenSize) * gl_Position.w;
}
v_Color = a_Color;
v_Color.a = 1.0 - fract(a_Color.a);
v_Color.rgb *= v_Color.aaa;
Expand All @@ -85,10 +93,11 @@ bool IsSomethingInFront(float t_DepthSample) {
void main() {
vec4 t_Color = v_Color;
if ((v_Flags & uint(${DebugDrawFlags.DepthTint})) != 0u) {
float t_DepthSample = texelFetch(SAMPLER_2D(u_TextureFramebufferDepth), ivec2(gl_FragCoord.xy), 0).r;
if (IsSomethingInFront(t_DepthSample))
t_Color.rgba *= 0.2;
t_Color.rgba *= 0.15;
}
gl_FragColor = t_Color;
Expand Down Expand Up @@ -123,7 +132,7 @@ class BufferPage {

public renderInst = new GfxRenderInst();

constructor(cache: GfxRenderCache, public behaviorType: BehaviorType, vertexCount: number, indexCount: number) {
constructor(cache: GfxRenderCache, public behaviorType: BehaviorType, vertexCount: number, indexCount: number, private lineThickness: number) {
const device = cache.device;

this.vertexData = new Float32Array(vertexCount * this.vertexStride);
Expand Down Expand Up @@ -188,10 +197,12 @@ class BufferPage {
], { buffer: this.indexBuffer, byteOffset: 0 });

setAttachmentStateSimple(this.renderInst.getMegaStateFlags(), { blendMode: GfxBlendMode.Add, blendSrcFactor: GfxBlendFactor.One, blendDstFactor: GfxBlendFactor.OneMinusSrcAlpha });
if (this.behaviorType === BehaviorType.Lines)
if (this.behaviorType === BehaviorType.Lines) {
this.renderInst.setMegaStateFlags({ depthCompare: GfxCompareMode.Always, depthWrite: false });
else if (this.behaviorType === BehaviorType.Transparent)
this.renderInst.setInstanceCount(1 + (this.lineThickness - 1) * 4);
} else if (this.behaviorType === BehaviorType.Transparent) {
this.renderInst.setMegaStateFlags({ depthWrite: false });
}

this.renderInst.setDrawCount(this.indexDataOffs);

Expand All @@ -216,6 +227,7 @@ export class DebugDraw {
private debugDrawProgram: GfxProgram;
private depthSampler: GfxSampler;
private currentPage: BufferPage | null = null; // for the batch system
private lineThickness = 3;

public static scratchVec3 = nArray(4, () => vec3.create());

Expand Down Expand Up @@ -273,7 +285,7 @@ export class DebugDraw {

vertexCount = align(vertexCount, this.defaultPageVertexCount);
indexCount = align(indexCount, this.defaultPageIndexCount);
const page = new BufferPage(this.cache, behaviorType, vertexCount, indexCount);
const page = new BufferPage(this.cache, behaviorType, vertexCount, indexCount, this.lineThickness);
this.pages.push(page);
return page;
}
Expand Down

0 comments on commit 831ba43

Please sign in to comment.