Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setAttribute() function for defining custom shader attributes #7276

Merged
merged 98 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from 76 commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
b305b1b
immediate mode geometry properties are fully deleted
lukeplowden Sep 14, 2024
4dcf0d9
updated renderer.setAttribute() call to reflect new method name
lukeplowden Sep 15, 2024
9d7ab37
removed debugging log
lukeplowden Sep 15, 2024
9b3909a
updated to take array or single value
lukeplowden Sep 15, 2024
913131e
removed empty lines
lukeplowden Sep 15, 2024
88c7fbc
beginShape updated for tesselated shapes and to reset new attributes …
lukeplowden Sep 15, 2024
19a3d70
set more than one custom attribute per shape/geometry. back fill with…
lukeplowden Sep 15, 2024
ae52039
refactor setAttribute method to update a userAttributes object and in…
lukeplowden Sep 15, 2024
72cf3bc
_tesselateShape works with custom attributes
lukeplowden Sep 15, 2024
03d2e5e
changed references to prototype.tessyvertexSize to this.tessyVertexSi…
lukeplowden Sep 15, 2024
616d7ca
free user buffers
lukeplowden Sep 15, 2024
8d2656f
moved user attributes section to the bottom of createBuffers()
lukeplowden Sep 15, 2024
859ac70
ignore my dev folder
lukeplowden Sep 15, 2024
cafdc1b
fixed a bug in _tesselateShape miscounting memory offset for custom a…
lukeplowden Sep 19, 2024
0e35884
define user attributes array upfront
lukeplowden Sep 22, 2024
d842f05
prepare immediate user buffers
lukeplowden Sep 22, 2024
33259c0
bool for immediate user attributes, arrays for user attributes p5.Ren…
lukeplowden Sep 22, 2024
b5c3213
used const reference to geometry which was already defined but unused
lukeplowden Sep 22, 2024
e038c9c
prepare user attribute buffers
lukeplowden Sep 22, 2024
f7d2b99
handled differences in custom attributes between geometries in the bu…
lukeplowden Sep 22, 2024
55a37af
user attributes is now a single Object instead of an array of them
lukeplowden Sep 22, 2024
dd08014
reflect the change of geometry custom attributes from objects in an a…
lukeplowden Sep 22, 2024
e2220e7
Merge pull request #3 from lukeplowden/p5-attributes
lukeplowden Sep 22, 2024
a801e49
Remove Object.keys intermediate array and loop directly on user attri…
lukeplowden Sep 23, 2024
6ec85c1
Added an optional size parameter to setAttribute so that bigger chunk…
lukeplowden Sep 23, 2024
59ed53b
Remove object.entries iteration for 'in' loop and changed to use the …
lukeplowden Sep 23, 2024
3d19521
remove old implementation with extra loop
lukeplowden Sep 23, 2024
04201c5
Early return if the does not have a custom attribute required by the …
lukeplowden Sep 23, 2024
68d9132
Replaced Object.keys/entries arrays with 'for in' loops and use new s…
lukeplowden Sep 23, 2024
504cf5a
simplify for loop and remove unnecessary check
lukeplowden Sep 23, 2024
e64b3f0
Fixed a bug where if an attribute was set but not applied to any vert…
lukeplowden Sep 23, 2024
f38819b
Changed <code> block to backticks in normal documentation
lukeplowden Sep 23, 2024
f30e3ee
Initial draft of setAttribute() function documentation
lukeplowden Sep 23, 2024
fc09ce1
added old documentation files back for compatibility & preview
lukeplowden Sep 23, 2024
d308bd8
add custom vertex attributes to the 'TESS preserves vertex data' test
lukeplowden Sep 23, 2024
cf03836
delete custom attributes on reset, change Object.keys to 'for in' loop
lukeplowden Sep 23, 2024
9f91d51
Add check in case there is an incorrect number of attributes on a geo…
lukeplowden Sep 23, 2024
fc47dcb
remove redundant checks
lukeplowden Sep 23, 2024
874c363
preparing to add immediate buffer strides re:custom attributes
lukeplowden Sep 23, 2024
27610ec
comment to remind me to convert custom attributes edgesToVertices
lukeplowden Sep 23, 2024
103d6eb
update the libtess combineCallback to handle custom attributes/ verte…
lukeplowden Sep 24, 2024
e004a8d
rebuild the tesselator in case tessyvertexsize changes (custom attrib…
lukeplowden Sep 24, 2024
7bae702
Added a function to update the libtess tesselator's combine callback …
lukeplowden Sep 24, 2024
c3ad1fc
update the tesselators combine callback instead of reinitializing the…
lukeplowden Sep 24, 2024
744141f
Correct bugs related to overlapping vertices
lukeplowden Sep 24, 2024
5bb41b5
remove old scripts
lukeplowden Sep 24, 2024
1a03229
Merge pull request #5 from lukeplowden/docs
lukeplowden Sep 24, 2024
3ffe8cc
convert tessy combinecallback to arrow function so that custom attrib…
lukeplowden Sep 24, 2024
afdf29d
remove redundant function calls to update tessy combine callback
lukeplowden Sep 24, 2024
383fe8e
Changed user attributes source array on geometry to include 'Src' on …
lukeplowden Sep 26, 2024
c2fd8b7
interpolate custom attributes on quadratic curves
lukeplowden Sep 26, 2024
7107d7c
Update custom attribute test as geometrie's source attribute names no…
lukeplowden Sep 26, 2024
bb5ac02
Interpolate user attributes across bezierVertex
lukeplowden Sep 26, 2024
233ed4b
updated setAttribute documentation
lukeplowden Sep 26, 2024
9f24576
Added a second example for setAttribute()
lukeplowden Sep 27, 2024
b41b013
add custom attributes to buffer strides for quad strip/ quad
lukeplowden Sep 27, 2024
ebce73e
added visual tests for setAttribute()
lukeplowden Sep 27, 2024
e46c2d7
improved error message for when working directly with geometry.setatt…
lukeplowden Sep 27, 2024
d8318e1
reset userattributes object instead of deleting (its likely to be use…
lukeplowden Sep 27, 2024
968a05e
remove old user defined render buffers when model is called
lukeplowden Sep 27, 2024
a66af69
Added a bunch of tests for setAttribute()
lukeplowden Sep 27, 2024
62a4afa
fixed some bugs in examples for setAttribute
lukeplowden Sep 27, 2024
01c2d3b
scaled down the example canvas size to correct (100,100) size
lukeplowden Sep 27, 2024
d8db76e
Added explanation to the setAttribute method in geometry.
lukeplowden Sep 27, 2024
8d97dc1
added initial docs for Geometry.setAttribute
lukeplowden Sep 27, 2024
65f8a08
added a reference to the geometry method at the end of setAttribute i…
lukeplowden Sep 27, 2024
eb18e6f
added an example to set geometry method
lukeplowden Sep 27, 2024
6b0ad95
fixed syntax error in set attribute example
lukeplowden Sep 27, 2024
70d64c7
changed some tests from assert.deepEqual() to expect().to.containSubset
lukeplowden Sep 28, 2024
9d1cd6e
add some comments to the example
lukeplowden Sep 28, 2024
656d37f
change x * x to pow(x, x)
lukeplowden Sep 28, 2024
5c967dc
Updated setAttribute Documentation.
lukeplowden Sep 28, 2024
a2dd8e4
Changed 'grey' to 'gray'
lukeplowden Sep 28, 2024
86cabd4
fix some bugs in one example
lukeplowden Sep 28, 2024
7539a7b
change cellSize in example
lukeplowden Sep 28, 2024
3b7d8f9
add a custom attribute helper type and refactor to use it
lukeplowden Sep 28, 2024
3bcce8b
Refactor to use the geometry user attributes helper object
lukeplowden Sep 28, 2024
0d6b6c9
added getName to user attributes
lukeplowden Sep 28, 2024
1b9e569
use new user Attribute Helper implementation of setAttribute
lukeplowden Sep 28, 2024
69e3f08
use new implementation for setAttribute with helper
lukeplowden Sep 28, 2024
0ccfb5d
fix silling in missing values for custom attribs
lukeplowden Sep 30, 2024
f5c667f
fix custom attributes geom building
lukeplowden Sep 30, 2024
550f666
add size parameter for geometry.setattribute
lukeplowden Sep 30, 2024
5fc8502
Typo
lukeplowden Sep 30, 2024
2fc7567
Fixed documentation
lukeplowden Sep 30, 2024
1244d80
remove auto current data setting
lukeplowden Sep 30, 2024
daf9cf0
changed method call for more direct version
lukeplowden Sep 30, 2024
4ad4770
Fixed bug where geometry custom src array was not reset properly
lukeplowden Sep 30, 2024
a91088f
moved this.tessyVertexSize declaration into _initTessy()
lukeplowden Sep 30, 2024
cdf7300
quadraticVertex working again.
lukeplowden Sep 30, 2024
5565023
custom attributes also on 6 vertices quadratic vertex
lukeplowden Sep 30, 2024
6b183b4
bezier vertex interpolates custom attributes
lukeplowden Sep 30, 2024
cff4074
updated tests for new custom attribute helper object
lukeplowden Sep 30, 2024
b086fea
updated documentation
lukeplowden Sep 30, 2024
9ab620f
Put quote marks in error message
lukeplowden Sep 30, 2024
38ae74b
Merge branch 'attribute-helpers'
lukeplowden Sep 30, 2024
0da4073
changed 'setAttribute()' to 'vertexProperty()' and all references, va…
lukeplowden Oct 1, 2024
22cad2b
remove my temporary gitignore for dev folder
lukeplowden Oct 1, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 26 additions & 25 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
*.DS_Store
.project
node_modules/*
experiments/*
lib_old/*
lib/p5.*
lib/modules
docs/reference/*
!*.gitkeep
examples/3d/
.idea
dist/
p5.zip
bower-repo/
p5-website/
.vscode/settings.json
.nyc_output/*
coverage/
lib/p5-test.js
release/
yarn.lock
docs/data.json
analyzer/
preview/
__screenshots__/
*.DS_Store
.project
node_modules/*
experiments/*
lib_old/*
lib/p5.*
lib/modules
docs/reference/*
!*.gitkeep
examples/3d/
.idea
dist/
p5.zip
bower-repo/
p5-website/
.vscode/settings.json
.nyc_output/*
coverage/
lib/p5-test.js
release/
yarn.lock
docs/data.json
analyzer/
preview/
__screenshots__/
attributes-example/
176 changes: 174 additions & 2 deletions src/core/shape/vertex.js
Original file line number Diff line number Diff line change
Expand Up @@ -2091,7 +2091,7 @@ p5.prototype.vertex = function(x, y, moveTo, u, v) {
* `normal()` will affect all following vertices until `normal()` is called
* again:
*
* <code>
* ```javascript
* beginShape();
*
* // Set the vertex normal.
Expand All @@ -2114,7 +2114,7 @@ p5.prototype.vertex = function(x, y, moveTo, u, v) {
* vertex(-30, 30, 0);
*
* endShape();
* </code>
* ```
*
* @method normal
* @param {p5.Vector} vector vertex normal as a <a href="#/p5.Vector">p5.Vector</a> object.
Expand Down Expand Up @@ -2253,4 +2253,176 @@ p5.prototype.normal = function(x, y, z) {
return this;
};

/** Sets the shader's vertex attribute variables.
*
* An attribute is a variable belonging to a vertex in a shader. p5.js provides some
* default attributes, such as `aPosition`, `aNormal`, `aVertexColor`, etc. These are
* set using <a href="#/p5/vertex">vertex()</a>, <a href="#/p5/normal">normal()</a>
* and <a href="#/p5/fill">fill()</a> respectively. Custom attribute data can also
* be defined within <a href="#/p5/beginShape">beginShape()</a> and
* <a href="#/p5/endShape">endShape()</a>.
*
* The first parameter, `attributeName`, is a string with the attribute's name.
lukeplowden marked this conversation as resolved.
Show resolved Hide resolved
* This is the same variable name which should be declared in the shader, similar to
* `setUniform()`.
*
* The second parameter, `data`, is the value assigned to the attribute. This
* value will be applied to subsequent vertices created with
* <a href="#/p5/vertex">vertex()</a>. It can be a Number or an array of numbers,
* and in the shader program the type can be declared according to the WebGL
* specification. Common types include `float`, `vec2`, `vec3`, `vec4` or matrices.
*
* See also the <a href="#/p5/setAttribute">setAttribute()</a> method on
* <a href="#/p5/Geometry">Geometry</a> objects.
*
* @example
* <div>
* <code>
* const vertSrc = `#version 300 es
* precision mediump float;
* uniform mat4 uModelViewMatrix;
* uniform mat4 uProjectionMatrix;
*
* in vec3 aPosition;
* in vec2 aOffset;
*
* void main(){
* vec4 positionVec4 = vec4(aPosition.xyz, 1.0);
* positionVec4.xy += aOffset;
* gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
lukeplowden marked this conversation as resolved.
Show resolved Hide resolved
* }
* `;
*
* const fragSrc = `#version 300 es
* precision mediump float;
* out vec4 outColor;
* void main(){
* outColor = vec4(0.0, 1.0, 1.0, 1.0);
* }
* `;
*
* function setup(){
* createCanvas(100, 100, WEBGL);
*
* // Create and use the custom shader.
* const myShader = createShader(vertSrc, fragSrc);
* shader(myShader);
*
* describe('A wobbly, cyan circle on a gray background.');
* }
*
* function draw(){
* // Set the styles
* background(125);
* noStroke();
*
* // Draw the circle.
* beginShape();
* for (let i = 0; i < 30; i++){
* const x = 40 * cos(i/30 * TWO_PI);
* const y = 40 * sin(i/30 * TWO_PI);
*
* // Apply some noise to the coordinates.
* const xOff = 10 * noise(x + millis()/1000) - 5;
* const yOff = 10 * noise(y + millis()/1000) - 5;
*
* // Apply these noise values to the following vertex.
* setAttribute('aOffset', [xOff, yOff]);
* vertex(x, y);
* }
* endShape(CLOSE);
* }
* </code>
* </div>
*
* <div>
* <code>
* let myShader;
* const cols = 10;
* const rows = 10;
* const cellSize = 6;
*
* const vertSrc = `#version 300 es
* precision mediump float;
* uniform mat4 uProjectionMatrix;
* uniform mat4 uModelViewMatrix;
*
* in vec3 aPosition;
* in vec3 aNormal;
* in vec3 aVertexColor;
* in float aDistance;
*
* out vec3 vVertexColor;
*
* void main(){
* vec4 positionVec4 = vec4(aPosition, 1.0);
* positionVec4.xyz += aDistance * aNormal * 2.0;;
* vVertexColor = aVertexColor;
* gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
* }
* `;
*
* const fragSrc = `#version 300 es
* precision mediump float;
*
* in vec3 vVertexColor;
* out vec4 outColor;
*
* void main(){
* outColor = vec4(vVertexColor, 1.0);
* }
* `;
*
* function setup(){
* createCanvas(100, 100, WEBGL);
*
* // Create and apply the custom shader.
* myShader = createShader(vertSrc, fragSrc);
* shader(myShader);
* noStroke();
* describe('A blue grid, which moves away from the mouse position, on a gray background.');
* }
*
* function draw(){
* background(200);
*
* // Draw the grid in the middle of the screen.
* translate(-cols*cellSize/2, -rows*cellSize/2);
* beginShape(QUADS);
* for (let i = 0; i < cols; i++) {
* for (let j = 0; j < rows; j++) {
*
* // Calculate the cell position.
* let x = i * cellSize;
* let y = j * cellSize;
*
* fill(j/rows*255, j/cols*255, 255);
*
* // Calculate the distance from the corner of each cell to the mouse.
* let distance = dist(x1,y1, mouseX, mouseY);
*
* // Send the distance to the shader.
* setAttribute('aDistance', min(distance, 100));
*
* vertex(x, y);
* vertex(x + cellSize, y);
* vertex(x + cellSize, y + cellSize);
* vertex(x, y + cellSize);
* }
* }
* endShape();
* }
* </code>
* </div>
*
* @method setAttribute
* @param {String} attributeName the name of the vertex attribute.
* @param {Number|Number[]} data the data tied to the vertex attribute.
*/
p5.prototype.setAttribute = function(attributeName, data){
// this._assert3d('setAttribute');
// p5._validateParameters('setAttribute', arguments);
this._renderer.setAttribute(attributeName, data);
};

export default p5;
Loading
Loading