Encountering a bug with my shaders while working on the vertex:
varying vec3 worldPosition;
varying vec3 viewDirection;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
worldPosition = vec3(modelMatrix * vec4(position, 1.0));
viewDirection = normalize(worldPosition - cameraPosition);
}
And for the fragment:
uniform float time;
varying vec3 worldPosition;
varying vec3 viewDirection;
/// utilities
bool sphereHit (vec3 p)
{
return distance(p,vec3(0,0,0)) < 1.0;
}
#define STEP_SIZE 0.01
bool raymarchHit (vec3 in_position, vec3 direction)
{
for (int i = 0; i < 2000; i++)
{
if ( sphereHit(in_position) )
return true;
in_position += direction * STEP_SIZE;
}
return false;
}
void main() {
if(raymarchHit(worldPosition, viewDirection)){
gl_FragColor = vec4(1.0,0.0,0.0,1.0);
}else{
gl_FragColor = vec4(0.0,0.0,1.0,0.5);
}
}
Trying to implement a raymarching shader by attaching it to a rotating cube in ThreeJS. However, the sphere/circle created appears to wobble along with the cube's rotation, rather than remaining fixed and camera-facing as intended. The volume containing the mesh should not affect this, but I am unable to identify the root cause of the issue.