问题
I am using three.js to call a fragment shader - that shader specifies a color for my material, broken out into rgb - and I want to see if I can multiply those colors by a random value. This is the code I'm using so far:
gl_FragColor = vec4( color.r*.5, color.g, color.b, smoothstep( 8000.0, -8000.0, gl_FragCoord.z / gl_FragCoord.w ) ); //MH - creates colors by multiplying color values
and this is what I'd want to do if it were javascript, though obviously this doesn't work inside a GLSL shader script:
gl_FragColor = vec4( color.r*Math.random(), color.g, color.b, smoothstep( 8000.0, -8000.0, gl_FragCoord.z / gl_FragCoord.w ) ); //MH - creates colors by multiplying color values
回答1:
For psuedorandom values in a shader I highly recommend Ashima's WebGL noise library. It takes in a x/y/z value and spits out simplex noise. This means it's stable from frame to frame. If you need random values that change pass a time-modulated value into the noise function.
来源:https://stackoverflow.com/questions/10803176/three-js-webgl-glsl-utilizing-random-math