three.js / webGL / GLSL - utilizing random math

不羁的心 提交于 2019-12-21 06:05:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!