Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix particle effect randomness #388

Merged
merged 5 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ogre2/src/Ogre2ParticleNoiseListener.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include <string>

#include <ignition/math/Rand.hh>

#include "ignition/rendering/ogre2/Ogre2Includes.hh"
#include "ignition/rendering/ogre2/Ogre2RenderTypes.hh"
#include "ignition/rendering/ogre2/Ogre2Scene.hh"
Expand Down Expand Up @@ -85,6 +87,8 @@ void Ogre2ParticleNoiseListener::preRenderTargetUpdate(
pass->getFragmentProgramParameters();
psParams->setNamedConstant("particleStddev",
static_cast<float>(particleStddev));
psParams->setNamedConstant("rnd",
static_cast<float>(ignition::math::Rand::DblUniform(0.0, 1.0)));

// get particle scatter ratio value from particle emitter user data
// and pass that to the shaders
Expand Down
9 changes: 5 additions & 4 deletions ogre2/src/media/materials/programs/depth_camera_fs.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ uniform vec3 backgroundColor;

uniform float particleStddev;
uniform float particleScatterRatio;
uniform float time;
// rnd is a random number in the range of [0-1]
uniform float rnd;

float packFloat(vec4 color)
{
Expand Down Expand Up @@ -106,7 +107,7 @@ void main()
if (particle.x > 0 && particleDepth < fDepth)
{
// apply scatter effect so that only some of the smoke pixels are visible
float r = rand(inPs.uv0 + vec2(time, time));
float r = rand(inPs.uv0 + vec2(rnd, rnd));
if (r < particleScatterRatio)
{
// set point to 3d pos of particle pixel
Expand All @@ -115,14 +116,14 @@ void main()
vec3 particlePoint = vec3(-particleViewSpacePos.z, -particleViewSpacePos.x,
particleViewSpacePos.y);

float rr = rand(inPs.uv0 + vec2(1.0/time, time)) - 0.5;
float rr = rand(inPs.uv0 + vec2(rnd, rnd)) - 0.5;

// apply gaussian noise to particle point cloud data
// With large particles, the range returned are all from the first large
// particle. So add noise with some mean values so that all the points are
// shifted further out. This gives depth readings beyond the first few
// particles and avoid too many early returns
vec3 noise = gaussrand(inPs.uv0, vec3(time, time, time),
vec3 noise = gaussrand(inPs.uv0, vec3(rnd, rnd, rnd),
particleStddev, rr*rr*particleStddev*0.5).xyz;
float noiseLength = length(noise);
float particlePointLength = length(particlePoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ uniform float max;

uniform float particleStddev;
uniform float particleScatterRatio;
uniform float time;
// rnd is a random number in the range of [0-1]
uniform float rnd;

// see gaussian_noise_fs.glsl for documentation on the rand and gaussrand
// functions
Expand Down Expand Up @@ -92,20 +93,20 @@ void main()
if (particle.x > 0.0 && particleDepth < fDepth)
{
// apply scatter effect so that only some of the smoke pixels are visible
float r = rand(inPs.uv0 + vec2(time, time));
float r = rand(inPs.uv0 + vec2(rnd, rnd));
if (r < particleScatterRatio)
{
float pd = projectionParams.y / (particleDepth - projectionParams.x);
vec3 point = inPs.cameraDir * pd;

float rr = rand(inPs.uv0 + vec2(1.0/time, time)) - 0.5;
float rr = rand(inPs.uv0 + vec2(rnd, rnd)) - 0.5;

// apply gaussian noise to particle range data
// With large particles, the range returned are all from the first large
// particle. So add noise with some mean values so that all the points are
// shifted further out. This gives depth readings beyond the first few
// particles and avoid too many early returns
vec3 noise = gaussrand(inPs.uv0, vec3(time, time, time),
vec3 noise = gaussrand(inPs.uv0, vec3(rnd, rnd, rnd),
particleStddev, rr*rr*particleStddev*0.5).xyz;
float noiseLength = length(noise);

Expand Down