Getting started with 3D

An entirely separate rendering pipeline — offscreen OpenGL 3.3 (core profile) via Silk.NET, rendered into a hidden window's framebuffer and read back as a normal PGraphics. Everything under this "3D" nav group requires RendererKind.Renderer3D and throws InvalidOperationException otherwise — the same way Processing itself errors when box()/rotateZ()/etc. are called under the default 2D renderer.

3D renderer

On this page · 3 entries

Size · CreateGraphics

  • Size(w, h, RendererKind.Renderer3D)
  • CreateGraphics(w, h, RendererKind.Renderer3D)

Requests the 3D pipeline for the main sketch canvas, or for an offscreen buffer. The renderer is fixed for that context's entire lifetime — calling either again with a different RendererKind later throws, matching Processing's own restriction that size(w, h, renderer) can't switch renderers mid-sketch.

Set it directly on the main canvas — compositing is automatic. Pass RendererKind.Renderer3D straight to the sketch's own Size() in Setup(). Sketch.RenderFrame() already runs your 3D draw calls against the GPU backend and composites the result with any 2D overlay — Fill()/Text() calls after your 3D geometry — automatically, every frame. Every built-in 3D example uses this direct form; a separate offscreen 3D PGraphics via CreateGraphics() is still there for when you actually want one (a texture rendered once in Setup(), say), just not as the default.

BeginDraw

  • BeginDraw() / EndDraw()

Only needed for an offscreen 3D PGraphics (from CreateGraphics()) — the main sketch canvas never needs them, since RenderFrame() calls the GPU backend's begin/end for you. An offscreen 3D buffer only accepts drawing calls between these two — Box(), Sphere(), Lights(), custom Vertex()/EndShape(), all of it — and throws InvalidOperationException outside the bracket.

Exception: CreateShape3D(). Building a reusable mesh doesn't need the bracket — it claims and releases the GL context on its own, so it's safe to call once, loose, in Setup(). See Custom 3D shapes.
C#
public class MySketch : Sketch
{
    public override void Setup() => Size(700, 500, RendererKind.Renderer3D);

    public override void Draw()
    {
        Background(20);
        Lights();                              // every frame — see Lights()
        Fill(160, 200, 255);
        PushMatrix();
        Translate(Width / 2f, Height / 2f, 0);
        RotateY(FrameCount / 60f);
        Box(160);
        PopMatrix();
    }
}

Runnable example

A complete sketch using this section. Test in Dana opens it in the IDE, or paste it into a new tab and press Run.

C#
// Size(w, h, RendererKind.Renderer3D) turns on 3D: lights, boxes, spheres.
public class Hello3D : Sketch
{
    public override void Setup() => Size(600, 400, RendererKind.Renderer3D);

    public override void Draw()
    {
        Background(25, 28, 40);
        Lights();
        NoStroke();

        Translate(Width / 2, Height / 2, 0);
        RotateY(FrameCount * 0.01f);
        RotateX(Map(MouseY, 0, Height, -1, 1));

        Fill(255, 150, 100);
        Box(140);

        Translate(200, 0, 0);
        Fill(120, 190, 255);
        Sphere(50);
    }
}