-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathp5.Camera.js
More file actions
4016 lines (3870 loc) · 118 KB
/
p5.Camera.js
File metadata and controls
4016 lines (3870 loc) · 118 KB
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 { Matrix } from '../math/p5.Matrix';
import { Vector } from '../math/p5.Vector';
import { Quat } from './p5.Quat';
import { RendererGL } from './p5.RendererGL';
class Camera {
constructor(renderer) {
this._renderer = renderer;
this.cameraType = 'default';
this.useLinePerspective = true;
this.cameraMatrix = new Matrix(4);
this.projMatrix = new Matrix(4);
this.yScale = 1;
}
////////////////////////////////////////////////////////////////////////////////
// Camera Projection Methods
////////////////////////////////////////////////////////////////////////////////
/**
* Sets a perspective projection for the camera.
*
* In a perspective projection, shapes that are further from the camera appear
* smaller than shapes that are near the camera. This technique, called
* foreshortening, creates realistic 3D scenes. It’s applied by default in new
* `p5.Camera` objects.
*
* `myCamera.perspective()` changes the camera’s perspective by changing its
* viewing frustum. The frustum is the volume of space that’s visible to the
* camera. The frustum’s shape is a pyramid with its top cut off. The camera
* is placed where the top of the pyramid should be and points towards the
* base of the pyramid. It views everything within the frustum.
*
* The first parameter, `fovy`, is the camera’s vertical field of view. It’s
* an angle that describes how tall or narrow a view the camera has. For
* example, calling `myCamera.perspective(0.5)` sets the camera’s vertical
* field of view to 0.5 radians. By default, `fovy` is calculated based on the
* sketch’s height and the camera’s default z-coordinate, which is 800. The
* formula for the default `fovy` is `2 * atan(height / 2 / 800)`.
*
* The second parameter, `aspect`, is the camera’s aspect ratio. It’s a number
* that describes the ratio of the top plane’s width to its height. For
* example, calling `myCamera.perspective(0.5, 1.5)` sets the camera’s field
* of view to 0.5 radians and aspect ratio to 1.5, which would make shapes
* appear thinner on a square canvas. By default, `aspect` is set to
* `width / height`.
*
* The third parameter, `near`, is the distance from the camera to the near
* plane. For example, calling `myCamera.perspective(0.5, 1.5, 100)` sets the
* camera’s field of view to 0.5 radians, its aspect ratio to 1.5, and places
* the near plane 100 pixels from the camera. Any shapes drawn less than 100
* pixels from the camera won’t be visible. By default, `near` is set to
* `0.1 * 800`, which is 1/10th the default distance between the camera and
* the origin.
*
* The fourth parameter, `far`, is the distance from the camera to the far
* plane. For example, calling `myCamera.perspective(0.5, 1.5, 100, 10000)`
* sets the camera’s field of view to 0.5 radians, its aspect ratio to 1.5,
* places the near plane 100 pixels from the camera, and places the far plane
* 10,000 pixels from the camera. Any shapes drawn more than 10,000 pixels
* from the camera won’t be visible. By default, `far` is set to `10 * 800`,
* which is 10 times the default distance between the camera and the origin.
*
* @for p5.Camera
* @param {Number} [fovy] camera frustum vertical field of view. Defaults to
* `2 * atan(height / 2 / 800)`.
* @param {Number} [aspect] camera frustum aspect ratio. Defaults to
* `width / height`.
* @param {Number} [near] distance from the camera to the near clipping plane.
* Defaults to `0.1 * 800`.
* @param {Number} [far] distance from the camera to the far clipping plane.
* Defaults to `10 * 800`.
*
* @example
* <div>
* <code>
* // Double-click to toggle between cameras.
*
* let cam1;
* let cam2;
* let isDefaultCamera = true;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create the first camera.
* // Keep its default settings.
* cam1 = createCamera();
*
* // Create the second camera.
* cam2 = createCamera();
*
* // Place it at the top-right.
* cam2.camera(400, -400, 800);
*
* // Set its fovy to 0.2.
* // Set its aspect to 1.5.
* // Set its near to 600.
* // Set its far to 1200.
* cam2.perspective(0.2, 1.5, 600, 1200);
*
* // Set the current camera to cam1.
* setCamera(cam1);
*
* describe('A white cube on a gray background. The camera toggles between a frontal view and a skewed aerial view when the user double-clicks.');
* }
*
* function draw() {
* background(200);
*
* // Draw the box.
* box();
* }
*
* // Toggle the current camera when the user double-clicks.
* function doubleClicked() {
* if (isDefaultCamera === true) {
* setCamera(cam2);
* isDefaultCamera = false;
* } else {
* setCamera(cam1);
* isDefaultCamera = true;
* }
* }
* </code>
* </div>
*
* <div>
* <code>
* // Double-click to toggle between cameras.
*
* let cam1;
* let cam2;
* let isDefaultCamera = true;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create the first camera.
* // Keep its default settings.
* cam1 = createCamera();
*
* // Create the second camera.
* cam2 = createCamera();
*
* // Place it at the top-right.
* cam2.camera(400, -400, 800);
*
* // Set its fovy to 0.2.
* // Set its aspect to 1.5.
* // Set its near to 600.
* // Set its far to 1200.
* cam2.perspective(0.2, 1.5, 600, 1200);
*
* // Set the current camera to cam1.
* setCamera(cam1);
*
* describe('A white cube moves left and right on a gray background. The camera toggles between a frontal and a skewed aerial view when the user double-clicks.');
* }
*
* function draw() {
* background(200);
*
* // Translate the origin left and right.
* let x = 100 * sin(frameCount * 0.01);
* translate(x, 0, 0);
*
* // Draw the box.
* box();
* }
*
* // Toggle the current camera when the user double-clicks.
* function doubleClicked() {
* if (isDefaultCamera === true) {
* setCamera(cam2);
* isDefaultCamera = false;
* } else {
* setCamera(cam1);
* isDefaultCamera = true;
* }
* }
* </code>
* </div>
*/
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 = new Matrix(4);
const f = 1.0 / Math.tan(this.cameraFOV / 2);
const nf = 1.0 / (this.cameraNear - this.cameraFar);
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);
if (this._isActive()) {
this._renderer.states.setValue('uPMatrix', this._renderer.states.uPMatrix.clone());
this._renderer.states.uPMatrix.set(this.projMatrix);
}
}
/**
* Sets an orthographic projection for the camera.
*
* In an orthographic projection, shapes with the same size always appear the
* same size, regardless of whether they are near or far from the camera.
*
* `myCamera.ortho()` changes the camera’s perspective by changing its viewing
* frustum from a truncated pyramid to a rectangular prism. The frustum is the
* volume of space that’s visible to the camera. The camera is placed in front
* of the frustum and views everything within the frustum. `myCamera.ortho()`
* has six optional parameters to define the viewing frustum.
*
* The first four parameters, `left`, `right`, `bottom`, and `top`, set the
* coordinates of the frustum’s sides, bottom, and top. For example, calling
* `myCamera.ortho(-100, 100, 200, -200)` creates a frustum that’s 200 pixels
* wide and 400 pixels tall. By default, these dimensions are set based on
* the sketch’s width and height, as in
* `myCamera.ortho(-width / 2, width / 2, -height / 2, height / 2)`.
*
* The last two parameters, `near` and `far`, set the distance of the
* frustum’s near and far plane from the camera. For example, calling
* `myCamera.ortho(-100, 100, 200, -200, 50, 1000)` creates a frustum that’s
* 200 pixels wide, 400 pixels tall, starts 50 pixels from the camera, and
* ends 1,000 pixels from the camera. By default, `near` and `far` are set to
* 0 and `max(width, height) + 800`, respectively.
*
* @for p5.Camera
* @param {Number} [left] x-coordinate of the frustum’s left plane. Defaults to `-width / 2`.
* @param {Number} [right] x-coordinate of the frustum’s right plane. Defaults to `width / 2`.
* @param {Number} [bottom] y-coordinate of the frustum’s bottom plane. Defaults to `height / 2`.
* @param {Number} [top] y-coordinate of the frustum’s top plane. Defaults to `-height / 2`.
* @param {Number} [near] z-coordinate of the frustum’s near plane. Defaults to 0.
* @param {Number} [far] z-coordinate of the frustum’s far plane. Defaults to `max(width, height) + 800`.
*
* @example
* <div>
* <code>
* // Double-click to toggle between cameras.
*
* let cam1;
* let cam2;
* let isDefaultCamera = true;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create the first camera.
* // Keep its default settings.
* cam1 = createCamera();
*
* // Create the second camera.
* cam2 = createCamera();
*
* // Apply an orthographic projection.
* cam2.ortho();
*
* // Set the current camera to cam1.
* setCamera(cam1);
*
* describe('A row of white cubes against a gray background. The camera toggles between a perspective and an orthographic projection when the user double-clicks.');
* }
*
* function draw() {
* background(200);
*
* // Translate the origin toward the camera.
* translate(-10, 10, 500);
*
* // Rotate the coordinate system.
* rotateY(-0.1);
* rotateX(-0.1);
*
* // Draw the row of boxes.
* for (let i = 0; i < 6; i += 1) {
* translate(0, 0, -40);
* box(10);
* }
* }
*
* // Toggle the current camera when the user double-clicks.
* function doubleClicked() {
* if (isDefaultCamera === true) {
* setCamera(cam2);
* isDefaultCamera = false;
* } else {
* setCamera(cam1);
* isDefaultCamera = true;
* }
* }
* </code>
* </div>
*
* <div>
* <code>
* // Double-click to toggle between cameras.
*
* let cam1;
* let cam2;
* let isDefaultCamera = true;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create the first camera.
* // Keep its default settings.
* cam1 = createCamera();
*
* // Create the second camera.
* cam2 = createCamera();
*
* // Apply an orthographic projection.
* cam2.ortho();
*
* // Set the current camera to cam1.
* setCamera(cam1);
*
* describe('A row of white cubes slither like a snake against a gray background. The camera toggles between a perspective and an orthographic projection when the user double-clicks.');
* }
*
* function draw() {
* background(200);
*
* // Translate the origin toward the camera.
* translate(-10, 10, 500);
*
* // Rotate the coordinate system.
* rotateY(-0.1);
* rotateX(-0.1);
*
* // Draw the row of boxes.
* for (let i = 0; i < 6; i += 1) {
* push();
* // Calculate the box's coordinates.
* let x = 10 * sin(frameCount * 0.02 + i * 0.6);
* let z = -40 * i;
* // Translate the origin.
* translate(x, 0, z);
* // Draw the box.
* box(10);
* pop();
* }
* }
*
* // Toggle the current camera when the user double-clicks.
* function doubleClicked() {
* if (isDefaultCamera === true) {
* setCamera(cam2);
* isDefaultCamera = false;
* } else {
* setCamera(cam1);
* isDefaultCamera = true;
* }
* }
* </code>
* </div>
*/
ortho(left, right, bottom, top, near, far) {
const source = this.fbo || this._renderer;
if (left === undefined) left = -source.width / 2;
if (right === undefined) right = +source.width / 2;
if (bottom === undefined) bottom = -source.height / 2;
if (top === undefined) top = +source.height / 2;
if (near === undefined) near = 0;
if (far === undefined) far = Math.max(source.width, source.height) + 800;
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 = new Matrix(4);
this.projMatrix.set(x, 0, 0, 0,
0, -y, 0, 0,
0, 0, z, 0,
tx, ty, tz, 1);
if (this._isActive()) {
this._renderer.states.setValue('uPMatrix', this._renderer.states.uPMatrix.clone());
this._renderer.states.uPMatrix.set(this.projMatrix);
}
this.cameraType = 'custom';
}
/**
* Sets the camera's frustum.
*
* In a frustum projection, shapes that are further from the camera appear
* smaller than shapes that are near the camera. This technique, called
* foreshortening, creates realistic 3D scenes.
*
* `myCamera.frustum()` changes the camera’s perspective by changing its
* viewing frustum. The frustum is the volume of space that’s visible to the
* camera. The frustum’s shape is a pyramid with its top cut off. The camera
* is placed where the top of the pyramid should be and points towards the
* base of the pyramid. It views everything within the frustum.
*
* The first four parameters, `left`, `right`, `bottom`, and `top`, set the
* coordinates of the frustum’s sides, bottom, and top. For example, calling
* `myCamera.frustum(-100, 100, 200, -200)` creates a frustum that’s 200
* pixels wide and 400 pixels tall. By default, these coordinates are set
* based on the sketch’s width and height, as in
* `myCamera.frustum(-width / 20, width / 20, height / 20, -height / 20)`.
*
* The last two parameters, `near` and `far`, set the distance of the
* frustum’s near and far plane from the camera. For example, calling
* `myCamera.frustum(-100, 100, 200, -200, 50, 1000)` creates a frustum that’s
* 200 pixels wide, 400 pixels tall, starts 50 pixels from the camera, and ends
* 1,000 pixels from the camera. By default, near is set to `0.1 * 800`, which
* is 1/10th the default distance between the camera and the origin. `far` is
* set to `10 * 800`, which is 10 times the default distance between the
* camera and the origin.
*
* @for p5.Camera
* @param {Number} [left] x-coordinate of the frustum’s left plane. Defaults to `-width / 20`.
* @param {Number} [right] x-coordinate of the frustum’s right plane. Defaults to `width / 20`.
* @param {Number} [bottom] y-coordinate of the frustum’s bottom plane. Defaults to `height / 20`.
* @param {Number} [top] y-coordinate of the frustum’s top plane. Defaults to `-height / 20`.
* @param {Number} [near] z-coordinate of the frustum’s near plane. Defaults to `0.1 * 800`.
* @param {Number} [far] z-coordinate of the frustum’s far plane. Defaults to `10 * 800`.
*
* @example
* <div>
* <code>
* // Double-click to toggle between cameras.
*
* let cam1;
* let cam2;
* let isDefaultCamera = true;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create the first camera.
* // Keep its default settings.
* cam1 = createCamera();
*
* // Create the second camera.
* cam2 = createCamera();
*
* // Adjust the frustum.
* // Center it.
* // Set its width and height to 20 pixels.
* // Place its near plane 300 pixels from the camera.
* // Place its far plane 350 pixels from the camera.
* cam2.frustum(-10, 10, -10, 10, 300, 350);
*
* // Set the current camera to cam1.
* setCamera(cam1);
*
* describe(
* 'A row of white cubes against a gray background. The camera zooms in on one cube when the user double-clicks.'
* );
* }
*
* function draw() {
* background(200);
*
* // Translate the origin toward the camera.
* translate(-10, 10, 600);
*
* // Rotate the coordinate system.
* rotateY(-0.1);
* rotateX(-0.1);
*
* // Draw the row of boxes.
* for (let i = 0; i < 6; i += 1) {
* translate(0, 0, -40);
* box(10);
* }
* }
*
* // Toggle the current camera when the user double-clicks.
* function doubleClicked() {
* if (isDefaultCamera === true) {
* setCamera(cam2);
* isDefaultCamera = false;
* } else {
* setCamera(cam1);
* isDefaultCamera = true;
* }
* }
* </code>
* </div>
*/
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;
if (top === undefined) top = -this._renderer.height * 0.05;
if (near === undefined) near = this.defaultCameraNear;
if (far === undefined) far = this.defaultCameraFar;
this.cameraNear = near;
this.cameraFar = far;
const w = right - left;
const h = top - bottom;
const d = far - near;
const x = +(2.0 * near) / w;
const y = +(2.0 * near) / h * this.yScale;
const z = -(2.0 * far * near) / d;
const tx = (right + left) / w;
const ty = (top + bottom) / h;
const tz = -(far + near) / d;
this.projMatrix = new Matrix(4);
this.projMatrix.set(x, 0, 0, 0,
0, -y, 0, 0,
tx, ty, tz, -1,
0, 0, z, 0);
if (this._isActive()) {
this._renderer.states.setValue('uPMatrix', this._renderer.states.uPMatrix.clone());
this._renderer.states.uPMatrix.set(this.projMatrix);
}
this.cameraType = 'custom';
}
////////////////////////////////////////////////////////////////////////////////
// Camera Orientation Methods
////////////////////////////////////////////////////////////////////////////////
/**
* Rotate camera view about arbitrary axis defined by x,y,z
* based on http://learnwebgl.brown37.net/07_cameras/camera_rotating_motion.html
* @private
*/
_rotateView(a, x, y, z) {
let centerX = this.centerX;
let centerY = this.centerY;
let centerZ = this.centerZ;
// move center by eye position such that rotation happens around eye position
centerX -= this.eyeX;
centerY -= this.eyeY;
centerZ -= this.eyeZ;
const rotation = new Matrix(4); // TODO Maybe pass p5
rotation.rotate4x4(this._renderer._pInst._toRadians(a), x, y, z);
const rotatedCenter = [
centerX * rotation.mat4[0] + centerY * rotation.mat4[4] + centerZ * rotation.mat4[8],
centerX * rotation.mat4[1] + centerY * rotation.mat4[5] + centerZ * rotation.mat4[9],
centerX * rotation.mat4[2] + centerY * rotation.mat4[6] + centerZ * rotation.mat4[10]
];
// add eye position back into center
rotatedCenter[0] += this.eyeX;
rotatedCenter[1] += this.eyeY;
rotatedCenter[2] += this.eyeZ;
this.camera(
this.eyeX,
this.eyeY,
this.eyeZ,
rotatedCenter[0],
rotatedCenter[1],
rotatedCenter[2],
this.upX,
this.upY,
this.upZ
);
}
/**
* Rotates the camera in a clockwise/counter-clockwise direction.
*
* Rolling rotates the camera without changing its orientation. The rotation
* happens in the camera’s "local" space.
*
* The parameter, `angle`, is the angle the camera should rotate. Passing a
* positive angle, as in `myCamera.roll(0.001)`, rotates the camera in counter-clockwise direction.
* Passing a negative angle, as in `myCamera.roll(-0.001)`, rotates the
* camera in clockwise direction.
*
* Note: Angles are interpreted based on the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* @method roll
* @param {Number} angle amount to rotate camera in current
* <a href="#/p5/angleMode">angleMode</a> units.
* @example
* <div>
* <code>
* let cam;
* let delta = 0.01;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
* normalMaterial();
* // Create a p5.Camera object.
* cam = createCamera();
*
* // Set the camera
* setCamera(cam);
* }
*
* function draw() {
* background(200);
*
* // Roll camera according to angle 'delta'
* cam.roll(delta);
*
* translate(0, 0, 0);
* box(20);
* translate(0, 25, 0);
* box(20);
* translate(0, 26, 0);
* box(20);
* translate(0, 27, 0);
* box(20);
* translate(0, 28, 0);
* box(20);
* translate(0,29, 0);
* box(20);
* translate(0, 30, 0);
* box(20);
* }
* </code>
* </div>
*
* @alt
* camera view rotates in counter clockwise direction with vertically stacked boxes in front of it.
*/
roll(amount) {
const local = this._getLocalAxes();
const axisQuaternion = Quat.fromAxisAngle(
this._renderer._pInst._toRadians(amount),
local.z[0], local.z[1], local.z[2]);
// const upQuat = new p5.Quat(0, this.upX, this.upY, this.upZ);
const newUpVector = axisQuaternion.rotateVector(
new Vector(this.upX, this.upY, this.upZ));
this.camera(
this.eyeX,
this.eyeY,
this.eyeZ,
this.centerX,
this.centerY,
this.centerZ,
newUpVector.x,
newUpVector.y,
newUpVector.z
);
}
/**
* Rotates the camera left and right.
*
* Panning rotates the camera without changing its position. The rotation
* happens in the camera’s "local" space.
*
* The parameter, `angle`, is the angle the camera should rotate. Passing a
* positive angle, as in `myCamera.pan(0.001)`, rotates the camera to the
* right. Passing a negative angle, as in `myCamera.pan(-0.001)`, rotates the
* camera to the left.
*
* Note: Angles are interpreted based on the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* @param {Number} angle amount to rotate in the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* @example
* <div>
* <code>
* let cam;
* let delta = 0.001;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create a p5.Camera object.
* cam = createCamera();
*
* // Set the camera
* setCamera(cam);
*
* // Place the camera at the top-center.
* cam.setPosition(0, -400, 800);
*
* // Point the camera at the origin.
* cam.lookAt(0, 0, 0);
*
* describe(
* 'A white cube on a gray background. The cube goes in and out of view as the camera pans left and right.'
* );
* }
*
* function draw() {
* background(200);
*
* // Pan with the camera.
* cam.pan(delta);
*
* // Switch directions every 120 frames.
* if (frameCount % 120 === 0) {
* delta *= -1;
* }
*
* // Draw the box.
* box();
* }
* </code>
* </div>
*/
pan(amount) {
const local = this._getLocalAxes();
this._rotateView(amount, local.y[0], local.y[1], local.y[2]);
}
/**
* Rotates the camera up and down.
*
* Tilting rotates the camera without changing its position. The rotation
* happens in the camera’s "local" space.
*
* The parameter, `angle`, is the angle the camera should rotate. Passing a
* positive angle, as in `myCamera.tilt(0.001)`, rotates the camera down.
* Passing a negative angle, as in `myCamera.tilt(-0.001)`, rotates the camera
* up.
*
* Note: Angles are interpreted based on the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* @param {Number} angle amount to rotate in the current
* <a href="#/p5/angleMode">angleMode()</a>.
*
* @example
* <div>
* <code>
* let cam;
* let delta = 0.001;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create a p5.Camera object.
* cam = createCamera();
*
* // Set the camera
* setCamera(cam);
*
* // Place the camera at the top-center.
* cam.setPosition(0, -400, 800);
*
* // Point the camera at the origin.
* cam.lookAt(0, 0, 0);
*
* describe(
* 'A white cube on a gray background. The cube goes in and out of view as the camera tilts up and down.'
* );
* }
*
* function draw() {
* background(200);
*
* // Pan with the camera.
* cam.tilt(delta);
*
* // Switch directions every 120 frames.
* if (frameCount % 120 === 0) {
* delta *= -1;
* }
*
* // Draw the box.
* box();
* }
* </code>
* </div>
*/
tilt(amount) {
const local = this._getLocalAxes();
this._rotateView(amount, local.x[0], local.x[1], local.x[2]);
}
/**
* Points the camera at a location.
*
* `myCamera.lookAt()` changes the camera’s orientation without changing its
* position.
*
* The parameters, `x`, `y`, and `z`, are the coordinates in "world" space
* where the camera should point. For example, calling
* `myCamera.lookAt(10, 20, 30)` points the camera at the coordinates
* `(10, 20, 30)`.
*
* @for p5.Camera
* @param {Number} x x-coordinate of the position where the camera should look in "world" space.
* @param {Number} y y-coordinate of the position where the camera should look in "world" space.
* @param {Number} z z-coordinate of the position where the camera should look in "world" space.
*
* @example
* <div>
* <code>
* // Double-click to look at a different cube.
*
* let cam;
* let isLookingLeft = true;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create a p5.Camera object.
* cam = createCamera();
*
* // Set the camera
* setCamera(cam);
*
* // Place the camera at the top-center.
* cam.setPosition(0, -400, 800);
*
* // Point the camera at the origin.
* cam.lookAt(-30, 0, 0);
*
* describe(
* 'A red cube and a blue cube on a gray background. The camera switches focus between the cubes when the user double-clicks.'
* );
* }
*
* function draw() {
* background(200);
*
* // Draw the box on the left.
* push();
* // Translate the origin to the left.
* translate(-30, 0, 0);
* // Style the box.
* fill(255, 0, 0);
* // Draw the box.
* box(20);
* pop();
*
* // Draw the box on the right.
* push();
* // Translate the origin to the right.
* translate(30, 0, 0);
* // Style the box.
* fill(0, 0, 255);
* // Draw the box.
* box(20);
* pop();
* }
*
* // Change the camera's focus when the user double-clicks.
* function doubleClicked() {
* if (isLookingLeft === true) {
* cam.lookAt(30, 0, 0);
* isLookingLeft = false;
* } else {
* cam.lookAt(-30, 0, 0);
* isLookingLeft = true;
* }
* }
* </code>
* </div>
*/
lookAt(x, y, z) {
this.camera(
this.eyeX,
this.eyeY,
this.eyeZ,
x,
y,
z,
this.upX,
this.upY,
this.upZ
);
}
////////////////////////////////////////////////////////////////////////////////
// Camera Position Methods
////////////////////////////////////////////////////////////////////////////////
/**
* Sets the position and orientation of the camera.
*
* `myCamera.camera()` allows objects to be viewed from different angles. It
* has nine parameters that are all optional.
*
* The first three parameters, `x`, `y`, and `z`, are the coordinates of the
* camera’s position in "world" space. For example, calling
* `myCamera.camera(0, 0, 0)` places the camera at the origin `(0, 0, 0)`. By
* default, the camera is placed at `(0, 0, 800)`.
*
* The next three parameters, `centerX`, `centerY`, and `centerZ` are the
* coordinates of the point where the camera faces in "world" space. For
* example, calling `myCamera.camera(0, 0, 0, 10, 20, 30)` places the camera
* at the origin `(0, 0, 0)` and points it at `(10, 20, 30)`. By default, the
* camera points at the origin `(0, 0, 0)`.
*
* The last three parameters, `upX`, `upY`, and `upZ` are the components of
* the "up" vector in "local" space. The "up" vector orients the camera’s
* y-axis. For example, calling
* `myCamera.camera(0, 0, 0, 10, 20, 30, 0, -1, 0)` places the camera at the
* origin `(0, 0, 0)`, points it at `(10, 20, 30)`, and sets the "up" vector
* to `(0, -1, 0)` which is like holding it upside-down. By default, the "up"
* vector is `(0, 1, 0)`.
*
* @for p5.Camera
* @param {Number} [x] x-coordinate of the camera. Defaults to 0.
* @param {Number} [y] y-coordinate of the camera. Defaults to 0.
* @param {Number} [z] z-coordinate of the camera. Defaults to 800.
* @param {Number} [centerX] x-coordinate of the point the camera faces. Defaults to 0.
* @param {Number} [centerY] y-coordinate of the point the camera faces. Defaults to 0.
* @param {Number} [centerZ] z-coordinate of the point the camera faces. Defaults to 0.
* @param {Number} [upX] x-component of the camera’s "up" vector. Defaults to 0.
* @param {Number} [upY] x-component of the camera’s "up" vector. Defaults to 1.
* @param {Number} [upZ] z-component of the camera’s "up" vector. Defaults to 0.
*
* @example
* <div>
* <code>
* // Double-click to toggle between cameras.
*
* let cam1;
* let cam2;
* let isDefaultCamera = true;
*
* function setup() {
* createCanvas(100, 100, WEBGL);
*
* // Create the first camera.
* // Keep its default settings.
* cam1 = createCamera();
*
* // Create the second camera.
* cam2 = createCamera();
*
* // Place it at the top-right: (1200, -600, 100)
* // Point it at the row of boxes: (-10, -10, 400)
* // Set its "up" vector to the default: (0, 1, 0)
* cam2.camera(1200, -600, 100, -10, -10, 400, 0, 1, 0);
*
* // Set the current camera to cam1.
* setCamera(cam1);
*
* describe(
* 'A row of white cubes against a gray background. The camera toggles between a frontal and an aerial view when the user double-clicks.'
* );
* }
*