Skip to content

Commit 8dafa9a

Browse files
committed
Update ShaderGenerator docs and examples
1 parent 1146190 commit 8dafa9a

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

src/webgl/ShaderGenerator.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,11 +1663,11 @@ if (typeof p5 !== 'undefined') {
16631663
* function setup() {
16641664
* createCanvas(200, 200, WEBGL);
16651665
* myShader = baseMaterialShader().modify(() => {
1666+
* let t = uniformFloat(() => millis());
16661667
* getWorldInputs(inputs => {
16671668
* // Move the vertex up and down in a wave in world space
16681669
* // In world space, moving the object (e.g., with translate()) will affect these coordinates
1669-
* let t = uniformFloat(() => millis());
1670-
* inputs.position.y += 20 * sin(t * 0.001 + inputs.position.x * 0.05);
1670+
* inputs.position.y += 0.5 * sin(t * 0.001 + inputs.position.x * 0.05);
16711671
* return inputs;
16721672
* });
16731673
* });
@@ -1714,20 +1714,15 @@ if (typeof p5 !== 'undefined') {
17141714
* createCanvas(200, 200, WEBGL);
17151715
* myShader = baseMaterialShader().modify(() => {
17161716
* combineColors(components => {
1717-
* // Custom color combination: add a red tint using vector properties
1718-
* let r = components.baseColor.r * components.diffuse +
1719-
* components.ambientColor.r * components.ambient +
1720-
* components.specularColor.r * components.specular +
1721-
* components.emissive.r + 0.2;
1722-
* let g = components.baseColor.g * components.diffuse +
1723-
* components.ambientColor.g * components.ambient +
1724-
* components.specularColor.g * components.specular +
1725-
* components.emissive.g;
1726-
* let b = components.baseColor.b * components.diffuse +
1727-
* components.ambientColor.b * components.ambient +
1728-
* components.specularColor.b * components.specular +
1729-
* components.emissive.b;
1730-
* return [r, g, b, components.opacity];
1717+
* // Custom color combination: add a green tint using vector properties
1718+
* return [
1719+
* components.baseColor * components.diffuse +
1720+
* components.ambientColor * components.ambient +
1721+
* components.specularColor * components.specular +
1722+
* components.emissive +
1723+
* [0, 0.2, 0], // Green tint for visibility
1724+
* components.opacity
1725+
* ];
17311726
* });
17321727
* });
17331728
* }
@@ -1846,10 +1841,12 @@ if (typeof p5 !== 'undefined') {
18461841
* function setup() {
18471842
* createCanvas(200, 200, WEBGL);
18481843
* myShader = baseMaterialShader().modify(() => {
1844+
* let t = uniformFloat(() => millis());
18491845
* getPixelInputs(inputs => {
1850-
* // Animate opacity based on x position
1851-
* let t = uniformFloat(() => millis());
1852-
* inputs.opacity = 0.5 + 0.5 * sin(inputs.texCoord.x * 10.0 + t * 0.002);
1846+
* // Animate alpha (transparency) based on x position
1847+
* if (inputs.color && inputs.texCoord) {
1848+
* inputs.color.a = 0.5 + 0.5 * sin(inputs.texCoord.x * 10.0 + t * 0.002);
1849+
* }
18531850
* return inputs;
18541851
* });
18551852
* });
@@ -1888,8 +1885,8 @@ if (typeof p5 !== 'undefined') {
18881885
* createCanvas(200, 200, WEBGL);
18891886
* myShader = baseStrokeShader().modify(() => {
18901887
* shouldDiscard(willDiscard => {
1891-
* // Discard fragments outside a circular region
1892-
* return willDiscard || (inputs.position.x * inputs.position.x + inputs.position.y * inputs.position.y > 2500.0);
1888+
* // Discard fragments based only on the default logic
1889+
* return willDiscard;
18931890
* });
18941891
* });
18951892
* }
@@ -1906,7 +1903,7 @@ if (typeof p5 !== 'undefined') {
19061903
/**
19071904
* @method getFinalColor
19081905
* @description
1909-
* Registers a callback to change the final color of each pixel after all lighting and mixing is done in the fragment shader. This hook can be used inside <a href="#/p5/baseColorShader">baseColorShader()</a>.modify() and similar shader modify calls to adjust the color before it appears on the screen. The callback receives a color array:
1906+
* Registers a callback to change the final color of each pixel after all lighting and mixing is done in the fragment shader. This hook can be used inside <a href="#/p5/baseColorShader">baseColorShader()</a>.modify() and similar shader modify calls to adjust the color before it appears on the screen. The callback receives a four component vector representing red, green, blue, and alpha:
19101907
* - `[r, g, b, a]`: the current color (red, green, blue, alpha)
19111908
*
19121909
* Return a new color array to change the output color.
@@ -1928,9 +1925,8 @@ if (typeof p5 !== 'undefined') {
19281925
* createCanvas(200, 200, WEBGL);
19291926
* myShader = baseColorShader().modify(() => {
19301927
* getFinalColor(color => {
1931-
* // Make the output color fully opaque and add a green tint
1932-
* color.a = 1;
1933-
* color.g += 0.2;
1928+
* // Add a blue tint to the output color
1929+
* color.b += 0.2;
19341930
* return color;
19351931
* });
19361932
* });
@@ -1994,10 +1990,14 @@ if (typeof p5 !== 'undefined') {
19941990
* @method getColor
19951991
* @description
19961992
* Registers a callback to set the final color for each pixel in a filter shader. This hook can be used inside <a href="#/p5/baseFilterShader">baseFilterShader()</a>.modify() and similar shader modify calls to control the output color for each pixel. The callback receives the following arguments:
1997-
* - `inputs`: an object with properties like `texCoord`, `canvasSize`, `texelSize`, etc.
1993+
* - `inputs`: an object with the following properties:
1994+
* - `texCoord`: a vec2 representing the texture coordinates of the pixel
1995+
* - `canvasSize`: a vec2 representing the size of the canvas in pixels
1996+
* - `texelSize`: a vec2 representing the size of a single texel (pixel) in texture space
1997+
* - `color`: a vec4 representing the color of the pixel before the filter is applied
1998+
* - (other properties may be available depending on the shader)
19981999
* - `canvasContent`: a sampler2D texture with the sketch's contents before the filter is applied
1999-
*
2000-
* Return a color array `[r, g, b, a]` for the pixel.
2000+
* Return a four component vector `[r, g, b, a]` for the pixel.
20012001
*
20022002
* This hook is available in:
20032003
* - <a href="#/p5/baseFilterShader">baseFilterShader()</a>
@@ -2057,10 +2057,10 @@ if (typeof p5 !== 'undefined') {
20572057
* function setup() {
20582058
* createCanvas(200, 200, WEBGL);
20592059
* myShader = baseColorShader().modify(() => {
2060+
* let t = uniformFloat(() => millis());
20602061
* getObjectInputs(inputs => {
20612062
* // Create a sine wave along the x axis in object space
2062-
* let t = uniformFloat(() => millis());
2063-
* inputs.position.y += 20 * sin(inputs.position.x * 0.1 + t * 0.002);
2063+
* inputs.position.y += 0.5 * sin(inputs.position.x * 0.1 + t * 0.002);
20642064
* return inputs;
20652065
* });
20662066
* });

0 commit comments

Comments
 (0)