{"id":1559,"date":"2017-03-17T02:47:53","date_gmt":"2017-03-16T14:47:53","guid":{"rendered":"http:\/\/www.funk.co.nz\/blog\/?p=1559"},"modified":"2017-03-17T02:57:46","modified_gmt":"2017-03-16T14:57:46","slug":"sine-wave-zion-additive-colour-demo-shadertoy","status":"publish","type":"post","link":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy","title":{"rendered":"Sine Wave Zion - Additive Colour Demo in Shadertoy"},"content":{"rendered":"<p>Chilin - my African friend asked me if I could make a shader to visualise a sound or sine wave with harmonics included, so I wrote <a href=\"https:\/\/www.shadertoy.com\/view\/4sscDS\">this<\/a> WebGL shadertoy:<\/p>\n<p><iframe loading=\"lazy\" src=\"https:\/\/www.shadertoy.com\/embed\/4sscDS?gui=true&amp;t=10&amp;paused=true&amp;muted=false\" width=\"640\" height=\"360\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<p>In the end, I didn't quite get there but this shader makes a nice illustration of the theory of additive colours, as various offsets of the sine waves are given their own channel: Red for the main frequency, Green for the second harmonic or octave, and Blue for the third harmonic.<\/p>\n<p><strong>Features:<\/strong> sine waves, pi, mouse input to alter parameters, interference patterns<\/p>\n<h2>Highlights of the code<\/h2>\n<p>This is just a normalised sine function of time multiplied by the pixel co-ordinates being rendered.<\/p>\n<p>vec2 S; \/\/ speed of fundamental<br \/>\nS.x = (xy.x + xy.y)*(xy.x - xy.y)*0.5; \/\/ \"velocity potential\" a square of the pixel distance so it gets big quick<br \/>\nS.x -= iGlobalTime *timespeed; \/\/ animate stream<br \/>\nvec2 sxy = sin(3.14159 * S * 1.0 ); \/\/ its a sine wave of time and pi<\/p>\n<h3>The Full Code<\/h3>\n<pre>\/\/ Concept: an animation of wave harmonics\r\n\/\/ show 1st, 2nd, 3rd order harmonics somehow\r\n\r\nfloat divs = 2.2;\r\n\r\nvoid mainImage( out vec4 fragColor, in vec2 fragCoord )\r\n{\r\nvec4 m = iMouse; \r\nif ( m.x == 0. &amp;&amp; m.y == 0.) {\r\nm.x = iResolution.x \/ 2.;\r\nm.y = iResolution.y \/ 2.;\r\n}\r\nfloat dist = m.x - ( iResolution.x \/ 2.0 ); \/\/ centererd\r\nfloat speed = 0.0009; \/\/ 0.9 camera movement to plane \r\nfloat timespeed = 0.1 ; \/\/ 0.1 wave movement on the ribbons\r\n  \r\n\/\/ MOUSE: multiple by my or mx to get value between 1 and 2\r\nfloat my = float( (m.y + iResolution.y ) \/ iResolution.y );\r\nfloat mx = float( (m.x + iResolution.y ) \/ iResolution.x );\r\n\r\nfloat t = 8.0 - speed ;\r\n\r\n\/\/ divs create staggered \/ staged division, old setting was divs += t \/ 0.20;\r\n\/\/ let the mouse distance control the zoom\r\ndivs += t \/ ( ( m.x + 2. ) \/ iResolution.x );\r\n \r\nvec2 div = vec2( divs, divs*iResolution.y\/iResolution.x );\r\nvec2 uv = fragCoord.xy \/ iResolution.xy;\r\n \r\n\/\/ center on screen then pan off:\r\nuv -= 0.1 + (iGlobalTime*0.0001); \r\n \r\nfloat b = 1.0*divs\/iResolution.x; \/\/ blur over 2.4 pixels\r\n\r\nvec2 xy = div*uv;\r\n \r\nvec2 S; \/\/ speed of fundamental\r\nvec2 S2; \/\/ speed of 1st octave\r\nvec2 S3; \/\/ speed of 2nd harmonic (perfect fifth?)\r\n\r\n\r\nS.x = (xy.x + xy.y)*(xy.x - xy.y)*0.5; \/\/ \"velocity potential\"\r\nS2.x = (xy.x + xy.y)*(xy.x - xy.y)*0.5; \/\/ \"velocity potential\"\r\nS3.x = (xy.x + xy.y)*(xy.x - xy.y)*0.5; \/\/ \"velocity potential\"\r\n\r\nS.y = xy.x*xy.y; \/\/ stream function\r\nS2.y = xy.x*xy.y; \/\/ stream function \r\nS3.y = xy.x*xy.y; \/\/ stream function\r\n\r\n\r\n\/\/ speed of the dots\r\nS.x -= iGlobalTime *timespeed; \/\/ animate stream\r\nS2.x -= iGlobalTime *timespeed + (my * 20. ); \/\/ animate stream\r\nS3.x -= iGlobalTime *timespeed + (my * 20.); \/\/ animate stream\r\n\r\n \r\n \r\n\/\/ HERE IS THE WAVE HARMONICS \r\n\/\/ SXY IS FUNDAMENTAL WAVE2 IS 2ND HARMONIC\r\n\/\/ sxy is *probably* the main wave\r\n\/\/ wave2 is the 2nd harmonic\r\n \r\nvec2 sxy = sin(3.14159 * S * 1.0 );\r\nvec2 wave2 = sin(3.14159 * S2 * 2.0 ); \/\/ 2.00 is double\r\nvec2 wave3 = sin(3.14159 * S3 * 3.0 ); \/\/ 3.00 is double\r\n \r\n \r\n\/\/ w2 is the 2nd harmonic\r\nfloat a = sxy.x * sxy.y; \/\/ combine sine waves using product\r\nfloat w2 = wave2.x * wave2.y; \/\/ combine sine waves using product\r\nfloat w3 = wave3.x * wave3.y; \/\/ combine sine waves using product\r\n \r\n\/\/ not sure what this does but we will do it to w2, w3 as well\r\na = 0.5*a + 0.5; \/\/ remap to [0..1]\r\na = smoothstep( 0.85-b, 0.85+b, a ); \/\/ threshold\r\n \r\nw2 = 0.5*a + 0.5; \/\/ remap to [0..1]\r\nw2 = smoothstep( 0.85-b, 0.85+b, a ); \/\/ threshold\r\n \r\nw3 = 0.5*a + 0.5; \/\/ remap to [0..1]\r\nw3 = smoothstep( 0.85-b, 0.85+b, a ); \/\/ threshold\r\n \r\nfloat c = sqrt( a ); \/\/ correct for gamma\r\nfloat w2gamma = sqrt( w2 ); \/\/ correct for gamma\r\nfloat w3gamma = sqrt( w3 ); \/\/ correct for gamma\r\n \r\nfloat red, green, blue;\r\n \r\nred = sxy.x - sxy.y ;\r\ngreen = wave2.x - wave2.y;\r\nblue = wave3.x - wave3.y;\r\n \r\nfloat crossover = -1.5;\r\n \r\nif (red &lt; crossover) {\r\nred = red * -1.;\r\n}\r\nif (green &lt; crossover) {\r\ngreen = green * -1.;\r\n} \r\nif (blue &lt; crossover) {\r\nblue = blue * -1.;\r\n} \r\n \r\nfragColor = vec4( red, green, blue, 1.0);\r\n\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Chilin &#8211; my African friend asked me if I could make a shader to visualise a sound or sine wave with harmonics included, so I wrote this WebGL shadertoy: In the end, I didn&#8217;t quite get there but this shader makes a nice illustration of the theory of additive colours, as various offsets of the [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[33],"tags":[48],"class_list":["post-1559","post","type-post","status-publish","format-standard","hentry","category-visuals","tag-webgl"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Sine Wave Zion - Additive Colour Demo in Shadertoy - The Funk Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Sine Wave Zion - Additive Colour Demo in Shadertoy - The Funk Blog\" \/>\n<meta property=\"og:description\" content=\"Chilin - my African friend asked me if I could make a shader to visualise a sound or sine wave with harmonics included, so I wrote this WebGL shadertoy: In the end, I didn&#039;t quite get there but this shader makes a nice illustration of the theory of additive colours, as various offsets of the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy\" \/>\n<meta property=\"og:site_name\" content=\"The Funk Blog\" \/>\n<meta property=\"article:published_time\" content=\"2017-03-16T14:47:53+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-03-16T14:57:46+00:00\" \/>\n<meta name=\"author\" content=\"tomachi\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tomachi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy\"},\"author\":{\"name\":\"tomachi\",\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/#\\\/schema\\\/person\\\/5e3b3851d9a5cbe644e6ccf190ea10f9\"},\"headline\":\"Sine Wave Zion - Additive Colour Demo in Shadertoy\",\"datePublished\":\"2017-03-16T14:47:53+00:00\",\"dateModified\":\"2017-03-16T14:57:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy\"},\"wordCount\":172,\"keywords\":[\"webgl\"],\"articleSection\":[\"Visuals\"],\"inLanguage\":\"en-NZ\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy\",\"url\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy\",\"name\":\"Sine Wave Zion - Additive Colour Demo in Shadertoy - The Funk Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/#website\"},\"datePublished\":\"2017-03-16T14:47:53+00:00\",\"dateModified\":\"2017-03-16T14:57:46+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/#\\\/schema\\\/person\\\/5e3b3851d9a5cbe644e6ccf190ea10f9\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy#breadcrumb\"},\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/visuals\\\/sine-wave-zion-additive-colour-demo-shadertoy#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Sine Wave Zion - Additive Colour Demo in Shadertoy\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/\",\"name\":\"The Funk Blog\",\"description\":\"The profound rantings of the one like Tom Atkinson... and now art gallery and shop.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-NZ\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/#\\\/schema\\\/person\\\/5e3b3851d9a5cbe644e6ccf190ea10f9\",\"name\":\"tomachi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3852adc4752b8dbdeb5e53ef14d50b7e30d76f3284b8f527c59e1d3efe5b170?s=96&d=monsterid&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3852adc4752b8dbdeb5e53ef14d50b7e30d76f3284b8f527c59e1d3efe5b170?s=96&d=monsterid&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/f3852adc4752b8dbdeb5e53ef14d50b7e30d76f3284b8f527c59e1d3efe5b170?s=96&d=monsterid&r=g\",\"caption\":\"tomachi\"},\"description\":\"Tomachi - drummer, composer, digital media and online marketing guru.\",\"sameAs\":[\"http:\\\/\\\/www.funk.co.nz\\\/\",\"https:\\\/\\\/x.com\\\/tomachi\",\"https:\\\/\\\/www.youtube.com\\\/user\\\/tomachinz\\\/\",\"tomachi\"],\"url\":\"https:\\\/\\\/www.funk.co.nz\\\/blog\\\/author\\\/tomachi\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Sine Wave Zion - Additive Colour Demo in Shadertoy - The Funk Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy","og_locale":"en_US","og_type":"article","og_title":"Sine Wave Zion - Additive Colour Demo in Shadertoy - The Funk Blog","og_description":"Chilin - my African friend asked me if I could make a shader to visualise a sound or sine wave with harmonics included, so I wrote this WebGL shadertoy: In the end, I didn't quite get there but this shader makes a nice illustration of the theory of additive colours, as various offsets of the [&hellip;]","og_url":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy","og_site_name":"The Funk Blog","article_published_time":"2017-03-16T14:47:53+00:00","article_modified_time":"2017-03-16T14:57:46+00:00","author":"tomachi","twitter_misc":{"Written by":"tomachi","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy#article","isPartOf":{"@id":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy"},"author":{"name":"tomachi","@id":"https:\/\/www.funk.co.nz\/blog\/#\/schema\/person\/5e3b3851d9a5cbe644e6ccf190ea10f9"},"headline":"Sine Wave Zion - Additive Colour Demo in Shadertoy","datePublished":"2017-03-16T14:47:53+00:00","dateModified":"2017-03-16T14:57:46+00:00","mainEntityOfPage":{"@id":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy"},"wordCount":172,"keywords":["webgl"],"articleSection":["Visuals"],"inLanguage":"en-NZ"},{"@type":"WebPage","@id":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy","url":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy","name":"Sine Wave Zion - Additive Colour Demo in Shadertoy - The Funk Blog","isPartOf":{"@id":"https:\/\/www.funk.co.nz\/blog\/#website"},"datePublished":"2017-03-16T14:47:53+00:00","dateModified":"2017-03-16T14:57:46+00:00","author":{"@id":"https:\/\/www.funk.co.nz\/blog\/#\/schema\/person\/5e3b3851d9a5cbe644e6ccf190ea10f9"},"breadcrumb":{"@id":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy#breadcrumb"},"inLanguage":"en-NZ","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.funk.co.nz\/blog\/visuals\/sine-wave-zion-additive-colour-demo-shadertoy#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.funk.co.nz\/blog"},{"@type":"ListItem","position":2,"name":"Sine Wave Zion - Additive Colour Demo in Shadertoy"}]},{"@type":"WebSite","@id":"https:\/\/www.funk.co.nz\/blog\/#website","url":"https:\/\/www.funk.co.nz\/blog\/","name":"The Funk Blog","description":"The profound rantings of the one like Tom Atkinson... and now art gallery and shop.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.funk.co.nz\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-NZ"},{"@type":"Person","@id":"https:\/\/www.funk.co.nz\/blog\/#\/schema\/person\/5e3b3851d9a5cbe644e6ccf190ea10f9","name":"tomachi","image":{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/secure.gravatar.com\/avatar\/f3852adc4752b8dbdeb5e53ef14d50b7e30d76f3284b8f527c59e1d3efe5b170?s=96&d=monsterid&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/f3852adc4752b8dbdeb5e53ef14d50b7e30d76f3284b8f527c59e1d3efe5b170?s=96&d=monsterid&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/f3852adc4752b8dbdeb5e53ef14d50b7e30d76f3284b8f527c59e1d3efe5b170?s=96&d=monsterid&r=g","caption":"tomachi"},"description":"Tomachi - drummer, composer, digital media and online marketing guru.","sameAs":["http:\/\/www.funk.co.nz\/","https:\/\/x.com\/tomachi","https:\/\/www.youtube.com\/user\/tomachinz\/","tomachi"],"url":"https:\/\/www.funk.co.nz\/blog\/author\/tomachi"}]}},"_links":{"self":[{"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1559","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/comments?post=1559"}],"version-history":[{"count":0,"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/posts\/1559\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/media?parent=1559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/categories?post=1559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.funk.co.nz\/blog\/wp-json\/wp\/v2\/tags?post=1559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}