reflecting around a vector


 The above image hopefully explains the line below marked as "the reflecting bit". In the code below, in Kodelife branded code, I'm assigning the mouse.x to define the angle value, multiplying it by PI to get a full range of rotation (not sure why it's not 2 PI here, will update when I figure it out)
Firstly, toward the bottom of the code, we're defining the distance from a line that goes from 0, to 0.5 on the x axis. We then use the smoothstep function on the distance variable to draw a thin line.

We actually rotate the line by reflecting it across a vector n, whose direction we define by the angle.

The dot product dot(uv,n) gives us the distance from the n line.
and the direction we want to move in is n. so multiplying n by the distance and by 2 will get us toward the line and across. 

a slight maths aside. dot(uv,n) *n is the actual projection of uv onto n. the above image is dealing with a normalised vector, where n would be 1 and isn't included in the equation. This is why we multiply by n in our code. by subtracting  dot(uv,n)*n , we are getting what would be the vertical component in the image above. We need two of these to get to the other side, thus we do it x2.

#ifdef GL_ES
precision highp float;
#endif

uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform vec3 spectrum;

uniform sampler2D texture0;
uniform sampler2D texture1;
uniform sampler2D texture2;
uniform sampler2D texture3;
uniform sampler2D prevFrame;
uniform sampler2D prevPass;

varying vec3 v_normal;
varying vec2 v_texcoord;

void main(void)
{
    vec2 uv = -1. + 2. * v_texcoord;
    vec3 col=vec3(0);
    
    
    float angle=mouse.x*3.1415926;
    vec2 n=vec2(sin(angle),cos(angle));
    float d=dot(uv,n);

//col+=d; //use this to visualise d, it might help you (me) understand
    uv-=n*min(0,d)*2.0;
    
 
    
    
    col.xy=uv.xy;
    
    //distance from line
    float b=length(uv-vec2(clamp(uv.x,0,0.5),0));
    
    col+=smoothstep(0.01,0,b);
    
    
    
    
    gl_FragColor = vec4(col,
        1.0);
}

 The vid below is what this code is from, mostly

https://www.youtube.com/watch?v=il_Qg9AqQkE&t=807s

Comments

Popular posts from this blog

setting VFX graph properties using C#

scripting custom render texture creation, assignment, shaders