From edd7d4836e4f7240c536de0555d7c7edbac7198c Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Sat, 25 Jul 2020 19:27:21 +0200 Subject: [PATCH] Tweak code --- .../src/scripts/drawing/webgl2-renderer.ts | 2 +- frontend/src/scripts/objects/types/tunnel.ts | 6 ++- frontend/src/shaders/cave-distance-fs.glsl | 52 +++++++------------ frontend/src/shaders/lights-shading-fs.glsl | 27 +++++----- 4 files changed, 39 insertions(+), 48 deletions(-) diff --git a/frontend/src/scripts/drawing/webgl2-renderer.ts b/frontend/src/scripts/drawing/webgl2-renderer.ts index 8931c0d..f080c9d 100644 --- a/frontend/src/scripts/drawing/webgl2-renderer.ts +++ b/frontend/src/scripts/drawing/webgl2-renderer.ts @@ -20,7 +20,7 @@ export class WebGl2Renderer implements Drawer { private distanceFieldFrameBuffer: IntermediateFrameBuffer; private lightingFrameBuffer: DefaultFrameBuffer; - private targetDeltaTime = (1 / 50) * 1000; + private targetDeltaTime = (1 / 30) * 1000; private deltaTimeError = (1 / 1000) * 1000; private additiveQualityIncrease = 0.05; private multiplicativeQualityDecrease = 1.5; diff --git a/frontend/src/scripts/objects/types/tunnel.ts b/frontend/src/scripts/objects/types/tunnel.ts index 7059f90..2f5fc58 100644 --- a/frontend/src/scripts/objects/types/tunnel.ts +++ b/frontend/src/scripts/objects/types/tunnel.ts @@ -7,6 +7,7 @@ export interface Line {} export class Tunnel extends GameObject { private boundingCircle: Circle; + private tangent: vec2; constructor( private from: vec2, @@ -20,12 +21,15 @@ export class Tunnel extends GameObject { vec2.fromValues(from.x / 2 + to.x / 2, from.y / 2 + to.y / 2), radiusFrom + radiusTo + vec2.distance(from, to) ); + + this.tangent = vec2.subtract(vec2.create(), to, from); + this.addCommandExecutor(DrawCommand, this.draw.bind(this)); } private draw(c: DrawCommand) { if (c.drawer.isOnScreen(this.boundingCircle)) { - c.drawer.appendToUniformList('lines', this.from, this.to); + c.drawer.appendToUniformList('lines', this.from, this.tangent); c.drawer.appendToUniformList('radii', this.radiusFrom, this.radiusTo); } } diff --git a/frontend/src/shaders/cave-distance-fs.glsl b/frontend/src/shaders/cave-distance-fs.glsl index 1c61959..a93a99b 100644 --- a/frontend/src/shaders/cave-distance-fs.glsl +++ b/frontend/src/shaders/cave-distance-fs.glsl @@ -3,47 +3,35 @@ precision mediump float; #define INFINITY 200.0 -#define LINE_COUNT 50 +#define LINE_COUNT 30 +#define CAVE_COLOR vec3(0.36, 0.38, 0.76) +#define AIR_COLOR vec3(0.7) +// start, end - start uniform vec2[LINE_COUNT * 2] lines; +// startRadius, endRadois uniform float[LINE_COUNT * 2] radii; -float lineDistance( - in vec2 target, - in vec2 start, - in vec2 end, - in float radiusFrom, - in float radiusTo -) { - vec2 pa = target - start, ba = end - start; - float baLength = length(ba); - float h = clamp(dot(pa / baLength, ba / baLength), 0.0, 1.0); - return length(pa - ba * h) - mix(radiusFrom, radiusTo, h); -} - -float getDistance(in vec2 target) { - float minDistance = INFINITY; - - for (int i = 0; i < LINE_COUNT; i++) { - vec2 start = lines[2 * i]; - vec2 end = lines[2 * i + 1]; - float rFrom = radii[2 * i]; - float rTo = radii[2 * i + 1]; - minDistance = min(minDistance, lineDistance(target, start, end, rFrom, rTo)); - } - - return -minDistance; -} - in vec2 worldCoordinates; out vec4 fragmentColor; void main() { - float distance = getDistance(worldCoordinates); - const vec3 caveColor = vec3(0.36, 0.38, 0.76); - const vec3 airColor = vec3(1.0); + float minDistance = INFINITY; + + for (int i = 0; i < LINE_COUNT; i++) { + vec2 pa = worldCoordinates - lines[2 * i]; + vec2 ba = lines[2 * i + 1]; + float baLength = length(ba); + float h = clamp(dot(pa / baLength, ba / baLength), 0.0, 1.0); + float lineDistance = distance(pa, ba * h) - mix(radii[2 * i], radii[2 * i + 1], h); + + minDistance = min(minDistance, lineDistance); + } + + float distance = -minDistance; + fragmentColor = vec4( - mix(caveColor, airColor, clamp(distance, 0.0, 1.0)), + mix(CAVE_COLOR, AIR_COLOR, clamp(distance, 0.0, 1.0)), distance / 32.0 ); } diff --git a/frontend/src/shaders/lights-shading-fs.glsl b/frontend/src/shaders/lights-shading-fs.glsl index c74b36c..4156412 100644 --- a/frontend/src/shaders/lights-shading-fs.glsl +++ b/frontend/src/shaders/lights-shading-fs.glsl @@ -3,10 +3,9 @@ precision mediump float; #define INFINITY 1000.0 -#define LIGHT_COUNT 10 +#define LIGHT_COUNT 8 #define AMBIENT_LIGHT vec3(0.15) -#define LIGHT_DROP 800.0 -#define SHADOW_BIAS 0.01 +#define LIGHT_DROP 400.0 uniform struct Light { vec2 center; @@ -17,9 +16,6 @@ uniform struct Light { uniform sampler2D distanceTexture; uniform vec2 viewBoxSize; -float square(in float a) { - return a*a; -} float getDistance(in vec2 target, out vec3 color) { vec4 values = texture(distanceTexture, target); @@ -44,14 +40,14 @@ float getFractionOfLightArriving( direction /= viewBoxSize; - for (int j = 0; j < 64; j++) { + for (int j = 0; j < 48; j++) { float minDistance = getDistance(target + direction * rayLength); movingAverageMeanDistance = movingAverageMeanDistance / 2.0 + minDistance / 2.0; q = min(q, movingAverageMeanDistance / rayLength); - rayLength = min(lightDistance, rayLength + max(1.0, minDistance)); + rayLength = min(lightDistance, rayLength + max(5.0, minDistance)); } - return smoothstep(0.0, 1.0, (q - SHADOW_BIAS) * (lightDistance + lightRadius) / lightRadius); + return clamp(q * (lightDistance + lightRadius) / lightRadius, 0.0, 1.0); } vec3 getPixelColor(in vec2 worldCoordinates, in vec2 uvCoordinates) { @@ -63,19 +59,22 @@ vec3 getPixelColor(in vec2 worldCoordinates, in vec2 uvCoordinates) { for (int i = 0; i < LIGHT_COUNT; i++) { Light light = lights[i]; - float lightDistance = distance(worldCoordinates, light.center) - light.radius; - vec3 lightColorAtPosition = light.value / square(max(0.0, lightDistance / LIGHT_DROP) + 1.0); - vec2 lightDirection = normalize(light.center - worldCoordinates); + 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, - max(0.0, lightDistance), light.radius + lightDistance, light.radius ); result += colorAtPosition * lightColorAtPosition * fractionOfLightArriving; } - return clamp(result, 0.0, 1.0); + return result; } in vec2 worldCoordinates;