Browse Source

Making lens flares better

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
ee2155a82c
4 changed files with 37 additions and 26 deletions
  1. 6 15
      shaders/LfSpritePass.frag.glsl
  2. 7 6
      shaders/LfSpritePass.vert.glsl
  3. 19 2
      src/event/LightEvent.cpp
  4. 5 3
      src/renderer/Lf.cpp

+ 6 - 15
shaders/LfSpritePass.frag.glsl

@@ -8,26 +8,17 @@
 #pragma anki type frag
 #pragma anki include "shaders/Common.glsl"
 
-#define SINGLE_FLARE 0
+layout(UBO_BINDING(0, 0)) uniform sampler2DArray u_tex;
 
-#if SINGLE_FLARE
-uniform sampler2D uImage;
-#else
-uniform DEFAULT_FLOAT_PRECISION sampler2DArray uImages;
-#endif
-
-layout(location = 0) in vec3 inTexCoords;
-layout(location = 1) flat in float inAlpha;
+layout(location = 0) in vec3 in_uv;
+layout(location = 1) flat in vec4 in_color;
 
 layout(location = 0) out vec3 outColor;
 
 void main()
 {
-#if SINGLE_FLARE
-	vec3 col = texture(uImage, inTexCoords.st).rgb;
-#else
-	vec3 col = texture(uImages, inTexCoords).rgb;
-#endif
+	vec3 col = texture(u_tex, in_uv).rgb;
 
-	outColor = col * inAlpha;
+	outColor = col * in_color.rgb;
+	outColor *= in_color.a;
 }

+ 7 - 6
shaders/LfSpritePass.vert.glsl

@@ -11,8 +11,9 @@
 // Per flare information
 struct Sprite
 {
-	vec4 posScale; // xy: Position, z: Scale, w: Scale again
-	vec4 alphaDepth; // x: alpha, y: texture depth
+	vec4 posScale; // xy: Position, zw: Scale
+	vec4 color;
+	vec4 depthPad3;
 };
 
 // The block contains data for all flares
@@ -21,8 +22,8 @@ layout(std140) uniform _blk
 	Sprite u_sprites[MAX_SPRITES];
 };
 
-layout(location = 0) out vec3 out_texCoord;
-layout(location = 1) flat out float out_alpha;
+layout(location = 0) out vec3 out_uv;
+layout(location = 1) flat out vec4 out_color;
 
 out gl_PerVertex
 {
@@ -42,10 +43,10 @@ void main()
 	Sprite sprite = u_sprites[gl_InstanceID];
 
 	// Write tex coords of the 2D array texture
-	out_texCoord = vec3((position * 0.5) + 0.5, sprite.alphaDepth.y);
+	out_uv = vec3((position * 0.5) + 0.5, sprite.depthPad3.x);
 
 	vec4 posScale = sprite.posScale;
 	gl_Position = vec4(position * posScale.zw + posScale.xy , 0.0, 1.0);
 
-	out_alpha = sprite.alphaDepth.x;
+	out_color = sprite.color;
 }

+ 19 - 2
src/event/LightEvent.cpp

@@ -5,6 +5,7 @@
 
 #include "anki/event/LightEvent.h"
 #include "anki/scene/Light.h"
+#include "anki/scene/LensFlareComponent.h"
 
 namespace anki {
 
@@ -57,8 +58,24 @@ Error LightEvent::update(F32 prevUpdateTime, F32 crntTime)
 		break;
 	}
 
-	lightc.setDiffuseColor(
-		m_originalDiffColor + factor * m_intensityMultiplier);
+	// Update the color and the lens flare's color if they are the same
+	if(m_intensityMultiplier != Vec4(0.0))
+	{
+		Vec4 outCol = m_originalDiffColor + factor * m_intensityMultiplier;
+
+		LensFlareComponent* lfc =
+			getSceneNode()->tryGetComponent<LensFlareComponent>();
+
+		if(lfc && lfc->getColorMultiplier().xyz()
+			== lightc.getDiffuseColor().xyz())
+		{
+			lfc->setColorMultiplier(
+				Vec4(outCol.xyz(), lfc->getColorMultiplier().w()));
+		}
+
+		lightc.setDiffuseColor(outCol);
+	}
+
 	lightc.setSpecularColor(
 		m_originalSpecColor + factor * m_specularIntensityMultiplier);
 

+ 5 - 3
src/renderer/Lf.cpp

@@ -26,9 +26,9 @@ struct Sprite
 {
 	Vec2 m_pos; ///< Position in NDC
 	Vec2 m_scale; ///< Scale of the quad
-	F32 m_alpha; ///< Alpha value
+	Vec4 m_color;
 	F32 m_depth; ///< Texture depth
-	U32 m_padding[2];
+	U32 m_padding[3];
 };
 
 //==============================================================================
@@ -277,9 +277,11 @@ void Lf::run(CommandBufferPtr& cmdb)
 				lf.getFirstFlareSize() * Vec2(1.0, m_r->getAspectRatio());
 			sprites[count].m_depth = 0.0;
 			// Fade the flare on the edges
-			sprites[count].m_alpha = lf.getColorMultiplier().w()
+			F32 alpha = lf.getColorMultiplier().w()
 				* (1.0 - pow(abs(posNdc.x()), 6.0))
 				* (1.0 - pow(abs(posNdc.y()), 6.0));
+
+			sprites[count].m_color = Vec4(lf.getColorMultiplier().xyz(), alpha);
 			++count;
 
 			// Render