Skip to content

Commit 32937e7

Browse files
authored
Merge pull request #5257 from JetStarBlues/add_p5.Camera.frustum_documentation
Add p5.Camera.frustum documentation on reference page (fixes #5256)
2 parents 35da313 + 8168b26 commit 32937e7

File tree

1 file changed

+200
-7
lines changed

1 file changed

+200
-7
lines changed

src/webgl/p5.Camera.js

Lines changed: 200 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,49 @@ p5.Camera = function(renderer) {
663663
////////////////////////////////////////////////////////////////////////////////
664664

665665
/**
666-
* Sets a perspective projection for a p5.Camera object and sets parameters
667-
* for that projection according to <a href="#/p5/perspective">perspective()</a>
668-
* syntax.
666+
* Sets a perspective projection.
667+
* Accepts the same parameters as the global
668+
* <a href="#/p5/perspective">perspective()</a>.
669+
* More information on this function can be found there.
669670
* @method perspective
670671
* @for p5.Camera
672+
* @example
673+
* <div>
674+
* <code>
675+
* // drag the mouse to look around!
676+
*
677+
* let cam;
678+
*
679+
* function setup() {
680+
* createCanvas(100, 100, WEBGL);
681+
* // create a camera
682+
* cam = createCamera();
683+
* // give it a perspective projection
684+
* cam.perspective(PI / 3.0, width / height, 0.1, 500);
685+
* }
686+
*
687+
* function draw() {
688+
* background(200);
689+
* orbitControl();
690+
* normalMaterial();
691+
*
692+
* rotateX(-0.3);
693+
* rotateY(-0.2);
694+
* translate(0, 0, -50);
695+
*
696+
* push();
697+
* translate(-15, 0, sin(frameCount / 30) * 95);
698+
* box(30);
699+
* pop();
700+
* push();
701+
* translate(15, 0, sin(frameCount / 30 + PI) * 95);
702+
* box(30);
703+
* pop();
704+
* }
705+
* </code>
706+
* </div>
707+
* @alt
708+
* two colored 3D boxes move back and forth, rotating as mouse is dragged.
671709
*/
672710
p5.Camera.prototype.perspective = function(fovy, aspect, near, far) {
673711
this.cameraType = arguments.length > 0 ? 'custom' : 'default';
@@ -743,10 +781,47 @@ p5.Camera.prototype.perspective = function(fovy, aspect, near, far) {
743781
};
744782

745783
/**
746-
* Sets an orthographic projection for a p5.Camera object and sets parameters
747-
* for that projection according to <a href="#/p5/ortho">ortho()</a> syntax.
784+
* Sets an orthographic projection.
785+
* Accepts the same parameters as the global
786+
* <a href="#/p5/ortho">ortho()</a>.
787+
* More information on this function can be found there.
748788
* @method ortho
749789
* @for p5.Camera
790+
* @example
791+
* <div>
792+
* <code>
793+
* // drag the mouse to look around!
794+
* // there's no vanishing point
795+
*
796+
* let cam;
797+
*
798+
* function setup() {
799+
* createCanvas(100, 100, WEBGL);
800+
* // create a camera
801+
* cam = createCamera();
802+
* // give it an orthographic projection
803+
* cam.ortho(-width / 2, width / 2, height / 2, -height / 2, 0, 500);
804+
* }
805+
* function draw() {
806+
* background(200);
807+
* orbitControl();
808+
* normalMaterial();
809+
*
810+
* rotateX(0.2);
811+
* rotateY(-0.2);
812+
* push();
813+
* translate(-15, 0, sin(frameCount / 30) * 65);
814+
* box(30);
815+
* pop();
816+
* push();
817+
* translate(15, 0, sin(frameCount / 30 + PI) * 65);
818+
* box(30);
819+
* pop();
820+
* }
821+
* </code>
822+
* </div>
823+
* @alt
824+
* two 3D boxes move back and forth along same plane, rotating as mouse is dragged.
750825
*/
751826
p5.Camera.prototype.ortho = function(left, right, bottom, top, near, far) {
752827
if (left === undefined) left = -this._renderer.width / 2;
@@ -802,8 +877,48 @@ p5.Camera.prototype.ortho = function(left, right, bottom, top, near, far) {
802877
};
803878

804879
/**
880+
* Sets the camera's frustum.
881+
* Accepts the same parameters as the global
882+
* <a href="#/p5/frustum">frustum()</a>.
883+
* More information on this function can be found there.
805884
* @method frustum
806885
* @for p5.Camera
886+
* @example
887+
* <div>
888+
* <code>
889+
* let cam;
890+
*
891+
* function setup() {
892+
* x = createCanvas(100, 100, WEBGL);
893+
* setAttributes('antialias', true);
894+
* // create a camera
895+
* cam = createCamera();
896+
* // set its frustum
897+
* cam.frustum(-0.1, 0.1, -0.1, 0.1, 0.1, 200);
898+
* }
899+
*
900+
* function draw() {
901+
* background(200);
902+
* orbitControl();
903+
* strokeWeight(10);
904+
* stroke(0, 0, 255);
905+
* noFill();
906+
*
907+
* rotateY(-0.2);
908+
* rotateX(-0.3);
909+
* push();
910+
* translate(-15, 0, sin(frameCount / 30) * 25);
911+
* box(30);
912+
* pop();
913+
* push();
914+
* translate(15, 0, sin(frameCount / 30 + PI) * 25);
915+
* box(30);
916+
* pop();
917+
* }
918+
* </code>
919+
* </div>
920+
* @alt
921+
* two 3D boxes move back and forth along same plane, rotating as mouse is dragged.
807922
*/
808923
p5.Camera.prototype.frustum = function(left, right, bottom, top, near, far) {
809924
if (left === undefined) left = -this._renderer.width / 2;
@@ -1090,10 +1205,88 @@ p5.Camera.prototype.lookAt = function(x, y, z) {
10901205
////////////////////////////////////////////////////////////////////////////////
10911206

10921207
/**
1093-
* Sets a camera's position and orientation. This is equivalent to calling
1094-
* <a href="#/p5/camera">camera()</a> on a p5.Camera object.
1208+
* Sets the camera's position and orientation.
1209+
* Accepts the same parameters as the global
1210+
* <a href="#/p5/camera">camera()</a>.
1211+
* More information on this function can be found there.
10951212
* @method camera
10961213
* @for p5.Camera
1214+
* @example
1215+
* <div>
1216+
* <code>
1217+
* let cam;
1218+
*
1219+
* function setup() {
1220+
* createCanvas(100, 100, WEBGL);
1221+
* // Create a camera.
1222+
* // createCamera() sets the newly created camera as
1223+
* // the current (active) camera.
1224+
* cam = createCamera();
1225+
* }
1226+
*
1227+
* function draw() {
1228+
* background(204);
1229+
* // Move the camera away from the plane by a sin wave
1230+
* cam.camera(0, 0, 20 + sin(frameCount * 0.01) * 10, 0, 0, 0, 0, 1, 0);
1231+
* plane(10, 10);
1232+
* }
1233+
* </code>
1234+
* </div>
1235+
* @alt
1236+
* White square repeatedly grows to fill canvas and then shrinks.
1237+
*
1238+
* @example
1239+
* <div>
1240+
* <code>
1241+
* // move slider to see changes!
1242+
* // sliders control the first 6 parameters of camera()
1243+
*
1244+
* let sliderGroup = [];
1245+
* let X;
1246+
* let Y;
1247+
* let Z;
1248+
* let centerX;
1249+
* let centerY;
1250+
* let centerZ;
1251+
* let h = 20;
1252+
* let cam;
1253+
*
1254+
* function setup() {
1255+
* createCanvas(100, 100, WEBGL);
1256+
* // create a camera
1257+
* cam = createCamera();
1258+
* // create sliders
1259+
* for (var i = 0; i < 6; i++) {
1260+
* if (i === 2) {
1261+
* sliderGroup[i] = createSlider(10, 400, 200);
1262+
* } else {
1263+
* sliderGroup[i] = createSlider(-400, 400, 0);
1264+
* }
1265+
* h = map(i, 0, 6, 5, 85);
1266+
* sliderGroup[i].position(10, height + h);
1267+
* sliderGroup[i].style('width', '80px');
1268+
* }
1269+
* }
1270+
*
1271+
* function draw() {
1272+
* background(60);
1273+
* // assigning sliders' value to each parameters
1274+
* X = sliderGroup[0].value();
1275+
* Y = sliderGroup[1].value();
1276+
* Z = sliderGroup[2].value();
1277+
* centerX = sliderGroup[3].value();
1278+
* centerY = sliderGroup[4].value();
1279+
* centerZ = sliderGroup[5].value();
1280+
* cam.camera(X, Y, Z, centerX, centerY, centerZ, 0, 1, 0);
1281+
* stroke(255);
1282+
* fill(255, 102, 94);
1283+
* box(85);
1284+
* }
1285+
* </code>
1286+
* </div>
1287+
* @alt
1288+
* An interactive example of a red cube with 3 sliders for moving it across x, y,
1289+
* z axis and 3 sliders for shifting it's center.
10971290
*/
10981291
p5.Camera.prototype.camera = function(
10991292
eyeX,

0 commit comments

Comments
 (0)