From 79bb7a64a2d5fed178bbac88bd30b5508bb33c5a Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Sun, 26 Jul 2020 19:11:03 +0200 Subject: [PATCH] Add more substitutions --- .../drawing/graphics-library/helper/create-shader.ts | 11 +++++++---- .../program/uniform-array-autoscaling-program.ts | 2 ++ frontend/src/scripts/drawing/webgl2-renderer.ts | 4 ++++ frontend/src/scripts/game.ts | 1 + frontend/src/shaders/cave-distance-fs.glsl | 5 +++-- frontend/src/shaders/lights-shading-fs.glsl | 11 ++++++----- 6 files changed, 23 insertions(+), 11 deletions(-) diff --git a/frontend/src/scripts/drawing/graphics-library/helper/create-shader.ts b/frontend/src/scripts/drawing/graphics-library/helper/create-shader.ts index c7c8d5e..0338fbe 100644 --- a/frontend/src/scripts/drawing/graphics-library/helper/create-shader.ts +++ b/frontend/src/scripts/drawing/graphics-library/helper/create-shader.ts @@ -4,10 +4,13 @@ export const createShader = ( source: string, substitutions: { [name: string]: string } ): WebGLShader => { - source = source.replace( - /{(.+)}/gm, - (_, name: string): string => substitutions[name] - ); + source = source.replace(/{(.+)}/gm, (_, name: string): string => { + const value = substitutions[name]; + if (Number.isInteger(value)) { + return `${value}.0`; + } + return value; + }); const shader = gl.createShader(type); gl.shaderSource(shader, source); diff --git a/frontend/src/scripts/drawing/graphics-library/program/uniform-array-autoscaling-program.ts b/frontend/src/scripts/drawing/graphics-library/program/uniform-array-autoscaling-program.ts index acdb447..3f17767 100644 --- a/frontend/src/scripts/drawing/graphics-library/program/uniform-array-autoscaling-program.ts +++ b/frontend/src/scripts/drawing/graphics-library/program/uniform-array-autoscaling-program.ts @@ -13,6 +13,7 @@ export class UniformArrayAutoScalingProgram implements IProgram { private gl: WebGL2RenderingContext, private vertexShaderSource: string, private fragmentShaderSource: string, + private substitutions: { [name: string]: any }, private options: { getValueFromUniforms: (values: { [name: string]: any }) => number; uniformArraySizeName: string; @@ -63,6 +64,7 @@ export class UniformArrayAutoScalingProgram implements IProgram { this.fragmentShaderSource, { [this.options.uniformArraySizeName]: Math.floor(arraySize).toString(), + ...this.substitutions, } ); diff --git a/frontend/src/scripts/drawing/webgl2-renderer.ts b/frontend/src/scripts/drawing/webgl2-renderer.ts index 11021bc..f71fa12 100644 --- a/frontend/src/scripts/drawing/webgl2-renderer.ts +++ b/frontend/src/scripts/drawing/webgl2-renderer.ts @@ -91,11 +91,14 @@ export class WebGl2Renderer implements IRenderer { throw new Error('WebGl2 is not supported'); } + const distanceScale = 64; + this.distanceFieldFrameBuffer = new IntermediateFrameBuffer(this.gl, [ new UniformArrayAutoScalingProgram( this.gl, shaderSources[0][0], shaderSources[0][1], + { distanceScale }, { getValueFromUniforms: (v) => (v.lines ? v.lines.length / 2 : 0), uniformArraySizeName: 'lineCount', @@ -111,6 +114,7 @@ export class WebGl2Renderer implements IRenderer { this.gl, shaderSources[1][0], shaderSources[1][1], + { distanceScale }, { getValueFromUniforms: (v) => (v.lights ? v.lights.length : 0), uniformArraySizeName: 'lightCount', diff --git a/frontend/src/scripts/game.ts b/frontend/src/scripts/game.ts index 4094716..a00c895 100644 --- a/frontend/src/scripts/game.ts +++ b/frontend/src/scripts/game.ts @@ -54,6 +54,7 @@ export class Game { this.objects.addObject(new InfoText()); createCharacter(this.objects); createDungeon(this.objects); + createDungeon(this.objects); } private handleVisibilityChange() { diff --git a/frontend/src/shaders/cave-distance-fs.glsl b/frontend/src/shaders/cave-distance-fs.glsl index c27772a..cbd4aa1 100644 --- a/frontend/src/shaders/cave-distance-fs.glsl +++ b/frontend/src/shaders/cave-distance-fs.glsl @@ -6,7 +6,8 @@ precision mediump float; #define LINE_COUNT {lineCount} #define CAVE_COLOR vec3(0.36, 0.38, 0.76) #define AIR_COLOR vec3(0.7) - +#define DISTANCE_SCALE {distanceScale} + // start, end - start uniform vec2[LINE_COUNT * 2] lines; // startRadius, endRadois @@ -31,6 +32,6 @@ void main() { float distance = -minDistance; fragmentColor = vec4( mix(CAVE_COLOR, AIR_COLOR, clamp(distance, -10.0, 0.0) / 10.0 + 1.0), - distance / 32.0 + distance / DISTANCE_SCALE ); } diff --git a/frontend/src/shaders/lights-shading-fs.glsl b/frontend/src/shaders/lights-shading-fs.glsl index 65c20cc..2d9df35 100644 --- a/frontend/src/shaders/lights-shading-fs.glsl +++ b/frontend/src/shaders/lights-shading-fs.glsl @@ -3,11 +3,12 @@ precision mediump float; #define INFINITY 1000.0 -#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 +#define LIGHT_COUNT {lightCount} +#define DISTANCE_SCALE {distanceScale} uniform struct Light { vec2 center; @@ -23,11 +24,11 @@ in vec2[LIGHT_COUNT] directions; float getDistance(in vec2 target, out vec3 color) { vec4 values = texture(distanceTexture, target); color = values.rgb; - return values.w * 32.0; + return values.w * DISTANCE_SCALE; } float getDistance(in vec2 target) { - return texture(distanceTexture, target).w * 32.0; + return texture(distanceTexture, target).w * DISTANCE_SCALE; } float getFractionOfLightArriving( @@ -43,7 +44,7 @@ float getFractionOfLightArriving( direction /= viewBoxSize; - for (int j = 0; j < 48; j++) { + for (int j = 0; j < 32; j++) { float minDistance = getDistance(target + direction * rayLength); movingAverageMeanDistance = movingAverageMeanDistance / 2.0 + minDistance / 2.0; q = min(q, movingAverageMeanDistance / rayLength); @@ -67,7 +68,7 @@ void main() { Light light = lights[i]; float lightDistance = distance(light.center, worldCoordinates); - float r = lightDistance / LIGHT_DROP + 1.0; + float r = (lightDistance + light.radius) / LIGHT_DROP + 1.0; vec3 lightColorAtPosition = light.value / (r * r); float fractionOfLightArriving = getFractionOfLightArriving(