Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
    • p5.js Introduction
    • p5.js Variables
    • p5.js Random Numbers
    • p5.js SVG + Images
    • p5.js WebGL
    • p5.js Classes and Objects
    • p5.js Events and Functions
    • p5.js Loops
    • p5.js Coordinates
    • P5js Nested Loops
    • p5.js Animation Exercise 1
    • p5.js Animation Exercise 2
    • p5.js Conditionals
    • p5.js Arrays and Lists
    • p5.js Simple Collision Detection
    • p5.js Reactors
    • p5.js Tiling and Repetition
    • p5.js Vectors
    • p5.js Animation Solution with Objects
    • p5.js Easing
    • p5.js Perlin Noise
    • p5.js Particle System
    • p5.js Sound
    • p5j.s Typography
    • P5js Archive
  • Programming in Processing (java)

/
p5.js WebGL
Updated Aug 10, 2021

p5.js WebGL

Analytics

Loading data...

In p5.js, there are two render modes: P2D (default renderer) and WEBGL. Both render modes utilize the html canvas element, however by enabling the WEBGL "context" on the canvas, we can now draw in both 2D and 3D. To enable WEBGL, simply specify as the third parameter in the createCanvas() function. In WEBGL mode we introduce a third dimension: Z that points toward you from the screen.

function setup() { createCanvas(windowWidth, windowHeight, WEBGL); } function draw(){ background(255); box(); }

 

In WEBGL mode, there are currently 3 camera functions. The default camera view in WEBGL mode is perspective with a 60 degree field of view.

camera(x,y,z) perspective(fovy, aspect, near, far) ortho(left, right, bottom, top, near, far)

 

There are 7 different 3D geometry primitives in p5.js.

box() plane() sphere() ellipsoid() cone() cylinder() torus()

The difference between drawing primitives in 2D and 3D is that 3D primitives take size parameters, but not position. To reposition 3D primitives, simply call:

translate(x,y,z)

Another way of drawing custom shapes in WEBGL mode is to use beginShape() and endShape(). This works exactly the same in 2D mode as it does in WEBGL, except in WEBGL, vertices receive x, y, and z as location coordinates.

beginShape(); vertex(100,23,-100); vertex(200,23,-50); vertex(150, 45,-100); endShape();
Related content
p5.js Programming
p5.js Programming
Luke Franzke
p5.js Introduction
p5.js Introduction
Luke Franzke
p5.js Variables
p5.js Variables
Luke Franzke
p5.js Particle System
p5.js Particle System
paulina.zybinska
p5.js Loops
p5.js Loops
Luke Franzke
p5.js Random Numbers
p5.js Random Numbers
Luke Franzke
{"serverDuration": 20, "requestCorrelationId": "4369c0ff5be34dabbc9e2782259d7e03"}