Skip to content

Commit aa35554

Browse files
mogumboscpeters
authored andcommitted
Lens flare cleanup and colorization (gazebosim#2927)
* Added color setting to LensFlare and LensFlareSensorPlugin. Added methods to expose scale and color to derived plugins. * Removed unused animated lens flare effect. * Removed noise_rgba.png, which is no longer used. * Changed a value in the lens flare shader back to its original 1.0. This means there will be no compensation for the removal of the legacy animation effect and the lens flares will look less intense.
1 parent fcffcd9 commit aa35554

File tree

8 files changed

+90
-42
lines changed

8 files changed

+90
-42
lines changed

gazebo/rendering/LensFlare.cc

+33-5
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ namespace gazebo
3636
namespace rendering
3737
{
3838
/// \brief We'll create an instance of this class for each camera, to be
39-
/// used to inject lens flare uniforms and time (for animating flare)
40-
/// in each render call.
39+
/// used to inject lens flare uniforms in each render call.
4140
class LensFlareCompositorListener
4241
: public Ogre::CompositorInstance::Listener
4342
{
@@ -72,6 +71,13 @@ namespace gazebo
7271
this->scale = _scale;
7372
}
7473

74+
/// \brief Set the color of lens flare.
75+
/// \param[in] _color Color of lens flare
76+
public: void SetColor(const ignition::math::Vector3d &_color)
77+
{
78+
this->color = _color;
79+
}
80+
7581
/// \brief Callback that OGRE will invoke for us on each render call
7682
/// \param[in] _passID OGRE material pass ID.
7783
/// \param[in] _mat Pointer to OGRE material.
@@ -97,9 +103,6 @@ namespace gazebo
97103
pass->getFragmentProgramParameters();
98104
GZ_ASSERT(!params.isNull(), "Null OGRE material GPU parameters");
99105

100-
// used for animating flare
101-
params->setNamedConstant("time", static_cast<Ogre::Real>(
102-
common::Time::GetWallTime().Double()));
103106
// for adjusting aspect ratio of flare
104107
params->setNamedConstant("viewport",
105108
Ogre::Vector3(static_cast<double>(this->camera->ViewportWidth()),
@@ -133,6 +136,8 @@ namespace gazebo
133136
params->setNamedConstant("lightPos", Conversions::Convert(pos));
134137
params->setNamedConstant("scale",
135138
static_cast<Ogre::Real>(lensFlareScale));
139+
params->setNamedConstant("color",
140+
Ogre::Vector3(this->color.X(), this->color.Y(), this->color.Z()));
136141
}
137142

138143
/// \brief Get the lens flare position and scale for a normal camera
@@ -338,6 +343,10 @@ namespace gazebo
338343

339344
/// \brief Scale of lens flare.
340345
private: double scale = 1.0;
346+
347+
/// \brief Color of lens flare.
348+
private: ignition::math::Vector3d color
349+
= ignition::math::Vector3d(1.0, 1.0, 1.0);
341350
};
342351

343352
/// \brief Private data class for LensFlare
@@ -373,6 +382,10 @@ namespace gazebo
373382

374383
/// \brief Scale of lens flare.
375384
public: double lensFlareScale = 1.0;
385+
386+
/// \brief Color of lens flare.
387+
public: ignition::math::Vector3d lensFlareColor
388+
= ignition::math::Vector3d(1.0, 1.0, 1.0);
376389
};
377390
}
378391
}
@@ -415,6 +428,8 @@ void LensFlare::SetCamera(CameraPtr _camera)
415428
LensFlareCompositorListener(this->dataPtr->camera, nullptr));
416429
this->dataPtr->lensFlareCompositorListener->SetScale(
417430
this->dataPtr->lensFlareScale);
431+
this->dataPtr->lensFlareCompositorListener->SetColor(
432+
this->dataPtr->lensFlareColor);
418433

419434
this->dataPtr->lensFlareInstance =
420435
Ogre::CompositorManager::getSingleton().addCompositor(
@@ -442,6 +457,19 @@ void LensFlare::SetScale(const double _scale)
442457
}
443458
}
444459

460+
//////////////////////////////////////////////////
461+
void LensFlare::SetColor(const ignition::math::Vector3d &_color)
462+
{
463+
// lensFlareColor is intentionally not clamped so the user can work in
464+
// HDR color spaces or be artistic.
465+
this->dataPtr->lensFlareColor = _color;
466+
if (this->dataPtr->lensFlareCompositorListener)
467+
{
468+
this->dataPtr->lensFlareCompositorListener->SetColor(
469+
this->dataPtr->lensFlareColor);
470+
}
471+
}
472+
445473
//////////////////////////////////////////////////
446474
void LensFlare::Update()
447475
{

gazebo/rendering/LensFlare.hh

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ namespace gazebo
5858
/// \param[in] _scale Scale of lens flare
5959
public: void SetScale(const double _scale);
6060

61+
/// \brief Set the color of lens flare.
62+
/// \param[in] _color Color of lens flare
63+
public: void SetColor(const ignition::math::Vector3d &_color);
64+
6165
/// \brief Update function to search light source
6266
private: void Update();
6367

media/materials/programs/camera_lens_flare_fs.glsl

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// The input texture, which is set up by the Ogre Compositor infrastructure.
22
uniform sampler2D RT;
3-
uniform sampler2D noiseRGBA;
43

5-
uniform float time;
64
uniform vec3 viewport;
75

86
// light pos in clip space
@@ -11,16 +9,8 @@ uniform vec3 lightPos;
119
// lens flare scale
1210
uniform float scale;
1311

14-
float noise(float t)
15-
{
16-
// 256 is the size of noiseRGBA texture
17-
return texture2D(noiseRGBA, vec2(t, 0.0) / vec2(256.0)).x;
18-
}
19-
20-
float noise(vec2 t)
21-
{
22-
return texture2D(noiseRGBA,(t + vec2(time)) / vec2(256.0)).x;
23-
}
12+
// lens flare color
13+
uniform vec3 color;
2414

2515
vec3 lensflare(vec2 uv,vec2 pos)
2616
{
@@ -30,12 +20,9 @@ vec3 lensflare(vec2 uv,vec2 pos)
3020

3121
float ang = atan(main.y, main.x);
3222
float dist = length(main); dist = pow(dist,.1);
33-
float n = noise(vec2((ang-time/9.0)*16.0,dist*32.0));
3423

3524
float f0 = 1.0/(length(uv-pos)*16.0/scale+1.0);
3625

37-
f0 = f0+f0*(sin((ang+time/18.0 + noise(abs(ang)+n/2.0)*2.0)*12.0)*.1+dist*.1+.8);
38-
3926
float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25;
4027
float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23;
4128
float f23 = max(1.0/(1.0+32.0*pow(length(uvd+0.9*pos),2.0)),.0)*00.21;
@@ -67,13 +54,6 @@ vec3 lensflare(vec2 uv,vec2 pos)
6754
return c;
6855
}
6956

70-
// color modifier
71-
vec3 cc(vec3 color, float factor, float factor2)
72-
{
73-
float w = color.x+color.y+color.z;
74-
return mix(color,vec3(w)*factor,w*factor2);
75-
}
76-
7757
void main()
7858
{
7959
// return if light is behind the view
@@ -97,9 +77,8 @@ void main()
9777
pos.x *= aspect;
9878

9979
// compute lens flare
100-
vec3 color = vec3(1.4,1.2,1.0)*lensflare(uv, pos.xy);
101-
color = cc(color,.5,.1);
80+
vec3 finalColor = color * lensflare(uv, pos.xy);
10281

10382
// apply lens flare
104-
gl_FragColor = texture2D(RT, gl_TexCoord[0].xy) + vec4(color, 1.0);
83+
gl_FragColor = texture2D(RT, gl_TexCoord[0].xy) + vec4(finalColor, 1.0);
10584
}

media/materials/scripts/gazebo.material

+5-10
Original file line numberDiff line numberDiff line change
@@ -1533,23 +1533,22 @@ material Gazebo/WideLensMap
15331533
}
15341534

15351535

1536-
vertex_program Gazebo/CameraLesnFlareVS glsl
1536+
vertex_program Gazebo/CameraLensFlareVS glsl
15371537
{
15381538
source camera_lens_flare_vs.glsl
15391539
}
15401540

1541-
fragment_program Gazebo/CameraLesnFlareFS glsl
1541+
fragment_program Gazebo/CameraLensFlareFS glsl
15421542
{
15431543
source camera_lens_flare_fs.glsl
15441544

15451545
default_params
15461546
{
15471547
param_named RT int 0
1548-
param_named noiseRGBA int 1
1549-
param_named time float 0.0
15501548
param_named viewport float3 0.0 0.0 0.0
15511549
param_named lightPos float3 0 0 0
15521550
param_named scale float 1.0
1551+
param_named color float3 1.4 1.2 1.0
15531552
}
15541553
}
15551554

@@ -1559,19 +1558,15 @@ material Gazebo/CameraLensFlare
15591558
{
15601559
pass
15611560
{
1562-
vertex_program_ref Gazebo/CameraLesnFlareVS { }
1563-
fragment_program_ref Gazebo/CameraLesnFlareFS { }
1561+
vertex_program_ref Gazebo/CameraLensFlareVS { }
1562+
fragment_program_ref Gazebo/CameraLensFlareFS { }
15641563

15651564
texture_unit RT
15661565
{
15671566
tex_coord_set 0
15681567
tex_address_mode border
15691568
filtering linear linear linear
15701569
}
1571-
texture_unit noiseRGBA
1572-
{
1573-
texture noise_rgba.png
1574-
}
15751570
}
15761571
}
15771572
}

media/materials/textures/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ set (files
1919
heightmap_bowl.png
2020
heightmap_valley.png
2121
motorway.jpg
22-
noise_rgba.png
2322
paintedWall.jpg
2423
pioneerBody.jpg
2524
primary.jpg
-258 KB
Binary file not shown.

plugins/LensFlareSensorPlugin.cc

+32
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ namespace gazebo
3535

3636
/// \brief Lens flare scale
3737
public: double scale = 1.0;
38+
39+
/// \brief Lens flare color
40+
public: ignition::math::Vector3d color
41+
= ignition::math::Vector3d(1.4, 1.2, 1.0);
3842
};
3943
}
4044

@@ -75,6 +79,11 @@ void LensFlareSensorPlugin::Load(sensors::SensorPtr _sensor,
7579
gzerr << "Lens flare scale must be greater than 0" << std::endl;
7680
}
7781

82+
if (_sdf->HasElement("color"))
83+
{
84+
this->dataPtr->color = _sdf->Get<ignition::math::Vector3d>("color");
85+
}
86+
7887
sensors::CameraSensorPtr cameraSensor =
7988
std::dynamic_pointer_cast<sensors::CameraSensor>(_sensor);
8089

@@ -101,6 +110,28 @@ void LensFlareSensorPlugin::Load(sensors::SensorPtr _sensor,
101110
return;
102111
}
103112

113+
/////////////////////////////////////////////////
114+
void LensFlareSensorPlugin::SetScale(const double _scale)
115+
{
116+
this->dataPtr->scale = _scale;
117+
118+
for (auto flare : this->dataPtr->lensFlares)
119+
{
120+
flare->SetScale(_scale);
121+
}
122+
}
123+
124+
/////////////////////////////////////////////////
125+
void LensFlareSensorPlugin::SetColor(const ignition::math::Vector3d &_color)
126+
{
127+
this->dataPtr->color = _color;
128+
129+
for (auto flare : this->dataPtr->lensFlares)
130+
{
131+
flare->SetColor(_color);
132+
}
133+
}
134+
104135
/////////////////////////////////////////////////
105136
void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera)
106137
{
@@ -111,5 +142,6 @@ void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera)
111142
lensFlare.reset(new rendering::LensFlare);
112143
lensFlare->SetCamera(_camera);
113144
lensFlare->SetScale(this->dataPtr->scale);
145+
lensFlare->SetColor(this->dataPtr->color);
114146
this->dataPtr->lensFlares.push_back(lensFlare);
115147
}

plugins/LensFlareSensorPlugin.hh

+12-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ namespace gazebo
2828
/// \brief Plugin that adds lens flare effect to a camera or multicamera
2929
/// sensor
3030
/// The plugin has the following optional parameter:
31-
/// <scale> Scale of lens flare. Must be greater than 0
31+
/// <scale> Scale of lens flare. Must be greater than 0
32+
/// <color> Color of lens flare.
33+
/// \todo A potentially useful feature would be an option for constantly
34+
/// updating the flare color to match the light source color.
3235
class GZ_PLUGIN_VISIBLE LensFlareSensorPlugin : public SensorPlugin
3336
{
3437
/// \brief Constructor.
@@ -41,6 +44,14 @@ namespace gazebo
4144
public: virtual void Load(sensors::SensorPtr _sensor,
4245
sdf::ElementPtr _sdf);
4346

47+
/// \brief Set the scale of lens flare.
48+
/// \param[in] _scale Scale of lens flare
49+
public: void SetScale(const double _scale);
50+
51+
/// \brief Set the color of lens flare.
52+
/// \param[in] _color Color of lens flare
53+
public: void SetColor(const ignition::math::Vector3d &_color);
54+
4455
/// \brief Add lens flare effect to a camera
4556
/// \param[in] _camera Camera to add the lens flare effect to.
4657
private: void AddLensFlare(rendering::CameraPtr _camera);

0 commit comments

Comments
 (0)