UNPKG

p5

Version:

[![npm version](https://badge.fury.io/js/p5.svg)](https://www.npmjs.com/package/p5)

813 lines (812 loc) • 23 kB
import { BaseType } from "./ir_types.js"; import { userError } from "./strands_FES.js"; import { T as createStrandsNode, d as matrixNode, u as matrixConstructorNode } from "../ir_builders-CVT9KlJB.js"; //#region src/strands/strands_transform.js /** * @module 3D * @submodule p5.strands * @for p5 */ function installTransformAPI(p5, fn, strandsContext, augmentFn) { const matrixConstructors = [ ["mat2", 2], ["mat2x2", 2], ["transform2D", 3], ["mat3", 3], ["mat3x3", 3], ["transform3D", 4], ["mat4", 4], ["mat4x4", 4] ]; const markTransformsExperimental = () => strandsContext.experimentalFeaturesUsed.add("p5.strands.transforms"); /** * Creates a 2D transform inside a p5.strands shader callback. * * A transform is a 3x3 matrix that stores a combination of moves, turns, * scales, and slants. Calling `transform2D()` without arguments returns the * identity transform, which leaves points exactly where they are. * * A transform is built up by passing it to * <a href="#/p5/translate">translate()</a>, * <a href="#/p5/rotate">rotate()</a>, * <a href="#/p5/scale">scale()</a>, * <a href="#/p5/shearX">shearX()</a>, and * <a href="#/p5/shearY">shearY()</a>. Each of those returns a new transform * instead of changing the one passed in, so the result has to be assigned * back, as in `t = translate(t, 20, 0)`. * * The finished transform is applied to a position with * <a href="#/p5/transformPoint">transformPoint()</a>. * * `transform2D()` is the same function as <a href="#/p5/mat3">mat3()</a>. * Use `transform2D()` when describing a transformation, and `mat3()` when * thinking of the value as a plain 3x3 matrix. * * It can also be called with a single number, which fills the diagonal of the * matrix; a single matrix, which is resized (a 4x4 keeps its upper-left 3x3 * block); 3 column vectors; or 9 values in column-major order. * * Note: `transform2D()` can only be used inside a p5.strands shader callback. * * @method transform2D * @beta * @param {...Number} [values] nothing for the identity transform, one number, * one matrix, 3 column vectors, or 9 values in * column-major order. * @returns {*} a 3x3 transform. * * @example * let myShader; * * function setup() { * createCanvas(200, 200, WEBGL); * myShader = buildFilterShader(spin); * } * * function spin() { * filterColor.begin(); * // Turn the texture coordinates around the middle of the canvas. * let t = transform2D(); * t = translate(t, 0.5, 0.5); * t = rotate(t, PI / 6); * t = translate(t, -0.5, -0.5); * let uv = transformPoint(t, filterColor.texCoord); * filterColor.set(getTexture(filterColor.canvasContent, uv)); * filterColor.end(); * } * * function draw() { * background(180); * noStroke(); * fill('yellow'); * rect(-50, -20, 100, 40); * filter(myShader); * } */ /** * Creates a 3D transform inside a p5.strands shader callback. * * A transform is a 4x4 matrix that stores a combination of moves, turns, and * scales in three dimensions. Calling `transform3D()` without arguments * returns the identity transform, which leaves points exactly where they are. * * A transform is built up by passing it to * <a href="#/p5/translate">translate()</a>, * <a href="#/p5/rotate">rotate()</a>, * <a href="#/p5/rotateX">rotateX()</a>, * <a href="#/p5/rotateY">rotateY()</a>, * <a href="#/p5/rotateZ">rotateZ()</a>, * <a href="#/p5/rotateAxisAngle">rotateAxisAngle()</a>, and * <a href="#/p5/scale">scale()</a>. Each of those returns a new transform * instead of changing the one passed in, so the result has to be assigned * back, as in `t = translate(t, 20, 0, 0)`. * * The finished transform is applied to a position with * <a href="#/p5/transformPoint">transformPoint()</a>, and to a surface * direction with <a href="#/p5/transformNormal">transformNormal()</a>. This * is the usual way to place each copy of a shape when drawing many instances * at once with <a href="#/p5/instances">instances()</a> or * <a href="#/p5/model">model()</a>. * * `transform3D()` is the same function as <a href="#/p5/mat4">mat4()</a>. * Use `transform3D()` when describing a transformation, and `mat4()` when * thinking of the value as a plain 4x4 matrix. * * It can also be called with a single number, which fills the diagonal of the * matrix; a single matrix, which is resized (a 3x3 is extended with the * identity matrix); 4 column vectors; or 16 values in column-major order. * * Note: `transform3D()` can only be used inside a p5.strands shader callback. * * @method transform3D * @beta * @param {...Number} [values] nothing for the identity transform, one number, * one matrix, 4 column vectors, or 16 values in * column-major order. * @returns {*} a 4x4 transform. * * @example * let myShader; * * function setup() { * createCanvas(200, 200, WEBGL); * myShader = buildMaterialShader(wobble); * } * * function wobble() { * worldInputs.begin(); * let t = transform3D(); * t = rotate(t, millis() * 0.001); * t = scale(t, 1.2); * worldInputs.position = transformPoint(t, worldInputs.position); * worldInputs.normal = transformNormal(t, worldInputs.normal); * worldInputs.end(); * } * * function draw() { * background(180); * lights(); * noStroke(); * fill('red'); * shader(myShader); * box(60); * } * * @example * // Place each instance of a shape with its own transform. * let myShader; * let count = 5; * * function setup() { * createCanvas(200, 200, WEBGL); * myShader = buildMaterialShader(placeEach); * describe('Five red spheres in a row, growing from small to large.'); * } * * function placeEach() { * worldInputs.begin(); * // Give every instance its own spot and size. * let spacing = width / count; * let t = transform3D(); * t = translate(t, (instanceIndex - (count - 1) / 2) * spacing, 0, 0); * t = scale(t, 0.4 + instanceIndex * 0.15); * worldInputs.position = transformPoint(t, worldInputs.position); * worldInputs.normal = transformNormal(t, worldInputs.normal); * worldInputs.end(); * } * * function draw() { * background(220); * lights(); * noStroke(); * fill('red'); * shader(myShader); * instances(count).sphere(20); * } */ /** * Creates a 2x2 matrix inside a p5.strands shader callback. * * A 2x2 matrix can rotate, scale, and slant a two-component vector, but it * has no room to store a move, so there is no `transform` alias for it. Use * <a href="#/p5/transform2D">transform2D()</a> to also move points around. * * Called without arguments it returns the identity matrix. It can also be * called with a single number, which fills the diagonal; a single matrix, * which is resized; 2 column vectors; or 4 values in column-major order. * * Note: `mat2()` can only be used inside a p5.strands shader callback. * * @method mat2 * @beta * @param {...Number} [values] nothing for the identity matrix, one number, * one matrix, 2 column vectors, or 4 values in * column-major order. * @returns {*} a 2x2 matrix. */ /** * A GLSL-style name for <a href="#/p5/mat2">mat2()</a>. * * Note: `mat2x2()` can only be used inside a p5.strands shader callback. * * @method mat2x2 * @beta * @param {...Number} [values] the same values accepted by * <a href="#/p5/mat2">mat2()</a>. * @returns {*} a 2x2 matrix. */ /** * Creates a 3x3 matrix inside a p5.strands shader callback. * * `mat3()` is the same function as * <a href="#/p5/transform2D">transform2D()</a>, named the way it is in GLSL. * * Passing a 4x4 matrix to `mat3()` keeps only its upper-left 3x3 block, which * drops the move part of a 3D transform and leaves the rotation and scale. * That block is what lighting calculations need, so this is a handy way to * turn a <a href="#/p5/transform3D">transform3D()</a> into a matrix for * surface directions. * * Note: `mat3()` can only be used inside a p5.strands shader callback. * * @method mat3 * @beta * @param {...Number} [values] nothing for the identity matrix, one number, * one matrix, 3 column vectors, or 9 values in * column-major order. * @returns {*} a 3x3 matrix. * * @example * let myShader; * * function setup() { * createCanvas(200, 200, WEBGL); * myShader = buildMaterialShader(shrink); * } * * function shrink() { * worldInputs.begin(); * let t = transform3D(); * t = translate(t, 30, 0, 0); * t = rotateY(t, PI / 4); * // mat3() drops the move, so only the turn is left. * let turnOnly = mat3(t); * worldInputs.position = turnOnly * worldInputs.position; * worldInputs.end(); * } * * function draw() { * background(180); * lights(); * noStroke(); * fill('blue'); * shader(myShader); * box(60); * } */ /** * A GLSL-style name for <a href="#/p5/mat3">mat3()</a>. * * Note: `mat3x3()` can only be used inside a p5.strands shader callback. * * @method mat3x3 * @beta * @param {...Number} [values] the same values accepted by * <a href="#/p5/mat3">mat3()</a>. * @returns {*} a 3x3 matrix. */ /** * Creates a 4x4 matrix inside a p5.strands shader callback. * * `mat4()` is the same function as * <a href="#/p5/transform3D">transform3D()</a>, named the way it is in GLSL. * * Passing a 3x3 matrix to `mat4()` extends it with the identity matrix, which * turns a 2D transform into a 3D one that leaves the z-axis alone. * * Note: `mat4()` can only be used inside a p5.strands shader callback. * * @method mat4 * @beta * @param {...Number} [values] nothing for the identity matrix, one number, * one matrix, 4 column vectors, or 16 values in * column-major order. * @returns {*} a 4x4 matrix. */ /** * A GLSL-style name for <a href="#/p5/mat4">mat4()</a>. * * Note: `mat4x4()` can only be used inside a p5.strands shader callback. * * @method mat4x4 * @beta * @param {...Number} [values] the same values accepted by * <a href="#/p5/mat4">mat4()</a>. * @returns {*} a 4x4 matrix. */ for (const [name, dimension] of matrixConstructors) augmentFn(fn, p5, name, function(...args) { if (!strandsContext.active) { p5._friendlyError(`It looks like you've called ${name} outside of a shader's modify() function.`); return; } markTransformsExperimental(); const { id, dimension: dim } = matrixNode(strandsContext, dimension, args); return createStrandsNode(id, dim, strandsContext); }); const isStrandsTransform = (t) => strandsContext.active && !!t?.isStrandsNode && t.typeInfo().baseType === BaseType.MAT; const transformStep = (t, values2D, values3D) => { if (!isStrandsTransform(t)) userError("type error", "The first argument to a transform function (translate, rotate, scale, shearX, shearY) must be a transform created with transform2D() or transform3D()."); const values = t.dimension === 4 ? values3D : values2D; const m = matrixConstructorNode(strandsContext, t.dimension, values); return t.mult(createStrandsNode(m.id, m.dimension, strandsContext)); }; const originalTranslate = fn.translate; augmentFn(fn, p5, "translate", function(...args) { const t = args[0]; if (!isStrandsTransform(t)) return originalTranslate.apply(this, args); markTransformsExperimental(); const [x = 0, y = 0, z = 0] = args.slice(1); return transformStep(t, [ 1, 0, 0, 0, 1, 0, x, y, 1 ], [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, x, y, z, 1 ]); }); const originalScale = fn.scale; augmentFn(fn, p5, "scale", function(...args) { const t = args[0]; if (!isStrandsTransform(t)) return originalScale.apply(this, args); markTransformsExperimental(); const scales = args.slice(1); const uniform = scales.length === 1; const x = scales[0] ?? 1; const y = scales[1] ?? (uniform ? x : 1); const z = scales[2] ?? (uniform ? x : 1); return transformStep(t, [ x, 0, 0, 0, y, 0, 0, 0, 1 ], [ x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 ]); }); const originalRotate = fn.rotate; augmentFn(fn, p5, "rotate", function(...args) { const t = args[0]; if (!isStrandsTransform(t)) return originalRotate.apply(this, args); markTransformsExperimental(); const angle = args[1]; const c = this.cos(angle); const s = this.sin(angle); const ns = s.mult(-1); return transformStep(t, [ c, s, 0, ns, c, 0, 0, 0, 1 ], [ c, s, 0, 0, ns, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]); }); const originalShearX = fn.shearX; augmentFn(fn, p5, "shearX", function(...args) { const t = args[0]; if (!isStrandsTransform(t)) return originalShearX.apply(this, args); markTransformsExperimental(); const angle = args[1]; const k = this.tan(angle); return transformStep(t, [ 1, 0, 0, k, 1, 0, 0, 0, 1 ], [ 1, 0, 0, 0, k, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]); }); const originalShearY = fn.shearY; augmentFn(fn, p5, "shearY", function(...args) { const t = args[0]; if (!isStrandsTransform(t)) return originalShearY.apply(this, args); markTransformsExperimental(); const angle = args[1]; const k = this.tan(angle); return transformStep(t, [ 1, k, 0, 0, 1, 0, 0, 0, 1 ], [ 1, k, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]); }); /** * Turns a 3D p5.strands transform about any axis. * * <a href="#/p5/rotateX">rotateX()</a>, * <a href="#/p5/rotateY">rotateY()</a>, and * <a href="#/p5/rotateZ">rotateZ()</a> only turn about the three main axes. * `rotateAxisAngle()` turns about a line pointing in any direction, given as * a three-component vector. The axis doesn't need to be a unit vector; it is * normalized before use. * * A new transform is returned and the one passed in is left untouched, so the * result has to be assigned back, as in * `t = rotateAxisAngle(t, [1, 1, 0], PI / 4)`. * * Angles are always measured in radians here, since * <a href="#/p5/angleMode">angleMode()</a> doesn't reach inside a shader. * * Note: `rotateAxisAngle()` only works on a transform created with * <a href="#/p5/transform3D">transform3D()</a>, and can only be used inside a * p5.strands shader callback. * * @method rotateAxisAngle * @beta * @param {*} transform a transform created with * <a href="#/p5/transform3D">transform3D()</a>. * @param {*} axis a three-component vector pointing along the line to turn * about. * @param {Number} angle angle to turn by, in radians. * @returns {*} a new transform with the turn applied. * * @example * let myShader; * * function setup() { * createCanvas(200, 200, WEBGL); * myShader = buildMaterialShader(tumble); * } * * function tumble() { * worldInputs.begin(); * let t = transform3D(); * // Turn about a diagonal axis instead of x, y, or z. * t = rotateAxisAngle(t, [1, 1, 0], millis() * 0.001); * worldInputs.position = transformPoint(t, worldInputs.position); * worldInputs.normal = transformNormal(t, worldInputs.normal); * worldInputs.end(); * } * * function draw() { * background(180); * lights(); * noStroke(); * fill('purple'); * shader(myShader); * box(60); * } */ augmentFn(fn, p5, "rotateAxisAngle", function(...args) { if (!strandsContext.active) { p5._friendlyError(`It looks like you've called rotateAxisAngle outside of a shader's modify() function.`); return; } markTransformsExperimental(); const [t, axis, angle] = args; if (!t?.isStrandsNode || t.typeInfo().baseType !== BaseType.MAT || t.dimension !== 4) userError("type error", "rotateAxisAngle() only works on a 3D transform created with transform3D()."); const n = this.normalize(axis); const x = n.x, y = n.y, z = n.z; const c = this.cos(angle); const s = this.sin(angle); const omc = p5.strandsNode(1).sub(c); const b00 = x.mult(x).mult(omc).add(c); const b01 = y.mult(x).mult(omc).add(z.mult(s)); const b02 = z.mult(x).mult(omc).sub(y.mult(s)); const b10 = x.mult(y).mult(omc).sub(z.mult(s)); const b11 = y.mult(y).mult(omc).add(c); const b12 = z.mult(y).mult(omc).add(x.mult(s)); const b20 = x.mult(z).mult(omc).add(y.mult(s)); const b21 = y.mult(z).mult(omc).sub(x.mult(s)); const b22 = z.mult(z).mult(omc).add(c); const m = matrixConstructorNode(strandsContext, 4, [ b00, b01, b02, 0, b10, b11, b12, 0, b20, b21, b22, 0, 0, 0, 0, 1 ]); return t.mult(createStrandsNode(m.id, m.dimension, strandsContext)); }); const original3DRotations = { rotateX: fn.rotateX, rotateY: fn.rotateY, rotateZ: fn.rotateZ }; const registerAxisRotation = (name, valuesFor) => { const original = original3DRotations[name]; augmentFn(fn, p5, name, function(...args) { const t = args[0]; if (!isStrandsTransform(t)) return original ? original.apply(this, args) : void 0; markTransformsExperimental(); if (t.dimension !== 4) userError("type error", `${name}() needs a 3D transform created with transform3D().`); const angle = args[1]; const c = this.cos(angle); const s = this.sin(angle); const ns = s.mult(-1); const m = matrixConstructorNode(strandsContext, 4, valuesFor(c, s, ns)); return t.mult(createStrandsNode(m.id, m.dimension, strandsContext)); }); }; registerAxisRotation("rotateX", (c, s, ns) => [ 1, 0, 0, 0, 0, c, s, 0, 0, ns, c, 0, 0, 0, 0, 1 ]); registerAxisRotation("rotateY", (c, s, ns) => [ c, 0, ns, 0, 0, 1, 0, 0, s, 0, c, 0, 0, 0, 0, 1 ]); registerAxisRotation("rotateZ", (c, s, ns) => [ c, s, 0, 0, ns, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]); /** * Applies a p5.strands transform to a position. * * A transform is a matrix, and moving a point with it means multiplying the * two together. Doing that by hand needs an extra `1` tacked onto the end of * the position first, and the extra component stripped off the answer. * `transformPoint()` takes care of both steps. * * With a 2D transform from <a href="#/p5/transform2D">transform2D()</a>, pass * a two-component position and a two-component position comes back. With a 3D * transform from <a href="#/p5/transform3D">transform3D()</a>, pass a * three-component position and a three-component position comes back. * * Use <a href="#/p5/transformNormal">transformNormal()</a> for surface * directions instead, since those must ignore the move part of a transform. * * Note: `transformPoint()` can only be used inside a p5.strands shader * callback. * * @method transformPoint * @beta * @param {*} transform a transform created with * <a href="#/p5/transform2D">transform2D()</a> or * <a href="#/p5/transform3D">transform3D()</a>. * @param {*} point the position to move. * @returns {*} the moved position, with as many components as the one passed * in. * * @example * let myShader; * * function setup() { * createCanvas(200, 200, WEBGL); * myShader = buildMaterialShader(place); * } * * function place() { * worldInputs.begin(); * let t = transform3D(); * t = translate(t, 0, -30, 0); * t = rotateY(t, millis() * 0.001); * worldInputs.position = transformPoint(t, worldInputs.position); * worldInputs.end(); * } * * function draw() { * background(180); * lights(); * noStroke(); * fill('cyan'); * shader(myShader); * box(50); * } */ augmentFn(fn, p5, "transformPoint", function(...args) { if (!strandsContext.active) { p5._friendlyError(`It looks like you've called transformPoint outside of a shader's modify() function.`); return; } markTransformsExperimental(); const [t, point] = args; if (!(t?.isStrandsNode && t.typeInfo().baseType === BaseType.MAT)) userError("type error", "transformPoint(t, point): the first argument must be a transform from transform2D()/transform3D()."); const p = p5.strandsNode(point); if (t.dimension === 4) return t.mult(fn.vec4(p, 1)).xyz; return t.mult(fn.vec3(p, 1)).xy; }); /** * Applies a p5.strands transform to a surface direction. * * A normal describes which way a surface faces, and lighting uses it to work * out how bright each spot should be. Normals can't be moved the way * positions are: a move must be ignored, and an uneven resize like * `scale(t, 2, 1, 1)` would tilt them the wrong way if the transform were * applied directly. `transformNormal()` handles both cases and returns a * normalized result, so lighting stays correct. * * Pass the same transform used with * <a href="#/p5/transformPoint">transformPoint()</a> for the position, so the * shape and its lighting stay in step. * * Note: `transformNormal()` can only be used inside a p5.strands shader * callback. * * @method transformNormal * @beta * @param {*} transform a transform created with * <a href="#/p5/transform2D">transform2D()</a> or * <a href="#/p5/transform3D">transform3D()</a>. * @param {*} normal the surface direction to transform. * @returns {*} the transformed surface direction, normalized. * * @example * let myShader; * * function setup() { * createCanvas(200, 200, WEBGL); * myShader = buildMaterialShader(stretch); * } * * function stretch() { * worldInputs.begin(); * let t = transform3D(); * // An uneven resize, so the normals need fixing up too. * t = scale(t, 2, 0.5, 1); * worldInputs.position = transformPoint(t, worldInputs.position); * worldInputs.normal = transformNormal(t, worldInputs.normal); * worldInputs.end(); * } * * function draw() { * background(180); * lights(); * noStroke(); * fill('white'); * shader(myShader); * sphere(40); * } */ augmentFn(fn, p5, "transformNormal", function(...args) { if (!strandsContext.active) { p5._friendlyError(`It looks like you've called transformNormal outside of a shader's modify() function.`); return; } markTransformsExperimental(); const [t, normal] = args; if (!(t?.isStrandsNode && t.typeInfo().baseType === BaseType.MAT)) userError("type error", "transformNormal(t, normal): the first argument must be a transform from transform2D()/transform3D()."); const n = p5.strandsNode(normal); const linear = fn[`mat${t.dimension - 1}`](t); return fn.normalize(fn.transpose(fn.inverse(linear)).mult(n)); }); } //#endregion export { installTransformAPI };