-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add support for webGL instancing #6276
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
Changes from all commits
e61c8e3
6874098
629ee21
a655759
c8e799a
6eda10d
4626423
988db4b
c5ad6a5
6c9557e
4bd9209
f407425
cb18f99
db5bfea
2f7a480
3563716
72e7b6a
8f219ef
c90f1b7
3b8da42
d6c7271
79e1f92
3c46ec0
38788a0
f61bcd5
7a0dbcf
371e1fe
cf20cd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -592,11 +592,19 @@ p5.prototype.endContour = function() { | |
* The <a href="#/p5/endShape">endShape()</a> function is the companion to <a href="#/p5/beginShape">beginShape()</a> and may only be | ||
* called after <a href="#/p5/beginShape">beginShape()</a>. When <a href="#/p5/endshape">endShape()</a> is called, all of the image | ||
* data defined since the previous call to <a href="#/p5/beginShape">beginShape()</a> is written into the image | ||
* buffer. The constant CLOSE as the value for the `mode` parameter to close | ||
* buffer. The constant CLOSE is the value for the `mode` parameter to close | ||
* the shape (to connect the beginning and the end). | ||
* When using instancing with <a href="#/p5/endShape">endShape()</a> the instancing will not apply to the strokes. | ||
* When the count parameter is used with a value greater than 1, it enables instancing for shapes built when in WEBGL mode. Instancing | ||
* is a feature that allows the GPU to efficiently draw multiples of the same shape. It's often used for particle effects or other | ||
* times when you need a lot of repetition. In order to take advantage of instancing, you will also need to write your own custom | ||
* shader using the gl_InstanceID keyword. You can read more about instancing | ||
* <a href="https://webglfundamentals.org/webgl/lessons/webgl-instanced-drawing.html">here</a> or by working from the example on this | ||
* page. | ||
* | ||
* @method endShape | ||
* @param {Constant} [mode] use CLOSE to close the shape | ||
* @param {Integer} [count] number of times you want to draw/instance the shape (for WebGL mode). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be renamed to "instanceCount" or something a little more descriptive? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The problem I have with the name There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If u look at the rest of the documentation most of the parameter names are decently simple. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was mainly thinking about the renaming in the context of helping future maintainers understand what this is for. I still think |
||
* @chainable | ||
* @example | ||
* <div> | ||
|
@@ -617,21 +625,105 @@ p5.prototype.endContour = function() { | |
* </code> | ||
* </div> | ||
* | ||
* @example | ||
* <div> | ||
* <code> | ||
* let fx; | ||
* let vs = `#version 300 es | ||
* | ||
* precision mediump float; | ||
* | ||
* in vec3 aPosition; | ||
* flat out int instanceID; | ||
* | ||
* uniform mat4 uModelViewMatrix; | ||
* uniform mat4 uProjectionMatrix; | ||
* | ||
* void main() { | ||
* | ||
* // copy the instance ID to the fragment shader | ||
* instanceID = gl_InstanceID; | ||
* vec4 positionVec4 = vec4(aPosition, 1.0); | ||
* | ||
* // gl_InstanceID represents a numeric value for each instance | ||
* // using gl_InstanceID allows us to move each instance separately | ||
* // here we move each instance horizontally by id * 100 | ||
* float xOffset = float(gl_InstanceID) * 100.0; | ||
* | ||
* // apply the offset to the final position | ||
* gl_Position = uProjectionMatrix * uModelViewMatrix * positionVec4 - | ||
* vec4(xOffset, 0.0, 0.0, 0.0); | ||
* } | ||
* `; | ||
* let fs = `#version 300 es | ||
* | ||
* precision mediump float; | ||
* | ||
* out vec4 outColor; | ||
* flat in int instanceID; | ||
* uniform float numInstances; | ||
* | ||
* void main() { | ||
* vec4 red = vec4(1.0, 0.0, 0.0, 1.0); | ||
* vec4 blue = vec4(0.0, 0.0, 1.0, 1.0); | ||
* | ||
* // Normalize the instance id | ||
* float normId = float(instanceID) / numInstances; | ||
* | ||
* // Mix between two colors using the normalized instance id | ||
* outColor = mix(red, blue, normId); | ||
* } | ||
* `; | ||
* | ||
* function setup() { | ||
* createCanvas(400, 400, WEBGL); | ||
* fx = createShader(vs, fs); | ||
* } | ||
* | ||
* function draw() { | ||
* background(220); | ||
* | ||
* // strokes aren't instanced, and are rather used for debug purposes | ||
RandomGamingDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* shader(fx); | ||
* fx.setUniform('numInstances', 4); | ||
* | ||
* beginShape(); | ||
* vertex(30, 20); | ||
* vertex(85, 20); | ||
* vertex(85, 75); | ||
* vertex(30, 75); | ||
* vertex(30, 20); | ||
* endShape(CLOSE, 4); | ||
* | ||
* resetShader(); | ||
* } | ||
* </code> | ||
* </div> | ||
* | ||
* @alt | ||
* Triangle line shape with smallest interior angle on bottom and upside-down L. | ||
*/ | ||
p5.prototype.endShape = function(mode) { | ||
p5.prototype.endShape = function(mode, count = 1) { | ||
p5._validateParameters('endShape', arguments); | ||
if (count < 1) { | ||
console.log('🌸 p5.js says: You can not have less than one instance'); | ||
count = 1; | ||
} | ||
|
||
if (this._renderer.isP3D) { | ||
this._renderer.endShape( | ||
mode, | ||
isCurve, | ||
isBezier, | ||
isQuadratic, | ||
isContour, | ||
shapeKind | ||
shapeKind, | ||
count | ||
); | ||
} else { | ||
if (count !== 1) { | ||
RandomGamingDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log('🌸 p5.js says: Instancing is only supported in WebGL2 mode'); | ||
aferriss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
if (vertices.length === 0) { | ||
return this; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.