Posts

Showing posts with the label dot

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...

Dot Product

I've typed this example straight into blogspot so who knows what will happen if you paste it straight into your IDE? Here we're using the dot product to create a basic fresnel rim shader. We allow the user to specify the colour of the rim and also the falloff. Things of note - in the Input struct we have a vector viewDir, which is the normal that the user- or camera -has of the object. We want to calculate the DOT product of  this vector with the surface normal. Without going into the actual maths - the dot prodcut will return a value of 1 if the 2 vectors are aligned in the same direction. They return a value of 0 if the vectors are at 90degrees/Perpendicular. They return a value of -1 if they are in opposite directions. In the surface function we take the 1- dotproduct, so that we get a 0 value on the polygons that face us and a 1 value on the polygons that are towards the edges. We then raise this value to the power of _rimPower, to make the falloff sharper or smoother, then...