Skip to content

Commit

Permalink
fix fog blending for sprites
Browse files Browse the repository at this point in the history
  • Loading branch information
siecvi committed Jan 12, 2025
1 parent 52a7d35 commit 5a4da7c
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 20 deletions.
16 changes: 13 additions & 3 deletions Resources/Shaders/LongSprite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@ varying vec4 fogDensity;

void main() {
gl_FragColor = texture2D(mainTexture, texCoord);

// linearize
#if LINEAR_FRAMEBUFFER
gl_FragColor.xyz *= gl_FragColor.xyz;
#endif

gl_FragColor.xyz *= gl_FragColor.w; // premultiplied alpha
gl_FragColor *= color;

vec4 fogColorPremuld = vec4(fogColor, 1.0);
fogColorPremuld *= gl_FragColor.w;
gl_FragColor = mix(gl_FragColor, fogColorPremuld, fogDensity);
vec4 fogColorP = vec4(fogColor, 1.0);

// linearize
#if LINEAR_FRAMEBUFFER
fogColorP.xyz *= fogColorP.xyz;
#endif

fogColorP *= gl_FragColor.w; // premultiplied alpha

gl_FragColor = mix(gl_FragColor, fogColorP, fogDensity);

if (dot(gl_FragColor, vec4(1.0)) < 0.002)
discard;
Expand Down
17 changes: 13 additions & 4 deletions Resources/Shaders/SoftLitSprite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

uniform sampler2D depthTexture;
uniform sampler2D mainTexture;

uniform vec3 sRGBFogColor;
uniform vec2 zNearFar;

Expand Down Expand Up @@ -68,15 +67,25 @@ void main() {
computedColor.xyz += emission;

gl_FragColor = texture2D(mainTexture, texCoord.xy);

// linearize
#if LINEAR_FRAMEBUFFER
gl_FragColor.xyz *= gl_FragColor.xyz;
#endif

gl_FragColor.xyz *= gl_FragColor.w; // premultiplied alpha
gl_FragColor *= computedColor;

vec3 fogColorPremuld = sRGBFogColor;
fogColorPremuld *= gl_FragColor.w;
gl_FragColor.xyz = mix(gl_FragColor.xyz, fogColorPremuld, fogDensity.xyz);
vec4 fogColorP = vec4(fogColor, 1.0);

// linearize
#if LINEAR_FRAMEBUFFER
fogColorP.xyz *= fogColorP.xyz;
#endif

fogColorP *= gl_FragColor.w; // premultiplied alpha

gl_FragColor = mix(gl_FragColor, fogColorP, fogDensity);

float soft = depth * depthRange.z + depthRange.x;
soft = smoothstep(0.0, 1.0, soft);
Expand Down
16 changes: 13 additions & 3 deletions Resources/Shaders/SoftSprite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,25 @@ void main() {
discard;

gl_FragColor = texture2D(mainTexture, texCoord.xy);

// linearize
#if LINEAR_FRAMEBUFFER
gl_FragColor.xyz *= gl_FragColor.xyz;
#endif

gl_FragColor.xyz *= gl_FragColor.w; // premultiplied alpha
gl_FragColor *= color;

vec3 fogColorPremuld = fogColor;
fogColorPremuld *= gl_FragColor.w;
gl_FragColor.xyz = mix(gl_FragColor.xyz, fogColorPremuld, fogDensity.xyz);
vec4 fogColorP = vec4(fogColor, 1.0);

// linearize
#if LINEAR_FRAMEBUFFER
fogColorP.xyz *= fogColorP.xyz;
#endif

fogColorP *= gl_FragColor.w; // premultiplied alpha

gl_FragColor = mix(gl_FragColor, fogColorP, fogDensity);

float soft = depth * depthRange.z + depthRange.x;
soft = smoothstep(0.0, 1.0, soft);
Expand Down
16 changes: 13 additions & 3 deletions Resources/Shaders/Sprite.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@ varying vec4 fogDensity;

void main() {
gl_FragColor = texture2D(mainTexture, texCoord);

// linearize
#if LINEAR_FRAMEBUFFER
gl_FragColor.xyz *= gl_FragColor.xyz;
#endif

gl_FragColor.xyz *= gl_FragColor.w; // premultiplied alpha
gl_FragColor *= color;

vec3 fogColorPremuld = fogColor;
fogColorPremuld *= gl_FragColor.w;
gl_FragColor.xyz = mix(gl_FragColor.xyz, fogColorPremuld, fogDensity.xyz);
vec4 fogColorP = vec4(fogColor, 1.0);

// linearize
#if LINEAR_FRAMEBUFFER
fogColorP.xyz *= fogColorP.xyz;
#endif

fogColorP *= gl_FragColor.w; // premultiplied alpha

gl_FragColor = mix(gl_FragColor, fogColorP, fogDensity);

if (dot(gl_FragColor, vec4(1.0)) < 0.002)
discard;
Expand Down
3 changes: 2 additions & 1 deletion Resources/Shaders/Sprite.vs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ void main() {
gl_Position = projectionViewMatrix * vec4(pos, 1.0);

color = colorAttribute;


// sprite texture coord
texCoord = spritePosAttribute.xy * 0.5 + 0.5;

// fog.
Expand Down
3 changes: 2 additions & 1 deletion Sources/Draw/GLLongSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "GLProgram.h"
#include "GLRenderer.h"
#include "IGLDevice.h"
#include "SWFeatureLevel.h"
#include "SWFeatureLevel.h" // for fastRcp
#include <Core/Debug.h>
#include <Core/Settings.h>

Expand Down Expand Up @@ -111,6 +111,7 @@ namespace spades {
viewOriginVector.SetValue(viewOrigin.x, viewOrigin.y, viewOrigin.z);

Vector3 fogCol = renderer.GetFogColor();
//fogCol *= fogCol; // linearize
fogColor.SetValue(fogCol.x, fogCol.y, fogCol.z);

const client::SceneDefinition &def = renderer.GetSceneDef();
Expand Down
4 changes: 2 additions & 2 deletions Sources/Draw/GLSoftLitSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include "GLRenderer.h"
#include "GLShadowShader.h"
#include "IGLDevice.h"
#include "SWFeatureLevel.h"
#include "SWFeatureLevel.h" // for fastRcp
#include <Core/Debug.h>
#include <Core/Settings.h>

Expand Down Expand Up @@ -230,7 +230,7 @@ namespace spades {
fogDistance.SetValue(renderer.GetFogDistance());

Vector3 fogCol = renderer.GetFogColor();
fogCol *= fogCol; // linearize
//fogCol *= fogCol; // linearize
fogColor.SetValue(fogCol.x, fogCol.y, fogCol.z);

const client::SceneDefinition& def = renderer.GetSceneDef();
Expand Down
2 changes: 1 addition & 1 deletion Sources/Draw/GLSoftSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace spades {
fogDistance.SetValue(renderer.GetFogDistance());

Vector3 fogCol = renderer.GetFogColor();
fogCol *= fogCol; // linearize
//fogCol *= fogCol; // linearize
fogColor.SetValue(fogCol.x, fogCol.y, fogCol.z);

const client::SceneDefinition &def = renderer.GetSceneDef();
Expand Down
4 changes: 2 additions & 2 deletions Sources/Draw/GLSpriteRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include "GLProgram.h"
#include "GLRenderer.h"
#include "IGLDevice.h"
#include "SWFeatureLevel.h"
#include "SWFeatureLevel.h" // for fastRcp
#include <Core/Debug.h>

namespace spades {
Expand Down Expand Up @@ -110,7 +110,7 @@ namespace spades {
viewOriginVector.SetValue(viewOrigin.x, viewOrigin.y, viewOrigin.z);

Vector3 fogCol = renderer.GetFogColor();
fogCol *= fogCol;
//fogCol *= fogCol; // linearize
fogColor.SetValue(fogCol.x, fogCol.y, fogCol.z);

const client::SceneDefinition &def = renderer.GetSceneDef();
Expand Down

0 comments on commit 5a4da7c

Please sign in to comment.