diff --git a/hydra/how.html b/hydra/how.html new file mode 100644 index 0000000..e9d3494 --- /dev/null +++ b/hydra/how.html @@ -0,0 +1,325 @@ + + + + + 🌱 how hydra works + + + +

🌱 how hydra works

+

+ hydra is a fascinating visual live + coding language, but how does it work? here's an example from Olivia Jack: +

+
+// licensed with CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/
+// by Olivia Jack
+// https://ojack.github.io
+
+osc(100, 0.01, 1.4)
+  .rotate(0, 0.1)
+  .mult(osc(10, 0.1).modulate(osc(10).rotate(0, -0.1), 1))
+  .color(2.83,0.91,0.39)
+  .out(o0)
+    
+

+ I've heard that hydra translates the JS above into a GLSL shader. I've dug + around a bit and found GlslSource.prototype.compile to be the part of the + code where the code is rendered. To look at the generated code, you can + set a breakpoint after the assignment to shaderInfo. The interesting bit + is in shaderInfo.fragColor: +

+
+color(mult(osc(rotate(st, 0., 0.1), 100., 0.01, 1.4), osc(modulate(st, osc(rotate(st, 0., -0.1), 10., 0.1, 0.), 1.), 10., 0.1, 0.), 1.), 2.83, 0.91, 0.39, 1.)
+    
+

Here's the same code, but formatted and commented:

+
+color(
+  mult(
+    osc(
+      rotate(st, 0., 0.1), // vec2 _st
+      100., // float frequency
+      0.01, // float sync
+      1.4 // float offset
+    ), // vec4 _c0
+    osc(
+      modulate(
+        st, // vec2 _st
+        osc(
+          rotate(st, 0., -0.1), // vec2 _st
+          10., // float frequency
+          0.1, // float sync
+          0. // float offset
+        ), // vec4 _c0
+        1. // float amount
+      ), // vec2 _st
+      10., // float frequency
+      0.1, // float sync
+      0. // float offset
+    ), // vec4 _c1
+    1. // float amount
+  ), // vec4 _c0
+  2.83, // float r
+  0.91, // float g
+  0.39, // float b
+  1. // float a
+)
+    
+

+ To run the code, we need all the GLSL function definitions (color, mult, + osc, rotate, modulate) and the variable st, which is the standard vector. + In the hydra source code, these can be found in glsl-functions.js. I've + converted the JS/GLSL mixture to just a giant blob of GLSL definitions. + With these in place, we can embed the fragColor calculation into a + shadertoy style mainImage function: +

+ + 💡 hit ctrl+enter to update the code. + hide + +

+ Now we have successfully converted the hydra code to a shadertoy style + fragment shader (You can paste this into + shadertoy). + The shader rendering on this page works identical to + real-shaders. +

+

+ Next, it would be interesting to see how to convert the hydra code + automatically into GLSL.. +

+
+ show page source +

+    
+

+ back to garten.salat +

+ + + diff --git a/index.html b/index.html index e11b2f7..d11c3ec 100644 --- a/index.html +++ b/index.html @@ -22,6 +22,9 @@

🌱 garten.salat.dev

+

day 16:
+ how hydra works +

day 15:
pixelfont editor: jgs7