Skip to content

Commit cc727b0

Browse files
committed
Update p5.strands hooks documentation based on reviewer feedback
1 parent ae09118 commit cc727b0

File tree

1 file changed

+152
-0
lines changed

1 file changed

+152
-0
lines changed

src/webgl/p5.strands.js

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* @typedef {Object} Vertex
3+
* @property {{x: number, y: number, z: number}} position - The position of the vertex in world space.
4+
* @property {{x: number, y: number, z: number}} normal - The normal vector at the vertex in world space.
5+
* @property {{x: number, y: number}} texCoord - The texture coordinates (x, y) for the vertex.
6+
* @property {{r: number, g: number, b: number, a: number}} color - The color at the vertex.
7+
*/
8+
9+
/**
10+
* @function getWorldInputs
11+
* @experimental
12+
* @description
13+
* Registers a callback to modify the world-space properties of each vertex in a shader. This hook can be used inside {@link p5.baseMaterialShader}.modify() and similar shader modify calls to customize vertex positions, normals, texture coordinates, and colors before rendering. "World space" refers to the coordinate system of the 3D scene, before any camera or projection transformations are applied.
14+
*
15+
* This hook is available in:
16+
* - {@link p5.baseMaterialShader}
17+
* - {@link p5.baseNormalShader}
18+
* - {@link p5.baseColorShader}
19+
* - {@link p5.baseStrokeShader}
20+
*
21+
* @param {function(Vertex): Vertex} callback
22+
* A callback function which receives and returns a Vertex struct.
23+
*
24+
* @see {@link p5.baseMaterialShader}
25+
* @see {@link p5.Shader#modify}
26+
*
27+
* @example
28+
* <div modernizr='webgl'>
29+
* <code>
30+
* let myShader;
31+
* function setup() {
32+
* createCanvas(200, 200, WEBGL);
33+
* myShader = baseMaterialShader().modify(() => {
34+
* getWorldInputs(inputs => {
35+
* // Move the vertex up and down in a wave
36+
* inputs.position.y += 20 * sin(
37+
* millis() * 0.001 + inputs.position.x * 0.05
38+
* );
39+
* return inputs;
40+
* });
41+
* });
42+
* }
43+
* function draw() {
44+
* background(255);
45+
* shader(myShader);
46+
* lights();
47+
* noStroke();
48+
* fill('red');
49+
* sphere(50);
50+
* }
51+
* </code>
52+
* </div>
53+
*/
54+
55+
/**
56+
* @function combineColors
57+
* @experimental
58+
* @description
59+
* Registers a callback to customize how color components are combined in the fragment shader. This hook can be used inside {@link p5.baseMaterialShader}.modify() and similar shader modify calls to control the final color output of a material. The callback receives a ColorComponents struct and should return an object with a `color` property ({ r, g, b }) and an `opacity` property (number).
60+
*
61+
* This hook is available in:
62+
* - {@link p5.baseMaterialShader}
63+
* - {@link p5.baseNormalShader}
64+
* - {@link p5.baseColorShader}
65+
* - {@link p5.baseStrokeShader}
66+
*
67+
* @param {function(ColorComponents): { color: {r: number, g: number, b: number}, opacity: number }} callback
68+
* A callback function which receives a ColorComponents struct and returns the final color and opacity.
69+
*
70+
* @see {@link p5.baseMaterialShader}
71+
* @see {@link p5.Shader#modify}
72+
*
73+
* @example
74+
* <div modernizr='webgl'>
75+
* <code>
76+
* let myShader;
77+
* function setup() {
78+
* createCanvas(200, 200, WEBGL);
79+
* myShader = baseMaterialShader().modify(() => {
80+
* combineColors(components => {
81+
* // Custom color combination: add a red tint
82+
* let color = {
83+
* r: components.baseColor.r * components.diffuse.r +
84+
* components.ambientColor.r * components.ambient.r +
85+
* components.specularColor.r * components.specular.r +
86+
* components.emissive.r + 0.2,
87+
* g: components.baseColor.g * components.diffuse.g +
88+
* components.ambientColor.g * components.ambient.g +
89+
* components.specularColor.g * components.specular.g +
90+
* components.emissive.g,
91+
* b: components.baseColor.b * components.diffuse.b +
92+
* components.ambientColor.b * components.ambient.b +
93+
* components.specularColor.b * components.specular.b +
94+
* components.emissive.b
95+
* };
96+
* return { color, opacity: components.opacity };
97+
* });
98+
* });
99+
* }
100+
* function draw() {
101+
* background(255);
102+
* shader(myShader);
103+
* lights();
104+
* noStroke();
105+
* fill('red');
106+
* sphere(50);
107+
* }
108+
* </code>
109+
* </div>
110+
*/
111+
112+
/**
113+
* @function getPointSize
114+
* @experimental
115+
* @description
116+
* Registers a callback to modify the size of points when rendering with a shader. This hook can be used inside {@link p5.baseMaterialShader}.modify() or similar, when drawing points (e.g., with the point() function in WEBGL mode). The callback receives the current point size (number) and should return the new size (number).
117+
*
118+
* This hook is available in:
119+
* - {@link p5.baseMaterialShader}
120+
* - {@link p5.baseNormalShader}
121+
* - {@link p5.baseColorShader}
122+
* - {@link p5.baseStrokeShader}
123+
*
124+
* @param {function(size: number): number} callback
125+
* A callback function which receives and returns the point size.
126+
*
127+
* @see {@link p5.baseMaterialShader}
128+
* @see {@link p5.Shader#modify}
129+
*
130+
* @example
131+
* <div modernizr='webgl'>
132+
* <code>
133+
* let myShader;
134+
* function setup() {
135+
* createCanvas(200, 200, WEBGL);
136+
* myShader = baseMaterialShader().modify(() => {
137+
* getPointSize(size => {
138+
* // Make points pulse in size over time
139+
* return size * (1.0 + 0.5 * sin(millis() * 0.002));
140+
* });
141+
* });
142+
* }
143+
* function draw() {
144+
* background(255);
145+
* shader(myShader);
146+
* strokeWeight(20);
147+
* stroke('blue');
148+
* point(0, 0);
149+
* }
150+
* </code>
151+
* </div>
152+
*/

0 commit comments

Comments
 (0)