Posts

Showing posts with the label smoothstep

GLSL plotting a line, 2 ways.

Firstly, to plot a "function"..  float plot(vec2 uv, float pct){     return step(pct-0.01,uv.y)-         step(pct+0.01,uv.y); } void main(void) {     vec2 uv = v_texcoord;     vec3 col=vec3(0);          float y = smoothstep(0.,1.,uv.x);     y=(sin(uv.x*6.28)+1)*0.5;     float pct=plot(uv,y);     col+=pct*vec3(0.0,1.0,0.0);          gl_FragColor = vec4(col,         1.0); }   In the main function, I've made a float value "y", which I intialise with a boring smoothstep function between the values 0 and 1. However we're not plotting that. I immediately overwrite y with what is essentially a sin(x) function. The numbers added and multiplied are just there to fit one cycle into the UV space of 0-1. (6.28 is a bad approximation of 2*PI - ie 360 degrees, +1 is to shift...

random number generation, noise and fbm, glsl

I should really change the name of this blog to Shader Shit.... anyway.. I'll get back to Unity specific crap one day. The following code is all for GLSL & will need adapting for use in a Unity fragment shader. I like to use Kodelife to test out my shader ideas. Look it up & give it some support if you like it! This post is about fake randomness, learnt from The Book of Shaders & The Art of Coding, on the website & youtube channel respectively. Within your main GLSL function, you might need to create a random value, often just a float value (remember you can make a vector from multiple floats). A great thing to feed such a random number generating function is your UV value. Here's an example- float random(vec2 uv){ return fract(sin(dot(uv,vec2(52.2957001,2355.957250024))*574.9829619)); } What we're doing here is getting the dot product of our UV coordinate, with an arbitrary vector that consists of large numbers with plenty of "complex" fractiona l...