Skip to content

Commit

Permalink
Post effect shaders (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk authored Dec 16, 2023
1 parent 5db8945 commit 08a890e
Show file tree
Hide file tree
Showing 6 changed files with 304 additions and 16 deletions.
141 changes: 131 additions & 10 deletions dist-single/gsots3d.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist-single/gsots3d.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/core/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,7 @@ export class ProgramCache {
public static PROG_SHADOWMAP = 'shadowmap'

/**
* Create a new program cache, needs a default program to be set
* @param defaultProg The default program that can be used by most things
* Create a new program cache, can't be used until init() is called
*/
private constructor() {
this.cache = new Map<string, ProgramInfo>()
Expand Down
40 changes: 37 additions & 3 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { ParticleSystem } from '../renderable/particles.ts'
import { Model } from '../renderable/model.ts'
import { HUD } from './hud.ts'
import { Stats } from './stats.ts'
import { PostEffects } from '../engine/post-effects.ts'

// Import shaders, tsup will inline these as text strings
import fragShaderPhong from '../../shaders/phong/glsl.frag'
Expand Down Expand Up @@ -61,6 +62,9 @@ export class Context {
/** Show extra debug details on the canvas */
public debug: boolean

/** Optional post effects filter to apply to the output image */
private postEffects?: PostEffects

/**
* The pre-render update function, called every frame.
* Hook in your custom logic and processing here
Expand Down Expand Up @@ -214,10 +218,19 @@ export class Context {
}

// -------------------------------------------------------------------------------------
// RENDER CORE - Render the scene from active camera into the main framebuffer
// RENDER CORE - FINAL: Render the scene from active camera into the main framebuffer
// -------------------------------------------------------------------------------------
twgl.bindFramebufferInfo(this.gl, null)
this.renderWithCamera(this.camera)
if (this.postEffects) {
// Render the main camera view into the post effects framebuffer
twgl.bindFramebufferInfo(this.gl, this.postEffects.frameBuffer)
this.renderWithCamera(this.camera)

// Then render the post effects to the screen
this.postEffects.renderToScreen(this.gl)
} else {
twgl.bindFramebufferInfo(this.gl, null)
this.renderWithCamera(this.camera)
}

// **** FINAL POST RENDER STEPS ****

Expand Down Expand Up @@ -682,4 +695,25 @@ export class Context {
this.instances.delete(instance.id)
this.instancesTrans.delete(instance.id)
}

/**
* Use a custom shader for post effects, user must provide their own shader
*/
setEffectCustomShader(shaderCode: string) {
this.postEffects = new PostEffects(this.gl, shaderCode)
log.info(`🌈 Post effects shader added`)
}

/**
* Use bulit-in scanlines post effect shader
* @param density - Density of the scanlines, default 1.5
* @param opacity - Opacity of the scanlines, default 0.5
* @param noise - Noise level, default 0.2
* @param flicker - Flicker ammount, default 0.015
*/
setEffectScanlines(density = 1.5, opacity = 0.5, noise = 0.2, flicker = 0.015) {
this.postEffects = PostEffects.scanlines(this.gl, density, opacity, noise, flicker)

log.info(`🌈 Post effects shader added`)
}
}
5 changes: 5 additions & 0 deletions src/engine/physics.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// ===== physics.ts ===============================================================================
// Helper functions for adding physics to instances using CANNON.js
// Ben Coleman, 2023
// =============================================================================================

import * as CANNON from 'cannon-es'
import { Instance } from '../renderable/instance.ts'
import { PrimitiveCube, PrimitiveSphere } from '../renderable/primitive.ts'
Expand Down
Loading

0 comments on commit 08a890e

Please sign in to comment.