sprite {
source: solid
size: 1920, 1080
hotspot: 0.5, 0.5
pos: 0, 0, 0
shader: fragment, shaders/Random.fsh
}
I was wondering if there is a way to create a random float in a shader that is different every time the shader is run.
Shaders provided in the demo
are copied from @EverthangForever Scene's
uniform vec3 iResolution; // viewport resolution (in pixels)
uniform float iTime; // shader playback time (in seconds)
uniform float iTimeDelta; // render time (in seconds)
uniform float iFrameRate; // shader frame rate
uniform int iFrame; // shader playback frame
uniform float iChannelTime[4]; // channel playback time (in seconds)
uniform vec3 iChannelResolution[4]; // channel resolution (in pixels)
uniform vec4 iMouse; // mouse pixel coords. xy: current (if MLB down), zw: click
uniform samplerXX iChannel0..3; // input channel. XX = 2D/Cube
uniform vec4 iDate; // (year, month, day, time in seconds)
uniform float iSampleRate; // sound sample rate (i.e., 44100)
iTime
iResolution
iChannel0..3
@WyldAnimal - thanks. That's one work-around. I may have another one coming as well.
void mainImage(out vec4 O, vec2 U)
{
// Calgon - Get a random number...
// Next 3 lines will generate an integer between 0 and 100 or thereabouts
vec4 myColor = texture(iChannel0, vec2 (0, 0));
float myNum = (myColor.r ) *31536000 + ((iTime * 6.)/8.);// count seconds
int myInt = int(myNum);
// We feed that to the clock and it gives a time of up to 1:40
int t = myInt;
ivec4 i = ivec4(0); // Convert everything to integers
// End of Calgon stuff
// Back to the clock display which will treat our random number as seconds
vec2 R = iResolution.xy;
U += U - R;
U /= R.y / 3.; // Global scaling with aspect ratio correction
O-=O; // Zero the pixel
// colon blink rate
float x = U.x - U.y * .2 - 2.8, // Slight skew to slant the digits
y = --U.y;
// changed to 60 = 1 second
i.w = int(iTime * 60.) % 100 // Replace with centiseconds
// Seconds (preceded by a colon)
Z(t % 60)
C
// Minutes (preceded by a colon)
Z(t % 60)
C
// Hours
Z(t)
// Smaller digits
x /= .6;
y /= .6;
R *= .6;
// Centiseconds
x -= 14.;
y += .53
Z(i.w)
// Day (preceded by a hyphen)
x -= .8;
y += 3.
// Z(i.z)
// H(1)
Z(t)
H(1)
// Month (preceded by a hyphen)
// Z((i.y + 1)) // Is it a bug in shadertoy that we have to add one?
// H(1)
Z((t)) // Is it a bug in shadertoy that we have to add one?
H(1)
// Year
// Z(i.x % 100)
// Z(i.x / 100)
Z(t % 100)
Z(t / 100)
}
/////////////////////////////////////////////////////////////////////////////////
void main ( void )
{
mainImage ( gl_FragColor, gl_FragCoord.xy );
}
If you Change the multiplier value you can get many more random times on the clock
You were using 100 as the multiplier
But if you change it to 525600
texture {
id: Torus
source: Textures/Surfaces - 512x512/Wood - 01.jpg
}
sprite {
source: Torus
size: 1920, 1080
hotspot: 0.5, 0.5
pos: 0, 0, 0
shader: fragment, Shaders/AieKick/Frozen SwampMod01.fsh
}
sprite {
size: 1920, 1080
pos: 0, 0, 0
source: Sweet2, 0 // combine 0
source: Torus2, 1 // combine 1
shader: fragment, Shaders/Combine-Exclude/combine2.fsh
blend: false
}
These all boil down to one of
1) Use a randomly selected image of some sort as your source of randomness
2) Use the least significant bits of the elapsed time as your source of randomness. The variability of a scene's startup times and timing jitter when running stop this being predictable.
uniform sampler2D texture0;...
uniform sampler2D texture1;
varying vec4 gl_TexCoord[];
highp vec4 zero = texture2D(texture0,vTexCoord.xy); // input zero 0
highp vec4 one = texture2D(texture1, vTexCoord.xy); // input one 1
highp vec4 zero = texture2D(texture0,vTexCoord.xy); // input zero 0
highp vec4 one = texture2D(texture1, vTexCoord.xy); // input one 1
... and further process that back into a format that a shadertoy shader would understand to be multiple textures that could then be used as normal.
2) Use the least significant bits of the elapsed time as your source of randomness. The variability of a scene's startup times and timing jitter when running stop this being predictable.
but of course this seems to have used up the one and only texture slot.
One random value to be used througout the run of the scene.
// Calgon - randomised start depending on time
float startSecs = iDate.w - iTime;
float randThing = sin(startSecs);
source: ImageA, 0 // Assign ImageA as the first source (Channel 0)
source: ImageB, 1 // Assign ImageB as the second source (Channel 1)
shader: fragment, TheShader.fsh
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
source: ImageA, 0 // Assign ImageA as the first source (Channel 0)
source: ImageB, 1 // Assign ImageB as the second source (Channel 1)
source: ImageC 2 // Assign ImageB as the second source (Channel 2)
source: ImageD, 3 // Assign ImageB as the second source (Channel 3)
shader: fragment, TheShader.fsh
uniform sampler2D iChannel0;
uniform sampler2D iChannel1;
uniform sampler2D iChannel2;
uniform sampler2D iChannel3;
Having first set up 100 shades of grey that could be picked at random. I was very pleased with the result but of course this seems to have used up the one and only texture slot.
vec4 myColor = texture(iChannel0, vec2 (0, 0));
uniform sampler2D texture0;
uniform sampler2D texture1;
varying vec4 gl_TexCoord[];
...
highp vec4 zero = texture2D(texture0,vTexCoord.xy); // input zero 0
highp vec4 one = texture2D(texture1, vTexCoord.xy); // input one 1
// random number
uniform sampler2D tex;
float rand(vec4 co) {
return fract(sin(dot(co.xyzw ,vec4(12.9898,78.233, 45.8438, 100.0)*fract(iTime))) * (43758.5453*fract(itime)));
}
vec4 effect(vec4 color, Image tex, vec2 tc, vec2 sc) {
vec4 textureColor = texture2D(tex, tc);
float randomValue = rand(textureColor);
return vec4(randomValue, randomValue, randomValue, 1.0);
}
#endif
// In this version, the rand function takes a vec4 as input, and uses the x, y, z, and w components (red, green, blue, and alpha) to generate a random number.
//The dot product is taken with a vec4 that includes the alpha channel as well.
//
// This way, the resulting random number will be different for each unique combination of red, green, blue, and alpha values in the texture,
//which means that the randomness will be influenced by the alpha channel as well.
Won't that change on every frame/pixel ?
uniform sampler2D texture0;
uniform sampler2D texture1;
varying vec4 gl_TexCoord[];
...
highp vec4 zero = texture2D(texture0,vTexCoord.xy); // input zero 0
highp vec4 one = texture2D(texture1, vTexCoord.xy); // input one 1
As the .scn and shader files are (I hope) read once when the scene is run, any output from the external program would have no effect on a running scene but would affect its next run.@Calgon maybe leave a space before writing your 's apostrophes.. avoids too many 's
outp.rgb = texture2D(texture1, vec2(0.0, 0.0)).rgb;should only return whatever texel is at 0,0 but instead it is behaving like vTexcoords.xy, even if
vec4 vTexCoord = gl_TexCoord[0];is commented out of the shader.
clipThe use of 2 texture directories for the ST01, ST02 was an attempt to fix the problem (in case it was fighting)
{
id: Clip
allow: pole
}
texture
{
id: Feedback
size: 1920, 1080
source: Empty/
//this is what causes the undocumented feature.
//normal behaviour is to grab a random texture from the directory but if the directory is empty
//it grabs the last sprite in previous frames camera node regardless of opacity or position.
}
texture
{
id: ST01
size: 1920, 1080
source: StartingTex/
}
texture
{
id: ST02
size: 1920, 1080
source: StartingTex2/
}
////////////////////////////////////////////////////////////////////////////////
framebuffer
{
id: StaticBuffer
size: 1920, 1080
pos: 960, 540
sprite
{
source: Feedback
}
}
framebuffer
{
id: RandomNumber
size: 1920, 1080
pos: 960, 540
sprite
{
source: ST01, 0
source: ST02, 1
source: StaticBuffer, 2
shader: Rnd.fsh
}
}
///////////////////////////////////
camera
{
type: 2D
size: 1920, 1080
pos: 960, 540
sprite
{
pos: 0,0
source: RandomNumber
}
sprite
{
opacity: 0.0
source: RandomNumber
}
}
#version 120
uniform sampler2D texture0; //random picture starting point 1
uniform sampler2D texture1; //random picture starting point 2
uniform sampler2D texture2; //static buffer
vec4 vTexCoord = gl_TexCoord[0];
vec3 outp;
void main(void)
{
//vec4 Pc1 = texture2D(texture0, vec2(0.0, 0.0));
//vec4 Rn = texture2D(texture2, vTexCoord.xy);
// if (Rn.r == 1.0 && Rn.g == 1.0 && Rn.b == 1.0 && Rn.a == 1.0)
// {
outp.rgb = texture2D(texture1, vec2(0.0, 0.0)).rgb;
// }
gl_FragColor = vec4(outp.rgb , 1.0);
}
clip
{
id: Clip
//deny: top
allow: pole
}
texture
{
id: Feedback //this is what causes the undocumented feature.
//normal behaviour is to grab a random texture from the directory but if the directory is empty
//it grabs the last sprite in last frames camera node
size: 1, 1
source: Empty/
}
texture
{
id: ST
source: StartingTex/
}
////////////////////////////////////////////////////////////////////////////////
framebuffer
{
id: StaticBuffer
size: 1, 1
sprite
{
source: Feedback
}
}
framebuffer
{
id: RandomNumber
size: 1, 1
sprite
{
source: ST, 0
source: StaticBuffer, 1
shader: Rnd.fsh
}
}
///////////////////////////////////
camera {
type: 2D
size: 1920, 1080
pos: 960, 540
sprite
{
pos: 0,0
scale: 1000,1000
source: RandomNumber
}
sprite
{
opacity:0.0
source: RandomNumber
}
}
#version 120
uniform sampler2D texture0; //random picture starting point 1
uniform sampler2D texture1; //random picture starting point 2
vec4 vTexCoord = gl_TexCoord[0];
uniform float u_Elapsed;
vec3 rgn (vec2 c)
{
vec4 Pc2 = texture2D(texture0, vec2(c.r,c.g));
return Pc2.rgb;
}
void main(void)
{
vec4 Pc1 = texture2D(texture0, vTexCoord.xy);
vec4 Rn = texture2D(texture1, vTexCoord.xy);
if (Rn.r == 1.0 && Rn.g == 1.0 && Rn.b == 1.0 && Rn.a == 1.0)
{
Rn.rgb = rgn(Pc1.rg);
}
gl_FragColor = vec4(Rn.rgb , 1.0);
}
Come utente gratuito iStripper, non ti è consentito rispondere a un argomento nel forum o creare un nuovo argomento
Ma potrai accedere alle categorie e ai concetti di base ed entrare in contatto con la nostra comunità.!