Skip to content

Commit ad26404

Browse files
authored
Merge pull request #8446 from nbogie/strands-add-doc-getTexture
add getTexture docs and examples for p5.strands
2 parents 5dbbab9 + a6e5bd3 commit ad26404

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
@@ -742,6 +742,146 @@ if (typeof p5 !== "undefined") {
742742
* }
743743
*/
744744

745+
/**
746+
* Retrieves the current color of a given texture at given coordinates.
747+
*
748+
* The given coordinates should be between [0, 0] representing the top-left of
749+
* the texture, and [1, 1] representing the bottom-right of the texture.
750+
*
751+
* The given texture could be, for example:
752+
* * <a href="#/p5.Image">p5.Image</a>,
753+
* * a <a href="#/p5.Graphics">p5.Graphics</a>, or
754+
* * a <a href="#/p5.Framebuffer">p5.Framebuffer</a>.
755+
*
756+
* The retrieved color that is returned will behave like a vec4, with components
757+
* for red, green, blue, and alpha, each between 0.0 and 1.0.
758+
*
759+
* Linear interpolation is used by default. For Framebuffer sources, you can
760+
* prevent this by creating the buffer with:
761+
* ```js
762+
* createFramebuffer({
763+
* textureFiltering: NEAREST
764+
* })
765+
* ```
766+
* This can be useful if you are using your texture to store data other than color.
767+
* See <a href="#/p5/createFramebuffer/">createFramebuffer</a>.
768+
*
769+
* Note: The `getTexture` function is only available when using p5.strands.
770+
*
771+
* @method getTexture
772+
* @beta
773+
*
774+
* @param texture The texture to sample from.
775+
* (e.g. a p5.Image, p5.Graphics, or p5.Framebuffer).
776+
*
777+
* @param coords The 2D coordinates to sample from.
778+
* This should be between [0,0] (the top-left) and [1,1] (the bottom-right)
779+
* of the texture. It should be compatible with a vec2.
780+
*
781+
* @returns {*} The color of the given texture at the given coordinates. This
782+
* 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.
783+
*
784+
* @example
785+
* <div modernizr='webgl'>
786+
* <code>
787+
* // A filter shader (using p5.strands) which will
788+
* // sample and invert the color of each pixel
789+
* // from the canvas.
790+
* function setup() {
791+
* createCanvas(100, 100, WEBGL);
792+
* let myShader = buildFilterShader(buildIt);
793+
*
794+
* background("white");
795+
* fill("red");
796+
* circle(0, 0, 50);
797+
*
798+
* filter(myShader); //Try commenting this out!
799+
*
800+
* describe("A cyan circle on black background");
801+
* }
802+
*
803+
* function buildIt() {
804+
* filterColor.begin();
805+
*
806+
* //Sample the color of the pixel from the
807+
* //canvas at the same coordinate.
808+
* let c = getTexture(filterColor.canvasContent,
809+
* filterColor.texCoord);
810+
*
811+
* //Make a new color by inverting r, g, and b
812+
* let newColor = [1 - c.r, 1 - c.g, 1 - c.b, c.a];
813+
*
814+
* //Finally, use it for this pixel!
815+
* filterColor.set(newColor);
816+
*
817+
* filterColor.end();
818+
* }
819+
* </code>
820+
*
821+
*
822+
* @example
823+
* <div modernizr='webgl'>
824+
* <code>
825+
* // This primitive edge-detection filter samples
826+
* // and compares the colors of the current pixel
827+
* // on the canvas, and a little to the right.
828+
* // It marks if they differ much.
829+
* let myShader;
830+
*
831+
* function setup() {
832+
* createCanvas(100, 100, WEBGL);
833+
* myShader = buildFilterShader(myShaderBuilder);
834+
* describe("A rough partial outline of a square rotating around a circle");
835+
* }
836+
*
837+
* function draw() {
838+
* drawADesign();
839+
*
840+
* filter(myShader); // try commenting this out
841+
* }
842+
*
843+
* function myShaderBuilder() {
844+
* filterColor.begin();
845+
*
846+
* //The position of the current pixel...
847+
* let coordHere = filterColor.texCoord;
848+
* //and some small amount to the right.
849+
* let coordRight = coordHere + [0.01, 0];
850+
*
851+
* //The canvas content is a texture.
852+
* let cnvTex = filterColor.canvasContent;
853+
*
854+
* //Sample the colors from it at our two positions
855+
* let colorHere = getTexture(cnvTex, coordHere);
856+
* let colorRight = getTexture(cnvTex, coordRight);
857+
*
858+
* // Calculate a (very rough) color difference.
859+
* let difference = length(colorHere - colorRight);
860+
*
861+
* //We'll use a black color by default...
862+
* let resultColor = [0, 0, 0, 1];
863+
* //or white if the samples were different.
864+
* if (difference > 0.3) {
865+
* resultColor = [1, 1, 1, 1];
866+
* }
867+
* filterColor.set(resultColor);
868+
*
869+
* filterColor.end();
870+
* }
871+
*
872+
* //Draw a few shapes, just to test the filter with
873+
* function drawADesign() {
874+
* background(50);
875+
* noStroke();
876+
* lights();
877+
* sphere(20);
878+
* rotate(frameCount / 300);
879+
* square(0, 0, 30);
880+
* }
881+
* </code>
882+
* </div>
883+
*/
884+
745885
/**
746886
* @method getWorldInputs
747887
* @param {Function} callback

src/webgl/material.js

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

0 commit comments

Comments
 (0)