{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/hacking-through-shaders/","result":{"data":{"site":{"siteMetadata":{"title":"A Game Studio","author":"Lythom"}},"markdownRemark":{"id":"a7b595fb-ea1b-5a5e-acb9-f813b76486fc","html":"<p>Any sane programmer would teach “Read the documentation, understand what you are doing, and you’ll be able to solve your problems”. While true, there is a flaw : the time required to fully understand something grows bigger and bigger as what you are learning is vast or deep. Sometimes, hacking its way in might be enough, and how fun would life be without a bit of crazyness? let’s hack into shaders !</p>\n<div style=\"text-align:center\">\n<video autoplay=\"autoplay\" loop=\"loop\" width=\"320\" height=\"320\">\n    <source src=\"/1e21a8b71d904eb4d8c1316559f078e5/test6.mp4\" type=\"video/mp4\" />\n</video>\n</div>\n<h2 id=\"just-a-step-back\" style=\"position:relative;\"><a href=\"#just-a-step-back\" aria-label=\"just a step back permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Just a step back</h2>\n<p>Ok yes there ARE things you must know before diving into shaders, here they are :</p>\n<ol>\n<li>\n<p>A shader contains 3 key parts : declarations, vertex function, and fragment function.</p>\n<ul>\n<li>Declarations allow you to drive or configure the shader from scripting.</li>\n<li>Vertex function allows you to affect the 3D world (displacement of vertex)</li>\n<li>Fragment function allows you to affect the rendered pixels on screen.</li>\n</ul>\n</li>\n<li>A color is represented as a 4 dimensions vector <code class=\"language-text\">fixed4</code>.</li>\n<li>In the fragment function, you manipulate only one pixel at a time.</li>\n<li><code class=\"language-text\">frac(float)</code> function return the decimal part of a float.</li>\n<li><code class=\"language-text\">text2D(sampler2D,float2)</code> gives the pixel color of the texture <code class=\"language-text\">sample2D</code> at the coordinate <code class=\"language-text\">float2</code>. You get this coordinate as input in the fragment function.</li>\n<li>Coordinates are between (0,0) and (1,1).</li>\n</ol>\n<p>It may looks very specific if you a new to shaders but this is actually things you’d find early while learning shaders.</p>\n<h2 id=\"multiple-layers-of-rendering\" style=\"position:relative;\"><a href=\"#multiple-layers-of-rendering\" aria-label=\"multiple layers of rendering permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Multiple layers of rendering</h2>\n<p>I’m working on a game called <a href=\"https://a-time-paradox.com/\">A Time Paradox</a> which takes place in a futuristic universe (in a spaceship riding a blackhole horizon). A way to evocate a futuristic world is using something both typical, visual appealing, and quick to create : holograms.</p>\n<p>So here is the idea : we will display a spotlight texture as a background, then animate a glitch texture on the top of it, to simulate scanlines. The plan? Find a simple enough sprite shader as starter, add a glitch texture as parameter on top of the main texture, do fun math to animate the glitch texture</p>\n<h3 id=\"find-a-starter-shader\" style=\"position:relative;\"><a href=\"#find-a-starter-shader\" aria-label=\"find a starter shader permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Find a starter shader</h3>\n<blockquote>\n<p>Note : I use Unity and paint.NET as tools but the workflow would work for any engine or technology supporting shaders and any image editor.</p>\n</blockquote>\n<p>On <a href=\"https://unity3d.com/fr/get-unity/download/archive\">https://unity3d.com/fr/get-unity/download/archive</a> you’ll find under “downloads (your platform)” an entry named “integrated shaders”. It contains all built-in shaders of unity and can be copy/pasted/modified in your project. I choose to start with the shader <code class=\"language-text\">DefaultResourcesExtra\\Unlit\\Unlit-Alpha.shader</code> since i’m working in 2D without lighting, and I want transparency in my result. It looks like :</p>\n<div class=\"gatsby-highlight has-highlighted-lines\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\"><span class=\"token comment\">// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)</span>\n\n<span class=\"token comment\">// Unlit alpha-blended shader.</span>\n<span class=\"token comment\">// - no lighting</span>\n<span class=\"token comment\">// - no lightmap support</span>\n<span class=\"token comment\">// - no per-material color</span>\n\nShader <span class=\"token string\">\"Unlit/Transparent\"</span> <span class=\"token punctuation\">{</span>\n<span class=\"gatsby-highlight-code-line\">Properties <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">    _MainTex <span class=\"token punctuation\">(</span><span class=\"token string\">\"Base (RGB) Trans (A)\"</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span>D<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token string\">\"white\"</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span></span><span class=\"gatsby-highlight-code-line\"><span class=\"token punctuation\">}</span></span>\nSubShader <span class=\"token punctuation\">{</span>\n    Tags <span class=\"token punctuation\">{</span><span class=\"token string\">\"Queue\"</span><span class=\"token operator\">=</span><span class=\"token string\">\"Transparent\"</span> <span class=\"token string\">\"IgnoreProjector\"</span><span class=\"token operator\">=</span><span class=\"token string\">\"True\"</span> <span class=\"token string\">\"RenderType\"</span><span class=\"token operator\">=</span><span class=\"token string\">\"Transparent\"</span><span class=\"token punctuation\">}</span>\n    LOD <span class=\"token number\">100</span>\n\n    ZWrite Off\n    Blend SrcAlpha OneMinusSrcAlpha\n\n    Pass <span class=\"token punctuation\">{</span>\n        CGPROGRAM\n            <span class=\"token preprocessor builtin\">#pragma</span> vertex vert\n            <span class=\"token preprocessor builtin\">#pragma</span> fragment frag\n            <span class=\"token preprocessor builtin\">#pragma</span> target <span class=\"token number\">2.0</span>\n            <span class=\"token preprocessor builtin\">#pragma</span> multi_compile_fog\n\n            <span class=\"token preprocessor builtin\">#</span>include <span class=\"token string\">\"UnityCG.cginc\"</span>\n\n            <span class=\"token keyword\">struct</span> appdata_t <span class=\"token punctuation\">{</span>\n                float4 vertex <span class=\"token punctuation\">:</span> POSITION<span class=\"token punctuation\">;</span>\n                float2 texcoord <span class=\"token punctuation\">:</span> TEXCOORD0<span class=\"token punctuation\">;</span>\n                UNITY_VERTEX_INPUT_INSTANCE_ID\n            <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n            <span class=\"token keyword\">struct</span> v2f <span class=\"token punctuation\">{</span>\n                float4 vertex <span class=\"token punctuation\">:</span> SV_POSITION<span class=\"token punctuation\">;</span>\n                float2 texcoord <span class=\"token punctuation\">:</span> TEXCOORD0<span class=\"token punctuation\">;</span>\n                <span class=\"token function\">UNITY_FOG_COORDS</span><span class=\"token punctuation\">(</span><span class=\"token number\">1</span><span class=\"token punctuation\">)</span>\n                UNITY_VERTEX_OUTPUT_STEREO\n            <span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n\n            <span class=\"token keyword\">sampler2D</span> _MainTex<span class=\"token punctuation\">;</span>\n            float4 _MainTex_ST<span class=\"token punctuation\">;</span>\n\n            v2f vert <span class=\"token punctuation\">(</span>appdata_t v<span class=\"token punctuation\">)</span>\n            <span class=\"token punctuation\">{</span>\n                v2f o<span class=\"token punctuation\">;</span>\n                <span class=\"token function\">UNITY_SETUP_INSTANCE_ID</span><span class=\"token punctuation\">(</span>v<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n                <span class=\"token function\">UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO</span><span class=\"token punctuation\">(</span>o<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n                o<span class=\"token punctuation\">.</span>vertex <span class=\"token operator\">=</span> <span class=\"token function\">UnityObjectToClipPos</span><span class=\"token punctuation\">(</span>v<span class=\"token punctuation\">.</span>vertex<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n                o<span class=\"token punctuation\">.</span>texcoord <span class=\"token operator\">=</span> <span class=\"token function\">TRANSFORM_TEX</span><span class=\"token punctuation\">(</span>v<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">,</span> _MainTex<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n                <span class=\"token function\">UNITY_TRANSFER_FOG</span><span class=\"token punctuation\">(</span>o<span class=\"token punctuation\">,</span>o<span class=\"token punctuation\">.</span>vertex<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n                <span class=\"token keyword\">return</span> o<span class=\"token punctuation\">;</span>\n            <span class=\"token punctuation\">}</span>\n\n<span class=\"gatsby-highlight-code-line\">            fixed4 frag <span class=\"token punctuation\">(</span>v2f i<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">:</span> SV_Target</span><span class=\"gatsby-highlight-code-line\">            <span class=\"token punctuation\">{</span></span><span class=\"gatsby-highlight-code-line\">                fixed4 col <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_MainTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></span><span class=\"gatsby-highlight-code-line\">                <span class=\"token function\">UNITY_APPLY_FOG</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">.</span>fogCoord<span class=\"token punctuation\">,</span> col<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></span><span class=\"gatsby-highlight-code-line\">                <span class=\"token keyword\">return</span> col<span class=\"token punctuation\">;</span></span><span class=\"gatsby-highlight-code-line\">            <span class=\"token punctuation\">}</span></span>        ENDCG\n    <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">}</span></code></pre></div>\n<p>Here we find “Properties”, and both “vert” and “frag” functions. The frag function is basically doing the following thing : lookup the pixel color in <code class=\"language-text\">_MainTex</code> that is located at <code class=\"language-text\">i.texcoord</code> position, run a <code class=\"language-text\">UNITY_APPLY_FOG</code> process we don’t know about, return the color.</p>\n<p>Let’s try this shader already :</p>\n<ol>\n<li>copy/paste file in project asset folder;</li>\n<li>in shader file line 8, rename shader from <code class=\"language-text\">Shader Unlit/Transparent</code> to <code class=\"language-text\">Shader Custom/MyHologram</code>;</li>\n<li>in unity editor right click on shader file -> create -> material to create a new material linked with the shader;</li>\n<li>drag and drop the “spotlight” sprite into the scene;</li>\n<li>drag and drop the material from asset folder to the sprite in the scene hierarchy, so that the sprite uses the material configured with our shader.</li>\n</ol>\n<p>The “spotlight” sprite? Let’s use this one :</p>\n<div style=\"width:250px;margin:auto;\">\n    <span class=\"gatsby-resp-image-wrapper\" style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 512px; \">\n      <a class=\"gatsby-resp-image-link\" href=\"/static/86cfd55c0239b23f0bf31a5c1be79530/01e7c/spot.png\" style=\"display: block\" target=\"_blank\" rel=\"noopener\">\n    <span class=\"gatsby-resp-image-background-image\" style=\"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url(&apos;data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAC4jAAAuIwF4pT92AAAB7ElEQVQ4y+2Ty27UMBSGj6dIrJAoA6pQ1UpVKULTMpfOJGn3gNruALHiUpUtay4SAp6AXh6BKuPkOJNYArZVF1VfomwRz8Fvx8lkphex6BJL3/z2sX38H8dD7URSZxBb2gOmdsK0jL6XMnUxbpnYII91rLLVNsYtrG2WYKxiuvzWgcO70T5Nhd9oNpQ0sx9SIwppNYUjZpqXEc3HETVY0qJi0eK+WGYpGnEoFjkW95jFHSnFguyLJkcE2zF1U54E9W6q6j1oz/ZLbhb0UuVgRz7G3C2zFlchrEs/UzdAy8vUEvS+watQjUGbBm+IiXWCTNXLsudQGiYCsAYegId+ziODdwb+kHWMV2ejPhXurE2cMI3+FngJXo2gy/7mGCb2OtBqusyFH5OsSP4EfAJvwfsL+ADegS9mjzWkE1Nl7hInCKe3wVcs2gU7jt1z2APbYKpaqXVoVauaG5tSfoA+iEfQiqEGCX6CF25PrZpr3OV18B0cIcEBOLRkuSJ+AI6BXtHJtereMmHF5YTT54j9ASdj/LJo9Rv61CWbOMudpQjigq+i/xG8AY/N0/B1sgF95j7GZy9LrhSJVtz+kebr4ksn//y39UoD6vQk7mNYembd1i7Cq6w9txUnBZUSgjHn9t3icEPx/v63y21/AXwWn2JP+WOxAAAAAElFTkSuQmCC&apos;); background-size: cover; display: block;\"></span>\n  <img class=\"gatsby-resp-image-image\" alt=\"spot\" title=\"spot\" src=\"/static/86cfd55c0239b23f0bf31a5c1be79530/01e7c/spot.png\" srcset=\"/static/86cfd55c0239b23f0bf31a5c1be79530/12f09/spot.png 148w,\n/static/86cfd55c0239b23f0bf31a5c1be79530/e4a3f/spot.png 295w,\n/static/86cfd55c0239b23f0bf31a5c1be79530/01e7c/spot.png 512w\" sizes=\"(max-width: 512px) 100vw, 512px\" style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\" loading=\"lazy\">\n  </a>\n    </span>\n</div>\n<p>You should end with something like this :\n<span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/ee36889d92e1a39aa6d0b8b9b3c03b1e/f6f7a/step1.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 89.86486486486487%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAClUlEQVQ4y5VUW2sTYRDdXNoku0kobbGJFtRcNslms5vrbmJjaIoWtT5Y8iL4YGmhBRUV/AdCwRdB8Qcf50wuJg2ofTh8s5PvO3M7E6PRaKBaraJYLKJQKCxQLJXRCg7g+N3Fb+Wy+Jo+6vW6+uYoFIpwXVdhOI6jhJVKBaVSSR/R5lkuFWHb02/CcWoYPX4kGCEIA/R6PUUYBOgPBgiCEIZt2yApo67DXdi8w7vtdhvdTge+74PVzeE3m+iI32DkXC6HnZ0d7O7uLs5l0JfP5zXLMAynpN0ujo6OMB6PMT48xJOnx+j3+1PCzc1NGIbxV6RSKW0NiTzP01IvL69wcXGB8/NzvD07w3A4nBLyMh9FIpE1orkvnU4rYavVQlvAMoNZtvS1WLIEU8JkMvnPDC3LUkJmEcgQSMLy/wwm1EC3JpxMJko6kKkSzKrdbqF/MBS7NyNMJP6LkHfZP5bZlBJ1ujJtVxQwOH6KDntIKZhbW4iaKcTk0RrSFiKWicz2thKyPMqDMqrVagvwW4Vti4AtkUW67iDr1pFZwNUz6zVgVSvISNCKBA9FGqPRSEmZ3TKYtZac2NhAxveQP32FvZPn2Ht5gpxg7+QF7k5OkXxwH6a0pSqZ6JSl5JtkFDZPoyKNTsQ3EMtk8PDdFYqfPqDwkXiP0pfP2H/zemUo89KWt4RY7LLq0DT10Z1nx/B+/UDt+iucb9do/PyOjOyv6lACkrA5y4S2/gdIG8hBuy7BVmQTkzNdsZHcvwdTyqR9UzZBr60lM9OG5yPoD/TbE5sKuJUOefdApEEd6nYIUW8mcpKx9HXC5fVbsklo65QHuiF8TJJ5T1d6yMvRaBTxeByxWGwF9PG3bDarhNxfSoeP51kt4zfXkfbqwU/yKAAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Unity screenshot\"\n        title=\"Unity screenshot\"\n        src=\"/static/ee36889d92e1a39aa6d0b8b9b3c03b1e/fcda8/step1.png\"\n        srcset=\"/static/ee36889d92e1a39aa6d0b8b9b3c03b1e/12f09/step1.png 148w,\n/static/ee36889d92e1a39aa6d0b8b9b3c03b1e/e4a3f/step1.png 295w,\n/static/ee36889d92e1a39aa6d0b8b9b3c03b1e/fcda8/step1.png 590w,\n/static/ee36889d92e1a39aa6d0b8b9b3c03b1e/efc66/step1.png 885w,\n/static/ee36889d92e1a39aa6d0b8b9b3c03b1e/f6f7a/step1.png 1009w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<h3 id=\"add-a-glitch-texture\" style=\"position:relative;\"><a href=\"#add-a-glitch-texture\" aria-label=\"add a glitch texture permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Add a glitch texture</h3>\n<p>Let’s add a new glitch texture. We could either find one online, or create it ourself. Here is how I created mine :</p>\n<ol>\n<li>Create a 1024x1024 png file filled with black.</li>\n<li>On a new layer, draw random horizontal white lines of 3 pixels height.</li>\n<li>Duplicate last created layer, apply a “motion blur effect” toward top, 10px</li>\n<li>repeat step 3. a few time until it creates a smooth gradient</li>\n<li>remove the original line layer (the pure white is too hard and sharp)</li>\n<li>merge all remaining layers</li>\n</ol>\n<p>You should get something like that (well, I also tweaked the luminosity curve a bit) :\n<span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/e4a0fde4181690628adfe4ea5f61f528/2bef9/scanlines.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAC4gAAAuIAHVHB4bAAAAZElEQVQ4y63TsQkAIRQE0c1/biKYWoOpIIiBfdiBbVjx3hUxC6+ACVZrLZOUczZJe2+T9N4zSXNOk/TPqN67STrnmMQnR4RJwp+CJ5dSTNK91yQ+OaVkksYYJvHJtVaT1Foz6QMZGXZb5VNICwAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"scanlines\"\n        title=\"scanlines\"\n        src=\"/static/e4a0fde4181690628adfe4ea5f61f528/fcda8/scanlines.png\"\n        srcset=\"/static/e4a0fde4181690628adfe4ea5f61f528/12f09/scanlines.png 148w,\n/static/e4a0fde4181690628adfe4ea5f61f528/e4a3f/scanlines.png 295w,\n/static/e4a0fde4181690628adfe4ea5f61f528/fcda8/scanlines.png 590w,\n/static/e4a0fde4181690628adfe4ea5f61f528/efc66/scanlines.png 885w,\n/static/e4a0fde4181690628adfe4ea5f61f528/2bef9/scanlines.png 1024w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<p>To use it into our shader, let’s mimic what is done for the <code class=\"language-text\">_MainText</code>.</p>\n<p>Line 11, add in the <code class=\"language-text\">_GlitchTex</code> property:</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">Properties <span class=\"token punctuation\">{</span>\n    _MainTex <span class=\"token punctuation\">(</span><span class=\"token string\">\"Base (RGB) Trans (A)\"</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span>D<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token string\">\"white\"</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n    _GlitchTex <span class=\"token punctuation\">(</span><span class=\"token string\">\"Glitch Texture\"</span><span class=\"token punctuation\">,</span> <span class=\"token number\">2</span>D<span class=\"token punctuation\">)</span> <span class=\"token operator\">=</span> <span class=\"token string\">\"white\"</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Then line 44, add the <code class=\"language-text\">_GlitchTex</code> declaration :</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\"><span class=\"token keyword\">sampler2D</span> _MainTex<span class=\"token punctuation\">;</span>\n<span class=\"token keyword\">sampler2D</span> _GlitchTex<span class=\"token punctuation\">;</span>\nfloat4 _MainTex_ST<span class=\"token punctuation\">;</span></code></pre></div>\n<p>From here, the “Glitch texture” property should appear in the spotlight inspector in Unity. Select the texture you just created (or use mine) as value.</p>\n<p>And… nothing happens. We now need to actually use the texture we declared in the shader code, the fun maths beggins !</p>\n<h3 id=\"fun-with-maths\" style=\"position:relative;\"><a href=\"#fun-with-maths\" aria-label=\"fun with maths permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Fun with maths</h3>\n<p>To change the rendering of the pixels, the fragment function is the one we should work with :</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">fixed4 frag <span class=\"token punctuation\">(</span>v2f i<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">:</span> SV_Target\n<span class=\"token punctuation\">{</span>\n    fixed4 col <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_MainTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token function\">UNITY_APPLY_FOG</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">.</span>fogCoord<span class=\"token punctuation\">,</span> col<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    <span class=\"token keyword\">return</span> col<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>We want to get the color of our glitch texture, let’s try to copy the function for the <code class=\"language-text\">_MainTex</code>, and mix the 2 colors.</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">    fixed4 mainCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_MainTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    fixed4 glitchCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_GlitchTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    fixed4 col <span class=\"token operator\">=</span> mainCol <span class=\"token operator\">+</span> glitchCol<span class=\"token punctuation\">;</span></code></pre></div>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 460px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/caabad309bea257b2486557113f3030d/08a84/test1.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 62.83783783783784%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAA7DAAAOwwHHb6hkAAACJ0lEQVQ4y62TXUhTYRjH3x1H4kXCqCw2CEqCrdaHJoSGS4m67yI1yYjE0iQqgsiMBanFjFVYmJ4p4T460+Zc2/QYOoONCqLRpxfVRd1kLvoQZ2RX/95zznt0qwZddPHj/zz/c/jzvM95DyGE4H/BabUgB252Ys9lGyrtdlTf6EAF34UaysEeByq7u7C3h0ed4EF9vxdHvALq3S40+n2o9Q3geDCAk6KIw8NhHJ2IYG1xMcgkgFeMScZLyosUffZb/5zVkh+nvKb0xePQaDQgpeetqHn6BFXR+9j3IMqIyVrF+mqZGIN6D2MM5Z39jx9BX1CgHJ3Ly4PZ44TJ64bxthNGwQWTQHuhj+FkuFJqBaP7FjaF/DA0NshhGo6TlKC8uRlXfv5A69QHXPyU+CfaEtOwffmMprdvsNRgUKaTjiwVy1auwrV379E7Pw9+dhaOZBKOuSR4laSCg6nkdc98g4vursJqVabLymKhyphYvnsXto0EUdjvwdY7wgKFjDRvwIOigA8bOztA1KBFFpvN9nZYxkSU3B3E9tDQAiVqHQ4oGvTDMi5ihaWUfQguPVCdMn+HBZdmvqIl8REt01MyrUxlqH+B7rn9+xwODQ1muuAkdaEwnz2DndEIykbDKI+MKkzcU3RsRPbLxBB0G9b/bbo/A7P1emzp5WG+fhXrzjUh//QprDlxTFaTrQ1F9HqtrqvNFJa+Q5UlubkZ/9dsnQ7anJyMz38BgRA5HpXDvFAAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"test1\"\n        title=\"test1\"\n        src=\"/static/caabad309bea257b2486557113f3030d/08a84/test1.png\"\n        srcset=\"/static/caabad309bea257b2486557113f3030d/12f09/test1.png 148w,\n/static/caabad309bea257b2486557113f3030d/e4a3f/test1.png 295w,\n/static/caabad309bea257b2486557113f3030d/08a84/test1.png 460w\"\n        sizes=\"(max-width: 460px) 100vw, 460px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<p>Great we mixed the 2 textures into 1 render ! But it looks weird, doesn’t it? The alpha of the first image is overriden by the second texture : we lose the smooth gradient we had and the edges looks wrong. Let’s try using multiply instead of addition to mix the colors.</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">    fixed4 mainCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_MainTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    fixed4 glitchCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_GlitchTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    fixed4 col <span class=\"token operator\">=</span> mainCol <span class=\"token operator\">*</span> glitchCol<span class=\"token punctuation\">;</span></code></pre></div>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 460px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/d4b52bc17f91c31135d469f5293eca65/08a84/test2.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 62.83783783783784%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAq0lEQVQ4y63TXQuDIBQGYDVLU6qboGLtg7n9/5/4zmPFRri1yIuHAyqv5xQyxhiS4kWBfwmiYhR4nk+BQmtkxhxjLbiUS5eJR57aFKFlumW3ZdTPQNowtyusczDu7muMW/FrzweKto106ImyhKxrr5rrhqoKZ3mWxQPTfsMZja66DqrvvaWuddDDANk024H0c+iwHkfv9FV5OYextwNTj/x+Pr+eFp/sCjzgBa1TEYEP1DfWAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"test2\"\n        title=\"test2\"\n        src=\"/static/d4b52bc17f91c31135d469f5293eca65/08a84/test2.png\"\n        srcset=\"/static/d4b52bc17f91c31135d469f5293eca65/12f09/test2.png 148w,\n/static/d4b52bc17f91c31135d469f5293eca65/e4a3f/test2.png 295w,\n/static/d4b52bc17f91c31135d469f5293eca65/08a84/test2.png 460w\"\n        sizes=\"(max-width: 460px) 100vw, 460px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<p>Not bad, but we barly recognize the original sprite. That said, this colors could look good as an “additional layer”. Let’s add the original color on top of this one.</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">    fixed4 mainCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_MainTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    fixed4 glitchCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_GlitchTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    fixed4 col <span class=\"token operator\">=</span> mainCol <span class=\"token operator\">*</span> glitchCol <span class=\"token operator\">+</span> mainCol<span class=\"token punctuation\">;</span></code></pre></div>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 460px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/1c86cf46ce6ee67f7b2beddb69753b25/08a84/test3.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 62.83783783783784%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAB5UlEQVQ4y63Ty0sbURQH4MnEJEYjJu1CA6P4lhJEqfMwRnHjs74iXfmKLqSltFURuit0q5QKLkRw4UKSGE184YNWRFp049qFrbSK1rYLpS78A36dO3cmGWvUjYuPczgXztxz71yGYRjcK4PFDIPJBIPZHMWqlDWLJYrVJCZeZ7WCYVkwqcXFcPD8VYIQI4qKBxqJkKIcsoceD2wFBXSHJHG2eZHW1Ij0liZVM9Wq1wKniuTp3qtMdrs6trxN59M2ZPT6wHV1gOvuVGQQPorzdclRp4fiujuQ9bwPjjJJPT8DiYxSkObDKPVPgw/6Y2b8EGa0GIAQCoAPkRiUawGIkTmUTIwjwWbTXwxNpIUIGv+eoe74EA2nx4r6X3onaJAp8bccfx6h+fICWc/6aCMjGxuZJCmuRyhbWYS0OCcLU0thiCpJEaG1hVm415dRNDYa79eJzo7cwX6Uf1yFEA5CnJ+9WSQE99oS7HwpbcKy8RtaMzNR/XUPtSc/UHP4DbX/OzpAzfd9PDn/g5LJCf1FXD9D7SvkTKp2d+DZ2kDl9paiQvNlE57PG3B/WkVyft4dDVXGpCS43g/D9WEEeW+GkPP6JbJfvUDu0AAK373F4+kpcJ3t8UaN35AgT/Gmt2qUn9ht6/8ADMyz7IELYsIAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"test3\"\n        title=\"test3\"\n        src=\"/static/1c86cf46ce6ee67f7b2beddb69753b25/08a84/test3.png\"\n        srcset=\"/static/1c86cf46ce6ee67f7b2beddb69753b25/12f09/test3.png 148w,\n/static/1c86cf46ce6ee67f7b2beddb69753b25/e4a3f/test3.png 295w,\n/static/1c86cf46ce6ee67f7b2beddb69753b25/08a84/test3.png 460w\"\n        sizes=\"(max-width: 460px) 100vw, 460px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<p>Hey ! We are getting there !</p>\n<p>Now it’s time to animate the glitch. For this, we need some kind of “time” external variable to calculate from. Every engine provide a way to access some time clock, in Unity we can use <code class=\"language-text\">_Time.y</code>. The idea is to modify the <code class=\"language-text\">texcoord</code> (reminder: it’s a <code class=\"language-text\">vector2</code> between 0,0 and 1,1 for x,y coordinates), to have the y value changed over time and looped between 0 and 1.</p>\n<p>To loop a variable that depends on time between 0 and 1 we can do as follow :</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">    <span class=\"token keyword\">fixed</span> scrollValue <span class=\"token operator\">=</span> <span class=\"token function\">frac</span><span class=\"token punctuation\">(</span><span class=\"token number\">0.04</span> <span class=\"token operator\">*</span> _Time<span class=\"token punctuation\">.</span>y<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p><code class=\"language-text\">_Time.y</code> is a value that consistently increase (elapsed time since start of the game ), <code class=\"language-text\">frac()</code> is keeping only the decimal part of a float (making the value loop between 0 and 1), 0.04 is an arbitrary constant used as a ratio to slow down the scrolling effect. This constant can be exported in a shader property to configure the speed, but let’s keep it inlined for the moment.</p>\n<p>In order to remap the <code class=\"language-text\">texcoord</code> from static to scrolled coordinates, we need to create a new float2 element, and change the y value only so that the glitch scrolls vertically.</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">    float2 offsetTexCoord <span class=\"token operator\">=</span> <span class=\"token function\">float2</span><span class=\"token punctuation\">(</span>\n        i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>x<span class=\"token punctuation\">,</span>\n        <span class=\"token function\">frac</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>y <span class=\"token operator\">+</span> <span class=\"token number\">0.04</span> <span class=\"token operator\">*</span> _Time<span class=\"token punctuation\">.</span>y<span class=\"token punctuation\">)</span>\n    <span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n    fixed4 glitchCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_GlitchTex<span class=\"token punctuation\">,</span> offsetTexCoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Here we keep the x value as is, and we add the current y value with the scrollValue that varies over time. This tells the shader to look at a different vertical position inside the texture over time for a given pixel, and because this vertical position moves slowly it renders the texture “offset” or “shifted”. Here we go :</p>\n<div style=\"text-align:center\">\n    <video autoplay=\"autoplay\" loop=\"loop\" width=\"320\" height=\"320\">\n        <source src=\"/885403ce0018471ca9d2721563772c9a/test4.mp4\" type=\"video/mp4\" />\n    </video>\n</div>\n<p>Note that I had to cheat a little to get a perfect looping animation, your hologram should move more slowly that that.</p>\n<h3 id=\"we-need-to-go-deeper\" style=\"position:relative;\"><a href=\"#we-need-to-go-deeper\" aria-label=\"we need to go deeper permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>We need to go deeper</h3>\n<p>It already looks fine, but out pattern recognition machine (aka. our brain) will quickly find out the repetition and loose interest for the visual quickly. No matter, we got more tricks! Let’s use multiple glitch overlay that run at different speed. Basically we copy/paste the shader code above to extract colors at different positions with different timing. It could write like :</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">fixed4 mainCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_MainTex<span class=\"token punctuation\">,</span> i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\n<span class=\"token comment\">// glitch 1</span>\nfloat2 offsetTexCoord <span class=\"token operator\">=</span> <span class=\"token function\">float2</span><span class=\"token punctuation\">(</span>\n    i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>x<span class=\"token punctuation\">,</span>\n    <span class=\"token function\">frac</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>y <span class=\"token operator\">+</span> _Time<span class=\"token punctuation\">.</span>y <span class=\"token operator\">*</span> <span class=\"token number\">0.33</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nfixed4 glitchCol <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_GlitchTex<span class=\"token punctuation\">,</span> offsetTexCoord<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token comment\">// glitch 2</span>\nfloat2 offsetTexCoord2 <span class=\"token operator\">=</span> <span class=\"token function\">float2</span><span class=\"token punctuation\">(</span>\n    i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>x<span class=\"token punctuation\">,</span>\n    <span class=\"token function\">frac</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>y <span class=\"token operator\">+</span>  _Time<span class=\"token punctuation\">.</span>y <span class=\"token operator\">*</span> <span class=\"token number\">0.4521</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nfixed4 glitchCol2 <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_GlitchTex<span class=\"token punctuation\">,</span> offsetTexCoord2<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n<span class=\"token comment\">// glitch 3</span>\nfloat2 offsetTexCoord3 <span class=\"token operator\">=</span> <span class=\"token function\">float2</span><span class=\"token punctuation\">(</span>\n    i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>x<span class=\"token punctuation\">,</span>\n    <span class=\"token function\">frac</span><span class=\"token punctuation\">(</span>i<span class=\"token punctuation\">.</span>texcoord<span class=\"token punctuation\">.</span>y <span class=\"token operator\">+</span> _Time<span class=\"token punctuation\">.</span>y <span class=\"token operator\">*</span> <span class=\"token number\">0.2541</span><span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\nfixed4 glitchCol3 <span class=\"token operator\">=</span> <span class=\"token function\">tex2D</span><span class=\"token punctuation\">(</span>_GlitchTex<span class=\"token punctuation\">,</span> offsetTexCoord3<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n\nfixed4 col <span class=\"token operator\">=</span> mainCol <span class=\"token operator\">+</span> mainCol <span class=\"token operator\">*</span> glitchCol <span class=\"token operator\">*</span> <span class=\"token number\">0.4</span> <span class=\"token operator\">+</span> mainCol <span class=\"token operator\">*</span> glitchCol2 <span class=\"token operator\">*</span> <span class=\"token number\">0.2</span> <span class=\"token operator\">+</span> mainCol <span class=\"token operator\">*</span> glitchCol3 <span class=\"token operator\">*</span> <span class=\"token number\">0.4</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>And looks like :</p>\n<div style=\"text-align:center\">\n<video autoplay=\"autoplay\" loop=\"loop\" width=\"320\" height=\"320\">\n    <source src=\"/8d8caa899b6929ed5686da0b93353d02/test5.mp4\" type=\"video/mp4\" />\n</video>\n</div>\n<p>Here it looks up 3 different positions from the same glitch texture, then add them like before but multiplied with a constant to soften the effect (else it becomes too bright). As a consequence, my animation capture cannot loop smoothly because it happens very rarely that the 3 glitch layers get back at their original position synchronously. Also, if it makes that the animation in-game looks like it never repeat itself, which is more satisfying to watch.</p>\n<h3 id=\"we-need-to-go-deeper-again\" style=\"position:relative;\"><a href=\"#we-need-to-go-deeper-again\" aria-label=\"we need to go deeper again permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>We need to go deeper AGAIN</h3>\n<p>This could be over, yet my brain is not fully satisfied. There are still some patterns I recognize sometimes so it can be even better.</p>\n<p>In short : we are adding colors together, which means at any given time for one layer there is always a “minimum” light value (recognized as a white band) which the eye can follow along the way and that will repeat. The fact that there are other lines over it makes it harder to track but not impossible. There is a solution : multiplying the colors together instead of adding them. The consequence is that sometimes the lines overlap (creating brighter lines) and sometimes they get further from each other (fading the lines away).</p>\n<p>The math becomes :</p>\n<div class=\"gatsby-highlight\" data-language=\"glsl\"><pre class=\"language-glsl\"><code class=\"language-glsl\">fixed4 col <span class=\"token operator\">=</span> mainCol <span class=\"token operator\">+</span> mainCol <span class=\"token operator\">*</span> glitchCol <span class=\"token operator\">*</span> glitchCol2 <span class=\"token operator\">*</span> glitchCol3<span class=\"token punctuation\">;</span></code></pre></div>\n<p>Also the texture must be tweaked so that there is more overlapping (the current texture has too much black and overlapping must happens more frequently so that the effect works). From the previous texture, I inverted the color, they played with the luminosity curve until the effect looks right. Here goes the texture and the result :</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 590px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/9a7a6cc1c105cd1ca473eadc46ed386d/2bef9/lightdust2.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAC4fAAAuHwF47oFfAAAAa0lEQVQ4y63TMQ0AIQBD0SpiuAkSZCABE7AhgbBcDgcEjz0Tf3gCfppqjGGSeu8mKYRgkuacJqnWapJyzibpfV+T+OQYo0n6vs8klVJMUkrJJN17TdJayyQ9z2OSWmsm6ZxjEj8Kfr29t0k/ZVanzn4iXgEAAAAASUVORK5CYII='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"lightdust2\"\n        title=\"lightdust2\"\n        src=\"/static/9a7a6cc1c105cd1ca473eadc46ed386d/fcda8/lightdust2.png\"\n        srcset=\"/static/9a7a6cc1c105cd1ca473eadc46ed386d/12f09/lightdust2.png 148w,\n/static/9a7a6cc1c105cd1ca473eadc46ed386d/e4a3f/lightdust2.png 295w,\n/static/9a7a6cc1c105cd1ca473eadc46ed386d/fcda8/lightdust2.png 590w,\n/static/9a7a6cc1c105cd1ca473eadc46ed386d/efc66/lightdust2.png 885w,\n/static/9a7a6cc1c105cd1ca473eadc46ed386d/2bef9/lightdust2.png 1024w\"\n        sizes=\"(max-width: 590px) 100vw, 590px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<div style=\"text-align:center\">\n<video autoplay=\"autoplay\" loop=\"loop\" width=\"320\" height=\"320\">\n    <source src=\"/1e21a8b71d904eb4d8c1316559f078e5/test6.mp4\" type=\"video/mp4\" />\n</video>\n</div>\n<p>As you can see, sometimes lines appear then fade away, if you have done it yourself you can look at it during hours without recognizing any pattern. The effect is now more subtle, intriguing.</p>\n<h2 id=\"final-words\" style=\"position:relative;\"><a href=\"#final-words\" aria-label=\"final words permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Final words</h2>\n<p>Thank you for reading! I’m working hard on <a href=\"https://a-time-paradox.com/\">A Time Paradox</a> so that it looks cool, check it out!</p>\n<p>Want to respond to this article? Made something out of it? Let me know!<br>\nPM or mention <a href=\"https://twitter.com/lythom\">@Lythom</a> or drop a message at <a href=\"mailto:samuel@a-game-studio.com\">samuel@a-game-studio.com</a>. I’ll re-tweet your work.</p>\n<p>Happy hacking!</p>","frontmatter":{"title":"Hacking through shaders","date":"2018-08-27","description":null}}},"pageContext":{"lang":"en","slug":"/blog/hacking-through-shaders/","previous":{"fields":{"slug":"/projects/a-time-paradox/"},"frontmatter":{"title":"A Time Paradox"}},"next":{"fields":{"slug":"/blog/what-babies-teach-us-about-game-design/"},"frontmatter":{"title":"What babies teach us about game design"}}}}}