-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
p5.Camera.js
2350 lines (2213 loc) · 67.9 KB
/
p5.Camera.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 3D
* @submodule Camera
* @requires core
*/
import p5 from '../core/main';
////////////////////////////////////////////////////////////////////////////////
// p5.Prototype Methods
////////////////////////////////////////////////////////////////////////////////
/**
* Sets the position of the current camera in a 3D sketch.
* Parameters for this function define the camera's position,
* the center of the sketch (where the camera is pointing),
* and an up direction (the orientation of the camera).
*
* This function simulates the movements of the camera, allowing objects to be
* viewed from various angles. Remember, it does not move the objects themselves
* but the camera instead. For example when the centerX value is positive,
* and the camera is rotating to the right side of the sketch,
* the object will seem like it's moving to the left.
*
* See this <a href = "https://www.openprocessing.org/sketch/740258">example</a>
* to view the position of your camera.
*
* If no parameters are given, the following default is used:
* camera(0, 0, (height/2) / tan(PI/6), 0, 0, 0, 0, 1, 0)
* @method camera
* @constructor
* @for p5
* @param {Number} [x] camera position value on x axis
* @param {Number} [y] camera position value on y axis
* @param {Number} [z] camera position value on z axis
* @param {Number} [centerX] x coordinate representing center of the sketch
* @param {Number} [centerY] y coordinate representing center of the sketch
* @param {Number} [centerZ] z coordinate representing center of the sketch
* @param {Number} [upX] x component of direction 'up' from camera
* @param {Number} [upY] y component of direction 'up' from camera
* @param {Number} [upZ] z component of direction 'up' from camera
* @chainable
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
* describe('a square moving closer and then away from the camera.');
* }
* function draw() {
* background(204);
* //move the camera away from the plane by a sin wave
* camera(0, 0, 20 + sin(frameCount * 0.01) * 10, 0, 0, 0, 0, 1, 0);
* plane(10, 10);
* }
* </code>
* </div>
*
* @example
* <div>
* <code>
* //move slider to see changes!
* //sliders control the first 6 parameters of camera()
* let sliderGroup = [];
* let X;
* let Y;
* let Z;
* let centerX;
* let centerY;
* let centerZ;
* let h = 20;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
* //create sliders
* for (var i = 0; i < 6; i++) {
* if (i === 2) {
* sliderGroup[i] = createSlider(10, 400, 200);
* } else {
* sliderGroup[i] = createSlider(-400, 400, 0);
* }
* h = map(i, 0, 6, 5, 85);
* sliderGroup[i].position(10, height + h);
* sliderGroup[i].style('width', '80px');
* }
* describe(
* 'White square repeatedly grows to fill canvas and then shrinks. An interactive example of a red cube with 3 sliders for moving it across x, y, z axis and 3 sliders for shifting its center.'
* );
* }
*
* function draw() {
* background(60);
* // assigning sliders' value to each parameters
* X = sliderGroup[0].value();
* Y = sliderGroup[1].value();
* Z = sliderGroup[2].value();
* centerX = sliderGroup[3].value();
* centerY = sliderGroup[4].value();
* centerZ = sliderGroup[5].value();
* camera(X, Y, Z, centerX, centerY, centerZ, 0, 1, 0);
* stroke(255);
* fill(255, 102, 94);
* box(85);
* }
* </code>
* </div>
* @alt
* White square repeatedly grows to fill canvas and then shrinks.
* An interactive example of a red cube with 3 sliders for moving it across x, y,
* z axis and 3 sliders for shifting its center.
*/
p5.prototype.camera = function (...args) {
this._assert3d('camera');
p5._validateParameters('camera', args);
this._renderer._curCamera.camera(...args);
return this;
};
/**
* Sets a perspective projection for the current camera in a 3D sketch.
* This projection represents depth through foreshortening: objects
* that are close to the camera appear their actual size while those
* that are further away from the camera appear smaller.
*
* The parameters to this function define the viewing frustum
* (the truncated pyramid within which objects are seen by the camera) through
* vertical field of view, aspect ratio (usually width/height), and near and far
* clipping planes.
*
* If no parameters are given, the default values are used as:
*
* - `fov` : The default field of view for the camera is such that the full height of renderer is visible when it is positioned at a default distance of 800 units from the camera.
* - `aspect` : The default aspect ratio is the ratio of renderer's width to renderer's height.
* - `near` : The default value for the near clipping plane is 80, which is 0.1 times the default distance from the camera to its subject.
* - `far` : The default value for the far clipping plane is 8000, which is 10 times the default distance from the camera to its subject.
*
* If you prefer a fixed field of view, follow these steps:
* 1. Choose your desired field of view angle (`fovy`). This is how wide the camera can see.
* 2. To ensure that you can see the entire width across horizontally and height across vertically, place the camera a distance of `(height / 2) / tan(fovy / 2)` back from its subject.
* 3. Call perspective with the chosen field of view, canvas aspect ratio, and near/far values:
* `perspective(fovy, width / height, cameraDistance / 10, cameraDistance * 10);`
*
* @method perspective
* @for p5
* @param {Number} [fovy] camera frustum vertical field of view,
* from bottom to top of view, in <a href="#/p5/angleMode">angleMode</a> units
* @param {Number} [aspect] camera frustum aspect ratio
* @param {Number} [near] frustum near plane length
* @param {Number} [far] frustum far plane length
* @chainable
* @example
* <div>
* <code>
* //drag the mouse to look around!
* function setup() {
* createCanvas(100, 100, WEBGL);
* perspective(PI / 3.0, width / height, 0.1, 500);
* describe(
* 'two colored 3D boxes move back and forth, rotating as mouse is dragged.'
* );
* }
* function draw() {
* background(200);
* orbitControl();
* normalMaterial();
*
* translate(0, 0, 550);
* rotateX(-0.3);
* rotateY(-0.2);
*
* push();
* translate(-15, 0, sin(frameCount / 30) * 95);
* box(30);
* pop();
* push();
* translate(15, 0, sin(frameCount / 30 + PI) * 95);
* box(30);
* pop();
* }
* </code>
* </div>
*
* @alt
* two colored 3D boxes move back and forth, rotating as mouse is dragged.
*/
p5.prototype.perspective = function (...args) {
this._assert3d('perspective');
p5._validateParameters('perspective', args);
this._renderer._curCamera.perspective(...args);
return this;
};
/**
* Sets an orthographic projection for the current camera in a 3D sketch
* and defines a box-shaped viewing frustum within which objects are seen.
* In this projection, all objects with the same dimension appear the same
* size, regardless of whether they are near or far from the camera.
*
* The parameters to this function specify the viewing frustum where
* left and right are the minimum and maximum x values, top and bottom are
* the minimum and maximum y values, and near and far are the minimum and
* maximum z values.
*
* If no parameters are given, the following default is used:
* ortho(-width/2, width/2, -height/2, height/2).
* @method ortho
* @for p5
* @param {Number} [left] camera frustum left plane
* @param {Number} [right] camera frustum right plane
* @param {Number} [bottom] camera frustum bottom plane
* @param {Number} [top] camera frustum top plane
* @param {Number} [near] camera frustum near plane
* @param {Number} [far] camera frustum far plane
* @chainable
* @example
* <div>
* <code>
* //drag the mouse to look around!
* //there's no vanishing point
* function setup() {
* createCanvas(100, 100, WEBGL);
* ortho(-width / 2, width / 2, height / 2, -height / 2, 0, 500);
* describe(
* 'two 3D boxes move back and forth along same plane, rotating as mouse is dragged.'
* );
* }
* function draw() {
* background(200);
* orbitControl();
* normalMaterial();
*
* translate(0,0,500);
* rotateX(0.2);
* rotateY(-0.2);
* push();
* translate(-15, 0, sin(frameCount / 30) * 65);
* box(30);
* pop();
* push();
* translate(15, 0, sin(frameCount / 30 + PI) * 65);
* box(30);
* pop();
* }
* </code>
* </div>
*
* @alt
* two 3D boxes move back and forth along same plane, rotating as mouse is dragged.
*/
p5.prototype.ortho = function (...args) {
this._assert3d('ortho');
p5._validateParameters('ortho', args);
this._renderer._curCamera.ortho(...args);
return this;
};
/**
* Sets the frustum of the current camera as defined by
* the parameters.
*
* A frustum is a geometric form: a pyramid with its top
* cut off. With the viewer's eye at the imaginary top of
* the pyramid, the six planes of the frustum act as clipping
* planes when rendering a 3D view. Thus, any form inside the
* clipping planes is visible; anything outside
* those planes is not visible.
*
* Setting the frustum changes the perspective of the scene being rendered.
* This can be achieved more simply in many cases by using
* <a href="https://p5js.org/reference/#/p5/perspective">perspective()</a>.
*
* If no parameters are given, the following default is used:
* frustum(-width/20, width/20, height/20, -height/20, eyeZ/10, eyeZ*10),
* where eyeZ is equal to ((height/2) / tan(PI/6)).
* @method frustum
* @for p5
* @param {Number} [left] camera frustum left plane
* @param {Number} [right] camera frustum right plane
* @param {Number} [bottom] camera frustum bottom plane
* @param {Number} [top] camera frustum top plane
* @param {Number} [near] camera frustum near plane
* @param {Number} [far] camera frustum far plane
* @chainable
* @example
* <div>
* <code>
* function setup() {
* createCanvas(100, 100, WEBGL);
* setAttributes('antialias', true);
* frustum(-0.1, 0.1, -0.1, 0.1, 0.1, 200);
* describe(
* 'two 3D boxes move back and forth along same plane, rotating as mouse is dragged.'
* );
* }
* function draw() {
* background(200);
* orbitControl();
* normalMaterial();
* translate(0,0,700);
* rotateY(-0.2);
* rotateX(-0.3);
* push();
* translate(-15, 0, sin(frameCount / 30) * 25);
* box(30);
* pop();
* push();
* translate(15, 0, sin(frameCount / 30 + PI) * 25);
* box(30);
* pop();
* }
* </code>
* </div>
*
* @alt
* two 3D boxes move back and forth along same plane, rotating as mouse is dragged.
*/
p5.prototype.frustum = function (...args) {
this._assert3d('frustum');
p5._validateParameters('frustum', args);
this._renderer._curCamera.frustum(...args);
return this;
};
////////////////////////////////////////////////////////////////////////////////
// p5.Camera
////////////////////////////////////////////////////////////////////////////////
/**
* Creates a new <a href="#/p5.Camera">p5.Camera</a> object and sets it
* as the current (active) camera.
*
* The new camera is initialized with a default position
* (see <a href="#/p5.Camera/camera">camera()</a>)
* and a default perspective projection
* (see <a href="#/p5.Camera/perspective">perspective()</a>).
* Its properties can be controlled with the <a href="#/p5.Camera">p5.Camera</a>
* methods.
*
* Note: Every 3D sketch starts with a default camera initialized.
* This camera can be controlled with the global methods
* <a href="#/p5/camera">camera()</a>,
* <a href="#/p5/perspective">perspective()</a>, <a href="#/p5/ortho">ortho()</a>,
* and <a href="#/p5/frustum">frustum()</a> if it is the only camera
* in the scene.
* @method createCamera
* @return {p5.Camera} The newly created camera object.
* @for p5
* @example
* <div><code>
* // Creates a camera object and animates it around a box.
* let camera;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(0);
* camera = createCamera();
* describe('An example that creates a camera and moves it around the box.');
* }
*
* function draw() {
* background(0);
* // The camera will automatically
* // rotate to look at [0, 0, 0].
* camera.lookAt(0, 0, 0);
*
* // The camera will move on the
* // x axis.
* camera.setPosition(sin(frameCount / 60) * 200, 0, 100);
* box(20);
*
* // A 'ground' box to give the viewer
* // a better idea of where the camera
* // is looking.
* translate(0, 50, 0);
* rotateX(HALF_PI);
* box(150, 150, 20);
* }
* </code></div>
*
* @alt
* An example that creates a camera and moves it around the box.
*/
p5.prototype.createCamera = function () {
this._assert3d('createCamera');
const _cam = new p5.Camera(this._renderer);
// compute default camera settings, then set a default camera
_cam._computeCameraDefaultSettings();
_cam._setDefaultCamera();
// set renderer current camera to the new camera
this._renderer._curCamera = _cam;
return _cam;
};
/**
* This class describes a camera for use in p5's
* <a href="https://github.com/processing/p5.js/wiki/Getting-started-with-WebGL-in-p5">
* WebGL mode</a>. It contains camera position, orientation, and projection
* information necessary for rendering a 3D scene.
*
* New p5.Camera objects can be made through the
* <a href="#/p5/createCamera">createCamera()</a> function and controlled through
* the methods described below. A camera created in this way will use a default
* position in the scene and a default perspective projection until these
* properties are changed through the various methods available. It is possible
* to create multiple cameras, in which case the current camera
* can be set through the <a href="#/p5/setCamera">setCamera()</a> method.
*
* Note:
* The methods below operate in two coordinate systems: the 'world' coordinate
* system describe positions in terms of their relationship to the origin along
* the X, Y and Z axes whereas the camera's 'local' coordinate system
* describes positions from the camera's point of view: left-right, up-down,
* and forward-backward. The <a href="#/p5.Camera/move">move()</a> method,
* for instance, moves the camera along its own axes, whereas the
* <a href="#/p5.Camera/setPosition">setPosition()</a>
* method sets the camera's position in world-space.
*
* The camera object propreties
* <code>eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ</code>
* which describes camera position, orientation, and projection
* are also accessible via the camera object generated using
* <a href="#/p5/createCamera">createCamera()</a>
*
* @class p5.Camera
* @param {rendererGL} rendererGL instance of WebGL renderer
* @example
* <div>
* <code>
* let cam;
* let delta = 0.01;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
* normalMaterial();
* cam = createCamera();
* // set initial pan angle
* cam.pan(-0.8);
* describe(
* 'camera view pans left and right across a series of rotating 3D boxes.'
* );
* }
*
* function draw() {
* background(200);
*
* // pan camera according to angle 'delta'
* cam.pan(delta);
*
* // every 160 frames, switch direction
* if (frameCount % 160 === 0) {
* delta *= -1;
* }
*
* rotateX(frameCount * 0.01);
* translate(-100, 0, 0);
* box(20);
* translate(35, 0, 0);
* box(20);
* translate(35, 0, 0);
* box(20);
* translate(35, 0, 0);
* box(20);
* translate(35, 0, 0);
* box(20);
* translate(35, 0, 0);
* box(20);
* translate(35, 0, 0);
* box(20);
* }
* </code>
* </div>
*
* @alt
* camera view pans left and right across a series of rotating 3D boxes.
*/
p5.Camera = class Camera {
constructor(renderer) {
this._renderer = renderer;
this.cameraType = 'default';
this.cameraMatrix = new p5.Matrix();
this.projMatrix = new p5.Matrix();
this.yScale = 1;
}
/**
* camera position value on x axis
* @property {Number} eyeX
* @readonly
* @example
*
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(0);
* cam = createCamera();
* div = createDiv();
* div.position(0, 0);
* describe('An example showing the use of camera object properties');
* }
*
* function draw() {
* orbitControl();
* box(10);
* div.html('eyeX = ' + cam.eyeX);
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* camera position value on y axis
* @property {Number} eyeY
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(0);
* cam = createCamera();
* div = createDiv();
* div.position(0, 0);
* describe('An example showing the use of camera object properties');
* }
*
* function draw() {
* orbitControl();
* box(10);
* div.html('eyeY = ' + cam.eyeY);
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* camera position value on z axis
* @property {Number} eyeZ
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(0);
* cam = createCamera();
* div = createDiv();
* div.position(0, 0);
* describe('An example showing the use of camera object properties');
* }
*
* function draw() {
* orbitControl();
* box(10);
* div.html('eyeZ = ' + cam.eyeZ);
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* x coordinate representing center of the sketch
* @property {Number} centerX
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(255);
* cam = createCamera();
* cam.lookAt(1, 0, 0);
* div = createDiv('centerX = ' + cam.centerX);
* div.position(0, 0);
* div.style('color', 'white');
* describe('An example showing the use of camera object properties');
* }
*
* function draw() {
* orbitControl();
* box(10);
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* y coordinate representing center of the sketch
* @property {Number} centerY
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(255);
* cam = createCamera();
* cam.lookAt(0, 1, 0);
* div = createDiv('centerY = ' + cam.centerY);
* div.position(0, 0);
* div.style('color', 'white');
* describe('An example showing the use of camera object properties');
* }
*
* function draw() {
* orbitControl();
* box(10);
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* z coordinate representing center of the sketch
* @property {Number} centerZ
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(255);
* cam = createCamera();
* cam.lookAt(0, 0, 1);
* div = createDiv('centerZ = ' + cam.centerZ);
* div.position(0, 0);
* div.style('color', 'white');
* describe('An example showing the use of camera object properties');
* }
*
* function draw() {
* orbitControl();
* box(10);
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* x component of direction 'up' from camera
* @property {Number} upX
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(255);
* cam = createCamera();
* div = createDiv('upX = ' + cam.upX);
* div.position(0, 0);
* div.style('color', 'blue');
* div.style('font-size', '18px');
* describe('An example showing the use of camera object properties');
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* y component of direction 'up' from camera
* @property {Number} upY
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(255);
* cam = createCamera();
* div = createDiv('upY = ' + cam.upY);
* div.position(0, 0);
* div.style('color', 'blue');
* div.style('font-size', '18px');
* describe('An example showing the use of camera object properties');
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
/**
* z component of direction 'up' from camera
* @property {Number} upZ
* @readonly
* @example
* <div class='norender'><code>
* let cam, div;
* function setup() {
* createCanvas(100, 100, WEBGL);
* background(255);
* cam = createCamera();
* div = createDiv('upZ = ' + cam.upZ);
* div.position(0, 0);
* div.style('color', 'blue');
* div.style('font-size', '18px');
* describe('An example showing the use of camera object properties');
* }
* </code></div>
*
* @alt
* An example showing the use of camera object properties
*
*/
////////////////////////////////////////////////////////////////////////////////
// Camera Projection Methods
////////////////////////////////////////////////////////////////////////////////
/**
* Sets a perspective projection.
* Accepts the same parameters as the global
* <a href="#/p5/perspective">perspective()</a>.
* More information on this function can be found there.
* @method perspective
* @for p5.Camera
* @example
* <div>
* <code>
* // drag the mouse to look around!
*
* let cam;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
* // create a camera
* cam = createCamera();
* // give it a perspective projection
* cam.perspective(PI / 3.0, width / height, 0.1, 500);
* }
*
* function draw() {
* background(200);
* orbitControl();
* normalMaterial();
*
* translate(0, 0, 550);
* rotateX(-0.3);
* rotateY(-0.2);
*
* push();
* translate(-15, 0, sin(frameCount / 30) * 95);
* box(30);
* pop();
* push();
* translate(15, 0, sin(frameCount / 30 + PI) * 95);
* box(30);
* pop();
* }
* </code>
* </div>
* @alt
* two colored 3D boxes move back and forth, rotating as mouse is dragged.
*/
perspective(fovy, aspect, near, far) {
this.cameraType = arguments.length > 0 ? 'custom' : 'default';
if (typeof fovy === 'undefined') {
fovy = this.defaultCameraFOV;
// this avoids issue where setting angleMode(DEGREES) before calling
// perspective leads to a smaller than expected FOV (because
// _computeCameraDefaultSettings computes in radians)
this.cameraFOV = fovy;
} else {
this.cameraFOV = this._renderer._pInst._toRadians(fovy);
}
if (typeof aspect === 'undefined') {
aspect = this.defaultAspectRatio;
}
if (typeof near === 'undefined') {
near = this.defaultCameraNear;
}
if (typeof far === 'undefined') {
far = this.defaultCameraFar;
}
if (near <= 0.0001) {
near = 0.01;
console.log(
'Avoid perspective near plane values close to or below 0. ' +
'Setting value to 0.01.'
);
}
if (far < near) {
console.log(
'Perspective far plane value is less than near plane value. ' +
'Nothing will be shown.'
);
}
this.aspectRatio = aspect;
this.cameraNear = near;
this.cameraFar = far;
this.projMatrix = p5.Matrix.identity();
const f = 1.0 / Math.tan(this.cameraFOV / 2);
const nf = 1.0 / (this.cameraNear - this.cameraFar);
/* eslint-disable indent */
this.projMatrix.set(f / aspect, 0, 0, 0,
0, -f * this.yScale, 0, 0,
0, 0, (far + near) * nf, -1,
0, 0, (2 * far * near) * nf, 0);
/* eslint-enable indent */
if (this._isActive()) {
this._renderer.uPMatrix.set(
this.projMatrix.mat4[0],
this.projMatrix.mat4[1],
this.projMatrix.mat4[2],
this.projMatrix.mat4[3],
this.projMatrix.mat4[4],
this.projMatrix.mat4[5],
this.projMatrix.mat4[6],
this.projMatrix.mat4[7],
this.projMatrix.mat4[8],
this.projMatrix.mat4[9],
this.projMatrix.mat4[10],
this.projMatrix.mat4[11],
this.projMatrix.mat4[12],
this.projMatrix.mat4[13],
this.projMatrix.mat4[14],
this.projMatrix.mat4[15]
);
}
}
/**
* Sets an orthographic projection.
* Accepts the same parameters as the global
* <a href="#/p5/ortho">ortho()</a>.
* More information on this function can be found there.
* @method ortho
* @for p5.Camera
* @example
* <div>
* <code>
* // drag the mouse to look around!
* // there's no vanishing point
*
* let cam;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
* // create a camera
* cam = createCamera();
* // give it an orthographic projection
* cam.ortho(-width / 2, width / 2, height / 2, -height / 2, 0, 500);
* }
* function draw() {
* background(200);
* orbitControl();
* normalMaterial();
* translate(0,0,500);
* rotateX(0.2);
* rotateY(-0.2);
* push();
* translate(-15, 0, sin(frameCount / 30) * 65);
* box(30);
* pop();
* push();
* translate(15, 0, sin(frameCount / 30 + PI) * 65);
* box(30);
* pop();
* }
* </code>
* </div>
* @alt
* two 3D boxes move back and forth along same plane, rotating as mouse is dragged.
*/
ortho(left, right, bottom, top, near, far) {
if (left === undefined) left = -this._renderer.width / 2;
if (right === undefined) right = +this._renderer.width / 2;
if (bottom === undefined) bottom = -this._renderer.height / 2;
if (top === undefined) top = +this._renderer.height / 2;
if (near === undefined) near = 0;
if (far === undefined)
far = Math.max(this._renderer.width, this._renderer.height);
this.cameraNear = near;
this.cameraFar = far;
const w = right - left;
const h = top - bottom;
const d = far - near;
const x = +2.0 / w;
const y = +2.0 / h * this.yScale;
const z = -2.0 / d;
const tx = -(right + left) / w;
const ty = -(top + bottom) / h;
const tz = -(far + near) / d;
this.projMatrix = p5.Matrix.identity();
/* eslint-disable indent */
this.projMatrix.set( x, 0, 0, 0,
0, -y, 0, 0,
0, 0, z, 0,
tx, ty, tz, 1);
/* eslint-enable indent */
if (this._isActive()) {
this._renderer.uPMatrix.set(
this.projMatrix.mat4[0],
this.projMatrix.mat4[1],
this.projMatrix.mat4[2],
this.projMatrix.mat4[3],
this.projMatrix.mat4[4],
this.projMatrix.mat4[5],
this.projMatrix.mat4[6],
this.projMatrix.mat4[7],
this.projMatrix.mat4[8],
this.projMatrix.mat4[9],
this.projMatrix.mat4[10],
this.projMatrix.mat4[11],
this.projMatrix.mat4[12],
this.projMatrix.mat4[13],
this.projMatrix.mat4[14],
this.projMatrix.mat4[15]
);
}
this.cameraType = 'custom';
}
/**
* Sets the camera's frustum.
* Accepts the same parameters as the global
* <a href="#/p5/frustum">frustum()</a>.
* More information on this function can be found there.
* @method frustum
* @for p5.Camera
* @example
* <div>
* <code>
* let cam;
*
* function setup() {
* x = createCanvas(100, 100, WEBGL);
* setAttributes('antialias', true);
* // create a camera
* cam = createCamera();
* // set its frustum
* cam.frustum(-0.1, 0.1, -0.1, 0.1, 0.1, 200);
* }
*
* function draw() {
* background(200);
* orbitControl();
* normalMaterial();
* translate(0,0,700);
* rotateY(-0.2);
* rotateX(-0.3);
* push();
* translate(-15, 0, sin(frameCount / 30) * 25);
* box(30);
* pop();
* push();
* translate(15, 0, sin(frameCount / 30 + PI) * 25);
* box(30);
* pop();
* }
* </code>
* </div>
* @alt
* two 3D boxes move back and forth along same plane, rotating as mouse is dragged.
*/
frustum(left, right, bottom, top, near, far) {
if (left === undefined) left = -this._renderer.width * 0.05;
if (right === undefined) right = +this._renderer.width * 0.05;
if (bottom === undefined) bottom = +this._renderer.height * 0.05;