diff --git a/gazebo/rendering/LensFlare.cc b/gazebo/rendering/LensFlare.cc index fe723faacf..606ae1d11d 100644 --- a/gazebo/rendering/LensFlare.cc +++ b/gazebo/rendering/LensFlare.cc @@ -35,8 +35,7 @@ namespace gazebo namespace rendering { /// \brief We'll create an instance of this class for each camera, to be - /// used to inject lens flare uniforms and time (for animating flare) - /// in each render call. + /// used to inject lens flare uniforms in each render call. class LensFlareCompositorListener : public Ogre::CompositorInstance::Listener { @@ -71,6 +70,13 @@ namespace gazebo this->scale = _scale; } + /// \brief Set the color of lens flare. + /// \param[in] _color Color of lens flare + public: void SetColor(const ignition::math::Vector3d &_color) + { + this->color = _color; + } + /// \brief Callback that OGRE will invoke for us on each render call /// \param[in] _passID OGRE material pass ID. /// \param[in] _mat Pointer to OGRE material. @@ -96,9 +102,6 @@ namespace gazebo pass->getFragmentProgramParameters(); GZ_ASSERT(!params.isNull(), "Null OGRE material GPU parameters"); - // used for animating flare - params->setNamedConstant("time", static_cast( - common::Time::GetWallTime().Double())); // for adjusting aspect ratio of flare params->setNamedConstant("viewport", Ogre::Vector3(static_cast(this->camera->ViewportWidth()), @@ -132,6 +135,8 @@ namespace gazebo params->setNamedConstant("lightPos", Conversions::Convert(pos)); params->setNamedConstant("scale", static_cast(lensFlareScale)); + params->setNamedConstant("color", + Ogre::Vector3(this->color.X(), this->color.Y(), this->color.Z())); } /// \brief Get the lens flare position and scale for a normal camera @@ -337,6 +342,10 @@ namespace gazebo /// \brief Scale of lens flare. private: double scale = 1.0; + + /// \brief Color of lens flare. + private: ignition::math::Vector3d color + = ignition::math::Vector3d(1.0, 1.0, 1.0); }; /// \brief Private data class for LensFlare @@ -372,6 +381,10 @@ namespace gazebo /// \brief Scale of lens flare. public: double lensFlareScale = 1.0; + + /// \brief Color of lens flare. + public: ignition::math::Vector3d lensFlareColor + = ignition::math::Vector3d(1.0, 1.0, 1.0); }; } } @@ -414,6 +427,8 @@ void LensFlare::SetCamera(CameraPtr _camera) LensFlareCompositorListener(this->dataPtr->camera, nullptr)); this->dataPtr->lensFlareCompositorListener->SetScale( this->dataPtr->lensFlareScale); + this->dataPtr->lensFlareCompositorListener->SetColor( + this->dataPtr->lensFlareColor); this->dataPtr->lensFlareInstance = Ogre::CompositorManager::getSingleton().addCompositor( @@ -441,6 +456,19 @@ void LensFlare::SetScale(const double _scale) } } +////////////////////////////////////////////////// +void LensFlare::SetColor(const ignition::math::Vector3d &_color) +{ + // lensFlareColor is intentionally not clamped so the user can work in + // HDR color spaces or be artistic. + this->dataPtr->lensFlareColor = _color; + if (this->dataPtr->lensFlareCompositorListener) + { + this->dataPtr->lensFlareCompositorListener->SetColor( + this->dataPtr->lensFlareColor); + } +} + ////////////////////////////////////////////////// void LensFlare::Update() { diff --git a/gazebo/rendering/LensFlare.hh b/gazebo/rendering/LensFlare.hh index 7f3e0af4fd..773e4a6baa 100644 --- a/gazebo/rendering/LensFlare.hh +++ b/gazebo/rendering/LensFlare.hh @@ -58,6 +58,10 @@ namespace gazebo /// \param[in] _scale Scale of lens flare public: void SetScale(const double _scale); + /// \brief Set the color of lens flare. + /// \param[in] _color Color of lens flare + public: void SetColor(const ignition::math::Vector3d &_color); + /// \brief Update function to search light source private: void Update(); diff --git a/media/materials/programs/camera_lens_flare_fs.glsl b/media/materials/programs/camera_lens_flare_fs.glsl index ef4f967c52..c0ec04479f 100644 --- a/media/materials/programs/camera_lens_flare_fs.glsl +++ b/media/materials/programs/camera_lens_flare_fs.glsl @@ -1,8 +1,6 @@ // The input texture, which is set up by the Ogre Compositor infrastructure. uniform sampler2D RT; -uniform sampler2D noiseRGBA; -uniform float time; uniform vec3 viewport; // light pos in clip space @@ -11,16 +9,8 @@ uniform vec3 lightPos; // lens flare scale uniform float scale; -float noise(float t) -{ - // 256 is the size of noiseRGBA texture - return texture2D(noiseRGBA, vec2(t, 0.0) / vec2(256.0)).x; -} - -float noise(vec2 t) -{ - return texture2D(noiseRGBA,(t + vec2(time)) / vec2(256.0)).x; -} +// lens flare color +uniform vec3 color; vec3 lensflare(vec2 uv,vec2 pos) { @@ -30,12 +20,9 @@ vec3 lensflare(vec2 uv,vec2 pos) float ang = atan(main.y, main.x); float dist = length(main); dist = pow(dist,.1); - float n = noise(vec2((ang-time/9.0)*16.0,dist*32.0)); float f0 = 1.0/(length(uv-pos)*16.0/scale+1.0); - f0 = f0+f0*(sin((ang+time/18.0 + noise(abs(ang)+n/2.0)*2.0)*12.0)*.1+dist*.1+.8); - float f2 = max(1.0/(1.0+32.0*pow(length(uvd+0.8*pos),2.0)),.0)*00.25; float f22 = max(1.0/(1.0+32.0*pow(length(uvd+0.85*pos),2.0)),.0)*00.23; 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) return c; } -// color modifier -vec3 cc(vec3 color, float factor, float factor2) -{ - float w = color.x+color.y+color.z; - return mix(color,vec3(w)*factor,w*factor2); -} - void main() { // return if light is behind the view @@ -97,9 +77,8 @@ void main() pos.x *= aspect; // compute lens flare - vec3 color = vec3(1.4,1.2,1.0)*lensflare(uv, pos.xy); - color = cc(color,.5,.1); + vec3 finalColor = color * lensflare(uv, pos.xy); // apply lens flare - gl_FragColor = texture2D(RT, gl_TexCoord[0].xy) + vec4(color, 1.0); + gl_FragColor = texture2D(RT, gl_TexCoord[0].xy) + vec4(finalColor, 1.0); } diff --git a/media/materials/scripts/gazebo.material b/media/materials/scripts/gazebo.material index a9344651be..2a7b54824a 100644 --- a/media/materials/scripts/gazebo.material +++ b/media/materials/scripts/gazebo.material @@ -1545,23 +1545,22 @@ material Gazebo/WideLensMap } -vertex_program Gazebo/CameraLesnFlareVS glsl +vertex_program Gazebo/CameraLensFlareVS glsl { source camera_lens_flare_vs.glsl } -fragment_program Gazebo/CameraLesnFlareFS glsl +fragment_program Gazebo/CameraLensFlareFS glsl { source camera_lens_flare_fs.glsl default_params { param_named RT int 0 - param_named noiseRGBA int 1 - param_named time float 0.0 param_named viewport float3 0.0 0.0 0.0 param_named lightPos float3 0 0 0 param_named scale float 1.0 + param_named color float3 1.4 1.2 1.0 } } @@ -1571,8 +1570,8 @@ material Gazebo/CameraLensFlare { pass { - vertex_program_ref Gazebo/CameraLesnFlareVS { } - fragment_program_ref Gazebo/CameraLesnFlareFS { } + vertex_program_ref Gazebo/CameraLensFlareVS { } + fragment_program_ref Gazebo/CameraLensFlareFS { } texture_unit RT { @@ -1580,10 +1579,6 @@ material Gazebo/CameraLensFlare tex_address_mode border filtering linear linear linear } - texture_unit noiseRGBA - { - texture noise_rgba.png - } } } } diff --git a/media/materials/textures/CMakeLists.txt b/media/materials/textures/CMakeLists.txt index e5584a0d3f..0932353028 100644 --- a/media/materials/textures/CMakeLists.txt +++ b/media/materials/textures/CMakeLists.txt @@ -18,7 +18,6 @@ set (files hardwood_floor.jpg heightmap_bowl.png motorway.jpg - noise_rgba.png paintedWall.jpg pioneerBody.jpg primary.jpg diff --git a/media/materials/textures/noise_rgba.png b/media/materials/textures/noise_rgba.png deleted file mode 100644 index 32a561c7f6..0000000000 Binary files a/media/materials/textures/noise_rgba.png and /dev/null differ diff --git a/plugins/LensFlareSensorPlugin.cc b/plugins/LensFlareSensorPlugin.cc index d90a2bfb17..96a166917b 100644 --- a/plugins/LensFlareSensorPlugin.cc +++ b/plugins/LensFlareSensorPlugin.cc @@ -35,6 +35,10 @@ namespace gazebo /// \brief Lens flare scale public: double scale = 1.0; + + /// \brief Lens flare color + public: ignition::math::Vector3d color + = ignition::math::Vector3d(1.4, 1.2, 1.0); }; } @@ -75,6 +79,11 @@ void LensFlareSensorPlugin::Load(sensors::SensorPtr _sensor, gzerr << "Lens flare scale must be greater than 0" << std::endl; } + if (_sdf->HasElement("color")) + { + this->dataPtr->color = _sdf->Get("color"); + } + sensors::CameraSensorPtr cameraSensor = std::dynamic_pointer_cast(_sensor); @@ -101,6 +110,28 @@ void LensFlareSensorPlugin::Load(sensors::SensorPtr _sensor, return; } +///////////////////////////////////////////////// +void LensFlareSensorPlugin::SetScale(const double _scale) +{ + this->dataPtr->scale = _scale; + + for (auto flare : this->dataPtr->lensFlares) + { + flare->SetScale(_scale); + } +} + +///////////////////////////////////////////////// +void LensFlareSensorPlugin::SetColor(const ignition::math::Vector3d &_color) +{ + this->dataPtr->color = _color; + + for (auto flare : this->dataPtr->lensFlares) + { + flare->SetColor(_color); + } +} + ///////////////////////////////////////////////// void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera) { @@ -111,5 +142,6 @@ void LensFlareSensorPlugin::AddLensFlare(rendering::CameraPtr _camera) lensFlare.reset(new rendering::LensFlare); lensFlare->SetCamera(_camera); lensFlare->SetScale(this->dataPtr->scale); + lensFlare->SetColor(this->dataPtr->color); this->dataPtr->lensFlares.push_back(lensFlare); } diff --git a/plugins/LensFlareSensorPlugin.hh b/plugins/LensFlareSensorPlugin.hh index ceb6de8af5..dd9b46f17a 100644 --- a/plugins/LensFlareSensorPlugin.hh +++ b/plugins/LensFlareSensorPlugin.hh @@ -28,7 +28,10 @@ namespace gazebo /// \brief Plugin that adds lens flare effect to a camera or multicamera /// sensor /// The plugin has the following optional parameter: - /// Scale of lens flare. Must be greater than 0 + /// Scale of lens flare. Must be greater than 0 + /// Color of lens flare. + /// \todo A potentially useful feature would be an option for constantly + /// updating the flare color to match the light source color. class GAZEBO_VISIBLE LensFlareSensorPlugin : public SensorPlugin { /// \brief Constructor. @@ -41,6 +44,14 @@ namespace gazebo public: virtual void Load(sensors::SensorPtr _sensor, sdf::ElementPtr _sdf); + /// \brief Set the scale of lens flare. + /// \param[in] _scale Scale of lens flare + public: void SetScale(const double _scale); + + /// \brief Set the color of lens flare. + /// \param[in] _color Color of lens flare + public: void SetColor(const ignition::math::Vector3d &_color); + /// \brief Add lens flare effect to a camera /// \param[in] _camera Camera to add the lens flare effect to. private: void AddLensFlare(rendering::CameraPtr _camera);