Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // raymarch2.bonzo
- #define tt iTime
- #define res iResolution
- const int MAX_MARCHING_STEPS = 255;
- const float MIN_DIST = 0.0;
- const float MAX_DIST = 100.0;
- const float EPSILON = 0.0001;
- float stillNoise (vec2 uv) {
- return sin(dot(uv.xy,vec2(130.9898,500.233)));}
- /* Signed distance function for a sphere centered at "position" */
- float sphereSDF(vec3 samplePoint, float radius, vec3 position) {
- samplePoint = vec3(samplePoint.x+position.x, samplePoint.y+position.y, samplePoint.z+position.z);
- return length(samplePoint) - radius * stillNoise(samplePoint.xy);
- }
- /*
- * Signed distance function describing the scene.
- *
- * Absolute value of the return value indicates the distance to the surface.
- * Sign indicates whether the point is inside or outside the surface,
- * negative indicating inside.
- */
- float sceneSDF(vec3 samplePoint) {
- return sphereSDF(samplePoint, .5, vec3(sin(tt),0.,0.));
- }
- /*
- * Return the shortest distance from the camera position to the scene surface along
- * the marching direction. If no part of the surface is found between start and end,
- * return end.
- *
- * cam: the camera position, acting as the origin of the ray
- * marchingDirection: the normalized direction to march in
- * minDepth: the starting distance away from the camera
- * maxDepth: the max distance away from the camera to march before giving up
- */
- float shortestDistanceToSurface(vec3 cam, vec3 marchingDirection, float minDepth, float maxDepth) {
- float depth = minDepth;
- for (int i = 0; i < MAX_MARCHING_STEPS; i++) {
- float dist = sceneSDF(cam + depth * marchingDirection);
- if (dist < EPSILON) {
- return depth;
- }
- depth += dist;
- if (depth >= maxDepth) {
- return maxDepth;
- }
- }
- return maxDepth;
- }
- /*
- * Return the normalized direction to march in from the cam point for a single pixel.
- *
- * fieldOfView: vertical field of view in degrees
- * size: resolution of the output image
- * fragCoord: the x,y coordinate of the pixel in the output image
- */
- vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) {
- vec2 xy = fragCoord - size / 2.0;
- float z = size.y / tan(radians(fieldOfView) / 2.0);
- return normalize(vec3(xy, -z));
- }
- void mainImage( out vec4 fragColor, in vec2 fragCoord )
- {
- vec3 dir = rayDirection(45.0, res.xy, fragCoord);
- vec3 cam = vec3(0., 0., 5.);
- float dist = shortestDistanceToSurface(cam, dir, MIN_DIST, MAX_DIST);
- if (dist > MAX_DIST - EPSILON) {
- // Didn't hit anything >> black
- fragColor = vec4(0., 0., 0., 0.);
- return;
- }
- fragColor = vec4(1., 1., 1., 1.);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement