Created
February 17, 2026 18:03
-
-
Save patriciogonzalezvivo/12f28dbe268eccbc3a657630f52b8376 to your computer and use it in GitHub Desktop.
Debug RayMarching Scenes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "frag": "// Copyright Patricio Gonzalez Vivo, 2021 - http://patriciogonzalezvivo.com/\n\n#ifdef GL_ES\nprecision mediump float;\n#endif\n\nuniform vec3 u_camera;\nuniform vec3 u_light;\nuniform vec3 u_lightColor;\n\nuniform vec2 u_resolution;\nuniform vec2 u_mouse;\n\n\n// SPACE\n#define LOOK_AT_RIGHT_HANDED\n#define LIGHT_DIRECTION u_light\n#define RESOLUTION u_resolution\n#define LIGHT_COLOR vec3(0.95, 0.65, 0.5)\n#define RAYMARCH_AMBIENT vec3(0.7, 0.9, 1.0)\n#define RAYMARCH_BACKGROUND (RAYMARCH_AMBIENT + rayDirection.y * 0.8)\n#define DEBUG_FLIPPED_SPACE\n\n#define PYRAMID_POS vec3(2.0, 0.10, 2.0)\n#define CONE_POS vec3(0.0, 0.75, -2.0)\n\n#include \"lygia/space/ratio.glsl\"\n#include \"lygia/draw/matrix.glsl\"\n#include \"lygia/draw/point.glsl\"\n#include \"lygia/draw/axis.glsl\"\n#include \"lygia/space/perspective.glsl\"\n#include \"lygia/math/inverse.glsl\"\n#include \"lygia/math/transpose.glsl\"\n#include \"lygia/sdf.glsl\"\n#include \"lygia/lighting/raymarch.glsl\"\n#include \"lygia/color/space/linear2gamma.glsl\"\n\nfloat checkBoard(vec2 uv, vec2 _scale) {\n uv = floor(fract(uv * _scale) * 2.0);\n return saturate(min(1.0, uv.x + uv.y) - (uv.x * uv.y));\n}\n\nMaterial raymarchMap( in vec3 pos ) {\n float check = 0.5 + checkBoard(pos.xz, vec2(1.0, 1.0)) * 0.5;\n Material res = materialNew(vec3(check), 0.0, 0.5, planeSDF(pos));\n\n res = opUnion( res, materialNew( vec3(1.0, 1.0, 1.0), 1.0, 0.0, sphereSDF( pos-vec3( 0.0, 0.60, 0.0), 0.5 ) ) );\n res = opUnion( res, materialNew( vec3(0.0, 1.0, 1.0), boxSDF( pos-vec3( 2.0, 0.5, 0.0), vec3(0.4, 0.4, 0.4) ) ) );\n res = opUnion( res, materialNew( vec3(0.3, 0.3, 1.0), torusSDF( pos-vec3( 0.0, 0.5, 2.0), vec2(0.4,0.1) ) ) );\n res = opUnion( res, materialNew( vec3(0.3, 0.1, 0.3), capsuleSDF( pos,vec3(-2.3, 0.4,-0.2), vec3(-1.6,0.75,0.2), 0.2 ) ) );\n res = opUnion( res, materialNew( vec3(0.5, 0.3, 0.4), triPrismSDF( pos-vec3(-2.0, 0.50,-2.0), vec2(0.5,0.1) ) ) );\n res = opUnion( res, materialNew( vec3(0.2, 0.2, 0.8), cylinderSDF( pos-vec3( 2.0, 0.50,-2.0), vec2(0.2,0.4) ) ) );\n res = opUnion( res, materialNew( vec3(0.7, 0.5, 0.2), coneSDF( pos-CONE_POS, vec3(0.8,0.6,0.6) ) ) );\n res = opUnion( res, materialNew( vec3(0.4, 0.2, 0.9), hexPrismSDF( pos-vec3(-2.0, 0.60, 2.0), vec2(0.5,0.1) ) ) );\n res = opUnion( res, materialNew( vec3(0.1, 0.3, 0.6), pyramidSDF( pos-PYRAMID_POS, 1.0 ) ) );;\n\n return res;\n}\n\nvoid main() {\n vec3 color = vec3(0.0);\n vec2 pixel = 1.0/u_resolution;\n vec2 st = gl_FragCoord.xy * pixel;\n vec2 uv = ratio(st, u_resolution);\n vec2 mouse = ratio(u_mouse * pixel, u_resolution);\n\n mat4 viewMatrix = lookAtView(u_camera, vec3(0.0));\n\n color = raymarch(viewMatrix, uv).rgb;\n color = linear2gamma(color);\n\n vec4 debug = vec4(0.0);\n\n // matrix\n debug += matrix(uv - vec2(0.26, .05), viewMatrix);\n\n // 3d points\n mat4 M = perspective(1.05, 1.0, -1.0, 1.0) * inverse(viewMatrix);\n debug += point(uv, M, PYRAMID_POS + vec3(0.0, 1.0, 0.0));\n debug += point(uv, M, CONE_POS);\n debug += axis(uv, M, vec3(0.0), max(pixel.x, pixel.y));\n\n // 2d point\n debug += point(uv, mouse);\n\n color.rgb = mix(saturate(color.rgb), debug.rgb, debug.a);\n\n gl_FragColor = vec4( color, 1.0 );\n}", | |
| "vert": "#ifdef GL_ES\nprecision mediump float;\n#endif\n\nattribute vec4 a_position;\nvarying vec4 v_position;\n\nvoid main() {\n v_position = a_position;\n gl_Position = a_position;\n}\n", | |
| "commands": [ | |
| "plot,off", | |
| "textures,off", | |
| "buffers,off", | |
| "cubemap,off", | |
| "camera_position,2.16416,-2.40479,-4.13991", | |
| "camera_look_at,-3.28701,2.33831,-1.60988", | |
| "camera,default" | |
| ], | |
| "assets": {}, | |
| "history": [ | |
| { | |
| "gistId": "28034c785f4ec72f8cae432597cd1ba5", | |
| "owner": { | |
| "login": "patriciogonzalezvivo", | |
| "id": 346914, | |
| "avatar_url": "https://avatars.githubusercontent.com/u/346914?v=4" | |
| }, | |
| "loadedAt": "2026-02-17T18:02:47.414Z" | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment