Skip to content

Commit

Permalink
initial refactor of debug draw
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed Dec 25, 2020
1 parent f5bf4e4 commit 3169d03
Show file tree
Hide file tree
Showing 11 changed files with 261 additions and 215 deletions.
30 changes: 20 additions & 10 deletions src/Client.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import { vec2 } from 'gl-matrix'
import { mat2d } from 'gl-matrix'
import { vec4 } from 'gl-matrix'
import { vec3 } from 'gl-matrix'
import { quat } from 'gl-matrix'
import { mat2d, quat, vec2, vec3 } from 'gl-matrix'

import { Camera2d } from '~/camera/Camera2d'
import { Camera3d } from '~/camera/Camera3d'
Expand All @@ -11,7 +7,7 @@ import {
SIMULATION_PERIOD_S,
TILE_SIZE,
} from '~/constants'
import { DebugLineModel } from '~/DebugDraw'
import { DebugDrawObject } from '~/DebugDraw'
import { EntityId } from '~/entities/EntityId'
import { EntityManager } from '~/entities/EntityManager'
import { GameState, gameProgression, initMap } from '~/Game'
Expand Down Expand Up @@ -52,7 +48,7 @@ export class Client {
camera2d: Camera2d
camera: Camera3d
debugDraw2dRenderables: Renderable2d[]
debugDraw3dModels: DebugLineModel[]
debugDraw3dModels: DebugDrawObject[]
emitters: ParticleEmitter[]
emitterHistory: Set<string>
enableDebugDraw: boolean
Expand Down Expand Up @@ -302,6 +298,10 @@ export class Client {
terrainLayer: this.terrainLayer,
frame: this.simulationFrame,
registerParticleEmitter: this.registerParticleEmitter,
debugDraw: {
draw2d: this.debugDraw2d.bind(this),
draw3d: this.debugDraw3d.bind(this),
},
},
this.state,
dt,
Expand Down Expand Up @@ -504,18 +504,28 @@ export class Client {
return res
})

const nextDebugDraw3d = []
if (this.enableDebugDraw) {
this.debugDraw2dRenderables.forEach((r) => {
this.renderer2d.render(r)
})

for (const m of this.debugDraw3dModels) {
this.renderer3d.drawLines(m.points, m.color)
this.renderer3d.renderWire((drawFunc) => {
drawFunc(m.object)
})

if (m.lifetime !== undefined) {
if (m.lifetime > 1) {
m.lifetime -= 1
nextDebugDraw3d.push(m)
}
}
}
}

this.debugDraw2dRenderables = []
this.debugDraw3dModels = []
this.debugDraw3dModels = nextDebugDraw3d

this.renderDurations.sample(time.current() - now)
}
Expand All @@ -541,7 +551,7 @@ export class Client {
)
}

debugDraw3d(makeModels: () => { points: Float32Array; color: vec4 }[]): void {
debugDraw3d(makeModels: () => DebugDrawObject[]): void {
if (!this.enableDebugDraw) {
return
}
Expand Down
14 changes: 5 additions & 9 deletions src/DebugDraw.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
import { vec4 } from 'gl-matrix'

import { Renderable2d } from '~/renderer/Renderer2d'
import { WireObject } from '~/renderer/Renderer3d'

export interface DebugLineModel {
points: Float32Array
color: vec4
export interface DebugDrawObject {
object: WireObject
lifetime?: number // lifetime in frames (minimum 1, default 1)
}

export interface DebugDraw {
draw2d: (makeRenderables: () => Renderable2d[]) => void
draw3d: (makeModels: () => { points: Float32Array; color: vec4 }[]) => void
draw3d: (makeModels: () => DebugDrawObject[]) => void
}

export const mockDebugDraw = {
draw2d: (_makeRenderables: () => Renderable2d[]): void => {
/* do nothing */
},
draw3d: (
_makeModels: () => { points: Float32Array; color: vec4 }[],
): void => {
draw3d: (_makeModels: () => DebugDrawObject[]): void => {
/* do nothing */
},
}
2 changes: 2 additions & 0 deletions src/Server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { vec2 } from 'gl-matrix'

import { TILE_SIZE } from '~/constants'
import { mockDebugDraw } from '~/DebugDraw'
import { EntityManager } from '~/entities/EntityManager'
import { GameState, gameProgression, initMap } from '~/Game'
import { Map } from '~/map/interfaces'
Expand Down Expand Up @@ -209,6 +210,7 @@ export class Server {
messages: frameMessages,
terrainLayer: this.terrainLayer,
frame: this.simulationFrame,
debugDraw: mockDebugDraw,
},
this.state,
dt,
Expand Down
6 changes: 3 additions & 3 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ const materials: { [key: string]: [number, number, number, number] } = {

type ModelTypes = keyof typeof models
export type Model = {
positions: Float32Array
colors: Float32Array
normals: Float32Array
primitive: 'TRIANGLES' | 'LINES'
positions: Float32Array
colors?: Float32Array
normals?: Float32Array
}

const defaultColor = [1.0, 0, 1.0, 1.0]
Expand Down
Loading

0 comments on commit 3169d03

Please sign in to comment.