-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
Copy path3d_primitives.js
2214 lines (2071 loc) · 62.1 KB
/
3d_primitives.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @module Shape
* @submodule 3D Primitives
* @for p5
* @requires core
* @requires p5.Geometry
*/
import p5 from '../core/main';
import './p5.Geometry';
import * as constants from '../core/constants';
/**
* Starts creating a new p5.Geometry. Subsequent shapes drawn will be added
* to the geometry and then returned when
* <a href="#/p5/endGeometry">endGeometry()</a> is called. One can also use
* <a href="#/p5/buildGeometry">buildGeometry()</a> to pass a function that
* draws shapes.
*
* If you need to draw complex shapes every frame which don't change over time,
* combining them upfront with `beginGeometry()` and `endGeometry()` and then
* drawing that will run faster than repeatedly drawing the individual pieces.
*
* @method beginGeometry
*
* @example
* <div>
* <code>
* let shapes;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
* makeShapes();
* }
*
* function makeShapes() {
* beginGeometry();
* scale(0.18);
*
* push();
* translate(100, -50);
* scale(0.5);
* rotateX(PI/4);
* cone();
* pop();
* cone();
*
* beginShape();
* vertex(-20, -50);
* quadraticVertex(
* -40, -70,
* 0, -60
* );
* endShape();
*
* beginShape(TRIANGLE_STRIP);
* for (let y = 20; y <= 60; y += 10) {
* for (let x of [20, 60]) {
* vertex(x, y);
* }
* }
* endShape();
*
* beginShape();
* vertex(-100, -120);
* vertex(-120, -110);
* vertex(-105, -100);
* endShape();
*
* shapes = endGeometry();
* }
*
* function draw() {
* background(255);
* lights();
* orbitControl();
* model(shapes);
* }
* </code>
* </div>
*
* @alt
* A series of different flat, curved, and 3D shapes floating in space.
*/
p5.prototype.beginGeometry = function() {
return this._renderer.beginGeometry();
};
/**
* Finishes creating a new <a href="#/p5.Geometry">p5.Geometry</a> that was
* started using <a href="#/p5/beginGeometry">beginGeometry()</a>. One can also
* use <a href="#/p5/buildGeometry">buildGeometry()</a> to pass a function that
* draws shapes.
*
* @method endGeometry
* @returns {p5.Geometry} The model that was built.
*/
p5.prototype.endGeometry = function() {
return this._renderer.endGeometry();
};
/**
* Creates a new <a href="#/p5.Geometry">p5.Geometry</a> that contains all
* the shapes drawn in a provided callback function. The returned combined shape
* can then be drawn all at once using <a href="#/p5/model">model()</a>.
*
* If you need to draw complex shapes every frame which don't change over time,
* combining them with `buildGeometry()` once and then drawing that will run
* faster than repeatedly drawing the individual pieces.
*
* One can also draw shapes directly between
* <a href="#/p5/beginGeometry">beginGeometry()</a> and
* <a href="#/p5/endGeometry">endGeometry()</a> instead of using a callback
* function.
*
* @method buildGeometry
* @param {Function} callback A function that draws shapes.
* @returns {p5.Geometry} The model that was built from the callback function.
*
* @example
* <div>
* <code>
* let particles;
* let button;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
* button = createButton('New');
* button.mousePressed(makeParticles);
* makeParticles();
* }
*
* function makeParticles() {
* if (particles) freeGeometry(particles);
*
* particles = buildGeometry(() => {
* for (let i = 0; i < 60; i++) {
* push();
* translate(
* randomGaussian(0, 20),
* randomGaussian(0, 20),
* randomGaussian(0, 20)
* );
* sphere(5);
* pop();
* }
* });
* }
*
* function draw() {
* background(255);
* noStroke();
* lights();
* orbitControl();
* model(particles);
* }
* </code>
* </div>
*
* @alt
* A cluster of spheres.
*/
p5.prototype.buildGeometry = function(callback) {
return this._renderer.buildGeometry(callback);
};
/**
* Clears the resources of a model to free up browser memory. A model whose
* resources have been cleared can still be drawn, but the first time it is
* drawn again, it might take longer.
*
* This method works on models generated with
* <a href="#/p5/buildGeometry">buildGeometry()</a> as well as those loaded
* from <a href="#/p5/loadModel">loadModel()</a>.
*
* @method freeGeometry
* @param {p5.Geometry} geometry The geometry whose resources should be freed
*/
p5.prototype.freeGeometry = function(geometry) {
this._renderer._freeBuffers(geometry.gid);
};
/**
* Draw a plane with given a width and height
* @method plane
* @param {Number} [width] width of the plane
* @param {Number} [height] height of the plane
* @param {Integer} [detailX] Optional number of triangle
* subdivisions in x-dimension
* @param {Integer} [detailY] Optional number of triangle
* subdivisions in y-dimension
* @chainable
* @example
* <div>
* <code>
* // draw a plane
* // with width 50 and height 50
* function setup() {
* createCanvas(100, 100, WEBGL);
* describe('a white plane with black wireframe lines');
* }
*
* function draw() {
* background(200);
* plane(50, 50);
* }
* </code>
* </div>
*
* @alt
* Nothing displayed on canvas
* Rotating interior view of a box with sides that change color.
* 3d red and green gradient.
* Rotating interior view of a cylinder with sides that change color.
* Rotating view of a cylinder with sides that change color.
* 3d red and green gradient.
* rotating view of a multi-colored cylinder with concave sides.
*/
p5.prototype.plane = function(width, height, detailX, detailY) {
this._assert3d('plane');
p5._validateParameters('plane', arguments);
if (typeof width === 'undefined') {
width = 50;
}
if (typeof height === 'undefined') {
height = width;
}
if (typeof detailX === 'undefined') {
detailX = 1;
}
if (typeof detailY === 'undefined') {
detailY = 1;
}
const gId = `plane|${detailX}|${detailY}`;
if (!this._renderer.geometryInHash(gId)) {
const _plane = function() {
let u, v, p;
for (let i = 0; i <= this.detailY; i++) {
v = i / this.detailY;
for (let j = 0; j <= this.detailX; j++) {
u = j / this.detailX;
p = new p5.Vector(u - 0.5, v - 0.5, 0);
this.vertices.push(p);
this.uvs.push(u, v);
}
}
};
const planeGeom = new p5.Geometry(detailX, detailY, _plane);
planeGeom.computeFaces().computeNormals();
if (detailX <= 1 && detailY <= 1) {
planeGeom._makeTriangleEdges()._edgesToVertices();
} else if (this._renderer._doStroke) {
console.log(
'Cannot draw stroke on plane objects with more' +
' than 1 detailX or 1 detailY'
);
}
this._renderer.createBuffers(gId, planeGeom);
}
this._renderer.drawBuffersScaled(gId, width, height, 1);
return this;
};
/**
* Draw a box with given width, height and depth
* @method box
* @param {Number} [width] width of the box
* @param {Number} [height] height of the box
* @param {Number} [depth] depth of the box
* @param {Integer} [detailX] Optional number of triangle
* subdivisions in x-dimension
* @param {Integer} [detailY] Optional number of triangle
* subdivisions in y-dimension
* @chainable
* @example
* <div>
* <code>
* // draw a spinning box
* // with width, height and depth of 50
* function setup() {
* createCanvas(100, 100, WEBGL);
* describe('a white box rotating in 3D space');
* }
*
* function draw() {
* background(200);
* rotateX(frameCount * 0.01);
* rotateY(frameCount * 0.01);
* box(50);
* }
* </code>
* </div>
*/
p5.prototype.box = function(width, height, depth, detailX, detailY) {
this._assert3d('box');
p5._validateParameters('box', arguments);
if (typeof width === 'undefined') {
width = 50;
}
if (typeof height === 'undefined') {
height = width;
}
if (typeof depth === 'undefined') {
depth = height;
}
const perPixelLighting =
this._renderer.attributes && this._renderer.attributes.perPixelLighting;
if (typeof detailX === 'undefined') {
detailX = perPixelLighting ? 1 : 4;
}
if (typeof detailY === 'undefined') {
detailY = perPixelLighting ? 1 : 4;
}
const gId = `box|${detailX}|${detailY}`;
if (!this._renderer.geometryInHash(gId)) {
const _box = function() {
const cubeIndices = [
[0, 4, 2, 6], // -1, 0, 0],// -x
[1, 3, 5, 7], // +1, 0, 0],// +x
[0, 1, 4, 5], // 0, -1, 0],// -y
[2, 6, 3, 7], // 0, +1, 0],// +y
[0, 2, 1, 3], // 0, 0, -1],// -z
[4, 5, 6, 7] // 0, 0, +1] // +z
];
//using custom edges
//to avoid diagonal stroke lines across face of box
this.edges = [
[0, 1],
[1, 3],
[3, 2],
[6, 7],
[8, 9],
[9, 11],
[14, 15],
[16, 17],
[17, 19],
[18, 19],
[20, 21],
[22, 23]
];
cubeIndices.forEach((cubeIndex, i) => {
const v = i * 4;
for (let j = 0; j < 4; j++) {
const d = cubeIndex[j];
//inspired by lightgl:
//https://github.com/evanw/lightgl.js
//octants:https://en.wikipedia.org/wiki/Octant_(solid_geometry)
const octant = new p5.Vector(
((d & 1) * 2 - 1) / 2,
((d & 2) - 1) / 2,
((d & 4) / 2 - 1) / 2
);
this.vertices.push(octant);
this.uvs.push(j & 1, (j & 2) / 2);
}
this.faces.push([v, v + 1, v + 2]);
this.faces.push([v + 2, v + 1, v + 3]);
});
};
const boxGeom = new p5.Geometry(detailX, detailY, _box);
boxGeom.computeNormals();
if (detailX <= 4 && detailY <= 4) {
boxGeom._edgesToVertices();
} else if (this._renderer._doStroke) {
console.log(
'Cannot draw stroke on box objects with more' +
' than 4 detailX or 4 detailY'
);
}
//initialize our geometry buffer with
//the key val pair:
//geometry Id, Geom object
this._renderer.createBuffers(gId, boxGeom);
}
this._renderer.drawBuffersScaled(gId, width, height, depth);
return this;
};
/**
* Draw a sphere with given radius.
*
* DetailX and detailY determines the number of subdivisions in the x-dimension
* and the y-dimension of a sphere. More subdivisions make the sphere seem
* smoother. The recommended maximum values are both 24. Using a value greater
* than 24 may cause a warning or slow down the browser.
* @method sphere
* @param {Number} [radius] radius of circle
* @param {Integer} [detailX] optional number of subdivisions in x-dimension
* @param {Integer} [detailY] optional number of subdivisions in y-dimension
*
* @chainable
* @example
* <div>
* <code>
* // draw a sphere with radius 40
* function setup() {
* createCanvas(100, 100, WEBGL);
* describe('a white sphere with black wireframe lines');
* }
*
* function draw() {
* background(205, 102, 94);
* sphere(40);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* let detailX;
* // slide to see how detailX works
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailX = createSlider(3, 24, 3);
* detailX.position(10, height + 5);
* detailX.style('width', '80px');
* describe(
* 'a white sphere with low detail on the x-axis, including a slider to adjust detailX'
* );
* }
*
* function draw() {
* background(205, 105, 94);
* rotateY(millis() / 1000);
* sphere(40, detailX.value(), 16);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* let detailY;
* // slide to see how detailY works
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailY = createSlider(3, 16, 3);
* detailY.position(10, height + 5);
* detailY.style('width', '80px');
* describe(
* 'a white sphere with low detail on the y-axis, including a slider to adjust detailY'
* );
* }
*
* function draw() {
* background(205, 105, 94);
* rotateY(millis() / 1000);
* sphere(40, 16, detailY.value());
* }
* </code>
* </div>
*/
p5.prototype.sphere = function(radius, detailX, detailY) {
this._assert3d('sphere');
p5._validateParameters('sphere', arguments);
if (typeof radius === 'undefined') {
radius = 50;
}
if (typeof detailX === 'undefined') {
detailX = 24;
}
if (typeof detailY === 'undefined') {
detailY = 16;
}
this.ellipsoid(radius, radius, radius, detailX, detailY);
return this;
};
/**
* @private
* Helper function for creating both cones and cylinders
* Will only generate well-defined geometry when bottomRadius, height > 0
* and topRadius >= 0
* If topRadius == 0, topCap should be false
*/
const _truncatedCone = function(
bottomRadius,
topRadius,
height,
detailX,
detailY,
bottomCap,
topCap
) {
bottomRadius = bottomRadius <= 0 ? 1 : bottomRadius;
topRadius = topRadius < 0 ? 0 : topRadius;
height = height <= 0 ? bottomRadius : height;
detailX = detailX < 3 ? 3 : detailX;
detailY = detailY < 1 ? 1 : detailY;
bottomCap = bottomCap === undefined ? true : bottomCap;
topCap = topCap === undefined ? topRadius !== 0 : topCap;
const start = bottomCap ? -2 : 0;
const end = detailY + (topCap ? 2 : 0);
//ensure constant slant for interior vertex normals
const slant = Math.atan2(bottomRadius - topRadius, height);
const sinSlant = Math.sin(slant);
const cosSlant = Math.cos(slant);
let yy, ii, jj;
for (yy = start; yy <= end; ++yy) {
let v = yy / detailY;
let y = height * v;
let ringRadius;
if (yy < 0) {
//for the bottomCap edge
y = 0;
v = 0;
ringRadius = bottomRadius;
} else if (yy > detailY) {
//for the topCap edge
y = height;
v = 1;
ringRadius = topRadius;
} else {
//for the middle
ringRadius = bottomRadius + (topRadius - bottomRadius) * v;
}
if (yy === -2 || yy === detailY + 2) {
//center of bottom or top caps
ringRadius = 0;
}
y -= height / 2; //shift coordiate origin to the center of object
for (ii = 0; ii < detailX; ++ii) {
const u = ii / (detailX - 1);
const ur = 2 * Math.PI * u;
const sur = Math.sin(ur);
const cur = Math.cos(ur);
//VERTICES
this.vertices.push(new p5.Vector(sur * ringRadius, y, cur * ringRadius));
//VERTEX NORMALS
let vertexNormal;
if (yy < 0) {
vertexNormal = new p5.Vector(0, -1, 0);
} else if (yy > detailY && topRadius) {
vertexNormal = new p5.Vector(0, 1, 0);
} else {
vertexNormal = new p5.Vector(sur * cosSlant, sinSlant, cur * cosSlant);
}
this.vertexNormals.push(vertexNormal);
//UVs
this.uvs.push(u, v);
}
}
let startIndex = 0;
if (bottomCap) {
for (jj = 0; jj < detailX; ++jj) {
const nextjj = (jj + 1) % detailX;
this.faces.push([
startIndex + jj,
startIndex + detailX + nextjj,
startIndex + detailX + jj
]);
}
startIndex += detailX * 2;
}
for (yy = 0; yy < detailY; ++yy) {
for (ii = 0; ii < detailX; ++ii) {
const nextii = (ii + 1) % detailX;
this.faces.push([
startIndex + ii,
startIndex + nextii,
startIndex + detailX + nextii
]);
this.faces.push([
startIndex + ii,
startIndex + detailX + nextii,
startIndex + detailX + ii
]);
}
startIndex += detailX;
}
if (topCap) {
startIndex += detailX;
for (ii = 0; ii < detailX; ++ii) {
this.faces.push([
startIndex + ii,
startIndex + (ii + 1) % detailX,
startIndex + detailX
]);
}
}
};
/**
* Draw a cylinder with given radius and height
*
* DetailX and detailY determines the number of subdivisions in the x-dimension
* and the y-dimension of a cylinder. More subdivisions make the cylinder seem smoother.
* The recommended maximum value for detailX is 24. Using a value greater than 24
* may cause a warning or slow down the browser.
*
* @method cylinder
* @param {Number} [radius] radius of the surface
* @param {Number} [height] height of the cylinder
* @param {Integer} [detailX] number of subdivisions in x-dimension;
* default is 24
* @param {Integer} [detailY] number of subdivisions in y-dimension;
* default is 1
* @param {Boolean} [bottomCap] whether to draw the bottom of the cylinder
* @param {Boolean} [topCap] whether to draw the top of the cylinder
* @chainable
* @example
* <div>
* <code>
* // draw a spinning cylinder
* // with radius 20 and height 50
* function setup() {
* createCanvas(100, 100, WEBGL);
* describe('a rotating white cylinder');
* }
*
* function draw() {
* background(205, 105, 94);
* rotateX(frameCount * 0.01);
* rotateZ(frameCount * 0.01);
* cylinder(20, 50);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* // slide to see how detailX works
* let detailX;
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailX = createSlider(3, 24, 3);
* detailX.position(10, height + 5);
* detailX.style('width', '80px');
* describe(
* 'a rotating white cylinder with limited X detail, with a slider that adjusts detailX'
* );
* }
*
* function draw() {
* background(205, 105, 94);
* rotateY(millis() / 1000);
* cylinder(20, 75, detailX.value(), 1);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* // slide to see how detailY works
* let detailY;
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailY = createSlider(1, 16, 1);
* detailY.position(10, height + 5);
* detailY.style('width', '80px');
* describe(
* 'a rotating white cylinder with limited Y detail, with a slider that adjusts detailY'
* );
* }
*
* function draw() {
* background(205, 105, 94);
* rotateY(millis() / 1000);
* cylinder(20, 75, 16, detailY.value());
* }
* </code>
* </div>
*/
p5.prototype.cylinder = function(
radius,
height,
detailX,
detailY,
bottomCap,
topCap
) {
this._assert3d('cylinder');
p5._validateParameters('cylinder', arguments);
if (typeof radius === 'undefined') {
radius = 50;
}
if (typeof height === 'undefined') {
height = radius;
}
if (typeof detailX === 'undefined') {
detailX = 24;
}
if (typeof detailY === 'undefined') {
detailY = 1;
}
if (typeof topCap === 'undefined') {
topCap = true;
}
if (typeof bottomCap === 'undefined') {
bottomCap = true;
}
const gId = `cylinder|${detailX}|${detailY}|${bottomCap}|${topCap}`;
if (!this._renderer.geometryInHash(gId)) {
const cylinderGeom = new p5.Geometry(detailX, detailY);
_truncatedCone.call(
cylinderGeom,
1,
1,
1,
detailX,
detailY,
bottomCap,
topCap
);
// normals are computed in call to _truncatedCone
if (detailX <= 24 && detailY <= 16) {
cylinderGeom._makeTriangleEdges()._edgesToVertices();
} else if (this._renderer._doStroke) {
console.log(
'Cannot draw stroke on cylinder objects with more' +
' than 24 detailX or 16 detailY'
);
}
this._renderer.createBuffers(gId, cylinderGeom);
}
this._renderer.drawBuffersScaled(gId, radius, height, radius);
return this;
};
/**
* Draw a cone with given radius and height
*
* DetailX and detailY determine the number of subdivisions in the x-dimension and
* the y-dimension of a cone. More subdivisions make the cone seem smoother. The
* recommended maximum value for detailX is 24. Using a value greater than 24
* may cause a warning or slow down the browser.
* @method cone
* @param {Number} [radius] radius of the bottom surface
* @param {Number} [height] height of the cone
* @param {Integer} [detailX] number of segments,
* the more segments the smoother geometry
* default is 24
* @param {Integer} [detailY] number of segments,
* the more segments the smoother geometry
* default is 1
* @param {Boolean} [cap] whether to draw the base of the cone
* @chainable
* @example
* <div>
* <code>
* // draw a spinning cone
* // with radius 40 and height 70
* function setup() {
* createCanvas(100, 100, WEBGL);
* describe('a rotating white cone');
* }
*
* function draw() {
* background(200);
* rotateX(frameCount * 0.01);
* rotateZ(frameCount * 0.01);
* cone(40, 70);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* // slide to see how detailx works
* let detailX;
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailX = createSlider(3, 16, 3);
* detailX.position(10, height + 5);
* detailX.style('width', '80px');
* describe(
* 'a rotating white cone with limited X detail, with a slider that adjusts detailX'
* );
* }
*
* function draw() {
* background(205, 102, 94);
* rotateY(millis() / 1000);
* cone(30, 65, detailX.value(), 16);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* // slide to see how detailY works
* let detailY;
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailY = createSlider(3, 16, 3);
* detailY.position(10, height + 5);
* detailY.style('width', '80px');
* describe(
* 'a rotating white cone with limited Y detail, with a slider that adjusts detailY'
* );
* }
*
* function draw() {
* background(205, 102, 94);
* rotateY(millis() / 1000);
* cone(30, 65, 16, detailY.value());
* }
* </code>
* </div>
*/
p5.prototype.cone = function(radius, height, detailX, detailY, cap) {
this._assert3d('cone');
p5._validateParameters('cone', arguments);
if (typeof radius === 'undefined') {
radius = 50;
}
if (typeof height === 'undefined') {
height = radius;
}
if (typeof detailX === 'undefined') {
detailX = 24;
}
if (typeof detailY === 'undefined') {
detailY = 1;
}
if (typeof cap === 'undefined') {
cap = true;
}
const gId = `cone|${detailX}|${detailY}|${cap}`;
if (!this._renderer.geometryInHash(gId)) {
const coneGeom = new p5.Geometry(detailX, detailY);
_truncatedCone.call(coneGeom, 1, 0, 1, detailX, detailY, cap, false);
if (detailX <= 24 && detailY <= 16) {
coneGeom._makeTriangleEdges()._edgesToVertices();
} else if (this._renderer._doStroke) {
console.log(
'Cannot draw stroke on cone objects with more' +
' than 24 detailX or 16 detailY'
);
}
this._renderer.createBuffers(gId, coneGeom);
}
this._renderer.drawBuffersScaled(gId, radius, height, radius);
return this;
};
/**
* Draw an ellipsoid with given radius
*
* DetailX and detailY determine the number of subdivisions in the x-dimension and
* the y-dimension of a cone. More subdivisions make the ellipsoid appear to be smoother.
* Avoid detail number above 150, it may crash the browser.
* @method ellipsoid
* @param {Number} [radiusx] x-radius of ellipsoid
* @param {Number} [radiusy] y-radius of ellipsoid
* @param {Number} [radiusz] z-radius of ellipsoid
* @param {Integer} [detailX] number of segments,
* the more segments the smoother geometry
* default is 24. Avoid detail number above
* 150, it may crash the browser.
* @param {Integer} [detailY] number of segments,
* the more segments the smoother geometry
* default is 16. Avoid detail number above
* 150, it may crash the browser.
* @chainable
* @example
* <div>
* <code>
* // draw an ellipsoid
* // with radius 30, 40 and 40.
* function setup() {
* createCanvas(100, 100, WEBGL);
* describe('a white 3d ellipsoid');
* }
*
* function draw() {
* background(205, 105, 94);
* ellipsoid(30, 40, 40);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* // slide to see how detailX works
* let detailX;
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailX = createSlider(2, 24, 12);
* detailX.position(10, height + 5);
* detailX.style('width', '80px');
* describe(
* 'a rotating white ellipsoid with limited X detail, with a slider that adjusts detailX'
* );
* }
*
* function draw() {
* background(205, 105, 94);
* rotateY(millis() / 1000);
* ellipsoid(30, 40, 40, detailX.value(), 8);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* // slide to see how detailY works
* let detailY;
* function setup() {
* createCanvas(100, 100, WEBGL);
* detailY = createSlider(2, 24, 6);
* detailY.position(10, height + 5);
* detailY.style('width', '80px');
* describe(
* 'a rotating white ellipsoid with limited Y detail, with a slider that adjusts detailY'
* );
* }
*
* function draw() {
* background(205, 105, 9);
* rotateY(millis() / 1000);
* ellipsoid(30, 40, 40, 12, detailY.value());
* }
* </code>
* </div>
*/
p5.prototype.ellipsoid = function(radiusX, radiusY, radiusZ, detailX, detailY) {
this._assert3d('ellipsoid');
p5._validateParameters('ellipsoid', arguments);
if (typeof radiusX === 'undefined') {
radiusX = 50;
}
if (typeof radiusY === 'undefined') {
radiusY = radiusX;
}
if (typeof radiusZ === 'undefined') {
radiusZ = radiusX;
}
if (typeof detailX === 'undefined') {
detailX = 24;
}
if (typeof detailY === 'undefined') {
detailY = 16;
}
const gId = `ellipsoid|${detailX}|${detailY}`;
if (!this._renderer.geometryInHash(gId)) {
const _ellipsoid = function() {
for (let i = 0; i <= this.detailY; i++) {
const v = i / this.detailY;
const phi = Math.PI * v - Math.PI / 2;
const cosPhi = Math.cos(phi);
const sinPhi = Math.sin(phi);
for (let j = 0; j <= this.detailX; j++) {
const u = j / this.detailX;
const theta = 2 * Math.PI * u;
const cosTheta = Math.cos(theta);
const sinTheta = Math.sin(theta);
const p = new p5.Vector(cosPhi * sinTheta, sinPhi, cosPhi * cosTheta);
this.vertices.push(p);
this.vertexNormals.push(p);
this.uvs.push(u, v);
}
}
};
const ellipsoidGeom = new p5.Geometry(detailX, detailY, _ellipsoid);
ellipsoidGeom.computeFaces();
if (detailX <= 24 && detailY <= 24) {
ellipsoidGeom._makeTriangleEdges()._edgesToVertices();
} else if (this._renderer._doStroke) {
console.log(
'Cannot draw stroke on ellipsoids with more' +
' than 24 detailX or 24 detailY'
);
}
this._renderer.createBuffers(gId, ellipsoidGeom);
}
this._renderer.drawBuffersScaled(gId, radiusX, radiusY, radiusZ);