Skip to content

Commit

Permalink
refactor 3d renderer to allow for more render passes
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffomatic committed Dec 25, 2020
1 parent b607a18 commit fcbedb6
Show file tree
Hide file tree
Showing 7 changed files with 378 additions and 190 deletions.
52 changes: 27 additions & 25 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SIMULATION_PERIOD_S,
TILE_SIZE,
} from '~/constants'
import { DebugLineModel } from '~/DebugDraw'
import { EntityId } from '~/entities/EntityId'
import { EntityManager } from '~/entities/EntityManager'
import { GameState, gameProgression, initMap } from '~/Game'
Expand All @@ -23,7 +24,7 @@ import { ServerMessage, ServerMessageType } from '~/network/ServerMessage'
import { ParticleEmitter } from '~/particles/ParticleEmitter'
import { Renderer2d } from '~/renderer/Renderer2d'
import { Primitive2d, Renderable2d, TextAlign } from '~/renderer/Renderer2d'
import { DebugLineModel, Renderer3d } from '~/renderer/Renderer3d'
import { Renderer3d } from '~/renderer/Renderer3d'
import { simulate } from '~/simulate'
import * as systems from '~/systems'
import { CursorMode } from '~/systems/client/playerInput'
Expand Down Expand Up @@ -182,25 +183,29 @@ export class Client {
this.entityManager.currentPlayer = this.playerNumber!

this.terrainLayer = initMap(this.entityManager, this.map)
this.renderer3d.loadModel('terrain', this.terrainLayer.getModel())
this.renderer3d.loadModel(
'terrain',
this.terrainLayer.getModel(),
'standard',
)

const gridModel = loadGrid()
this.renderer3d.loadModel('grid', gridModel)
this.renderer3d.loadModel('grid', gridModel, 'standard')

const wallModel = getModel('wall')
this.renderer3d.loadModel('wall', wallModel)
this.renderer3d.loadModel('wall', wallModel, 'standard')

const tankModel = getModel('tank')
this.renderer3d.loadModel('tank', tankModel)
this.renderer3d.loadModel('tank', tankModel, 'standard')

const turretModel = getModel('turret')
this.renderer3d.loadModel('turret', turretModel)
this.renderer3d.loadModel('turret', turretModel, 'standard')

const treeModel = getModel('tree')
this.renderer3d.loadModel('tree', treeModel)
this.renderer3d.loadModel('tree', treeModel, 'standard')

const bulletModel = getModel('bullet')
this.renderer3d.loadModel('bullet', bulletModel)
this.renderer3d.loadModel('bullet', bulletModel, 'standard')

this.camera2d.minWorldPos = this.terrainLayer.minWorldPos()
this.camera2d.worldDimensions = this.terrainLayer.dimensions()
Expand Down Expand Up @@ -294,11 +299,6 @@ export class Client {
systems.playerInput(this, this.simulationFrame)
}

// We're unsure if we should increment the frame here;
// turret shooting seems to indicate that it's actually
// firing one frame early
// this.simulationFrame++

simulate(
{
entityManager: this.entityManager,
Expand Down Expand Up @@ -379,23 +379,25 @@ export class Client {
this.renderFrameDurations.sample(now - this.lastRenderAt)
this.lastRenderAt = now

this.renderer3d.clear('magenta')
if (this.state === GameState.Connecting) {
return
}

this.renderer3d.clear()

this.renderer3d.setWvTransform(this.camera.getWvTransform())
this.renderer3d.drawModel('terrain', vec2.create(), 0)

// GRID DEBUG
this.renderer3d.drawModel('grid', vec2.create(), 0)
this.renderer3d.renderStandard((drawModel) => {
drawModel('terrain', vec2.create(), 0)

for (const [entityId, model] of this.entityManager.renderables) {
const transform = this.entityManager.transforms.get(entityId)!
// GRID DEBUG
drawModel('grid', vec2.create(), 0)

this.renderer3d.drawModel(
model,
transform.position,
transform.orientation,
)
}
for (const [entityId, model] of this.entityManager.renderables) {
const transform = this.entityManager.transforms.get(entityId)!
drawModel(model, transform.position, transform.orientation)
}
})

// this.emitters!.forEach((e) =>
// e.getRenderables().forEach((r) => this.renderer.render(r)),
Expand Down
25 changes: 25 additions & 0 deletions src/DebugDraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { vec4 } from 'gl-matrix'

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

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

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

export const mockDebugDraw = {
draw2d: (_makeRenderables: () => Renderable2d[]): void => {
/* do nothing */
},
draw3d: (
_makeModels: () => { points: Float32Array; color: vec4 }[],
): void => {
/* do nothing */
},
}
6 changes: 3 additions & 3 deletions src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const materials: { [key: string]: [number, number, number, number] } = {

type ModelTypes = keyof typeof models
export type Model = {
vertices: Float32Array
positions: Float32Array
colors: Float32Array
normals: Float32Array
primitive: 'TRIANGLES' | 'LINES'
Expand Down Expand Up @@ -114,7 +114,7 @@ export const getModel: (modelType: ModelTypes) => Model = (modelType) => {
})

return {
vertices: new Float32Array(vertices),
positions: new Float32Array(vertices),
colors: new Float32Array(colors),
normals: new Float32Array(normals),
primitive: 'TRIANGLES',
Expand Down Expand Up @@ -150,7 +150,7 @@ export const loadGrid = (): Model => {
}

return {
vertices: new Float32Array(vertices),
positions: new Float32Array(vertices),
colors: new Float32Array(colors),
normals: new Float32Array(),
primitive: 'LINES',
Expand Down
Loading

0 comments on commit fcbedb6

Please sign in to comment.