<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>curiouslyminded TIL</title>
	<subtitle>A feed of the latest TIL posts.</subtitle>
  <description>shaders and code fun ✨</description>
  <language>en-US</language>
	<link href="https://www.curiouslyminded.xyz/feed.xml" rel="self"/>
	<link href="https://www.curiouslyminded.xyz/"/>
	<updated>2021-06-03T00:00:00Z</updated>
	<id>https://www.curiouslyminded.xyz</id>
	<author>
    <name>curiouslyminded</name>
	</author>
	
    
    <entry>
      <title>Radial Uv</title>
      <link href="https://www.curiouslyminded.xyz/til/radial-uv/"/>
      <updated>2021-06-03T00:00:00Z</updated>
      <id>https://www.curiouslyminded.xyz/til/radial-uv/</id>
      <content type="html"><![CDATA[
      <img src="https://www.curiouslyminded.xyz/images/meta/social-share.jpg" alt="Shader artwork from curiouslyminded">   
      <p>During our <a href="https://www.youtube.com/watch?v=gOZo1oDOsNc" target="_blank" rel="external noopener noreferrer">stream</a> with <a href="https://twitter.com/bruno_simon" target="_blank" rel="external noopener noreferrer" title="Bruno's twitter profile">@bruno_simon</a>, we implemented a tube shaped procedural scene that required augmenting GLSL's <em>atan()</em> function a little bit so that we could play with our uv's on a radial plane.</p>
<p>Using two coordinate parameters, commonly <em>x</em> and <em>y</em>, the GLSL function <em>atan()</em> allows us to access an angle for each pixel position.</p>
<p>From there, Bruno constructed a larger function around it, adding a few calculations in order to create a near perfect tubural form that creates a really satisfying sense of depth.</p>
<p>With one caveat — there's a visible edge demarcating the start and end of the radial shape.</p>
<p>ilithya found a solution for this by adding a mirroring effect to Bruno's function. By applying GLSL's <em>abs()</em> function on the angle, she accessed half the radial circle and fliiped it, hidding that pesky line in the process.</p>
<p>Teamwork makes the dream work!</p>
<pre class="language-glsl"><code class="language-glsl"><span class="token comment">/* 
* RADIAL UV
* - authored by @bruno_simon -
* (uv) transforms uv
* returns => radial uv
*/</span>

<span class="token keyword">vec2</span> <span class="token function">getRadialUv</span><span class="token punctuation">(</span><span class="token keyword">vec2</span> uv<span class="token punctuation">)</span> <span class="token punctuation">{</span>
	<span class="token keyword">float</span> angle <span class="token operator">=</span> <span class="token function">atan</span><span class="token punctuation">(</span>uv<span class="token punctuation">.</span>x<span class="token punctuation">,</span> <span class="token operator">-</span>uv<span class="token punctuation">.</span>y<span class="token punctuation">)</span><span class="token punctuation">;</span>
	angle <span class="token operator">=</span> <span class="token function">abs</span><span class="token punctuation">(</span>angle<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">vec2</span> radialUv <span class="token operator">=</span> <span class="token keyword">vec2</span><span class="token punctuation">(</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	radialUv<span class="token punctuation">.</span>x <span class="token operator">=</span> angle <span class="token operator">/</span> <span class="token punctuation">(</span>PI <span class="token operator">*</span> <span class="token number">2.0</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token number">0.5</span><span class="token punctuation">;</span>
	radialUv<span class="token punctuation">.</span>y <span class="token operator">=</span> <span class="token number">1.0</span> <span class="token operator">-</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token number">1.0</span> <span class="token operator">-</span> <span class="token function">length</span><span class="token punctuation">(</span>uv<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">4.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	
	<span class="token keyword">return</span> radialUv<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<div 
    class="codepen" 
    data-prefill='{
    "title": "#curiouslyminded",
    "tags": ["curiouslyminded"],
    "scripts": ["https://cdnjs.cloudflare.com/ajax/libs/three.js/r124/three.min.js"]
    }'
    data-height="500" 
    data-theme-id="1"
    data-default-tab="result"
    data-editable="false"     
>
<pre data-lang="html">
&lt;!--
 * #curiouslyminded
 * 
 * Follow:
 * https://www.twitch.tv/curiouslyminded
 * https://www.youtube.com/curiouslyminded
--&gt;
<p>&lt;div id=&quot;example&quot;&gt;&lt;/div&gt;
&lt;script id=&quot;vertex&quot; type=&quot;x-shader/x-vertex&quot;&gt;
void main() { gl_Position = vec4(position, 1.0); }
&lt;/script&gt;
&lt;script id=&quot;fragment&quot; type=&quot;x-shader/x-vertex&quot;&gt;
precision mediump float;
#define PI 3.14159265359
uniform vec2 u_resolution;
uniform float u_time;</p>
<p>// RADIAL UV
// authored by @bruno_simon
// (uv) transform uv to radial uv
vec2 getRadialUv(vec2 uv) {
float angle = atan(uv.x, -uv.y);
angle = abs(angle);</p>
<p>vec2 radialUv = vec2(0.0);
radialUv.x = angle / (PI * 2.0) + 0.5;
radialUv.y = 1.0 - pow(1.0 - length(uv), 4.0);</p>
<p>return radialUv;
}</p>
<p>void main() {
vec2 uv = (gl_FragCoord.xy - u_resolution * 0.5) / u_resolution.yy;
vec3 color = vec3(0.0);
vec2 radialUv = getRadialUv(uv);
color = vec3(radialUv, 0.5);
gl_FragColor = vec4(color, 1.0);
}
&lt;/script&gt;
</pre></p>
<pre data-lang="css" data-options-autoprefixer="true">
* {
user-select: none;
}

body {
height: 100vh;
background-color: black;
margin: 0;
padding: 0;
overflow: hidden;
position: relative;
}
</pre>
<pre data-lang="js">
/* 
 * #curiouslyminded
 *
 * This JS is an adapted template from the https://thebookofshaders.com/
 */

let camera, scene, renderer, clock;
let uniforms;

function init() {
	const container = document.getElementById("example");

	clock = new THREE.Clock();
	camera = new THREE.Camera();
	camera.position.z = 1;

	scene = new THREE.Scene();

	const geometry = new THREE.PlaneBufferGeometry(2, 2);

	uniforms = {
		u_time: { type: "f", value: 1.0 },
		u_resolution: { type: "v2", value: new THREE.Vector2() },
	};

	const material = new THREE.ShaderMaterial({
		uniforms,
		vertexShader: document.getElementById("vertex").textContent,
		fragmentShader: document.getElementById("fragment").textContent
	});

	const mesh = new THREE.Mesh(geometry, material);
	scene.add(mesh);

	renderer = new THREE.WebGLRenderer();
	renderer.setPixelRatio(window.devicePixelRatio);

	container.appendChild(renderer.domElement);

	onWindowResize();
	window.addEventListener("resize", onWindowResize);
}

function onWindowResize() {
	renderer.setSize(window.innerWidth, window.innerHeight);
	uniforms.u_resolution.value.x = renderer.domElement.width;
	uniforms.u_resolution.value.y = renderer.domElement.height;
}

function render() {
	uniforms.u_time.value = clock.getElapsedTime();
	renderer.render(scene, camera);
}

function animate() {
	render();
	requestAnimationFrame(animate);
}

init();
animate();
</pre>
</div>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
    ]]></content>
    </entry>
	
    
    <entry>
      <title>Smooth Mod</title>
      <link href="https://www.curiouslyminded.xyz/til/smooth-mod/"/>
      <updated>2021-03-26T00:00:00Z</updated>
      <id>https://www.curiouslyminded.xyz/til/smooth-mod/</id>
      <content type="html"><![CDATA[
      <img src="https://www.curiouslyminded.xyz/images/meta/social-share.jpg" alt="Shader artwork from curiouslyminded">   
      <p>The GLSL function <em>mod()</em> creates a saw tooth shape — think pointy edges repeating over and over.</p>
<p>But what if you want a little more control over the sharpness of the tip of each saw ridge? Enter <em>smoothMod()</em>. When <a href="https://www.instagram.com/charstiles/" target="_blank" rel="external noopener noreferrer" title="Char's instagram profile">@charstiles</a> came on
<a href="https://www.youtube.com/watch?v=11XuUX8QVmA/" target="_blank" rel="external noopener noreferrer">the stream</a>, she showed us her riff on the idea with the below function.</p>
<p>If you've worked with CSS, it might also be helpful to think of this like you would the <em>border-radius</em> property.</p>
<p>In the CodePen example below, we've commented out an implementation of the regular <em>mod()</em> function so that you can compare the two.</p>
<pre class="language-glsl"><code class="language-glsl"><span class="token comment">/* 
* SMOOTH MOD
* - authored by @charstiles -
* based on https://math.stackexchange.com/questions/2491494/does-there-exist-a-smooth-approximation-of-x-bmod-y
* (axis) input axis to modify
* (amp) amplitude of each edge/tip
* (rad) radius of each edge/tip
* returns => smooth edges
*/</span>

<span class="token keyword">float</span> <span class="token function">smoothMod</span><span class="token punctuation">(</span><span class="token keyword">float</span> axis<span class="token punctuation">,</span> <span class="token keyword">float</span> amp<span class="token punctuation">,</span> <span class="token keyword">float</span> rad<span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">float</span> top <span class="token operator">=</span> <span class="token function">cos</span><span class="token punctuation">(</span>PI <span class="token operator">*</span> <span class="token punctuation">(</span>axis <span class="token operator">/</span> amp<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token function">sin</span><span class="token punctuation">(</span>PI <span class="token operator">*</span> <span class="token punctuation">(</span>axis <span class="token operator">/</span> amp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">float</span> bottom <span class="token operator">=</span> <span class="token function">pow</span><span class="token punctuation">(</span><span class="token function">sin</span><span class="token punctuation">(</span>PI <span class="token operator">*</span> <span class="token punctuation">(</span>axis <span class="token operator">/</span> amp<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token function">pow</span><span class="token punctuation">(</span>rad<span class="token punctuation">,</span> <span class="token number">2.0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">float</span> at <span class="token operator">=</span> <span class="token function">atan</span><span class="token punctuation">(</span>top <span class="token operator">/</span> bottom<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> amp <span class="token operator">*</span> <span class="token punctuation">(</span><span class="token number">1.0</span> <span class="token operator">/</span> <span class="token number">2.0</span><span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token punctuation">(</span><span class="token number">1.0</span> <span class="token operator">/</span> PI<span class="token punctuation">)</span> <span class="token operator">*</span> at<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<div 
    class="codepen" 
    data-prefill='{
    "title": "#curiouslyminded",
    "tags": ["curiouslyminded"],
    "scripts": ["https://cdnjs.cloudflare.com/ajax/libs/three.js/r124/three.min.js"]
    }'
    data-height="500" 
    data-theme-id="1"
    data-default-tab="result"
    data-editable="false"     
>
<pre data-lang="html">
&lt;!--
 * #curiouslyminded
 * 
 * Follow:
 * https://www.twitch.tv/curiouslyminded
 * https://www.youtube.com/curiouslyminded
--&gt;
<p>&lt;div id=&quot;example&quot;&gt;&lt;/div&gt;
&lt;script id=&quot;vertex&quot; type=&quot;x-shader/x-vertex&quot;&gt;
void main() { gl_Position = vec4(position, 1.0); }
&lt;/script&gt;
&lt;script id=&quot;fragment&quot; type=&quot;x-shader/x-vertex&quot;&gt;
precision mediump float;
#define PI 3.14159265359
uniform vec2 u_resolution;
uniform float u_time;</p>
<p>// SMOOTH MOD
// authored by @charstiles
// based on https://math.stackexchange.com/questions/2491494/does-there-exist-a-smooth-approximation-of-x-bmod-y
// (axis) input axis to modify
// (amp) amplitude of each edge/tip
// (rad) radius of each edge/tip
float smoothMod(float axis, float amp, float rad){
float top = cos(PI * (axis / amp)) * sin(PI * (axis / amp));
float bottom = pow(sin(PI * (axis / amp)), 2.0) + pow(rad, 2.0);
float at = atan(top / bottom);
return amp * (1.0 / 2.0) - (1.0 / PI) * at;
}</p>
<p>void main() {
vec2 uv = (gl_FragCoord.xy - u_resolution * .5) / u_resolution.yy + 0.5;
vec3 color = vec3(0.);
// loop time
float t = sin(u_time);
// regular mod
float xMod = mod(uv.x, 0.5);
// smoothMod
float xSmoothMod = smoothMod(uv.x, 0.5, 0.25);<br>
// uv.x = mix(uv.x, xMod, t);
uv.x = mix(uv.x, xSmoothMod, t);
color += step(uv.y, uv.x);
gl_FragColor = vec4(color, 1.0);
}
&lt;/script&gt;
</pre></p>
<pre data-lang="css" data-options-autoprefixer="true">
* {
user-select: none;
}

body {
height: 100vh;
background-color: black;
margin: 0;
padding: 0;
overflow: hidden;
position: relative;
}
</pre>
<pre data-lang="js">
/* 
 * #curiouslyminded
 *
 * This JS is an adapted template from the https://thebookofshaders.com/
 */

let camera, scene, renderer, clock;
let uniforms;

function init() {
	const container = document.getElementById("example");

	clock = new THREE.Clock();
	camera = new THREE.Camera();
	camera.position.z = 1;

	scene = new THREE.Scene();

	const geometry = new THREE.PlaneBufferGeometry(2, 2);

	uniforms = {
		u_time: { type: "f", value: 1.0 },
		u_resolution: { type: "v2", value: new THREE.Vector2() },
	};

	const material = new THREE.ShaderMaterial({
		uniforms,
		vertexShader: document.getElementById("vertex").textContent,
		fragmentShader: document.getElementById("fragment").textContent
	});

	const mesh = new THREE.Mesh(geometry, material);
	scene.add(mesh);

	renderer = new THREE.WebGLRenderer();
	renderer.setPixelRatio(window.devicePixelRatio);

	container.appendChild(renderer.domElement);

	onWindowResize();
	window.addEventListener("resize", onWindowResize);
}

function onWindowResize() {
	renderer.setSize(window.innerWidth, window.innerHeight);
	uniforms.u_resolution.value.x = renderer.domElement.width;
	uniforms.u_resolution.value.y = renderer.domElement.height;
}

function render() {
	uniforms.u_time.value = clock.getElapsedTime();
	renderer.render(scene, camera);
}

function animate() {
	render();
	requestAnimationFrame(animate);
}

init();
animate();
</pre>
</div>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
    ]]></content>
    </entry>
	
    
    <entry>
      <title>Wiggly Noise</title>
      <link href="https://www.curiouslyminded.xyz/til/wiggly-noise/"/>
      <updated>2021-02-25T00:00:00Z</updated>
      <id>https://www.curiouslyminded.xyz/til/wiggly-noise/</id>
      <content type="html"><![CDATA[
      <img src="https://www.curiouslyminded.xyz/images/meta/social-share.jpg" alt="Shader artwork from curiouslyminded">   
      <p>While trying to implement noise on a 2D shape, our experiments led us to create our first community generated function!</p>
<p>Big thanks to <a href="https://www.instagram.com/mimetikmusic/" target="_blank" rel="external noopener noreferrer" title="Alessandro's instagram profile">@mimetikmusic</a> for helping us tease out the math needed to apply some noise to the edges of our shape function (aka distance function) in order to create some satisfyingly wiggly borders.</p>
<p>Henceforth known as the wiggly noise effect 🔥🔥🔥 — here is the function:</p>
<pre class="language-glsl"><code class="language-glsl"><span class="token comment">/* 
* WIGGLY
* - authored with the math help by @mimetikmusic -
* (cx) centerPoint in the x axis
* (cy) centerPoint in the y axis
* (freq) frequency of the wobble
* (mag) magnitude of the wobble
* (sp) spread of the wobble 
* returns => wiggly noise
*/</span>

<span class="token keyword">float</span> <span class="token function">wiggly</span><span class="token punctuation">(</span><span class="token keyword">float</span> cx<span class="token punctuation">,</span> <span class="token keyword">float</span> cy<span class="token punctuation">,</span> <span class="token keyword">float</span> mag<span class="token punctuation">,</span> <span class="token keyword">float</span> freq<span class="token punctuation">,</span> <span class="token keyword">float</span> sp<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">float</span> w <span class="token operator">=</span> <span class="token function">sin</span><span class="token punctuation">(</span>cx <span class="token operator">*</span> mag <span class="token operator">*</span> freq <span class="token operator">*</span> PI<span class="token punctuation">)</span> <span class="token operator">*</span> <span class="token function">cos</span><span class="token punctuation">(</span>cy <span class="token operator">*</span> mag <span class="token operator">*</span> freq <span class="token operator">*</span> PI<span class="token punctuation">)</span> <span class="token operator">*</span> sp<span class="token punctuation">;</span>
    <span class="token keyword">return</span> w<span class="token punctuation">;</span>
<span class="token punctuation">}</span></code></pre>
<div 
    class="codepen" 
    data-prefill='{
    "title": "#curiouslyminded",
    "tags": ["curiouslyminded"],
    "scripts": ["https://cdnjs.cloudflare.com/ajax/libs/three.js/r124/three.min.js"]
    }'
    data-height="500" 
    data-theme-id="1"
    data-default-tab="result"
    data-editable="false"     
>
<pre data-lang="html">
&lt;!--
 * #curiouslyminded
 * 
 * Follow:
 * https://www.twitch.tv/curiouslyminded
 * https://www.youtube.com/curiouslyminded
--&gt;
<p>&lt;div id=&quot;example&quot;&gt;&lt;/div&gt;
&lt;script id=&quot;vertex&quot; type=&quot;x-shader/x-vertex&quot;&gt;
void main() { gl_Position = vec4(position, 1.0); }
&lt;/script&gt;
&lt;script id=&quot;fragment&quot; type=&quot;x-shader/x-vertex&quot;&gt;
precision mediump float;
#define PI 3.14159265359
uniform vec2 u_resolution;
uniform float u_time;</p>
<p>// WIGGLY
// authored with the math help by @mimetikmusic - https://www.instagram.com/mimetikmusic/
// (cx) centerPoint in the x axis
// (cy) centerPoint in the y axis
// (freq) frequency of the wobble
// (mag) magnitude of the wobble
// (sp) spread of the wobble
float wiggly(float cx, float cy, float mag, float freq, float sp) {
float w = sin(cx * mag * freq * PI) * cos(cy * mag * freq * PI) * sp;
return w;
}</p>
<p>void main() {
vec2 uv = (gl_FragCoord.xy - u_resolution * .5) / u_resolution.yy + 0.5;
float circle = distance(uv, vec2(0.5));
circle += wiggly(
uv.x + u_time * 0.05,
uv.y - u_time * 0.05,
3.,
6.,
0.005
);
circle = step(0.02, abs(circle - 0.3));
vec3 color = vec3(1.0-circle);
gl_FragColor = vec4(color, 1.0);
}
&lt;/script&gt;
</pre></p>
<pre data-lang="css" data-options-autoprefixer="true">
* {
user-select: none;
}

body {
height: 100vh;
background-color: black;
margin: 0;
padding: 0;
overflow: hidden;
position: relative;
}
</pre>
<pre data-lang="js">
/* 
 * #curiouslyminded
 *
 * This JS is an adapted template from the https://thebookofshaders.com/
 */

let camera, scene, renderer, clock;
let uniforms;

function init() {
	const container = document.getElementById("example");

	clock = new THREE.Clock();
	camera = new THREE.Camera();
	camera.position.z = 1;

	scene = new THREE.Scene();

	const geometry = new THREE.PlaneBufferGeometry(2, 2);

	uniforms = {
		u_time: { type: "f", value: 1.0 },
		u_resolution: { type: "v2", value: new THREE.Vector2() },
	};

	const material = new THREE.ShaderMaterial({
		uniforms,
		vertexShader: document.getElementById("vertex").textContent,
		fragmentShader: document.getElementById("fragment").textContent
	});

	const mesh = new THREE.Mesh(geometry, material);
	scene.add(mesh);

	renderer = new THREE.WebGLRenderer();
	renderer.setPixelRatio(window.devicePixelRatio);

	container.appendChild(renderer.domElement);

	onWindowResize();
	window.addEventListener("resize", onWindowResize);
}

function onWindowResize() {
	renderer.setSize(window.innerWidth, window.innerHeight);
	uniforms.u_resolution.value.x = renderer.domElement.width;
	uniforms.u_resolution.value.y = renderer.domElement.height;
}

function render() {
	uniforms.u_time.value = clock.getElapsedTime();
	renderer.render(scene, camera);
}

function animate() {
	render();
	requestAnimationFrame(animate);
}

init();
animate();
</pre>
</div>
<script async src="https://static.codepen.io/assets/embed/ei.js"></script>
    ]]></content>
    </entry>
	
</feed>
