Add dynamic shader generation
This commit is contained in:
parent
edd7d4836e
commit
4369cf1770
22 changed files with 289 additions and 113 deletions
|
|
@ -1,8 +1,8 @@
|
|||
import { Drawer } from '../../drawing/drawer';
|
||||
import { IRenderer } from '../../drawing/i-renderer';
|
||||
import { Command } from '../command';
|
||||
|
||||
export class BeforeDrawCommand extends Command {
|
||||
public constructor(public readonly drawer?: Drawer) {
|
||||
public constructor(public readonly drawer?: IRenderer) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
import { Drawer } from '../../drawing/drawer';
|
||||
import { IRenderer } from '../../drawing/i-renderer';
|
||||
import { Command } from '../command';
|
||||
|
||||
export class DrawCommand extends Command {
|
||||
public constructor(public readonly drawer?: Drawer) {
|
||||
public constructor(public readonly drawer?: IRenderer) {
|
||||
super();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
import { IProgram } from '../program/i-program';
|
||||
import { FrameBuffer } from './frame-buffer';
|
||||
|
||||
export class DefaultFrameBuffer extends FrameBuffer {
|
||||
constructor(gl: WebGL2RenderingContext, programs: Array<IProgram>) {
|
||||
super(gl, programs);
|
||||
this.frameBuffer = null;
|
||||
|
||||
this.setSize();
|
||||
}
|
||||
|
||||
public setSize() {
|
||||
super.setSize();
|
||||
|
||||
if (
|
||||
this.gl.canvas.width !== this.size.x ||
|
||||
this.gl.canvas.height !== this.size.y
|
||||
) {
|
||||
this.gl.canvas.width = this.size.x;
|
||||
this.gl.canvas.height = this.size.y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { IProgram } from '../program/i-program';
|
||||
|
||||
export abstract class FrameBuffer {
|
||||
public renderScale = 1;
|
||||
public enableHighDpiRendering = false;
|
||||
|
||||
protected size: vec2;
|
||||
protected frameBuffer: WebGLFramebuffer;
|
||||
|
||||
constructor(
|
||||
protected gl: WebGL2RenderingContext,
|
||||
protected programs: Array<IProgram>
|
||||
) {}
|
||||
|
||||
public render(uniforms: any, colorInput?: WebGLTexture) {
|
||||
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.frameBuffer);
|
||||
|
||||
if (colorInput !== null) {
|
||||
this.gl.bindTexture(this.gl.TEXTURE_2D, colorInput);
|
||||
}
|
||||
|
||||
this.gl.viewport(0, 0, this.size.x, this.size.y);
|
||||
this.gl.clearColor(0, 0, 0, 0);
|
||||
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
|
||||
|
||||
this.programs.forEach((p) => {
|
||||
p.bindAndSetUniforms(uniforms);
|
||||
p.draw();
|
||||
});
|
||||
}
|
||||
|
||||
public setSize() {
|
||||
const realToCssPixels = window.devicePixelRatio * this.renderScale;
|
||||
const canvasWidth = (this.gl.canvas as HTMLCanvasElement).clientWidth;
|
||||
const canvasHeight = (this.gl.canvas as HTMLCanvasElement).clientHeight;
|
||||
|
||||
const displayWidth = Math.floor(canvasWidth * realToCssPixels);
|
||||
const displayHeight = Math.floor(canvasHeight * realToCssPixels);
|
||||
|
||||
this.size = vec2.fromValues(displayWidth, displayHeight);
|
||||
}
|
||||
|
||||
public getSize(): vec2 {
|
||||
return this.size;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
import { IProgram } from '../program/i-program';
|
||||
import { FrameBuffer } from './frame-buffer';
|
||||
|
||||
export class IntermediateFrameBuffer extends FrameBuffer {
|
||||
private frameTexture: WebGLTexture;
|
||||
|
||||
constructor(gl: WebGL2RenderingContext, programs: Array<IProgram>) {
|
||||
super(gl, programs);
|
||||
|
||||
this.frameTexture = this.gl.createTexture();
|
||||
this.configureTexture();
|
||||
|
||||
this.frameBuffer = this.gl.createFramebuffer();
|
||||
this.configureFrameBuffer();
|
||||
|
||||
this.setSize();
|
||||
}
|
||||
|
||||
public get colorTexture(): WebGLTexture {
|
||||
return this.frameTexture;
|
||||
}
|
||||
|
||||
public setSize() {
|
||||
super.setSize();
|
||||
|
||||
this.gl.bindTexture(this.gl.TEXTURE_2D, this.frameTexture);
|
||||
|
||||
this.gl.texImage2D(
|
||||
this.gl.TEXTURE_2D,
|
||||
0,
|
||||
this.gl.RGBA,
|
||||
this.size.x,
|
||||
this.size.y,
|
||||
0,
|
||||
this.gl.RGBA,
|
||||
this.gl.UNSIGNED_BYTE,
|
||||
null
|
||||
);
|
||||
}
|
||||
|
||||
private configureTexture() {
|
||||
this.gl.bindTexture(this.gl.TEXTURE_2D, this.frameTexture);
|
||||
this.gl.texParameteri(
|
||||
this.gl.TEXTURE_2D,
|
||||
this.gl.TEXTURE_MAG_FILTER,
|
||||
this.gl.LINEAR
|
||||
);
|
||||
this.gl.texParameteri(
|
||||
this.gl.TEXTURE_2D,
|
||||
this.gl.TEXTURE_MIN_FILTER,
|
||||
this.gl.LINEAR
|
||||
);
|
||||
this.gl.texParameteri(
|
||||
this.gl.TEXTURE_2D,
|
||||
this.gl.TEXTURE_WRAP_S,
|
||||
this.gl.CLAMP_TO_EDGE
|
||||
);
|
||||
this.gl.texParameteri(
|
||||
this.gl.TEXTURE_2D,
|
||||
this.gl.TEXTURE_WRAP_T,
|
||||
this.gl.CLAMP_TO_EDGE
|
||||
);
|
||||
}
|
||||
|
||||
private configureFrameBuffer() {
|
||||
this.gl.bindFramebuffer(this.gl.FRAMEBUFFER, this.frameBuffer);
|
||||
const attachmentPoint = this.gl.COLOR_ATTACHMENT0;
|
||||
this.gl.framebufferTexture2D(
|
||||
this.gl.FRAMEBUFFER,
|
||||
attachmentPoint,
|
||||
this.gl.TEXTURE_2D,
|
||||
this.frameTexture,
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
export const createShader = (
|
||||
gl: WebGL2RenderingContext,
|
||||
type: GLenum,
|
||||
source: string,
|
||||
substitutions: { [name: string]: string }
|
||||
): WebGLShader => {
|
||||
source = source.replace(
|
||||
/{(.+)}/gm,
|
||||
(_, name: string): string => substitutions[name]
|
||||
);
|
||||
|
||||
const shader = gl.createShader(type);
|
||||
gl.shaderSource(shader, source);
|
||||
gl.compileShader(shader);
|
||||
const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS);
|
||||
|
||||
if (!success) {
|
||||
throw new Error(gl.getShaderInfoLog(shader));
|
||||
}
|
||||
|
||||
return shader;
|
||||
};
|
||||
|
|
@ -0,0 +1,91 @@
|
|||
import { mat3, ReadonlyVec3, vec2, vec3 } from 'gl-matrix';
|
||||
|
||||
const loaderMat3 = mat3.create();
|
||||
|
||||
export const loadUniform = (
|
||||
gl: WebGL2RenderingContext,
|
||||
value: any,
|
||||
type: GLenum,
|
||||
location: WebGLUniformLocation
|
||||
): any => {
|
||||
const converters: Map<
|
||||
GLenum,
|
||||
(
|
||||
gl: WebGL2RenderingContext,
|
||||
value: any,
|
||||
location: WebGLUniformLocation
|
||||
) => void
|
||||
> = new Map();
|
||||
{
|
||||
converters.set(WebGL2RenderingContext.FLOAT, (gl, v, l) => {
|
||||
if (v instanceof Array) {
|
||||
if (v.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
gl.uniform1fv(l, new Float32Array(v));
|
||||
} else {
|
||||
gl.uniform1f(l, v);
|
||||
}
|
||||
});
|
||||
|
||||
converters.set(
|
||||
WebGL2RenderingContext.FLOAT_VEC2,
|
||||
(gl, v: vec2 | Array<vec2>, l) => {
|
||||
if (v.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (v[0] instanceof Array) {
|
||||
const result = new Float32Array(v.length * 2);
|
||||
|
||||
for (let i = 0; i < v.length; i++) {
|
||||
result[2 * i] = (v[i] as Array<number>).x;
|
||||
result[2 * i + 1] = (v[i] as Array<number>).y;
|
||||
}
|
||||
|
||||
gl.uniform2fv(l, result);
|
||||
} else {
|
||||
gl.uniform2fv(l, v as vec2);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
converters.set(
|
||||
WebGL2RenderingContext.FLOAT_VEC3,
|
||||
(gl, v: ReadonlyVec3 | Array<vec3>, l) => {
|
||||
if (v.length == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (v[0] instanceof Array) {
|
||||
const result = new Float32Array(v.length * 3);
|
||||
|
||||
for (let i = 0; i < v.length; i++) {
|
||||
result[3 * i] = (v[i] as Array<number>)[0];
|
||||
result[3 * i + 1] = (v[i] as Array<number>)[1];
|
||||
result[3 * i + 2] = (v[i] as Array<number>)[2];
|
||||
}
|
||||
|
||||
gl.uniform3fv(l, result);
|
||||
} else {
|
||||
gl.uniform3fv(l, v as vec3);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
converters.set(WebGL2RenderingContext.FLOAT_MAT3, (gl, v, l) => {
|
||||
gl.uniformMatrix3fv(l, true, mat3.fromMat2d(loaderMat3, v));
|
||||
});
|
||||
|
||||
converters.set(WebGL2RenderingContext.BOOL, (gl, v, l) =>
|
||||
gl.uniform1i(l, v)
|
||||
);
|
||||
|
||||
if (!converters.has(type)) {
|
||||
throw new Error(`Unimplemented webgl type: ${type}`);
|
||||
}
|
||||
|
||||
converters.get(type)(gl, value, location);
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
import { InfoText } from '../../../objects/types/info-text';
|
||||
|
||||
// https://www.khronos.org/registry/webgl/extensions/EXT_disjoint_timer_query_webgl2/
|
||||
|
||||
export class WebGlStopwatch {
|
||||
private timerExtension: any;
|
||||
private timerQuery: WebGLQuery;
|
||||
private isReady = true;
|
||||
|
||||
private resultsInNanoSeconds: number;
|
||||
|
||||
constructor(private gl: WebGL2RenderingContext) {
|
||||
if (
|
||||
this.gl
|
||||
.getSupportedExtensions()
|
||||
.indexOf('EXT_disjoint_timer_query_webgl2') == -1
|
||||
) {
|
||||
throw new Error('Unsupported extension');
|
||||
}
|
||||
|
||||
this.timerExtension = this.gl.getExtension(
|
||||
'EXT_disjoint_timer_query_webgl2'
|
||||
);
|
||||
}
|
||||
|
||||
public start() {
|
||||
if (this.isReady) {
|
||||
this.timerQuery = this.gl.createQuery();
|
||||
this.gl.beginQuery(this.timerExtension.TIME_ELAPSED_EXT, this.timerQuery);
|
||||
}
|
||||
}
|
||||
|
||||
public stop() {
|
||||
if (this.isReady) {
|
||||
this.gl.endQuery(this.timerExtension.TIME_ELAPSED_EXT);
|
||||
this.isReady = false;
|
||||
}
|
||||
|
||||
const available = this.gl.getQueryParameter(
|
||||
this.timerQuery,
|
||||
this.gl.QUERY_RESULT_AVAILABLE
|
||||
);
|
||||
const disjoint = this.gl.getParameter(this.timerExtension.GPU_DISJOINT_EXT);
|
||||
|
||||
if (available && !disjoint) {
|
||||
this.resultsInNanoSeconds = this.gl.getQueryParameter(
|
||||
this.timerQuery,
|
||||
this.gl.QUERY_RESULT
|
||||
);
|
||||
|
||||
InfoText.modifyRecord(
|
||||
'Draw time',
|
||||
`${this.resultsInMilliSeconds.toFixed(2)} ms`
|
||||
);
|
||||
|
||||
this.isReady = true;
|
||||
}
|
||||
}
|
||||
|
||||
public get resultsInMilliSeconds(): number {
|
||||
return this.resultsInNanoSeconds / 1000 / 1000;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
import { Program } from './program';
|
||||
|
||||
export class FragmentShaderOnlyProgram extends Program {
|
||||
private vao: WebGLVertexArrayObject;
|
||||
|
||||
constructor(
|
||||
gl: WebGL2RenderingContext,
|
||||
vertexShaderSource: string,
|
||||
fragmentShaderSource: string,
|
||||
substitutions: { [name: string]: string }
|
||||
) {
|
||||
super(gl, vertexShaderSource, fragmentShaderSource, substitutions);
|
||||
this.prepareScreenQuad('a_position');
|
||||
}
|
||||
|
||||
public bind() {
|
||||
super.bind();
|
||||
this.gl.bindVertexArray(this.vao);
|
||||
}
|
||||
|
||||
public draw() {
|
||||
this.gl.drawArrays(this.gl.TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
|
||||
private prepareScreenQuad(attributeName: string) {
|
||||
const positionAttributeLocation = this.gl.getAttribLocation(
|
||||
this.program,
|
||||
attributeName
|
||||
);
|
||||
|
||||
const positionBuffer = this.gl.createBuffer();
|
||||
|
||||
this.gl.bindBuffer(this.gl.ARRAY_BUFFER, positionBuffer);
|
||||
this.gl.bufferData(
|
||||
this.gl.ARRAY_BUFFER,
|
||||
new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]),
|
||||
this.gl.STATIC_DRAW
|
||||
);
|
||||
this.vao = this.gl.createVertexArray();
|
||||
|
||||
this.gl.bindVertexArray(this.vao);
|
||||
this.gl.enableVertexAttribArray(positionAttributeLocation);
|
||||
this.gl.vertexAttribPointer(
|
||||
positionAttributeLocation,
|
||||
2,
|
||||
this.gl.FLOAT,
|
||||
false,
|
||||
0,
|
||||
0
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
export interface IProgram {
|
||||
bindAndSetUniforms(values: { [name: string]: any }): void;
|
||||
draw(): void;
|
||||
delete(): void;
|
||||
}
|
||||
116
frontend/src/scripts/drawing/graphics-library/program/program.ts
Normal file
116
frontend/src/scripts/drawing/graphics-library/program/program.ts
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
import { createShader } from '../helper/create-shader';
|
||||
import { loadUniform } from '../helper/load-uniform';
|
||||
import { IProgram } from './i-program';
|
||||
|
||||
export abstract class Program implements IProgram {
|
||||
protected program: WebGLProgram;
|
||||
private shaders: Array<WebGLShader> = [];
|
||||
|
||||
private uniforms: Array<{
|
||||
name: Array<string>;
|
||||
location: WebGLUniformLocation;
|
||||
type: GLenum;
|
||||
}> = [];
|
||||
|
||||
constructor(
|
||||
protected gl: WebGL2RenderingContext,
|
||||
vertexShaderSource: string,
|
||||
fragmentShaderSource: string,
|
||||
substitutions: { [name: string]: string }
|
||||
) {
|
||||
this.createProgram(vertexShaderSource, fragmentShaderSource, substitutions);
|
||||
this.queryUniforms();
|
||||
}
|
||||
|
||||
public bindAndSetUniforms(values: { [name: string]: any }) {
|
||||
this.bind();
|
||||
this.setUniforms(values);
|
||||
}
|
||||
|
||||
public bind() {
|
||||
this.gl.useProgram(this.program);
|
||||
}
|
||||
|
||||
public setUniforms(values: { [name: string]: any }) {
|
||||
this.uniforms.forEach(({ name, location, type }) => {
|
||||
const value = name.reduce(
|
||||
(prev, prop) => (prev !== null && prop in prev ? prev[prop] : null),
|
||||
values
|
||||
);
|
||||
|
||||
if (value !== null) {
|
||||
loadUniform(this.gl, value, type, location);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public delete() {
|
||||
this.shaders.forEach((s) => this.gl.deleteShader(s));
|
||||
this.gl.deleteProgram(this.program);
|
||||
}
|
||||
|
||||
public abstract draw(): void;
|
||||
|
||||
private queryUniforms() {
|
||||
const uniformCount = this.gl.getProgramParameter(
|
||||
this.program,
|
||||
WebGL2RenderingContext.ACTIVE_UNIFORMS
|
||||
);
|
||||
|
||||
for (let i = 0; i < uniformCount; i++) {
|
||||
const glUniform = this.gl.getActiveUniform(this.program, i);
|
||||
|
||||
this.uniforms.push({
|
||||
name: glUniform.name.split(/\[|\]\.|\]|\./).filter((p) => p !== ''),
|
||||
location: this.gl.getUniformLocation(this.program, glUniform.name),
|
||||
type: glUniform.type,
|
||||
});
|
||||
}
|
||||
|
||||
this.uniforms.map((u1) => {
|
||||
const isSingle =
|
||||
this.uniforms.filter((u2) => u2.name.includes(u1.name[0])).length == 1;
|
||||
if (u1.name.includes('0') && isSingle) {
|
||||
u1.name = u1.name.slice(0, -1);
|
||||
}
|
||||
return u1;
|
||||
});
|
||||
}
|
||||
|
||||
private createProgram(
|
||||
passthroughVertexShaderSource: string,
|
||||
fragmentShaderSource: string,
|
||||
substitutions: { [name: string]: string }
|
||||
) {
|
||||
this.program = this.gl.createProgram();
|
||||
|
||||
const vertexShader = createShader(
|
||||
this.gl,
|
||||
this.gl.VERTEX_SHADER,
|
||||
passthroughVertexShaderSource,
|
||||
substitutions
|
||||
);
|
||||
this.gl.attachShader(this.program, vertexShader);
|
||||
this.shaders.push(vertexShader);
|
||||
|
||||
const fragmentShader = createShader(
|
||||
this.gl,
|
||||
this.gl.FRAGMENT_SHADER,
|
||||
fragmentShaderSource,
|
||||
substitutions
|
||||
);
|
||||
this.gl.attachShader(this.program, fragmentShader);
|
||||
this.shaders.push(fragmentShader);
|
||||
|
||||
this.gl.linkProgram(this.program);
|
||||
|
||||
const success = this.gl.getProgramParameter(
|
||||
this.program,
|
||||
this.gl.LINK_STATUS
|
||||
);
|
||||
|
||||
if (!success) {
|
||||
throw new Error(this.gl.getProgramInfoLog(this.program));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
import { InfoText } from '../../../objects/types/info-text';
|
||||
import { FragmentShaderOnlyProgram } from './fragment-shader-only-program';
|
||||
import { IProgram } from './i-program';
|
||||
|
||||
export class UniformArrayAutoScalingProgram implements IProgram {
|
||||
private programs: Array<{
|
||||
program: FragmentShaderOnlyProgram;
|
||||
value: number;
|
||||
}> = [];
|
||||
private current: FragmentShaderOnlyProgram;
|
||||
|
||||
constructor(
|
||||
private gl: WebGL2RenderingContext,
|
||||
private vertexShaderSource: string,
|
||||
private fragmentShaderSource: string,
|
||||
private options: {
|
||||
getValueFromUniforms: (values: { [name: string]: any }) => number;
|
||||
uniformArraySizeName: string;
|
||||
startingValue: number;
|
||||
steps: number;
|
||||
maximumValue: number;
|
||||
}
|
||||
) {
|
||||
for (
|
||||
let i = options.startingValue;
|
||||
i < options.maximumValue;
|
||||
i += options.steps
|
||||
) {
|
||||
this.createProgram(i);
|
||||
}
|
||||
}
|
||||
|
||||
bindAndSetUniforms(values: { [name: string]: any }): void {
|
||||
let value = this.options.getValueFromUniforms(values);
|
||||
value = Math.min(this.options.maximumValue, value);
|
||||
|
||||
InfoText.modifyRecord(this.options.uniformArraySizeName, value.toString());
|
||||
|
||||
const closest = this.programs.find(
|
||||
(p) => value < p.value && value + this.options.steps >= p.value
|
||||
);
|
||||
if (closest) {
|
||||
this.current = closest.program;
|
||||
} else {
|
||||
this.current = this.createProgram(value + this.options.steps);
|
||||
}
|
||||
|
||||
this.current.bindAndSetUniforms(values);
|
||||
}
|
||||
|
||||
draw(): void {
|
||||
this.current.draw();
|
||||
}
|
||||
|
||||
delete(): void {
|
||||
this.programs.forEach((p) => p.program.delete());
|
||||
}
|
||||
|
||||
private createProgram(arraySize: number): FragmentShaderOnlyProgram {
|
||||
const program = new FragmentShaderOnlyProgram(
|
||||
this.gl,
|
||||
this.vertexShaderSource,
|
||||
this.fragmentShaderSource,
|
||||
{
|
||||
[this.options.uniformArraySizeName]: Math.floor(arraySize).toString(),
|
||||
}
|
||||
);
|
||||
|
||||
this.programs.push({
|
||||
program,
|
||||
value: arraySize,
|
||||
});
|
||||
|
||||
return program;
|
||||
}
|
||||
}
|
||||
16
frontend/src/scripts/drawing/i-renderer.ts
Normal file
16
frontend/src/scripts/drawing/i-renderer.ts
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
import { vec2 } from 'gl-matrix';
|
||||
import { Circle } from '../math/circle';
|
||||
|
||||
export interface IRenderer {
|
||||
startFrame(deltaTime: DOMHighResTimeStamp): void;
|
||||
finishFrame(): void;
|
||||
giveUniforms(uniforms: any): void;
|
||||
appendToUniformList(listName: string, ...values: Array<any>): void;
|
||||
setCameraPosition(position: vec2): void;
|
||||
setCursorPosition(position: vec2): void;
|
||||
setInViewArea(size: number): vec2;
|
||||
screenUvToWorldCoordinate(mousePosition: vec2): vec2;
|
||||
drawInfoText(text: string): void;
|
||||
isOnScreen(boundingCircle: Circle): boolean;
|
||||
isPositionOnScreen(position: vec2): boolean;
|
||||
}
|
||||
|
|
@ -3,13 +3,13 @@ import { clamp } from '../helper/clamp';
|
|||
import { Circle } from '../math/circle';
|
||||
import { Rectangle } from '../math/rectangle';
|
||||
import { InfoText } from '../objects/types/info-text';
|
||||
import { Drawer } from './drawer';
|
||||
import { DefaultFrameBuffer } from './graphics-library/default-frame-buffer';
|
||||
import { FragmentShaderOnlyProgram } from './graphics-library/fragment-shader-only-program';
|
||||
import { IntermediateFrameBuffer } from './graphics-library/intermediate-frame-buffer';
|
||||
import { WebGlStopwatch } from './graphics-library/stopwatch';
|
||||
import { DefaultFrameBuffer } from './graphics-library/frame-buffer/default-frame-buffer';
|
||||
import { IntermediateFrameBuffer } from './graphics-library/frame-buffer/intermediate-frame-buffer';
|
||||
import { WebGlStopwatch } from './graphics-library/helper/stopwatch';
|
||||
import { UniformArrayAutoScalingProgram } from './graphics-library/program/uniform-array-autoscaling-program';
|
||||
import { IRenderer } from './i-renderer';
|
||||
|
||||
export class WebGl2Renderer implements Drawer {
|
||||
export class WebGl2Renderer implements IRenderer {
|
||||
private gl: WebGL2RenderingContext;
|
||||
private stopwatch?: WebGlStopwatch;
|
||||
|
||||
|
|
@ -22,10 +22,10 @@ export class WebGl2Renderer implements Drawer {
|
|||
|
||||
private targetDeltaTime = (1 / 30) * 1000;
|
||||
private deltaTimeError = (1 / 1000) * 1000;
|
||||
private additiveQualityIncrease = 0.05;
|
||||
private multiplicativeQualityDecrease = 1.5;
|
||||
private additiveQualityIncrease = 0.03;
|
||||
private multiplicativeQualityDecrease = 1.2;
|
||||
private timeSinceLastAdjusment = 0;
|
||||
private adjusmentRate = 500;
|
||||
private adjusmentRate = 300;
|
||||
private maxRenderScale = 1.5;
|
||||
private minRenderScale = 0.2;
|
||||
|
||||
|
|
@ -46,7 +46,7 @@ export class WebGl2Renderer implements Drawer {
|
|||
this.targetDeltaTime - this.deltaTimeError
|
||||
) {
|
||||
this.distanceFieldFrameBuffer.renderScale +=
|
||||
this.additiveQualityIncrease / 2;
|
||||
this.additiveQualityIncrease / 4;
|
||||
this.lightingFrameBuffer.renderScale += this.additiveQualityIncrease;
|
||||
} else if (
|
||||
this.exponentialDecayedDeltaTime >
|
||||
|
|
@ -58,7 +58,7 @@ export class WebGl2Renderer implements Drawer {
|
|||
|
||||
this.distanceFieldFrameBuffer.renderScale = clamp(
|
||||
this.distanceFieldFrameBuffer.renderScale,
|
||||
this.minRenderScale,
|
||||
0.1,
|
||||
this.maxRenderScale
|
||||
);
|
||||
this.lightingFrameBuffer.renderScale = clamp(
|
||||
|
|
@ -92,11 +92,33 @@ export class WebGl2Renderer implements Drawer {
|
|||
}
|
||||
|
||||
this.distanceFieldFrameBuffer = new IntermediateFrameBuffer(this.gl, [
|
||||
new FragmentShaderOnlyProgram(this.gl, ...shaderSources[0]),
|
||||
new UniformArrayAutoScalingProgram(
|
||||
this.gl,
|
||||
shaderSources[0][0],
|
||||
shaderSources[0][1],
|
||||
{
|
||||
getValueFromUniforms: (v) => (v.lines ? v.lines.length / 2 : 0),
|
||||
uniformArraySizeName: 'lineCount',
|
||||
startingValue: 5,
|
||||
steps: 5,
|
||||
maximumValue: 100,
|
||||
}
|
||||
),
|
||||
]);
|
||||
|
||||
this.lightingFrameBuffer = new DefaultFrameBuffer(this.gl, [
|
||||
new FragmentShaderOnlyProgram(this.gl, ...shaderSources[1]),
|
||||
new UniformArrayAutoScalingProgram(
|
||||
this.gl,
|
||||
shaderSources[1][0],
|
||||
shaderSources[1][1],
|
||||
{
|
||||
getValueFromUniforms: (v) => (v.lights ? v.lights.length : 0),
|
||||
uniformArraySizeName: 'lightCount',
|
||||
startingValue: 1,
|
||||
steps: 1,
|
||||
maximumValue: 8,
|
||||
}
|
||||
),
|
||||
]);
|
||||
|
||||
this.distanceFieldFrameBuffer.renderScale = 0.5;
|
||||
|
|
@ -118,7 +140,6 @@ export class WebGl2Renderer implements Drawer {
|
|||
|
||||
public finishFrame() {
|
||||
this.calculateOwnUniforms();
|
||||
|
||||
this.distanceFieldFrameBuffer.render(this.uniforms);
|
||||
this.lightingFrameBuffer.render(
|
||||
this.uniforms,
|
||||
|
|
@ -235,4 +256,8 @@ export class WebGl2Renderer implements Drawer {
|
|||
public isOnScreen(boundingCircle: Circle): boolean {
|
||||
return this.viewCircle.areIntersecting(boundingCircle);
|
||||
}
|
||||
|
||||
public isPositionOnScreen(position: vec2): boolean {
|
||||
return this.viewCircle.isInside(position);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ import { createCharacter } from './objects/world/create-character';
|
|||
import { createDungeon } from './objects/world/create-dungeon';
|
||||
|
||||
export class Game {
|
||||
private previousTime: DOMHighResTimeStamp = 0;
|
||||
private previousTime?: DOMHighResTimeStamp = null;
|
||||
private objects: ObjectContainer = new ObjectContainer();
|
||||
private renderer: WebGl2Renderer;
|
||||
private previousFpsValues: Array<number> = [];
|
||||
|
|
@ -27,6 +27,11 @@ export class Game {
|
|||
const canvas: HTMLCanvasElement = document.querySelector('canvas#main');
|
||||
const overlay: HTMLElement = document.querySelector('#overlay');
|
||||
|
||||
document.addEventListener(
|
||||
'visibilitychange',
|
||||
this.handleVisibilityChange.bind(this)
|
||||
);
|
||||
|
||||
new CommandBroadcaster(
|
||||
[
|
||||
new KeyboardListener(document.body),
|
||||
|
|
@ -51,8 +56,18 @@ export class Game {
|
|||
createDungeon(this.objects);
|
||||
}
|
||||
|
||||
private handleVisibilityChange() {
|
||||
if (!document.hidden) {
|
||||
this.previousTime = null;
|
||||
}
|
||||
}
|
||||
|
||||
@timeIt()
|
||||
private gameLoop(time: DOMHighResTimeStamp) {
|
||||
if (this.previousTime === null) {
|
||||
this.previousTime = time;
|
||||
}
|
||||
|
||||
const deltaTime = time - this.previousTime;
|
||||
this.previousTime = time;
|
||||
this.calculateFps(deltaTime);
|
||||
|
|
@ -70,6 +85,7 @@ export class Game {
|
|||
private calculateFps(deltaTime: number) {
|
||||
this.previousFpsValues.push(1000 / deltaTime);
|
||||
if (this.previousFpsValues.length > 30) {
|
||||
this.previousFpsValues.sort();
|
||||
const text = `Min: ${this.previousFpsValues[0].toFixed(
|
||||
2
|
||||
)}\n\tMedian: ${this.previousFpsValues[
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ export class CircleLight extends GameObject {
|
|||
}
|
||||
|
||||
private draw(c: DrawCommand) {
|
||||
if (c.drawer.isOnScreen(this.boundingCircle)) {
|
||||
if (c.drawer.isPositionOnScreen(this.center)) {
|
||||
c.drawer.appendToUniformList('lights', {
|
||||
center: this.center,
|
||||
radius: this.radius,
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ export const createDungeon = (objects: ObjectContainer) => {
|
|||
new Tunnel(previousEnd, currentEnd, previousRadius, currentToRadius)
|
||||
);
|
||||
|
||||
if (deltaHeight > 0 && Math.random() > 0.5) {
|
||||
if (deltaHeight > 0 && Math.random() > 0.8) {
|
||||
objects.addObject(
|
||||
new CircleLight(
|
||||
currentEnd,
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
precision mediump float;
|
||||
|
||||
#define INFINITY 200.0
|
||||
#define LINE_COUNT 30
|
||||
#define LINE_COUNT {lineCount}
|
||||
#define CAVE_COLOR vec3(0.36, 0.38, 0.76)
|
||||
#define AIR_COLOR vec3(0.7)
|
||||
|
||||
|
|
@ -29,9 +29,8 @@ void main() {
|
|||
}
|
||||
|
||||
float distance = -minDistance;
|
||||
|
||||
fragmentColor = vec4(
|
||||
mix(CAVE_COLOR, AIR_COLOR, clamp(distance, 0.0, 1.0)),
|
||||
mix(CAVE_COLOR, AIR_COLOR, clamp(distance, -10.0, 0.0) / 10.0 + 1.0),
|
||||
distance / 32.0
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,9 +3,11 @@
|
|||
precision mediump float;
|
||||
|
||||
#define INFINITY 1000.0
|
||||
#define LIGHT_COUNT 8
|
||||
#define LIGHT_COUNT {lightCount}
|
||||
#define AMBIENT_LIGHT vec3(0.15)
|
||||
#define LIGHT_DROP 400.0
|
||||
#define MIN_STEP 3.0
|
||||
#define EDGE_SMOOTHING 5.0
|
||||
|
||||
uniform struct Light {
|
||||
vec2 center;
|
||||
|
|
@ -16,6 +18,7 @@ uniform struct Light {
|
|||
uniform sampler2D distanceTexture;
|
||||
uniform vec2 viewBoxSize;
|
||||
|
||||
in vec2[LIGHT_COUNT] directions;
|
||||
|
||||
float getDistance(in vec2 target, out vec3 color) {
|
||||
vec4 values = texture(distanceTexture, target);
|
||||
|
|
@ -44,43 +47,40 @@ float getFractionOfLightArriving(
|
|||
float minDistance = getDistance(target + direction * rayLength);
|
||||
movingAverageMeanDistance = movingAverageMeanDistance / 2.0 + minDistance / 2.0;
|
||||
q = min(q, movingAverageMeanDistance / rayLength);
|
||||
rayLength = min(lightDistance, rayLength + max(5.0, minDistance));
|
||||
rayLength = min(lightDistance, rayLength + max(MIN_STEP, minDistance));
|
||||
}
|
||||
|
||||
return clamp(q * (lightDistance + lightRadius) / lightRadius, 0.0, 1.0);
|
||||
}
|
||||
|
||||
vec3 getPixelColor(in vec2 worldCoordinates, in vec2 uvCoordinates) {
|
||||
vec3 colorAtPosition;
|
||||
float startingDistance = getDistance(uvCoordinates, colorAtPosition);
|
||||
|
||||
vec3 result = colorAtPosition * AMBIENT_LIGHT;
|
||||
|
||||
for (int i = 0; i < LIGHT_COUNT; i++) {
|
||||
Light light = lights[i];
|
||||
|
||||
vec2 lightDelta = light.center - worldCoordinates;
|
||||
float lightDistance = length(lightDelta);
|
||||
vec2 lightDirection = lightDelta / lightDistance;
|
||||
|
||||
float r = lightDistance / LIGHT_DROP + 1.0;
|
||||
vec3 lightColorAtPosition = light.value / (r * r);
|
||||
|
||||
float fractionOfLightArriving = getFractionOfLightArriving(
|
||||
uvCoordinates, lightDirection, startingDistance,
|
||||
lightDistance, light.radius
|
||||
);
|
||||
|
||||
result += colorAtPosition * lightColorAtPosition * fractionOfLightArriving;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
in vec2 worldCoordinates;
|
||||
in vec2 uvCoordinates;
|
||||
out vec4 fragmentColor;
|
||||
|
||||
void main() {
|
||||
fragmentColor = vec4(getPixelColor(worldCoordinates, uvCoordinates), 1.0);
|
||||
vec3 colorAtPosition;
|
||||
float startingDistance = getDistance(uvCoordinates, colorAtPosition);
|
||||
|
||||
vec3 ligthing = vec3(0.0);
|
||||
|
||||
for (int i = 0; i < LIGHT_COUNT; i++) {
|
||||
Light light = lights[i];
|
||||
float lightDistance = distance(light.center, worldCoordinates);
|
||||
|
||||
float r = lightDistance / LIGHT_DROP + 1.0;
|
||||
vec3 lightColorAtPosition = light.value / (r * r);
|
||||
|
||||
float fractionOfLightArriving = getFractionOfLightArriving(
|
||||
uvCoordinates, normalize(directions[i]), startingDistance,
|
||||
lightDistance, light.radius
|
||||
);
|
||||
|
||||
ligthing += lightColorAtPosition * fractionOfLightArriving;
|
||||
}
|
||||
|
||||
fragmentColor = vec4(
|
||||
colorAtPosition * (AMBIENT_LIGHT +
|
||||
step(EDGE_SMOOTHING / 2.0, clamp(startingDistance, 0.0, EDGE_SMOOTHING)) * ligthing),
|
||||
1.0
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
#version 300 es
|
||||
|
||||
precision mediump float;
|
||||
|
||||
uniform mat3 ndcToWorld;
|
||||
in vec4 a_position;
|
||||
out vec2 worldCoordinates;
|
||||
|
|
|
|||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue