Skip to content

Instantly share code, notes, and snippets.

@CedricGuillemet
Created July 17, 2020 09:20
Show Gist options
  • Save CedricGuillemet/7c4cc0b8e15be56c08e4112f1bd8c9d1 to your computer and use it in GitHub Desktop.
Save CedricGuillemet/7c4cc0b8e15be56c08e4112f1bd8c9d1 to your computer and use it in GitHub Desktop.
float distanceFromSurface = 0.0;
float step = 0.03;
vec3 previousPosition = vPosition;
float previousDistance = 0.;
for (int i = 0; i < 100; i++) {
vec3 currentPosition = vPosition + distanceFromSurface * rayDir;
float distance = sphereDistance(currentPosition, sphereCenter, 1.0);
if (distance < 0.) {
vec3 hitPosition = mix(previousPosition, currentPosition, previousDistance / (previousDistance - distance));
vec3 normal = (hitPosition - sphereCenter);
float dotLight = dot(normal, ligthDirection) * 0.5 + 0.5;
float noiseTexture = simplex3d(hitPosition * 4.0) * 0.5 + 0.5;
gl_FragColor = vec4(vec3(1.0, 0.5, 0.2) * dotLight * noiseTexture, 1.0);
return;
}
distanceFromSurface += step;
previousPosition = currentPosition;
previousDistance = distance;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment