diff --git a/public/assets/mont-photo-low.jpg b/public/assets/mont-photo-low.jpg new file mode 100644 index 0000000000..01a1b98002 Binary files /dev/null and b/public/assets/mont-photo-low.jpg differ diff --git a/src/content/tutorials/en/hello-color-world-p5-strands.mdx b/src/content/tutorials/en/hello-color-world-p5-strands.mdx new file mode 100644 index 0000000000..a951df02f0 --- /dev/null +++ b/src/content/tutorials/en/hello-color-world-p5-strands.mdx @@ -0,0 +1,857 @@ +--- +title: "Hello Color World: p5.strands" +description: An introduction to fragment shaders using p5.strands +category: "2.0" +categoryIndex: 0 +featuredImage: ../images/featured/intro-to-p5-strands.png +featuredImageAlt: " " +relatedContent: + references: + - en/p5/p5shader +authors: + - ilithya +--- + +import { Columns, Column } from "../../../components/Columns"; +import EditableSketch from "../../../components/EditableSketch/index.astro"; +import Callout from "../../../components/Callout/index.astro"; + +## 3 Ways To Work With Color In Shaders + +Let's discover a few techniques for manipulating color using p5.strands, and learn a bit of fragment shader fundamentals in the process. + +We'll explore color in shaders to enhance our p5.js sketches with more sophisticated effects that pure JavaScript wouldn't let us do. + + +It's a new way of writing shaders using JavaScript in p5.js. +- [p5.strands: Writing shaders in JavaScript](https://www.davepagurek.com/blog/writing-shaders-in-js/), blog post by Dave Pagurek +- [p5.strands: Introduction to shaders](/tutorials/intro-to-p5-strands), tutorial by Luke Plowden + + +To understand what advantages using p5.strands brings to us when dealing with shaders, we'll look at each technique through examples with variations of creating a p5 sketch together with p5.strands and comparing them to GLSL. + +Each technique will present a few ideas for using p5.js syntax that you can try in the sketches with p5.strands. This can help you learn how to combine them. + + +A set of instructions implementing algorithms that control how a mesh is rendered into pixels on the screen. In WebGL shaders are written in GLSL, and there are two types: vertex and fragment. +- [Introduction to GLSL](https://p5js.org/tutorials/intro-to-glsl/), tutorial by Dave Pagurek, Austin Lee Slominski, Adam Ferriss + + +Although GLSL can be used alongside JavaScript in a p5.js sketch, it is a separate shader language that shaders require. With p5.strands you can focus only in JavaScript, given that GLSL is managed by p5.js. + +Let's start from simple to complex, like having a three-course meal, by first dipping our toes into an appetizer. + +
+ +### 1. Solid Colors + +We'll begin by simply painting the canvas of our sketch and one 3D primitive. + + +A shader that sets the color of each pixel on the screen, using RGBA values from 0.0 to 1.0. +- [RGBA color](https://hughsk.io/fragment-foundry/chapters/03-rgb.html), self-guided workshop on fragment shaders by Hugh Kennedy +- [RGB color model](https://courses.ilithya.rocks/p/courses/intro-to-shaders/3486602-module-02-color/11410544-07-rgb-color-model), fragment shader course by ilithya + + +With p5.strands we can modify the color of a shader and have the benefit of using several color notations and models that we already know from p5.js, since it translates them for us into the GLSL specific one with hooks (e.g. [`finalColor`](/reference/p5/finalColor/)). + +These hooks allow us to speak GLSL when we use them with familiar p5.js language: + + + + + + + + + + + + + + +
p5.strandsGLSL (fragment)
hook.set(val)gl_FragColor = val
+ + + +We can use p5.js functions like [`color()`](/reference/p5/color) to paint with the shader: +- named color, `color('blue')` +- hex color value, `color('#0000ff')` +- hsl values, `color('hsl(40, 100%, 50%)')` +- RGB 8-bit values, `color(0, 255, 0)` + + + + + +#### sketch with p5.strands + + + + + + +#### sketch with GLSL + + + + + +### 2. Textured Colors +We'll continue evolving our canvas and 3D primitive sketch as we learn more techniques to color with our fragment shaders. Let's add a colored texture to them. + + +An access point into a shader that connects p5.js with GLSL to modify its behavior. +- `Cheat sheet link?` + + +With p5.strands we can apply randomness to a color equation, creating visually smooth organic textures such as clouds, as well as rougher textures found in crystals, by using the [`noise()`](/reference/p5/noise) p5.js function with a hook (e.g. [`pixelInputs`](/reference/p5/pixelInputs/)) instead of implementing GLSL algorithms from scratch. + +We can also add the time elapsed with [`millis()`](/reference/p5/millis) to our textures to simulate natural-looking movements. This value becomes a GLSL uniform, useful to adjust the shader. + +Here are other p5.strands syntax examples we'll use, and their GLSL translations. + + + + + + + + + + + + + + + + + + +
p5.strandsGLSL (fragment)
hook.texCoord varying vec2 uv
hook.colorgl_FragColor
+ + +Experiment with different inputs for the cloud texture: +- static, `noise(uv);` +- animated, `noise(uv + millis() * 0.00015);` +- horizontal mouse movement, `noise(uv + mouseX / width);` +- vertical mouse movement, `noise(uv + mouseY / height);` + + + + + +#### sketch with p5.strands + + + + + + +#### sketch with GLSL + + + + + +### 3. Image Color Filters +Let's add an image to our canvas and 3D primitive to learn how we can create a color filter with adjustments in the fragment shader. To simplify things, we’ll use the same image for both. + + +It's a color distortion that occurs when the colors of light don't focus on the same point. The RGB channels appear to be separated, making the colors of an image appear shifted. +- [Chromatic aberration](https://en.wikipedia.org/wiki/Chromatic_aberration), wikipedia +- [Filter Shader (p5.strands)](/examples/3D-Filter-Shader-p5strands/), example by Dave Pagurek +- [Filter Shader (GLSL)](/examples/3D-Filter-Shader/), example by Dave Pagurek + + +With p5.strands we can customize an image filter to our liking, and create a chromatic aberration effect with hooks (e.g. [`filterColor`](/reference/p5/filterColor/)). We can displace the image colors vertically and horizontally, using the p5.js variables [`mouseX`](/reference/p5/mouseX) and [`mouseY`](/reference/p5/mouseY), which are translated into GLSL uniforms to control the shader. + +We'll add these p5.strands vs. GLSL translations to our knowledge base: + + + + + + + + + + + + + + + + + + + + + + +
p5.strandsGLSL (fragment)
hook.canvasContentuniform sampler2D u_texture
uniformTexture(texture)uniform sampler2D u_texture
getTexture(texture, uv)texture2D(u_texture, uv)
+ + +Explore different offsets, in both axes (X, Y), for the image displacement: +- animated, `noise(uv + millis() * 0.00025)` +- linear animation, `sin(millis() * 0.001) * 0.5` +- centered horizontal mouse movement, `(0.5 - mouseX / width) * 0.5` +- centered vertical mouse movement, `(0.5 - mouseY / height) * 0.5` + + + + + +#### sketch with p5.strands + + + + + + +#### sketch with GLSL + + + + \ No newline at end of file