Skip to content

Commit 2690ca2

Browse files
committed
pass rand to shaders
Signed-off-by: Ian Chen <[email protected]>
1 parent 28c1320 commit 2690ca2

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

ogre2/src/Ogre2ParticleNoiseListener.cc

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
#include <string>
1919

20+
#include <ignition/math/Rand.hh>
21+
2022
#include "ignition/rendering/ogre2/Ogre2Includes.hh"
2123
#include "ignition/rendering/ogre2/Ogre2RenderTypes.hh"
2224
#include "ignition/rendering/ogre2/Ogre2Scene.hh"
@@ -85,6 +87,8 @@ void Ogre2ParticleNoiseListener::preRenderTargetUpdate(
8587
pass->getFragmentProgramParameters();
8688
psParams->setNamedConstant("particleStddev",
8789
static_cast<float>(particleStddev));
90+
psParams->setNamedConstant("rnd",
91+
static_cast<float>(ignition::math::Rand::DblUniform(0.0, 1.0)));
8892

8993
// get particle scatter ratio value from particle emitter user data
9094
// and pass that to the shaders

ogre2/src/media/materials/programs/depth_camera_fs.glsl

+5-7
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ uniform vec3 backgroundColor;
3939

4040
uniform float particleStddev;
4141
uniform float particleScatterRatio;
42-
uniform float time;
42+
// rnd is a random number in the range of [0-1]
43+
uniform float rnd;
4344

4445
float packFloat(vec4 color)
4546
{
@@ -105,11 +106,8 @@ void main()
105106
// by other objects in the camera view
106107
if (particle.x > 0 && particleDepth < fDepth)
107108
{
108-
// time is used to add randomness. Limit value to [0-1]
109-
float t = fract(time);
110-
111109
// apply scatter effect so that only some of the smoke pixels are visible
112-
float r = rand(inPs.uv0 + vec2(t, t));
110+
float r = rand(inPs.uv0 + vec2(rnd, rnd));
113111
if (r < particleScatterRatio)
114112
{
115113
// set point to 3d pos of particle pixel
@@ -118,14 +116,14 @@ void main()
118116
vec3 particlePoint = vec3(-particleViewSpacePos.z, -particleViewSpacePos.x,
119117
particleViewSpacePos.y);
120118

121-
float rr = rand(inPs.uv0 + vec2(t, t)) - 0.5;
119+
float rr = rand(inPs.uv0 + vec2(rnd, rnd)) - 0.5;
122120

123121
// apply gaussian noise to particle point cloud data
124122
// With large particles, the range returned are all from the first large
125123
// particle. So add noise with some mean values so that all the points are
126124
// shifted further out. This gives depth readings beyond the first few
127125
// particles and avoid too many early returns
128-
vec3 noise = gaussrand(inPs.uv0, vec3(t, t, t),
126+
vec3 noise = gaussrand(inPs.uv0, vec3(rnd, rnd, rnd),
129127
particleStddev, rr*rr*particleStddev*0.5).xyz;
130128
float noiseLength = length(noise);
131129
float particlePointLength = length(particlePoint);

ogre2/src/media/materials/programs/gpu_rays_1st_pass_fs.glsl

+5-7
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ uniform float max;
3838

3939
uniform float particleStddev;
4040
uniform float particleScatterRatio;
41-
uniform float time;
41+
// rnd is a random number in the range of [0-1]
42+
uniform float rnd;
4243

4344
// see gaussian_noise_fs.glsl for documentation on the rand and gaussrand
4445
// functions
@@ -91,24 +92,21 @@ void main()
9192
// check if need to apply scatter effect
9293
if (particle.x > 0.0 && particleDepth < fDepth)
9394
{
94-
// time is used to add randomness. Limit value to [0-1]
95-
float t = fract(time);
96-
9795
// apply scatter effect so that only some of the smoke pixels are visible
98-
float r = rand(inPs.uv0 + vec2(t, t));
96+
float r = rand(inPs.uv0 + vec2(rnd, rnd));
9997
if (r < particleScatterRatio)
10098
{
10199
float pd = projectionParams.y / (particleDepth - projectionParams.x);
102100
vec3 point = inPs.cameraDir * pd;
103101

104-
float rr = rand(inPs.uv0 + vec2(t, t)) - 0.5;
102+
float rr = rand(inPs.uv0 + vec2(rnd, rnd)) - 0.5;
105103

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

0 commit comments

Comments
 (0)