Tweak code

This commit is contained in:
schmelczerandras 2020-07-25 19:27:21 +02:00
parent b6bef3c77d
commit edd7d4836e
4 changed files with 39 additions and 48 deletions

View file

@ -20,7 +20,7 @@ export class WebGl2Renderer implements Drawer {
private distanceFieldFrameBuffer: IntermediateFrameBuffer; private distanceFieldFrameBuffer: IntermediateFrameBuffer;
private lightingFrameBuffer: DefaultFrameBuffer; private lightingFrameBuffer: DefaultFrameBuffer;
private targetDeltaTime = (1 / 50) * 1000; private targetDeltaTime = (1 / 30) * 1000;
private deltaTimeError = (1 / 1000) * 1000; private deltaTimeError = (1 / 1000) * 1000;
private additiveQualityIncrease = 0.05; private additiveQualityIncrease = 0.05;
private multiplicativeQualityDecrease = 1.5; private multiplicativeQualityDecrease = 1.5;

View file

@ -7,6 +7,7 @@ export interface Line {}
export class Tunnel extends GameObject { export class Tunnel extends GameObject {
private boundingCircle: Circle; private boundingCircle: Circle;
private tangent: vec2;
constructor( constructor(
private from: vec2, 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), vec2.fromValues(from.x / 2 + to.x / 2, from.y / 2 + to.y / 2),
radiusFrom + radiusTo + vec2.distance(from, to) radiusFrom + radiusTo + vec2.distance(from, to)
); );
this.tangent = vec2.subtract(vec2.create(), to, from);
this.addCommandExecutor(DrawCommand, this.draw.bind(this)); this.addCommandExecutor(DrawCommand, this.draw.bind(this));
} }
private draw(c: DrawCommand) { private draw(c: DrawCommand) {
if (c.drawer.isOnScreen(this.boundingCircle)) { 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); c.drawer.appendToUniformList('radii', this.radiusFrom, this.radiusTo);
} }
} }

View file

@ -3,47 +3,35 @@
precision mediump float; precision mediump float;
#define INFINITY 200.0 #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; uniform vec2[LINE_COUNT * 2] lines;
// startRadius, endRadois
uniform float[LINE_COUNT * 2] radii; 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; in vec2 worldCoordinates;
out vec4 fragmentColor; out vec4 fragmentColor;
void main() { void main() {
float distance = getDistance(worldCoordinates); float minDistance = INFINITY;
const vec3 caveColor = vec3(0.36, 0.38, 0.76);
const vec3 airColor = vec3(1.0); 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( 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 distance / 32.0
); );
} }

View file

@ -3,10 +3,9 @@
precision mediump float; precision mediump float;
#define INFINITY 1000.0 #define INFINITY 1000.0
#define LIGHT_COUNT 10 #define LIGHT_COUNT 8
#define AMBIENT_LIGHT vec3(0.15) #define AMBIENT_LIGHT vec3(0.15)
#define LIGHT_DROP 800.0 #define LIGHT_DROP 400.0
#define SHADOW_BIAS 0.01
uniform struct Light { uniform struct Light {
vec2 center; vec2 center;
@ -17,9 +16,6 @@ uniform struct Light {
uniform sampler2D distanceTexture; uniform sampler2D distanceTexture;
uniform vec2 viewBoxSize; uniform vec2 viewBoxSize;
float square(in float a) {
return a*a;
}
float getDistance(in vec2 target, out vec3 color) { float getDistance(in vec2 target, out vec3 color) {
vec4 values = texture(distanceTexture, target); vec4 values = texture(distanceTexture, target);
@ -44,14 +40,14 @@ float getFractionOfLightArriving(
direction /= viewBoxSize; direction /= viewBoxSize;
for (int j = 0; j < 64; j++) { for (int j = 0; j < 48; j++) {
float minDistance = getDistance(target + direction * rayLength); float minDistance = getDistance(target + direction * rayLength);
movingAverageMeanDistance = movingAverageMeanDistance / 2.0 + minDistance / 2.0; movingAverageMeanDistance = movingAverageMeanDistance / 2.0 + minDistance / 2.0;
q = min(q, movingAverageMeanDistance / rayLength); 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) { 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++) { for (int i = 0; i < LIGHT_COUNT; i++) {
Light light = lights[i]; Light light = lights[i];
float lightDistance = distance(worldCoordinates, light.center) - light.radius; vec2 lightDelta = light.center - worldCoordinates;
vec3 lightColorAtPosition = light.value / square(max(0.0, lightDistance / LIGHT_DROP) + 1.0); float lightDistance = length(lightDelta);
vec2 lightDirection = normalize(light.center - worldCoordinates); vec2 lightDirection = lightDelta / lightDistance;
float r = lightDistance / LIGHT_DROP + 1.0;
vec3 lightColorAtPosition = light.value / (r * r);
float fractionOfLightArriving = getFractionOfLightArriving( float fractionOfLightArriving = getFractionOfLightArriving(
uvCoordinates, lightDirection, startingDistance, uvCoordinates, lightDirection, startingDistance,
max(0.0, lightDistance), light.radius lightDistance, light.radius
); );
result += colorAtPosition * lightColorAtPosition * fractionOfLightArriving; result += colorAtPosition * lightColorAtPosition * fractionOfLightArriving;
} }
return clamp(result, 0.0, 1.0); return result;
} }
in vec2 worldCoordinates; in vec2 worldCoordinates;