@@ -532,7 +532,7 @@ export class Renderer3D extends Renderer {
532532 }
533533
534534 endShape ( mode , count ) {
535- this . drawShapeCount = count ;
535+ this . drawShapeCount = count ?? this . _instanceCount ?? 1 ;
536536 super . endShape ( mode , count ) ;
537537 }
538538
@@ -2038,61 +2038,55 @@ function renderer3D(p5, fn) {
20382038 * item is accessed by index, and its properties are available by name.
20392039 *
20402040 * ```js example
2041- * let instanceData;
2042- * let instancesShader;
2043- * let instance;
2044- * let count = 5;
2045- *
2046- * async function setup() {
2047- * await createCanvas(200, 200, WEBGPU);
2048- *
2049- * let data = [];
2050- * for (let i = 0; i < count; i++) {
2051- * data.push({
2052- * position: createVector(
2053- * random(-1, 1) * width / 2,
2054- * random(-1, 1) * height / 2,
2055- * 0,
2056- * ),
2057- * color: color(
2058- * random(255),
2059- * random(255),
2060- * random(255)
2061- * )
2062- * });
2063- * }
2064- * instanceData = createStorage(data);
2065- * instance = buildGeometry(drawInstance);
2066- * instancesShader = buildMaterialShader(drawInstances);
2067- * describe('Five spheres at random positions, each a different random color.');
2068- * }
2069- *
2070- * function drawInstance() {
2071- * sphere(15);
2072- * }
2073- *
2074- * function drawInstances() {
2075- * let data = uniformStorage(instanceData);
2076- * let itemColor = sharedVec4();
2077- *
2078- * worldInputs.begin();
2079- * let item = data[instanceID()];
2080- * itemColor = item.color;
2081- * worldInputs.position += item.position;
2082- * worldInputs.end();
2083- *
2084- * finalColor.begin();
2085- * finalColor.set(itemColor);
2086- * finalColor.end();
2087- * }
2088- *
2089- * function draw() {
2090- * background(220);
2091- * lights();
2092- * noStroke();
2093- * shader(instancesShader);
2094- * model(instance, count);
2095- * }
2041+ * let instanceData;
2042+ * let instancesShader;
2043+ * let count = 5;
2044+ *
2045+ * async function setup() {
2046+ * await createCanvas(200, 200, WEBGPU);
2047+ *
2048+ * let data = [];
2049+ * for (let i = 0; i < count; i++) {
2050+ * data.push({
2051+ * position: createVector(
2052+ * random(-1, 1) * width / 2,
2053+ * random(-1, 1) * height / 2,
2054+ * 0,
2055+ * ),
2056+ * color: color(
2057+ * random(255),
2058+ * random(255),
2059+ * random(255)
2060+ * )
2061+ * });
2062+ * }
2063+ * instanceData = createStorage(data);
2064+ * instancesShader = buildMaterialShader(drawInstances);
2065+ * describe('Five spheres at random positions, each a different random color.');
2066+ * }
2067+ *
2068+ * function drawInstances() {
2069+ * let data = uniformStorage(instanceData);
2070+ * let itemColor = sharedVec4();
2071+ *
2072+ * worldInputs.begin();
2073+ * let item = data[instanceIndex];
2074+ * itemColor = item.color;
2075+ * worldInputs.position += item.position;
2076+ * worldInputs.end();
2077+ *
2078+ * finalColor.begin();
2079+ * finalColor.set(itemColor);
2080+ * finalColor.end();
2081+ * }
2082+ *
2083+ * function draw() {
2084+ * background(220);
2085+ * lights();
2086+ * noStroke();
2087+ * shader(instancesShader);
2088+ * instances(count).sphere(15);
2089+ * }
20962090 * ```
20972091 *
20982092 * You can also store a plain list of numbers by passing an array of numbers.
@@ -2246,23 +2240,21 @@ function renderer3D(p5, fn) {
22462240 * initial data. Connect your iteration function to the storage by passing the storage
22472241 * into <a href="#/p5/uniformStorage">`uniformStorage`</a>.
22482242 *
2249- * Often, compute shaders are paired with <a href="#/p5/model ">`model(myGeometry, count)`</a>
2243+ * Often, compute shaders are paired with <a href="#/p5/instances ">`instances( count)`</a>
22502244 * to draw one instance per object in the storage, and a shader that uses
2251- * <a href="#/p5/instanceID ">`instanceID() `</a> to position each instance.
2245+ * <a href="#/p5/instanceIndex ">`instanceIndex `</a> to position each instance.
22522246 *
22532247 * ```js example
22542248 * let particles;
22552249 * let computeShader;
22562250 * let displayShader;
2257- * let instance;
22582251 * const numParticles = 100;
22592252 *
22602253 * async function setup() {
22612254 * await createCanvas(100, 100, WEBGPU);
22622255 * particles = createStorage(makeParticles(width / 2, height / 2));
22632256 * computeShader = buildComputeShader(simulate);
22642257 * displayShader = buildMaterialShader(display);
2265- * instance = buildGeometry(drawParticle);
22662258 * describe('100 orange particles shooting outward.');
22672259 * }
22682260 *
@@ -2279,10 +2271,6 @@ function renderer3D(p5, fn) {
22792271 * return data;
22802272 * }
22812273 *
2282- * function drawParticle() {
2283- * sphere(2);
2284- * }
2285- *
22862274 * function simulate() {
22872275 * let data = uniformStorage(particles);
22882276 * let idx = index.x;
@@ -2292,7 +2280,7 @@ function renderer3D(p5, fn) {
22922280 * function display() {
22932281 * let data = uniformStorage(particles);
22942282 * worldInputs.begin();
2295- * let pos = data[instanceID() ].position;
2283+ * let pos = data[instanceIndex ].position;
22962284 * worldInputs.position.xy += pos - [width / 2, height / 2];
22972285 * worldInputs.end();
22982286 * }
@@ -2306,15 +2294,14 @@ function renderer3D(p5, fn) {
23062294 * noStroke();
23072295 * fill(255, 200, 50);
23082296 * shader(displayShader);
2309- * model(instance, numParticles);
2297+ * instances( numParticles).sphere(2 );
23102298 * }
23112299 * ```
23122300 *
23132301 * ```js example
23142302 * let particles;
23152303 * let computeShader;
23162304 * let displayShader;
2317- * let instance;
23182305 * const numParticles = 50;
23192306 *
23202307 * async function setup() {
@@ -2337,14 +2324,9 @@ function renderer3D(p5, fn) {
23372324 *
23382325 * computeShader = buildComputeShader(simulate);
23392326 * displayShader = buildMaterialShader(display);
2340- * instance = buildGeometry(drawParticle);
23412327 * describe('50 white spheres bouncing around the canvas.');
23422328 * }
23432329 *
2344- * function drawParticle() {
2345- * sphere(3);
2346- * }
2347- *
23482330 * function simulate() {
23492331 * let r = 3;
23502332 * let data = uniformStorage(particles);
@@ -2367,7 +2349,7 @@ function renderer3D(p5, fn) {
23672349 * function display() {
23682350 * let data = uniformStorage(particles);
23692351 * worldInputs.begin();
2370- * let pos = data[instanceID() ].position;
2352+ * let pos = data[instanceIndex ].position;
23712353 * worldInputs.position.xy += pos;
23722354 * worldInputs.end();
23732355 * }
@@ -2379,7 +2361,7 @@ function renderer3D(p5, fn) {
23792361 * fill(255);
23802362 * lights();
23812363 * shader(displayShader);
2382- * model(instance, numParticles);
2364+ * instances( numParticles).sphere(3 );
23832365 * }
23842366 * ```
23852367 *
@@ -2416,7 +2398,6 @@ function renderer3D(p5, fn) {
24162398 * let particles;
24172399 * let computeShader;
24182400 * let displayShader;
2419- * let instance;
24202401 * const numParticles = 50;
24212402 *
24222403 * async function setup() {
@@ -2439,14 +2420,9 @@ function renderer3D(p5, fn) {
24392420 *
24402421 * computeShader = buildComputeShader(simulate);
24412422 * displayShader = buildMaterialShader(display);
2442- * instance = buildGeometry(drawParticle);
24432423 * describe('50 white spheres bouncing around the canvas.');
24442424 * }
24452425 *
2446- * function drawParticle() {
2447- * sphere(3);
2448- * }
2449- *
24502426 * function simulate() {
24512427 * let r = 3;
24522428 * let data = uniformStorage(particles);
@@ -2469,7 +2445,7 @@ function renderer3D(p5, fn) {
24692445 * function display() {
24702446 * let data = uniformStorage(particles);
24712447 * worldInputs.begin();
2472- * let pos = data[instanceID() ].position;
2448+ * let pos = data[instanceIndex ].position;
24732449 * worldInputs.position.xy += pos;
24742450 * worldInputs.end();
24752451 * }
@@ -2481,7 +2457,7 @@ function renderer3D(p5, fn) {
24812457 * fill(255);
24822458 * lights();
24832459 * shader(displayShader);
2484- * model(instance, numParticles);
2460+ * instances( numParticles).sphere(3 );
24852461 * }
24862462 * ```
24872463 *
0 commit comments