项目作者: freshfork

项目描述 :
A p5.js library for easy 3D camera control.
高级语言: JavaScript
项目地址: git://github.com/freshfork/p5.EasyCam.git
创建时间: 2019-09-12T21:19:28Z
项目社区:https://github.com/freshfork/p5.EasyCam

开源协议:MIT License

下载


MultiView

p5.EasyCam

Simple 3D camera control for p5js and the WEBGL renderer.

To get started quickly with EasyCam, take a look at the following tutorial on OpenProcessing.org:

—> openprocessing tutorial

Releases

Examples

EasyCam.js - Advanced Shader/Lighting

EasyCam.js - Basic

Alternate first-person camera

For a simple first-person point-of-view p5.js camera library, take a look at p5.RoverCam

Usage

  1. function setup() {
  2. createCanvas(windowWidth, windowHeight, WEBGL);
  3. // the simplest method to enable the camera
  4. createEasyCam();
  5. // suppress right-click context menu
  6. document.oncontextmenu = function() { return false; }
  7. }
  8. function draw(){
  9. background(64);
  10. lights();
  11. box(200);
  12. }

To control the movement of the camera:

rotate around the look-at point
mouse: left-click and drag
touch: one-finger drag

pan the scene
mouse: middle-click and drag
touch: two-finger drag

zoom out/in
mouse: right-click drag
touch: pinch/spread

reset to the starting state
mouse: double-click
touch: double-tap

Camera control

The camera is positioned on a sphere whose radius is a given distance from the look-at point. Rotations are around the looked-at point.

EasyCam is impervious to gimbal lock, and has no known “singularities” or discontinuities in its behavior.

Simple examples to play with:

Node.js, npm, IntelliSense, and TypeScript

The npm dependencies are located in the package.json file.

If node.js is installed then this command will download the dependencies for the project:

  1. npm install

Once the dependencies are downloaded then the types can be generated with the command

  1. npm run build

This will generate p5.easycam.d.ts.

Next, it may be necessary to include the following three lines to the top of a sketch file to refer to the generated type files.

  1. // @ts-check
  2. /// <reference path="./p5.easycam.d.ts" ></reference>
  3. /// <reference path="./node_modules/@types/p5/global.d.ts" ></reference>

When operating correctly, Visual Studio Code’s IntelliSense feature will display code-completion information. Below is contextual data shown for the setViewport method in p5.EasyCam:

Acknowledgements

Special thanks to Jonathan Feinberg and Thomas Diewald. This repository is maintained by James Dunn and future contributors.

History

This library is a derivative of the original PeasyCam Library by Jonathan Feinberg and combines new useful features with the great look and feel of the original version. This p5js library was started by Thomas Diewald in 2017. The code and examples have been updated to work with the release version of p5.js (v1.0.0) and will be maintained going forward.

Processing/Java version of this project: https://github.com/diwi/peasycam/tree/PeasyCam3

Original (presumed abandoned) fork source: https://github.com/diwi/p5.EasyCam

Reference Documentation

p5.EasyCam.documentation

Quick Reference

Camera Setup

  1. // constructor
  2. new Dw.EasyCam(p5.RendererGL, state);
  3. new Dw.EasyCam(p5.RendererGL, {
  4. distance : z, // scalar (optional)
  5. center : [x, y, z], // vector (optional)
  6. rotation : [q0, q1, q2, q3], // quaternion (optional)
  7. viewport : [x, y, w, h], // array (optional)
  8. }
  9. // examples
  10. createEasyCam(); // uses the primary WEBGL renderer and default settings
  11. ...
  12. createEasyCam({distance:z});
  13. createEasyCam(p5.RendererGL);
  14. createEasyCam(p5.RendererGL, {distance:z});
  15. createEasyCam(p5.RendererGL, {distance:z, center:[x,y,z]});
  16. ...
  17. new Dw.EasyCam(p5.RendererGL);
  18. new Dw.EasyCam(p5.RendererGL, {distance:z});
  19. new Dw.EasyCam(p5.RendererGL, {distance:z, center:[x,y,z]});
  20. ...

The constructors above return an EasyCam object whose methods are listed below.

Camera Methods

  1. // CAMERA, MISC
  2. setCanvas(renderer) // webgl-renderer
  3. getCanvas()
  4. setViewport(viewport) // viewport as bounding screen-rectangle [x,y,w,h]
  5. getViewport()
  6. update() // update camera state
  7. apply(renderer) // apply camera state to webgl-renderer
  8. dispose()
  9. setAutoUpdate(status)
  10. getAutoUpdate()
  11. attachMouseListeners(renderer) // input handler
  12. removeMouseListeners()
  13. // INPUT BEHAVIOUR/SCALE/SPEED
  14. setZoomScale(scale_zoom)
  15. getZoomScale()
  16. setPanScale(scale_pan)
  17. getPanScale()
  18. setRotationScale(scale_rotation)
  19. getRotationScale()
  20. setWheelScale(wheelScale)
  21. getWheelScale()
  22. setDefaultInterpolationTime(duration)
  23. setDamping(damping)
  24. setRotationConstraint(yaw, pitch, roll)
  25. // GET ZOOM/PAN/ROTATE/POSITION/UP
  26. getCenter()
  27. getDistance()
  28. getRotation()
  29. getUpVector(dst)
  30. getPosition(dst)
  31. // SET ZOOM/PAN/ROTATE
  32. setDistanceMin(distance_min)
  33. setDistanceMax(distance_max)
  34. setDistance(distance, duration)
  35. setCenter(center, duration)
  36. setRotation(rotation, duration)
  37. setInterpolatedCenter(valA, valB, t)
  38. setInterpolatedDistance(valA, valB, t)
  39. setInterpolatedRotation(valA, valB, t)
  40. // MODIFY ZOOM/PAN/ROTATE
  41. zoom(dz)
  42. panX(dx)
  43. panY(dy)
  44. pan(dx, dy)
  45. rotateX(rx)
  46. rotateY(ry)
  47. rotateZ(rz)
  48. rotate(axis, angle)
  49. // CAMERA STATES
  50. setState(other, duration)
  51. getState()
  52. pushState()
  53. popState(duration)
  54. pushResetState()
  55. reset(duration)
  56. // HEAD_UP_DISPLAY
  57. beginHUD(renderer, w, h)
  58. endHUD(renderer)

Camera State Object

  1. var easy=createEasyCam();
  2. let state = {
  3. distance : z, // scalar
  4. center : [x, y, z], // vector
  5. rotation : [q0, q1, q2, q3], // quaternion
  6. };
  7. easy.setState(state, 1000); // animate to state over the period of 1 second

Example: openprocessing

Screenshots

RandomBoxes

MultiView

PerPixelPhong

CameraStates

MultiCanvas