Skip to content

ShaderPlugin

Generic shader plugin

ShaderPlugin is a specialization of a Plugin for ShaderRenderer. They apply complex computationally demanding post-processing effects to the input stream. For example, simple mono-chrome or sepia effects, or more complex face beatification and dynamic geometry filters. ShaderPlugin shares webgl context with the main renderer. Basic implementation uses ShaderProgram created for shaders provided to plugin’s constructor. Plugins are organized in chain within ShaderRenderer, input of the next shader is output of the previous and initial input is original video image, output of the last plugin is rendered.

ResultT extends object = { }

Type of processing results

new ShaderPlugin<ResultT>(inputs?, uniforms?, fragSrc?, vertSrc?): ShaderPlugin<ResultT>

Constructor

string[]

Shader texture inputs (names of sampler uniforms)

Shader uniforms as name-type map object

string

Code of fragment shader (copy shader by default)

string

Vertex shader source (copy shader by default)

ShaderPlugin<ResultT>

Plugin.constructor

protected optional fragSrc: string

Code of fragment shader (copy shader by default)


protected optional inputs: string[]

Shader texture inputs (names of sampler uniforms)


loaded: boolean = false

Loaded state

Plugin.loaded


ordinal: number = PluginOrdinal.Render

Ordinal number

Plugin.ordinal


protected optional renderer: Renderer<ResultT>

Renderer loaded the plugin

Plugin.renderer


protected optional shader: ShaderProgram

Rendering shader


protected optional shaderCtx: WebGL2RenderingContext

Rendering context


protected size: Size

Image size


protected optional uniforms: object

Shader uniforms as name-type map object

[key: string]: UniformType


protected optional vertSrc: string

Vertex shader source (copy shader by default)

dispose(): void

Dispose video plugin

void

Plugin.dispose


load(renderer): Promise<void>

Initialize plugin

Initializes resources required for shader effect. Acquires webgl context of the main webgl renderer. Basic implementation creates and compiles rendering program for shaders provided to plugin’s constructor. Overridden by derived classes for particular effect.

Renderer<ResultT>

Renderer this plugin is attached to

Promise<void>

Promise resolving when initialization is finished

Plugin.load


process(result, input): Promise<boolean>

Process the image

Main method implementing webgl shader effect or filter. It’s called by update() and should be overridden to add custom logic. process() should return true if effect is successfully applied and false if it is skipped, for example, if effect applies only when result is not empty.

ResultT

Results of video processing

WebGLTexture

Current image texture

Promise<boolean>

True on success, false otherwise


setupCamera(ratio, angle): void

Set camera parameters

Could be overridden to adjust plugin’s pipeline.

number

Aspect ration of input video

number

Vertical field of view in radians

void

Plugin.setupCamera


setupVideo(size): void

Set video size

Adjusts shader and texture to a new size.

Size

Resolution of input video

void

Plugin.setupVideo


unload(): void

Reset plugin

Releases all resources and instances created in load(). Overridden by derived classes for particular effect.

void

Plugin.unload


update(result, stream): Promise<void>

Sealed

Update the image

Main method implementing webgl shader effect or filter. ShaderRenderer keeps track of the current iamge texture that will be be rendered. ShaderPlugin uses the current texture as input and writes results to ShaderPlugin#output. ShaderPlugin#output becomes new current texture of ShaderRenderer. This way all ShaderPlugins attached to renderer organize a chain of effects applied on top of each other. Method process() implements shader effect itself, it’s intended to be overridden by effect authors.

ResultT

Results of video processing

HTMLCanvasElement

Captured video frame

Promise<void>

Promise resolving when update is finished

Plugin.update