From 374796f39824c6749ab739d9ffd5b511b9a6f883 Mon Sep 17 00:00:00 2001 From: schmelczerandras Date: Fri, 2 Oct 2020 11:23:10 +0200 Subject: [PATCH] Fix flashlights --- src/drawables/lights/flashlight.ts | 4 ++-- .../rendering/shaders/shading-fs-100.glsl | 17 +++++++++++------ src/graphics/rendering/shaders/shading-fs.glsl | 14 +++++++++----- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/drawables/lights/flashlight.ts b/src/drawables/lights/flashlight.ts index 51bff76..ffeda4c 100644 --- a/src/drawables/lights/flashlight.ts +++ b/src/drawables/lights/flashlight.ts @@ -19,7 +19,7 @@ export class Flashlight extends LightDrawable { vec2.fromValues(0, 0), vec3.fromValues(0, 0, 0), 0, - vec2.fromValues(0, 0) + vec2.fromValues(1, 0) ), }; @@ -35,7 +35,7 @@ export class Flashlight extends LightDrawable { protected getObjectToSerialize(transform2d: mat2d, transform1d: number): any { return { ...super.getObjectToSerialize(transform2d, transform1d), - direction: this.direction, + direction: vec2.normalize(vec2.create(), this.direction), }; } } diff --git a/src/graphics/rendering/shaders/shading-fs-100.glsl b/src/graphics/rendering/shaders/shading-fs-100.glsl index a715668..1b9f502 100644 --- a/src/graphics/rendering/shaders/shading-fs-100.glsl +++ b/src/graphics/rendering/shaders/shading-fs-100.glsl @@ -54,7 +54,11 @@ float shadowTransparency(float startingDistance, float lightCenterDistance, vec2 varying vec2 flashlightActualDirections[FLASHLIGHT_COUNT]; float intensityInDirection(vec2 lightDirection, vec2 targetDirection) { - return smoothstep(0.0, 1.0, 10.0 * max(0.0, dot(targetDirection, lightDirection) - 0.9)); + return smoothstep( + 0.0, + 1.0, + 10.0 * max(0.0, dot(targetDirection, -lightDirection) - 0.9) + ); } #endif #endif @@ -92,15 +96,16 @@ void main() { #ifdef FLASHLIGHT_COUNT #if FLASHLIGHT_COUNT > 0 for (int i = 0; i < FLASHLIGHT_COUNT; i++) { - vec2 originalDirection = normalize(flashlightDirections[i]); + vec2 originalDirection = normalize(flashlightActualDirections[i]); - vec3 lightColorAtPosition = colorInPosition(i, originalDirection, lightCenterDistance); - float lightCenterDistance = intensityInDirection(flashlightDirections[i], positionDirection) - * flashlightColors[lightIndex] / pow( + float lightCenterDistance = distance(flashlightCenters[i], position); + + vec3 lightColorAtPosition = intensityInDirection(flashlightDirections[i], originalDirection) + * flashlightColors[i] / pow( lightCenterDistance / flashlightIntensities[i] + 1.0, 2.0 ); - vec2 direction = originalDirection / squareToAspectRatioTimes2 + vec2 direction = originalDirection / squareToAspectRatioTimes2; if (length(lightColorAtPosition) < 0.0) { continue; diff --git a/src/graphics/rendering/shaders/shading-fs.glsl b/src/graphics/rendering/shaders/shading-fs.glsl index 9472300..3b8204b 100644 --- a/src/graphics/rendering/shaders/shading-fs.glsl +++ b/src/graphics/rendering/shaders/shading-fs.glsl @@ -63,7 +63,11 @@ float shadowTransparency(float startingDistance, float lightCenterDistance, vec2 in vec2 flashlightActualDirections[FLASHLIGHT_COUNT]; float intensityInDirection(vec2 lightDirection, vec2 targetDirection) { - return smoothstep(0.0, 1.0, 10.0 * max(0.0, dot(targetDirection, lightDirection) - 0.9)); + return smoothstep( + 0.0, + 1.0, + 10.0 * max(0.0, dot(targetDirection, -lightDirection) - 0.9) + ); } vec3 colorInPosition(int lightIndex, vec2 positionDirection, out float lightCenterDistance) { @@ -84,7 +88,7 @@ void main() { vec3 colorAtPosition = rgbaColorAtPosition.rgb; vec3 lighting = ambientLight; - vec3 lightingInside = lighting * INTENSITY_INSIDE_RATIO; + vec3 lightingInside = lighting; #ifdef CIRCLE_LIGHT_COUNT #if CIRCLE_LIGHT_COUNT > 0 @@ -107,12 +111,12 @@ void main() { #ifdef FLASHLIGHT_COUNT #if FLASHLIGHT_COUNT > 0 for (int i = 0; i < FLASHLIGHT_COUNT; i++) { - vec2 originalDirection = normalize(flashlightDirections[i]); + vec2 originalDirection = normalize(flashlightActualDirections[i]); float lightCenterDistance; vec3 lightColorAtPosition = colorInPosition(i, originalDirection, lightCenterDistance); - vec2 direction = originalDirection / squareToAspectRatioTimes2 + vec2 direction = originalDirection / squareToAspectRatioTimes2; if (length(lightColorAtPosition) < 0.0) { continue; @@ -130,7 +134,7 @@ void main() { #endif vec3 outsideColor = backgroundColor.rgb * lighting; - vec3 insideColor = colorAtPosition * lightingInside; + vec3 insideColor = colorAtPosition * lightingInside * INTENSITY_INSIDE_RATIO; float edge = clamp(startingDistance / shadingNdcPixelSize, 0.0, 1.0); vec3 antialiasedColor = mix(insideColor, outsideColor, edge);