Skip to content

Commit

Permalink
🌿 FPS controls and Instance overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Mar 5, 2024
1 parent 0da0369 commit 84207ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ export class Context {
if (this.lights.length > MAX_LIGHTS) {
// Sort lights by distance to camera so we can use the closest ones
this.lights.sort((lightA, lightB) => {
// Handle lights that are disabled
if (!lightA.enabled) return 1
if (!lightB.enabled) return -1
const ad = vec3.distance(lightA.position, this.camera.position)
const bd = vec3.distance(lightB.position, this.camera.position)
return ad - bd
Expand Down
8 changes: 7 additions & 1 deletion src/engine/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export class Camera {
private fpHandlersAdded: boolean
private fpTurnSpeed: number
private fpMoveSpeed: number
private fpFly: boolean

// Used to clamp first person up/down angle
private maxAngleUp: number = Math.PI / 2 - 0.01
Expand Down Expand Up @@ -91,6 +92,7 @@ export class Camera {
this.fpTurnSpeed = 0.001
this.fpMoveSpeed = 1.0
this.fpHandlersAdded = false
this.fpFly = false

this.keysDown = new Set()
}
Expand Down Expand Up @@ -210,12 +212,13 @@ export class Camera {
* @param turnSpeed Speed of looking in radians, default 0.001
* @param moveSpeed Speed of moving in units, default 1.0
*/
enableFPControls(angleY = 0, angleX = 0, turnSpeed = 0.001, moveSpeed = 1.0) {
enableFPControls(angleY = 0, angleX = 0, turnSpeed = 0.001, moveSpeed = 1.0, fly = false) {
this.fpMode = true
this.fpAngleY = angleY
this.fpAngleX = angleX
this.fpTurnSpeed = turnSpeed
this.fpMoveSpeed = moveSpeed
this.fpFly = fly

if (this.fpHandlersAdded) return // Prevent multiple event listeners being added

Expand Down Expand Up @@ -341,6 +344,7 @@ export class Camera {

// Use fpAngleY to calculate the direction we are facing
const dZ = -Math.cos(this.fpAngleY) * this.fpMoveSpeed
const dY = Math.sin(this.fpAngleX) * this.fpMoveSpeed
const dX = -Math.sin(this.fpAngleY) * this.fpMoveSpeed

// Use keysDown to move the camera
Expand All @@ -349,6 +353,7 @@ export class Camera {
case 'ArrowUp':
case 'w':
this.position[0] += dX
if (this.fpFly) this.position[1] += dY
this.position[2] += dZ
this.lookAt[0] += dX
this.lookAt[2] += dZ
Expand All @@ -357,6 +362,7 @@ export class Camera {
case 'ArrowDown':
case 's':
this.position[0] -= dX
if (this.fpFly) this.position[1] -= dY
this.position[2] -= dZ
this.lookAt[0] -= dX
this.lookAt[2] -= dZ
Expand Down
4 changes: 4 additions & 0 deletions src/renderable/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ export class Instance extends Node {
* This is only useful on simple models that probably consist of one surface */
public material?: Material

/** Override just some material properties, warning advanced feature! */
public uniformOverrides?: UniformSet

/**
* Create a new instace of a renderable thing
* @param {Renderable} renderable - Renderable to use for this instance
Expand Down Expand Up @@ -83,6 +86,7 @@ export class Instance extends Node {
uniforms.u_flipTextureX = this.flipTextureX
uniforms.u_flipTextureY = this.flipTextureY
uniforms.u_receiveShadow = this.receiveShadow
if (this.uniformOverrides) uniforms = { ...uniforms, ...this.uniformOverrides }

// Render the renderable thing wrapped by this instance
this.renderable.render(gl, uniforms, this.material, programOverride)
Expand Down

0 comments on commit 84207ae

Please sign in to comment.