Giter VIP home page Giter VIP logo

p5.easycam's Introduction

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

function setup() { 
  createCanvas(windowWidth, windowHeight, WEBGL);
  
  // the simplest method to enable the camera
  createEasyCam();

  // suppress right-click context menu
  document.oncontextmenu = function() { return false; }
} 

function draw(){
  background(64);
  lights();
  box(200);
}

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:

npm install

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

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.

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

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

// constructor
new Dw.EasyCam(p5.RendererGL, state);
new Dw.EasyCam(p5.RendererGL, {
      distance : z,                 // scalar (optional)
      center   : [x, y, z],         // vector (optional)
      rotation : [q0, q1, q2, q3],  // quaternion (optional)
      viewport : [x, y, w, h],      // array (optional)
    }

// examples
createEasyCam(); // uses the primary WEBGL renderer and default settings
 ...
createEasyCam({distance:z});
createEasyCam(p5.RendererGL);
createEasyCam(p5.RendererGL, {distance:z});
createEasyCam(p5.RendererGL, {distance:z, center:[x,y,z]});
  ... 
new Dw.EasyCam(p5.RendererGL);
new Dw.EasyCam(p5.RendererGL, {distance:z});
new Dw.EasyCam(p5.RendererGL, {distance:z, center:[x,y,z]});
  ... 

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

Camera Methods

// CAMERA, MISC
setCanvas(renderer) // webgl-renderer
getCanvas()
setViewport(viewport) // viewport as bounding screen-rectangle [x,y,w,h]
getViewport()
update() // update camera state
apply(renderer) // apply camera state to webgl-renderer
dispose()
setAutoUpdate(status)
getAutoUpdate()
attachMouseListeners(renderer) // input handler
removeMouseListeners()

// INPUT BEHAVIOUR/SCALE/SPEED
setZoomScale(scale_zoom)
getZoomScale()
setPanScale(scale_pan)
getPanScale()
setRotationScale(scale_rotation)
getRotationScale()
setWheelScale(wheelScale)
getWheelScale()
setDefaultInterpolationTime(duration)
setDamping(damping)
setRotationConstraint(yaw, pitch, roll)

// GET ZOOM/PAN/ROTATE/POSITION/UP
getCenter()
getDistance()
getRotation()
getUpVector(dst)
getPosition(dst)

// SET ZOOM/PAN/ROTATE
setDistanceMin(distance_min)
setDistanceMax(distance_max)
setDistance(distance, duration)
setCenter(center, duration)
setRotation(rotation, duration)
setInterpolatedCenter(valA, valB, t)
setInterpolatedDistance(valA, valB, t)
setInterpolatedRotation(valA, valB, t)

// MODIFY ZOOM/PAN/ROTATE
zoom(dz)
panX(dx)
panY(dy)
pan(dx, dy)
rotateX(rx)
rotateY(ry)
rotateZ(rz)
rotate(axis, angle)

// CAMERA STATES
setState(other, duration)
getState()
pushState()
popState(duration)
pushResetState()
reset(duration)

// HEAD_UP_DISPLAY
beginHUD(renderer, w, h)
endHUD(renderer)

Camera State Object

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

Example: openprocessing

Screenshots

RandomBoxes

MultiView

PerPixelPhong

CameraStates

MultiCanvas

p5.easycam's People

Contributors

asukaminato0721 avatar jwdunn1 avatar luis-c465 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

p5.easycam's Issues

Mouse wheel behavior in offset canvas

View a reproduction of the issue here
In this example, the red zone represents a non-offset canvas. The mouse wheel only works (zooms) where the red overlaps the offset canvas. The wheel fails while over the canvas outside the red zone.
Expected behavior: mouse wheel should work if the mouse pointer is anywhere over the canvas, even when offset.
Code at line 299 should account for canvas offset in a manner similar to the handling of mousedown and dblclick.

Constrain Rotation

The setRotationConstraint(boolean, boolean, boolean) function doesn't seem to have an effect on rotation. Is it possible to lock rotation along one or more axes (pitch/roll/yaw)?

2d text renders badly

My canvas uses WEBGL and i am trying to draw regular debug text on top in 2D.

I am following this WEBGL method https://p5js.org/reference/#/p5/text

function preload() {
  inconsolata = loadFont(
    "https://cdnjs.cloudflare.com/ajax/libs/bwip-js/2.0.6/Inconsolata.otf"
  );
}

function setup() {
  pixelDensity(1); //lower is faster

  var canvas = createCanvas(windowWidth, windowHeight, WEBGL);
  
  textFont(inconsolata);
  textSize(20);
  textAlign(CENTER, CENTER);

Drawing text inside displayHUD() makes it look very glitchy.
drawing text inside draw() is glitchy too but in 3D space.

Screen Shot 2022-02-26 at 3 52 25 PM

Screen Shot 2022-02-26 at 3 53 15 PM

Updates, NPM, and Instance Mode

Hi! 👋 Is this library still maintained? I've been unable to import it (cleanly, anyway) into a Yarn/TypeScript project, as no package is published on npm.

Furthermore, when I tried to copy the source code in manually, I couldn't get it to work with p5's Instance Mode. The library seems to assume p5 is always available globally, which is rarely the case in modern JS projects.

Thanks!

hide and show HUD

I am working off the CameraStates example.
What do i have to do to hide or show the text and screenshot HUD overlays?
Unfortunately i am not super experienced with css and html to know how to set the hide/show from JS.

Thanks a bunch.

Dragging state persists when mouse is dragged outside canvas

If you click the mouse, drag the cursor outside of the canvas, and then release it, the dragging state persists until you click inside of the canvas again. In the MultiCanvas example, I can get all six to follow the mouse movements by clicking, dragging, and releasing each of them outside their canvases.

Thanks again for your work on this!

3D object is not smooth when we use along with react app

I tried easy cam in a demo it's working smooth with mouse move. But I tried with one my React project it's not smooth,
behaving so weird and major part (Right, bottom) of the canvas is not sensing mouse at all.

  1. Standalone demo -> Works smooth and detects mouse movement throughout the canvas

  2. With React app --> below the red line and right side of the line is not listening to the mouse event. And also it's not smooth at all.
    image

I don't know what I'm missing here.

world point to screen coordinates

Thanks for making this library. It works very well.

Would anyone have advice on how to implement point picking with easycam?

In openframeworks i can use worldToScreen and then check the distance to the mouse to find the nearest 3d object.
But i have no idea how to do this conversion from 3d to 2d here?

Thanks a bunch.

Problem/fix for instance mode with p5 1.4.2

Hello.

When using the library in instance mode, it breaks with p5 1.4.2. I don't have the error here, but it complains that does not know the function camera.

The problem is at line 628 (no minified), somehow the reference to the p5 object (I believe should be _curCamera) is lost and p5's function camera() is not recognized. I fixed it in a local copy, using the hidden p5's variable _pInst instead of _curCamera, everything went fine that way. Although this fixes the issue, I believe it's probably not the right way to do it, hence not a pull request, just this issue message.
Thanks for the excellent work.
cheers
Vicente

ps: I'm not with my computer right now, so I can't provide a MRE, but can do it later if you guys want. Sorry about that.

multiple canvases

Hello!

Thank you for this excellent resource! I am trying to implement a webpage that contains 13 separate WEBGL canvases. I have gotten this to work with p5's built-in instancing functionality, and it works splendidly.
However, when I realized I definitely needed to get EasyCam instead of using the default OrbitControl, I was surprised and disappointed to see that it doesn't seem to work on anything beside the first canvas that is created in the script. Incredibly, the latest version of OrbitControl actually detects and works on all 13 separately! The only real drawback is that it doesn't seem to have a way to disable its capturing the scrollwheel, the same way EasyCam does.
I have a function in my page script that sets up and runs the canvas; EasyCam is where it should be, in the setup. The overall p5 function then gets called 13 times, creating separate WEBGL canvases on the page.
As I said, the behavior is that EasyCam works flawlessly on the first canvas but not the other twelve.

Is there any chance this could be fixed soon? Or am I doing something wrong?

cheers, and thanks again!

HUD Lights

If lights are on, then screen-aligned objects rendered in HUD mode can be affected by the orientation of the camera. For example, given a scene with lights() and a box, if the camera is rotated around the scene, a filled rectangle rendered in the HUD context will vary in brightness. The lights should probably be turned off automatically for the HUD rendering, perhaps using noLights() (when that is implemented) or by setting this._renderer._enableLighting = false;

Option to rotating camera around a global axis

I am trying to modify the camera behavior when i drag on the screen.
Basically this is the same as my problem:
https://gamedev.stackexchange.com/questions/136174/im-rotating-an-object-on-two-axes-so-why-does-it-keep-twisting-around-the-thir?noredirect=1&lq=1

Now the default behavior that is the rotations are applied relative to the camera coordinate system.
I would like to apply yaw rotation around the global Y axis.

How this can be achieved?
Animation

IE11 support?

According to w3counter and statcounter, IE has between 3 and 5 percent of the market. It is end of life for this browser: is it necessary to support it? p5.EasyCam presently contains ES6 and Math.log()

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.