Created
February 6, 2026 14:16
-
-
Save patriciogonzalezvivo/bbdf24b2741f92fe45caa1e90b2e8bb7 to your computer and use it in GitHub Desktop.
glslViewer Shader: head
This file has been truncated, but you can view the full file.
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": "#version 100\n\n#ifdef GL_ES\nprecision mediump float;\n#endif\n\nuniform mat4 u_projectionMatrix;\n\nuniform vec3 u_camera;\nuniform float u_cameraNearClip;\nuniform float u_cameraFarClip;\n\nuniform vec3 u_light;\nuniform vec3 u_lightColor;\nuniform float u_lightFalloff;\nuniform float u_lightIntensity;\n\nuniform float u_iblLuminance;\n\nuniform samplerCube u_cubeMap;\nuniform vec3 u_SH[9];\n\n#ifdef LIGHT_SHADOWMAP\nuniform sampler2D u_lightShadowMap;\nuniform mat4 u_lightMatrix;\nvarying vec4 v_lightCoord;\n#endif\n\nuniform vec2 u_resolution;\nuniform float u_time;\n\nvarying vec4 v_position;\nvarying vec4 v_color;\nvarying vec3 v_normal;\n\n#ifdef MODEL_VERTEX_TEXCOORD\nvarying vec2 v_texcoord;\n#endif\n\n#ifdef MODEL_VERTEX_TANGENT\nvarying vec4 v_tangent;\nvarying mat3 v_tangentToWorld;\n#endif\n\n#define SURFACE_POSITION v_position\n#define CAMERA_POSITION u_camera\n#define IBL_LUMINANCE u_iblLuminance\n\n#define LIGHT_DIRECTION u_light\n#define LIGHT_COLOR u_lightColor\n#define LIGHT_FALLOFF u_lightFalloff\n#define LIGHT_INTENSITY u_lightIntensity\n#define LIGHT_COORD v_lightCoord\n\n#include \"lygia/lighting/material/new.glsl\"\n#include \"lygia/lighting/pbr.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 min(1.0, uv.x + uv.y) - (uv.x * uv.y);\n}\n\nvoid main(void) {\n vec4 color = vec4(0.0, 0.0, 0.0, 1.0);\n vec2 pixel = 1.0/u_resolution;\n vec2 st = gl_FragCoord.xy * pixel;\n \n#ifdef MODEL_PRIMITIVE_GSPLATS\n float A = -dot(v_texcoord, v_texcoord);\n \n // Stricter clipping for finer edges\n if (A < -4.0) discard;\n \n // Use smoother attenuation curve\n float gaussian = exp(A);\n \n // Add edge smoothing to reduce aliasing\n float edgeSmoothness = smoothstep(-4.0, -3.5, A);\n float B = gaussian * v_color.a * edgeSmoothness;\n \n color.rgb = v_color.rgb;\n color.a = B;\n\n#else\n Material material = materialNew();\n material.albedo.rgb = vec3(0.);\n \n #if defined(FLOOR) && defined(MODEL_VERTEX_TEXCOORD)\n material.albedo.rgb = vec3(0.5) + checkBoard(v_texcoord, vec2(18.0)) * 0.5;\n #endif\n\n color = pbr(material);\n color = linear2gamma(color);\n#endif\n\n gl_FragColor = color;\n}\n", | |
| "vert": "#version 100\n\n#ifdef GL_ES\nprecision mediump float;\n#endif\n\nuniform mat4 u_modelViewProjectionMatrix;\nuniform mat4 u_projectionMatrix;\nuniform mat4 u_modelMatrix;\nuniform mat4 u_viewMatrix;\nuniform mat3 u_normalMatrix;\nuniform vec2 u_resolution;\n\n#ifdef MODEL_PRIMITIVE_GSPLATS\nuniform sampler2D u_GsplatData;\nuniform vec2 u_GsplatDataResolution; // Must be passed: vec2(4096.0, height)\n\nuniform vec2 u_focal;\n\nattribute vec2 a_position;\nattribute float a_index;\n#else \n\nattribute vec4 a_position;\n#endif\n\nvarying vec4 v_position;\n\n#ifdef MODEL_VERTEX_COLOR\nattribute vec4 a_color;\nvarying vec4 v_color;\n#endif\n\n#ifdef MODEL_VERTEX_NORMAL\nattribute vec3 a_normal;\nvarying vec3 v_normal;\n#endif\n\n#ifdef MODEL_VERTEX_TEXCOORD\nattribute vec2 a_texcoord;\n#endif\nvarying vec2 v_texcoord;\n\n#ifdef MODEL_VERTEX_TANGENT\nattribute vec4 a_tangent;\nvarying vec4 v_tangent;\nvarying mat3 v_tangentToWorld;\n#endif\n\n#ifdef LIGHT_SHADOWMAP\nuniform mat4 u_lightMatrix;\nvarying vec4 v_lightCoord;\n#endif\n\n#include \"lygia/math/transpose.glsl\"\n#include \"lygia/math/toMat3.glsl\"\n\nvoid main(void) {\n\n#ifdef MODEL_PRIMITIVE_GSPLATS\n v_texcoord = a_position;\n v_color = vec4(v_texcoord.x, v_texcoord.y, 0.0, 1.0);\n\n float width = u_GsplatDataResolution.x;\n float height = u_GsplatDataResolution.y;\n\n float fIndex = a_index;\n float row = floor(fIndex / 1024.0);\n float colStart = mod(fIndex, 1024.0) * 4.0;\n \n float v = (row + 0.5) / height;\n \n // p1: pos.xyz (position)\n vec4 p1 = texture2D(u_GsplatData, vec2((colStart + 0.5) / width, v));\n v_position = vec4(p1.xyz, 1.0);\n \n // Transform position to camera space\n vec4 cam = u_viewMatrix * u_modelMatrix * v_position;\n vec4 pos2d = u_projectionMatrix * cam;\n \n // Frustum culling\n float clip = 1.2 * pos2d.w;\n if (pos2d.z < -pos2d.w || pos2d.z > pos2d.w || \n pos2d.x < -clip || pos2d.x > clip || \n pos2d.y < -clip || pos2d.y > clip) {\n gl_Position = vec4(0.0, 0.0, 2.0, 1.0);\n return;\n }\n \n // Covariance\n // p2: cov.xx, cov.xy, cov.xz, cov.yy\n // p3: cov.yz, cov.zz, 0, 0\n vec4 p2 = texture2D(u_GsplatData, vec2((colStart + 1.5) / width, v));\n vec4 p3 = texture2D(u_GsplatData, vec2((colStart + 2.5) / width, v));\n\n // p4: color.rgba\n vec4 p4 = texture2D(u_GsplatData, vec2((colStart + 3.5) / width, v));\n v_color = p4;\n \n // Construct covariance matrix\n mat3 Vrk = mat3(\n p2.x, p2.y, p2.z,\n p2.y, p2.w, p3.x,\n p2.z, p3.x, p3.y\n );\n \n // Compute 2D covariance\n mat3 J = mat3(\n u_focal.x / cam.z, 0.0, -(u_focal.x * cam.x) / (cam.z * cam.z),\n 0.0, u_focal.y / cam.z, -(u_focal.y * cam.y) / (cam.z * cam.z),\n 0.0, 0.0, 0.0\n );\n \n mat3 T = transpose(toMat3(u_viewMatrix * u_modelMatrix)) * J;\n mat3 cov2d = transpose(T) * Vrk * T;\n \n // Add low-pass filter (reduce value for finer splats)\n cov2d[0][0] += 0.1;\n cov2d[1][1] += 0.1;\n \n // Compute eigenvalues for ellipse\n float mid = (cov2d[0][0] + cov2d[1][1]) / 2.0;\n float radius = length(vec2((cov2d[0][0] - cov2d[1][1]) / 2.0, cov2d[0][1]));\n float lambda1 = mid + radius;\n float lambda2 = mid - radius;\n \n if (lambda2 < 0.0) {\n gl_Position = vec4(0.0, 0.0, 2.0, 1.0);\n return;\n }\n \n vec2 diagonalVector = normalize(vec2(cov2d[0][1], lambda1 - cov2d[0][0]));\n\n // Reduce scale for finer splat coverage\n float scale = 2.5;\n vec2 majorAxis = scale * min(sqrt(2.0 * lambda1), 1024.0) * diagonalVector;\n vec2 minorAxis = scale * min(sqrt(2.0 * lambda2), 1024.0) * vec2(diagonalVector.y, -diagonalVector.x);\n \n // Compute final position\n vec2 vCenter = vec2(pos2d) / pos2d.w;\n vec2 pixel = 1.0 / u_resolution;\n gl_Position = vec4(\n vCenter + \n a_position.x * majorAxis * pixel + \n a_position.y * minorAxis * pixel,\n pos2d.z / pos2d.w, 1.0\n );\n\n#else\n v_position = u_modelMatrix * a_position;\n v_texcoord = a_position.xy * 0.5 + 0.5;\n \n #ifdef MODEL_VERTEX_COLOR\n v_color = a_color;\n #endif\n \n #ifdef MODEL_VERTEX_NORMAL\n v_normal = vec4(u_modelMatrix * vec4(a_normal, 0.0) ).xyz;\n #endif\n \n #ifdef MODEL_VERTEX_TEXCOORD\n v_texcoord = a_texcoord;\n #endif\n \n #ifdef MODEL_VERTEX_TANGENT\n v_tangent = a_tangent;\n vec3 worldTangent = a_tangent.xyz;\n vec3 worldBiTangent = cross(v_normal, worldTangent);// * sign(a_tangent.w);\n v_tangentToWorld = mat3(normalize(worldTangent), normalize(worldBiTangent), normalize(v_normal));\n #endif\n \n gl_Position = u_projectionMatrix * u_viewMatrix * v_position;\n#endif\n\n#ifdef LIGHT_SHADOWMAP\n v_lightCoord = u_lightMatrix * v_position;\n#endif\n}\n", | |
| "assets": { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment