{"id":24557831,"url":"https://github.com/timclicks/glsl-shader-intro","last_synced_at":"2026-02-05T20:10:48.267Z","repository":{"id":237446786,"uuid":"615910436","full_name":"timClicks/glsl-shader-intro","owner":"timClicks","description":"An introduction to the OpenGL Shader Language (GLSL) and using it within a webpage with WebGL and the \u003ccanvas\u003e tag.","archived":false,"fork":false,"pushed_at":"2023-03-19T03:13:50.000Z","size":8,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2026-01-13T11:22:04.875Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"HTML","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/timClicks.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-03-19T03:08:22.000Z","updated_at":"2026-01-04T12:52:39.000Z","dependencies_parsed_at":"2024-05-02T00:33:13.883Z","dependency_job_id":"1311b88b-91a1-48bb-91c1-498205378bb6","html_url":"https://github.com/timClicks/glsl-shader-intro","commit_stats":null,"previous_names":["timclicks/glsl-shader-intro"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/timClicks/glsl-shader-intro","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fglsl-shader-intro","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fglsl-shader-intro/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fglsl-shader-intro/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fglsl-shader-intro/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/timClicks","download_url":"https://codeload.github.com/timClicks/glsl-shader-intro/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/timClicks%2Fglsl-shader-intro/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29132894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-05T19:36:52.185Z","status":"ssl_error","status_checked_at":"2026-02-05T19:35:40.941Z","response_time":65,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-23T05:29:38.907Z","updated_at":"2026-02-05T20:10:48.252Z","avatar_url":"https://github.com/timClicks.png","language":"HTML","funding_links":[],"categories":[],"sub_categories":[],"readme":"# glsl-intro-tutorial\n\n![](https://img.youtube.com/vi/sMkgXh7ThT4/maxresdefault.jpg)\n\n# About\n\nAn introduction to using the OpenGL Shading Language (GLSL) language\non the web via WebGL. \n\n# Usage\n\nYou should load the `glsl.html` file via a local web server.\nFor example, Python should work fine to start a server:\n\n```console\n$ python3 -m http.server\n```\n\nNow open a browser at \u003chttp://localhost:8000/glsl.html\u003e.\n\n\n# Discussion\n\n\nI've always wanted to create a \"shader\" -- a graphics program that works really fast (it runs on your GPU) -- but I've always been put off because they seemed really difficult.\n\nShaders come in two forms: fragment shaders and vertex shaders. Fragment shaders are what we care about here, because they're more useful for drawing. A fragment shader is a type of shader that processes individual fragments (pixels) on the screen. It is responsible for determining the color of each pixel in the final rendered image. GLSL is a C-like language used for writing shaders in OpenGL.\n\nAll of this complexity has kept me away for a long time. Today I managed to put that fear aside and create this - a fun dynamic gradient background:\n\n\n## Running the shader yourself..\n\n### .. in ShaderToy\n\nShaderToy is an interactive web playground for running shaders.\n\n```glsl\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord)\n{\n    // Normalized pixel coordinates (from 0 to 1)\n    vec2 uv = fragCoord / iResolution.xy;\n\n    // Time-based variables for smooth animation\n    float t = iTime * 1.5;\n    float t1 = sin(t) * 0.5 + 0.5;\n    float t2 = cos(t) * 0.5 + 0.5;\n\n    // Color components\n    float r = sin(3.0 * uv.x + t1) * 0.5 + 0.5;\n    float g = cos(3.0 * uv.y + t2) * 0.5 + 0.5;\n    float b = sin(4.0 * (uv.x + uv.y) + t1 + t2) * 0.5 + 0.5;\n\n    // Output the final color\n    fragColor = vec4(r, g, b, 1.0);\n}\n```\n\nTo use this shader, follow these steps:\n\n1. Go to \u003chttps://www.shadertoy.com/new\u003e.\n1. Replace the existing code in the \"`mainImage`\" function with the code provided above.\n1. Click the \"Play\" button.\n\n### .. locally \n\nIf you want to learn how to use a shader in your own projects, you'll need to do a little more work to set things up.\n\nThe project has two files:\n\n- `glsl.html` - the main webpage\n- `shader.frag` - a _fragment shader_\n\n`glsl.html`:\n\n```html\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en\"\u003e\n\u003chead\u003e\n    \u003cmeta charset=\"UTF-8\"\u003e\n    \u003ctitle\u003eGLSL/WebGL Shader Example\u003c/title\u003e\n    \u003cmeta name=\"author\" content=\"Tim McNamara\"\u003e\n    \u003cmeta property=”og:url content=”https://github.com/timClicks/glsl-shader-intro” /\u003e\n    \u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\"\u003e\n    \u003cscript type=\"text/javascript\" src=\"https://rawgit.com/patriciogonzalezvivo/glslCanvas/master/dist/GlslCanvas.js\"\u003e\u003c/script\u003e\n    \u003cstyle\u003e\n        html, body {\n          width:  100%;\n          height: 100%;\n          margin: 0;\n        }\n        canvas {\n            display: block;\n            width: 100%;\n            height: 100%;\n        }\n    \u003c/style\u003e\n\u003c/head\u003e\n\u003cbody\u003e\n    \u003ccanvas class=\"glslCanvas\" data-fragment-url=\"shader.frag\"\u003e\u003c/canvas\u003e\n    \u003cscript\u003e\n        const canvas = document.querySelector('.glslCanvas');\n        canvas.width = window.innerWidth;\n        canvas.height = window.innerHeight;\n        const sandbox = new GlslCanvas(canvas);\n    \u003c/script\u003e\n\u003c/body\u003e\n\u003c/html\u003e\n```\n\n`shader.frag`:\n\n```glsl\n#ifdef GL_ES\nprecision mediump float;\n#endif\n\nuniform float u_time;\nuniform vec2 u_resolution;\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord)\n{\n    // Normalized pixel coordinates (from 0 to 1)\n    vec2 uv = fragCoord / u_resolution.xy;\n\n    // Time-based variables for smooth animation\n    float t = u_time * 1.5;\n    float t1 = sin(t) * 0.5 + 0.5;\n    float t2 = cos(t) * 0.5 + 0.5;\n\n    // Color components\n    float r = sin(2.0 * uv.x + t1) * 0.5 + 0.5;\n    float g = cos(3.0 * uv.y + t2) * 0.5 + 0.5;\n    float b = sin(4.0 * (uv.x + uv.y) + t1 + t2) * 0.5 + 0.5;\n\n    // Output the final color\n    fragColor = vec4(r, g, b, 1.0);\n}\n\nvoid main() {\n    mainImage(gl_FragColor, gl_FragCoord.xy);\n}\n```\n\nNote: the variables in WebGL are different than those available in ShaderToy. ShaderToy's `iResolution` becomes `u_resolution` and its `iTime` becomes `u_time`.\n\n## Explaining the code\n\nThe GLSL code in `shader.frag` is relatively short, but still fairly off-putting. Let's break it down.\n\n**Preprocessor**\n\nTo start, we have a \"Preprocessor directive\". The compiler has stages, and one of the initial stages is called the \"Preprocessor\". \n\n  ```glsl\n  #ifdef GL_ES\n  precision mediump float;\n  #endif\n  ```\n\n  This part checks if the shader is running on OpenGL ES (Embedded Systems), a subset of OpenGL for mobile and embedded devices. If so, it sets the default floating-point precision to \"medium.\" Precision indicates the accuracy and range of floating-point variables. \"Medium\" precision offers a balance between performance and accuracy.\n\n**Creating two \"uniform\" (global) variables**\n\n```glsl\nuniform float u_time;\nuniform vec2 u_resolution;\n```\n\nUniform variables are global variables that can be set from outside the shader, usually from the application code. In this case, `u_time` represents the elapsed time, and `u_resolution` represents the screen resolution as a 2D vector (width, height).\n\n**mainImage function**\n\n```glsl\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord)\n```\n\nThe `mainImage` function processes the fragment color (`fragColor`) based on the fragment coordinates (`fragCoord`). It takes an \"output variable\" `fragColor` of type `vec4` rather than use a return value to set the color of the fragment (pixel) and an input variable `fragCoord` of type `vec2` that describes the fragment's coordinates.\n\n**Normalize pixel coordinates**\n\n```glsl\nvec2 uv = fragCoord / u_resolution.xy;\n```\nThis line translates the fragment's coordinates between absolute position and a relative position between 0 and 100%. I use the term normalized because the coordinates `uv` are independent from the device that is rendering the shader, whereas `fragCoord` is device-specific. The code calculates `uv` by dividing the fragment coordinates by the resolution. The uv coordinates will range from (0, 0) to (1, 1), representing the bottom-left and top-right corners of the screen, respectively.\n\n**Changing what's displayed over time**\n\n```glsl\nfloat t = u_time * 1.5;\nfloat t1 = sin(t) * 0.5 + 0.5;\nfloat t2 = cos(t) * 0.5 + 0.5;\n```\n\nThese lines create time-based variables for smooth animation. The `t` variable scales the elapsed time (`u_time`), while `t1` and `t2` use sine and cosine functions to create smoothly changing values that oscillate between 0 and 1. \n\nChanging the constant being multiplied with `u_time` changes how quickly the image changes. Adjusting how `t1` and `t2` behave will affect the cycle that produces the colors.\n\n**Calculate color components (red, green, blue)**\n\n```glsl\nfloat r = sin(2.0 * uv.x + t1) * 0.5 + 0.5;\nfloat g = cos(3.0 * uv.y + t2) * 0.5 + 0.5;\nfloat b = sin(4.0 * (uv.x + uv.y) + t1 + t2) * 0.5 + 0.5;\n```\n\nThese lines calculate the red, green, and blue color components for each fragment using the time-based variables and the normalized pixel coordinates. The sine and cosine functions generate smoothly varying values, creating color gradients on the screen.\n\n**Set the pixel's color**\n\n```glsl\nfragColor = vec4(r, g, b, 1.0);\n```\n\nThis line sets the output `fragColor` with the calculated color components (`r`, `g`, `b`) and an alpha (transparency) value of 1.0 (fully opaque).\n\n**Main function**\n\n```glsl\nvoid main() {\n    mainImage(gl_FragColor, gl_FragCoord.xy);\n}\n```\n\nThe `main` function is the entry point of the shader. Its only job is to call the `mainImage` function that we've just chatted about. I've included `mainImage` to simplify refactoring and translating between your own HTML and ShaderToy's hosted environment.\n\n\n## Expanding the shader\n\nYou can further experiment with this shader by adjusting the parameters, such as the coefficients in the sine (`sin`) and cosine (`cos`) functions that produce color components, to create different effects.\n\n\n# Legal\n\nCopyright owner: Tim McNamara \u003ctim@mcnamara.nz\u003e\n\nCode available for re-use under the MIT-0 licence.\n\nDocumentation available for re-use under the CC-BY-4.0 licence.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimclicks%2Fglsl-shader-intro","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftimclicks%2Fglsl-shader-intro","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftimclicks%2Fglsl-shader-intro/lists"}