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

@ -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;