Skip to content

Commit 082e85a

Browse files
committed
Add getTexture docs and 2 examples (using flat api)
link getTexture docs from buildFilterShader
1 parent 626996e commit 082e85a

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

src/strands/p5.strands.js

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,146 @@ if (typeof p5 !== "undefined") {
680680
* </div>
681681
*/
682682

683+
/**
684+
* Retrieves the current color of a given texture at given coordinates.
685+
*
686+
* The given coordinates should be between [0, 0] representing the top-left of
687+
* the texture, and [1, 1] representing the bottom-right of the texture.
688+
*
689+
* The given texture could be, for example:
690+
* * <a href="#/p5.Image">p5.Image</a>,
691+
* * a <a href="#/p5.Graphics">p5.Graphics</a>, or
692+
* * a <a href="#/p5.Framebuffer">p5.Framebuffer</a>.
693+
*
694+
* The retrieved color that is returned will behave like a vec4, with components
695+
* for red, green, blue, and alpha, each between 0.0 and 1.0.
696+
*
697+
* Linear interpolation is used by default. For Framebuffer sources, you can
698+
* prevent this by creating the buffer with:
699+
* ```js
700+
* createFramebuffer({
701+
* textureFiltering: NEAREST
702+
* })
703+
* ```
704+
* This can be useful if you are using your texture to store data other than color.
705+
* See <a href="#/p5/createFramebuffer/">createFramebuffer</a>.
706+
*
707+
* Note: The `getTexture` function is only available when using p5.strands.
708+
*
709+
* @method getTexture
710+
* @beta
711+
*
712+
* @param {*} texture The texture to sample from.
713+
* (e.g. a p5.Image, p5.Graphics, or p5.Framebuffer).
714+
*
715+
* @param {*} coords The 2D coordinates to sample from.
716+
* This should be between [0,0] (the top-left) and [1,1] (the bottom-right)
717+
* of the texture. It should be compatible with a vec2.
718+
*
719+
* @returns {*} The color of the given texture at the given coordinates. This
720+
* will behave as a vec4 holding components r, g, b, and a (alpha), with each component being in the range 0.0 to 1.0.
721+
*
722+
* @example
723+
* <div modernizr='webgl'>
724+
* <code>
725+
* // A filter shader (using p5.strands) which will
726+
* // sample and invert the color of each pixel
727+
* // from the canvas.
728+
* function setup() {
729+
* createCanvas(100, 100, WEBGL);
730+
* let myShader = buildFilterShader(buildIt);
731+
*
732+
* background("white");
733+
* fill("red");
734+
* circle(0, 0, 50);
735+
*
736+
* filter(myShader); //Try commenting this out!
737+
*
738+
* describe("A cyan circle on black background");
739+
* }
740+
*
741+
* function buildIt() {
742+
* filterColor.begin();
743+
*
744+
* //Sample the color of the pixel from the
745+
* //canvas at the same coordinate.
746+
* let c = getTexture(filterColor.canvasContent,
747+
* filterColor.texCoord);
748+
*
749+
* //Make a new color by inverting r, g, and b
750+
* let newColor = [1 - c.r, 1 - c.g, 1 - c.b, c.a];
751+
*
752+
* //Finally, use it for this pixel!
753+
* filterColor.set(newColor);
754+
*
755+
* filterColor.end();
756+
* }
757+
* </code>
758+
*
759+
*
760+
* @example
761+
* <div modernizr='webgl'>
762+
* <code>
763+
* // This primitive edge-detection filter samples
764+
* // and compares the colors of the current pixel
765+
* // on the canvas, and a little to the right.
766+
* // It marks if they differ much.
767+
* let myShader;
768+
*
769+
* function setup() {
770+
* createCanvas(100, 100, WEBGL);
771+
* myShader = buildFilterShader(myShaderBuilder);
772+
* describe("A rough partial outline of a square rotating around a circle");
773+
* }
774+
*
775+
* function draw() {
776+
* drawADesign();
777+
*
778+
* filter(myShader); // try commenting this out
779+
* }
780+
*
781+
* function myShaderBuilder() {
782+
* filterColor.begin();
783+
*
784+
* //The position of the current pixel...
785+
* let coordHere = filterColor.texCoord;
786+
* //and some small amount to the right.
787+
* let coordRight = coordHere + [0.01, 0];
788+
*
789+
* //The canvas content is a texture.
790+
* let cnvTex = filterColor.canvasContent;
791+
*
792+
* //Sample the colors from it at our two positions
793+
* let colorHere = getTexture(cnvTex, coordHere);
794+
* let colorRight = getTexture(cnvTex, coordRight);
795+
*
796+
* // Calculate a (very rough) color difference.
797+
* let difference = length(colorHere - colorRight);
798+
*
799+
* //We'll use a black color by default...
800+
* let resultColor = [0, 0, 0, 1];
801+
* //or white if the samples were different.
802+
* if (difference > 0.3) {
803+
* resultColor = [1, 1, 1, 1];
804+
* }
805+
* filterColor.set(resultColor);
806+
*
807+
* filterColor.end();
808+
* }
809+
*
810+
* //Draw a few shapes, just to test the filter with
811+
* function drawADesign() {
812+
* background(50);
813+
* noStroke();
814+
* lights();
815+
* sphere(20);
816+
* rotate(frameCount / 300);
817+
* square(0, 0, 30);
818+
* }
819+
* </code>
820+
* </div>
821+
*/
822+
683823
/**
684824
* @method getWorldInputs
685825
* @param {Function} callback

src/webgl/material.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ function material(p5, fn) {
562562
* In your function, you can use <a href="#/p5/filterColor">`filterColor`</a> with a function
563563
* that will be called for each pixel on the image to determine its final color. You can
564564
* read the color of the current pixel with `getTexture(canvasContent, coord)`.
565+
* See <a href="#/p5/getTexture">getTexture()</a>.
565566
*
566567
* ```js example
567568
* async function setup() {

0 commit comments

Comments
 (0)