Skip to content

Commit 3131111

Browse files
authored
Merge pull request #7276 from lukeplowden/main
setAttribute() function for defining custom shader attributes
2 parents 34d41a5 + 22cad2b commit 3131111

File tree

17 files changed

+964
-62
lines changed

17 files changed

+964
-62
lines changed

.gitignore

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
*.DS_Store
2-
.project
3-
node_modules/*
4-
experiments/*
5-
lib_old/*
6-
lib/p5.*
7-
lib/modules
8-
docs/reference/*
9-
!*.gitkeep
10-
examples/3d/
11-
.idea
12-
dist/
13-
p5.zip
14-
bower-repo/
15-
p5-website/
16-
.vscode/settings.json
17-
.nyc_output/*
18-
coverage/
19-
lib/p5-test.js
20-
release/
21-
yarn.lock
22-
docs/data.json
23-
analyzer/
24-
preview/
1+
*.DS_Store
2+
.project
3+
node_modules/*
4+
experiments/*
5+
lib_old/*
6+
lib/p5.*
7+
lib/modules
8+
docs/reference/*
9+
!*.gitkeep
10+
examples/3d/
11+
.idea
12+
dist/
13+
p5.zip
14+
bower-repo/
15+
p5-website/
16+
.vscode/settings.json
17+
.nyc_output/*
18+
coverage/
19+
lib/p5-test.js
20+
release/
21+
yarn.lock
22+
docs/data.json
23+
analyzer/
24+
preview/
2525
__screenshots__/

src/core/shape/vertex.js

+174-2
Original file line numberDiff line numberDiff line change
@@ -2091,7 +2091,7 @@ p5.prototype.vertex = function(x, y, moveTo, u, v) {
20912091
* `normal()` will affect all following vertices until `normal()` is called
20922092
* again:
20932093
*
2094-
* <code>
2094+
* ```javascript
20952095
* beginShape();
20962096
*
20972097
* // Set the vertex normal.
@@ -2114,7 +2114,7 @@ p5.prototype.vertex = function(x, y, moveTo, u, v) {
21142114
* vertex(-30, 30, 0);
21152115
*
21162116
* endShape();
2117-
* </code>
2117+
* ```
21182118
*
21192119
* @method normal
21202120
* @param {p5.Vector} vector vertex normal as a <a href="#/p5.Vector">p5.Vector</a> object.
@@ -2253,4 +2253,176 @@ p5.prototype.normal = function(x, y, z) {
22532253
return this;
22542254
};
22552255

2256+
/** Sets the shader's vertex property or attribute variables.
2257+
*
2258+
* An vertex property or vertex attribute is a variable belonging to a vertex in a shader. p5.js provides some
2259+
* default properties, such as `aPosition`, `aNormal`, `aVertexColor`, etc. These are
2260+
* set using <a href="#/p5/vertex">vertex()</a>, <a href="#/p5/normal">normal()</a>
2261+
* and <a href="#/p5/fill">fill()</a> respectively. Custom properties can also
2262+
* be defined within <a href="#/p5/beginShape">beginShape()</a> and
2263+
* <a href="#/p5/endShape">endShape()</a>.
2264+
*
2265+
* The first parameter, `propertyName`, is a string with the property's name.
2266+
* This is the same variable name which should be declared in the shader, such as
2267+
* `in vec3 aProperty`, similar to .`setUniform()`.
2268+
*
2269+
* The second parameter, `data`, is the value assigned to the shader variable. This
2270+
* value will be applied to subsequent vertices created with
2271+
* <a href="#/p5/vertex">vertex()</a>. It can be a Number or an array of numbers,
2272+
* and in the shader program the type can be declared according to the WebGL
2273+
* specification. Common types include `float`, `vec2`, `vec3`, `vec4` or matrices.
2274+
*
2275+
* See also the <a href="#/p5/vertexProperty">vertexProperty()</a> method on
2276+
* <a href="#/p5/Geometry">Geometry</a> objects.
2277+
*
2278+
* @example
2279+
* <div>
2280+
* <code>
2281+
* const vertSrc = `#version 300 es
2282+
* precision mediump float;
2283+
* uniform mat4 uModelViewMatrix;
2284+
* uniform mat4 uProjectionMatrix;
2285+
*
2286+
* in vec3 aPosition;
2287+
* in vec2 aOffset;
2288+
*
2289+
* void main(){
2290+
* vec4 positionVec4 = vec4(aPosition.xyz, 1.0);
2291+
* positionVec4.xy += aOffset;
2292+
* gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
2293+
* }
2294+
* `;
2295+
*
2296+
* const fragSrc = `#version 300 es
2297+
* precision mediump float;
2298+
* out vec4 outColor;
2299+
* void main(){
2300+
* outColor = vec4(0.0, 1.0, 1.0, 1.0);
2301+
* }
2302+
* `;
2303+
*
2304+
* function setup(){
2305+
* createCanvas(100, 100, WEBGL);
2306+
*
2307+
* // Create and use the custom shader.
2308+
* const myShader = createShader(vertSrc, fragSrc);
2309+
* shader(myShader);
2310+
*
2311+
* describe('A wobbly, cyan circle on a gray background.');
2312+
* }
2313+
*
2314+
* function draw(){
2315+
* // Set the styles
2316+
* background(125);
2317+
* noStroke();
2318+
*
2319+
* // Draw the circle.
2320+
* beginShape();
2321+
* for (let i = 0; i < 30; i++){
2322+
* const x = 40 * cos(i/30 * TWO_PI);
2323+
* const y = 40 * sin(i/30 * TWO_PI);
2324+
*
2325+
* // Apply some noise to the coordinates.
2326+
* const xOff = 10 * noise(x + millis()/1000) - 5;
2327+
* const yOff = 10 * noise(y + millis()/1000) - 5;
2328+
*
2329+
* // Apply these noise values to the following vertex.
2330+
* vertexProperty('aOffset', [xOff, yOff]);
2331+
* vertex(x, y);
2332+
* }
2333+
* endShape(CLOSE);
2334+
* }
2335+
* </code>
2336+
* </div>
2337+
*
2338+
* <div>
2339+
* <code>
2340+
* let myShader;
2341+
* const cols = 10;
2342+
* const rows = 10;
2343+
* const cellSize = 6;
2344+
*
2345+
* const vertSrc = `#version 300 es
2346+
* precision mediump float;
2347+
* uniform mat4 uProjectionMatrix;
2348+
* uniform mat4 uModelViewMatrix;
2349+
*
2350+
* in vec3 aPosition;
2351+
* in vec3 aNormal;
2352+
* in vec3 aVertexColor;
2353+
* in float aDistance;
2354+
*
2355+
* out vec3 vVertexColor;
2356+
*
2357+
* void main(){
2358+
* vec4 positionVec4 = vec4(aPosition, 1.0);
2359+
* positionVec4.xyz += aDistance * aNormal * 2.0;;
2360+
* vVertexColor = aVertexColor;
2361+
* gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4;
2362+
* }
2363+
* `;
2364+
*
2365+
* const fragSrc = `#version 300 es
2366+
* precision mediump float;
2367+
*
2368+
* in vec3 vVertexColor;
2369+
* out vec4 outColor;
2370+
*
2371+
* void main(){
2372+
* outColor = vec4(vVertexColor, 1.0);
2373+
* }
2374+
* `;
2375+
*
2376+
* function setup(){
2377+
* createCanvas(100, 100, WEBGL);
2378+
*
2379+
* // Create and apply the custom shader.
2380+
* myShader = createShader(vertSrc, fragSrc);
2381+
* shader(myShader);
2382+
* noStroke();
2383+
* describe('A blue grid, which moves away from the mouse position, on a gray background.');
2384+
* }
2385+
*
2386+
* function draw(){
2387+
* background(200);
2388+
*
2389+
* // Draw the grid in the middle of the screen.
2390+
* translate(-cols*cellSize/2, -rows*cellSize/2);
2391+
* beginShape(QUADS);
2392+
* for (let i = 0; i < cols; i++) {
2393+
* for (let j = 0; j < rows; j++) {
2394+
*
2395+
* // Calculate the cell position.
2396+
* let x = i * cellSize;
2397+
* let y = j * cellSize;
2398+
*
2399+
* fill(j/rows*255, j/cols*255, 255);
2400+
*
2401+
* // Calculate the distance from the corner of each cell to the mouse.
2402+
* let distance = dist(x1,y1, mouseX, mouseY);
2403+
*
2404+
* // Send the distance to the shader.
2405+
* vertexProperty('aDistance', min(distance, 100));
2406+
*
2407+
* vertex(x, y);
2408+
* vertex(x + cellSize, y);
2409+
* vertex(x + cellSize, y + cellSize);
2410+
* vertex(x, y + cellSize);
2411+
* }
2412+
* }
2413+
* endShape();
2414+
* }
2415+
* </code>
2416+
* </div>
2417+
*
2418+
* @method vertexProperty
2419+
* @param {String} attributeName the name of the vertex attribute.
2420+
* @param {Number|Number[]} data the data tied to the vertex attribute.
2421+
*/
2422+
p5.prototype.vertexProperty = function(attributeName, data){
2423+
// this._assert3d('vertexProperty');
2424+
// p5._validateParameters('vertexProperty', arguments);
2425+
this._renderer.vertexProperty(attributeName, data);
2426+
};
2427+
22562428
export default p5;

0 commit comments

Comments
 (0)