Ray-Sphere Intersection camera direction

Hi, I’m currently working on an atmosphere shader and I’m encountering difficulties with the Ray-Sphere Intersection. My initial goal is to ensure the Ray-Sphere Intersection is functioning correctly. I am following a tutorial video that demonstrates the implementation, which can be found here.

For the calculation of the camera direction, I am using the camera position as the ray origin and the camera direction as the ray direction. However, I suspect that there might be an error in the way I’m calculating the ray direction.

Here’s the vertex shader code snippet I’m working with:

const vertexShader = `
varying vec4 modelViewPosition;
varying vec3 vCameraDirection;

  void main() {
    // Standard vertex transformation
     modelViewPosition = modelViewMatrix * vec4(position, 1.0);
    vec4 cameraPosition = modelViewMatrix * vec4(cameraPosition, 1.0);
    vCameraDirection = normalize(cameraPosition.xyz - modelViewPosition.xyz);

    
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
  }
`;

const fragmentShader = `
  varying vec3 vCameraDirection;


vec2 raySphereIntersect(vec3 ro, vec3 rd, vec3 s, float sr) {
  float t = dot(s-ro,rd);
  vec3 P = ro + rd*t;
  float y = length(s-P);

  if (y>sr){
  return vec2(-1.0);
  }

  float x = sqrt(sr*sr-y*y);
  float t1 = max(t-x,0.0);
  float t2 = t+x;
return vec2(t1,t2);
}


  void main() {
    vec3 rayOrigin = cameraPosition;
    vec3 rayDirection = vCameraDirection;
    vec3 SphereOrigin = vec3(0,0,0);
    float radius = 1.0;
    vec2 rsi = raySphereIntersect(rayOrigin,rayDirection,SphereOrigin,radius);
    
    float intensity = 0.8;
    float alpha = clamp((rsi.y-rsi.x)*intensity,0.0,1.0);
    gl_FragColor = vec4(1.0,1.0,1.0,alpha); 

  }
`;

I would greatly appreciate your assistance in figuring out the correct way to calculate the ray direction for the Ray-Sphere Intersection in my shader

Thank you in advance for your help!