PlayCanvas Engine

repository·main·Indexed 12 days ago

https://github.com/playcanvas/engine

An open-source, high-performance WebGL2 and WebGPU 3D engine for creating interactive applications and games in the browser. Version 2.22.0-beta.12 includes support for WebAssembly modules such as ammo.js for physics, basis.js for texture compression, and zstd for Gaussian splat parsing.

Tokens
8.3K
Snippets
19
Records
38
Agent score
95%

What's inside PlayCanvas

  1. Overview of PlayCanvas Engine features

    main

    PlayCanvas is a full-featured engine built on WebGL2 and WebGPU. Key capabilities include:

    • Graphics: Advanced 2D + 3D engine.
    • Gaussian Splatting: Native support for loading and rendering 3D Gaussian Splats.
    • XR: Built-in support for immersive AR and VR via WebXR.
    • Physics: 3D rigid body physics using ammo.js.
    • Animation: State-based animation for characters and scenes.
    • Input: API for mouse, keyboard, touch, and gamepad.
    • Sound: 3D positional sound via Web Audio API.
    • Assets: Asynchronous streaming system supporting glTF 2.0, Draco, and Basis compression.
    • Scripting: Support for TypeScript and JavaScript.
  2. Explore the PlayCanvas Ecosystem

    main

    In addition to the core engine, the ecosystem includes several specialized packages:

    PackageDescription
    playcanvasCore engine
    @playcanvas/reactReact renderer for PlayCanvas
    @playcanvas/web-componentsDeclarative 3D via custom elements
    create-playcanvasProject scaffolding CLI
    PlayCanvas EditorBrowser-based visual editor
  3. Available PlayCanvas Engine Script categories

    main

    The engine scripts collection includes several specialized modules for common tasks:

    • Camera and character controllers: Orbit, fly, and pan camera controls, as well as first and third person controllers.
    • Rendering helpers: Post-processing via camera frame, planar reflections, shadow catcher, reference grid, and procedural sky.
    • Gaussian splatting: Streamed splat loading with LOD presets, reveal animations, shader effects, weather, and text/image splats.
    • XR: Session lifecycle management, controller support, teleport navigation, object manipulation, and 3D menus.
    • Annotations: 3D hotspots with DOM labels.
  4. Use precompiled WebAssembly modules with PlayCanvas

    main

    The PlayCanvas engine can optionally utilize precompiled WebAssembly (Wasm) modules for high-performance tasks. These modules are provided in the examples/assets/wasm/ directory and include physics, texture compression, and decompression utilities.

    Available modules:

    • ammo.js: A direct port of the Bullet physics engine.
    • basis.js: The Basis Universal GPU Texture Codec.
    • zstd: A Zstandard decompressor used specifically by the SPZ gaussian splat parser.
  5. What is zstd and how is it used?

    main

    zstd is a Zstandard (ZSTD) decompressor used by the SPZ gaussian splat parser.

    Technical details:

    • The Wasm binary is the single-file zstd decoder (zstddeclib) from the Facebook zstd repository.
    • It is compiled to WebAssembly via zstddec.
    • The module includes a hand-written glue script (zstd.wasm.js) that conforms to the WasmModule contract used by the engine.
  6. Use Example Modules in PlayCanvas Examples

    main

    When creating or working with examples, you can import specific modules to interact with the example environment (like device selection or UI controls). Use the following paths depending on the module type:

    • examples/context: Provides the data observer object and the selected deviceType (graphics settings).
    • examples/assets/*: Shared example modules. Use the prefix ./assets/... when a runtime asset URL string is required.
    • playcanvas/scripts/*: Shared engine script modules. Use the prefix ./scripts/... when a runtime script URL string is required.
  7. Create a new PlayCanvas example

    main

    Examples are implemented as classes in JavaScript located at ./src/examples/<category>/<exampleName>.example.mjs. An example typically consists of two main modules:

    1. <exampleName>.example.mjs (Required)

    This file contains the core logic. The code is executed every time the example is played. It must retrieve the canvas from the DOM and initialize a PlayCanvas Application or AppBase.

    Configuration via Comments: You can define metadata and engine settings using special comment blocks:

    • @config: Default configuration.
    • @keybinds: Key bindings.
    • @credit: Metadata (title, author, license).
    • @flag: Engine flags (e.g., WEBGPU_DISABLED, ENGINE=performance).

    2. <exampleName>.controls.jsx (Optional)

    Used to create a PCUI-based control panel via React. The component must be named Controls and accepts a single prop: observer (a pcui observer).

    3. Sidecar Files

    Any file added to the example folder with the example name prepended (e.g., <exampleName>.shader.vert) can be imported.

    • .frag, .vert, .wgsl, .glsl, .html, .css, .txt are imported as strings.
    • .json files are imported as parsed values.
    // <exampleName>.example.mjs
    import { Application } from 'playcanvas';
    
    // @config
    // @flag WEBGPU_DISABLED
    
    const canvas = /** @type {HTMLCanvasElement} */ (document.getElementById('application-canvas'));
    window.focus();
    
    const app = new Application(canvas, {});
    
    // Export destroy if you need to clean up non-app resources
    export function destroy() {
        // cleanup logic
    }
    // <exampleName>.controls.jsx
    import { Button } from '@playcanvas/pcui/react';
    
    /**
     * @param {{ observer: Observer }} props
     */
    export function Controls({ observer }) {
        return (
            <Button
                text='Flash'
                onClick={() => observer.set('flash', !observer.get('flash'))}
            />
        );
    }
  8. Install the PlayCanvas Engine

    main

    You can install the core engine via npm, or use the create-playcanvas scaffolding tool to set up a complete project structure immediately.

    # Install the core engine
    npm install playcanvas
    
    # Scaffold a full project
    npm create playcanvas@latest
  9. Deploy the Examples Browser

    main

    To deploy the examples browser, follow these steps to install dependencies, build the assets, and serve the application. Note that generating thumbnails is a separate step required only when adding new examples or updating existing thumbnails.

    # 1. Install Engine packages (run in /engine)
    npm install
    
    # 2. Build and launch the examples browser (run in /engine/examples)
    npm install
    npm run build
    npm run serve
    
    # 3. Generate thumbnails (run if examples are updated)
    npm run build:thumbnails
  10. Develop the Examples Browser locally

    main

    To develop the examples browser application locally, ensure Node.js is installed. You can run the dev server with automatic browser and example reloads, or use a version of the engine from a specific path (e.g., a built ESM version or directly from source).

    By default, the server binds to 0.0.0.0:5555. You can override this using EXAMPLES_HOST or EXAMPLES_PORT environment variables.

    # Install dependencies
    npm install
    
    # Run with automatic reloads
    npm run dev
    
    # Run without automatic reloads
    npm run develop
    
    # Run with a specific built ESM engine
    ENGINE_PATH=../build/playcanvas.mjs npm run dev
    
    # Run directly from source
    ENGINE_PATH=../src/index.js npm run dev