PlayCanvas React

repository·main·Indexed 19 days ago

https://github.com/playcanvas/react

A library for interactive 3D in React that provides a declarative wrapper around the PlayCanvas engine using an Entity Component System (ECS) pattern. It includes @playcanvas/react for core scene rendering with components like <Application>, <Entity>, and <Camera>, as well as @playcanvas/blocks for high-level 3D primitives and a specialized SplatViewer for displaying Gaussian splats with built-in UI controls and camera management.

Tokens
18.6K
Snippets
62
Records
88
Agent score
65%

What's inside playcanvas-react

  1. Configure Tailwind CSS for @playcanvas/blocks

    main

    If you are using Tailwind CSS, you must import the @playcanvas/blocks styles and configure the content source so Tailwind can scan the package for utility classes. Add the following to your CSS file:

    @import "@playcanvas/blocks";
    @source "../../node_modules/@playcanvas/blocks";
    @import "@playcanvas/blocks"
    @source "../../node_modules/@playcanvas/blocks";
  2. Configure AI-assisted editors (Cursor)

    main

    To improve IDE support in Cursor, you can install the PlayCanvas React rules by downloading the .mdc rule file into your .cursor/rules directory.

    mkdir -p .cursor/rules && curl -s https://raw.githubusercontent.com/playcanvas/react/main/packages/lib/.playcanvas-react.mdc -o .cursor/rules/playcanvas-react.mdc
  3. Understand the structure of @playcanvas/react

    main

    The library is organized into three main layers to build 3D content:

    1. Core: Provides fundamental building blocks like Application and Entity.
    2. Components: Provides behaviors that you attach to Entity nodes (e.g., Light, Camera, Render, Collision).
    3. Hooks: Provides logic and asset management (e.g., useModel, useSplat, useApp).
    // Core 
    import { Application, Entity } from '@playcanvas/react'
    // Components add behaviors to Entities
    import { Light, Camera, Render, Gsplat, Screen, Collision, RigidBody, Anim } from '@playcanvas/react/components'
    // Hooks add functionality
    import { useModel, useSplat, useAppEvent, useParent, useApp } from '@playcanvas/react/hooks'
  4. Use SplatViewer controls and buttons

    main

    The SplatViewer ecosystem provides several sub-components for managing the viewer UI:

    Controls Wrapper

    • Controls: A wrapper for layout that provides a container for UI elements. It supports an autoHide prop which causes the controls to fade out when not in use.

    UI Buttons

    These components should be placed inside the <SplatViewer /> component (typically within <Controls />) to function as overlays:

    • FullScreenButton: Toggles full-screen mode.
    • DownloadButton: Triggers a download of the asset.
    • MenuButton: Opens the viewer menu.
    • CameraModeToggle: Switches between different camera control modes.
    • HelpButton: Displays help information.
  5. Use the Entity component to create 3D objects

    main

    The Entity component is the fundamental building block of a PlayCanvas scene. It represents a node in the scene graph and can have components attached to it via its children. You can control its transform properties (name, position, scale, rotation) and attach pointer event listeners directly through props.

    To use Entity, wrap other components (like Render) inside it. The Entity component also provides a ParentContext so that child components can access the underlying PlayCanvas Entity instance.

    // Basic usage
    <Entity name="myEntity" position={[0, 1, 0]}>
      <Render type="box" />
    </Entity>
    
    // With pointer events
    <Entity 
      position={[0, 1, 0]}
      onPointerDown={(e) => console.log('Clicked!')}
      onClick={(e) => console.log('Mouse clicked!')}
    >
      <Render type="sphere" />
    </Entity>
    <Entity name="myEntity" position={[0, 1, 0]}>
      <Render type="box" />
    </Entity>
  6. Use the SplatViewer component

    main

    The SplatViewer is a responsive component for displaying Gaussian splats. It supports lazy loading for large assets, automatic framing, and built-in camera controls. You can pass UI elements (like buttons or menus) as children to the component to create overlays.

    import * as Splat from '@/components/ui/splat-viewer';
    
    export function SplatViewerDemo() {
      const splatUrl = 'https://6rpjo46zo7.ufs.sh/f/dTTBXSHFOX4z7irrg5DfZElT6yeQYKvdjgi5IsoB0cmu9OtF';
    
      return (
        <Splat.Viewer src={splatUrl} autoPlay className="rounded-t-lg lg:rounded-lg shadow-xl cursor-grab active:cursor-grabbing" >
          <Splat.Controls autoHide >
            <div className="flex gap-1 pointer-events-auto flex-grow">
                <Splat.FullScreenButton />
                <Splat.DownloadButton />
            </div>
            <div className="flex gap-1 pointer-events-auto">
              <Splat.CameraModeToggle />
              <Splat.HelpButton />
              <Splat.MenuButton />
            </div>
          </Splat.Controls>
        </Splat.Viewer>
      )
    }
  7. Rules for creating 3D content with @playcanvas/react

    main

    When building 3D scenes, adhere to these architectural rules:

    • Initialization: Always wrap your root components in an <Application /> component to initialize the PlayCanvas engine.
    • Transformations: Use the <Entity /> component to define a transform node. It handles position, rotation, and scale but does not render anything by itself.
    • Functionality: Attach behaviors to entities using components from @playcanvas/react/components (e.g., <Camera />, <Light />, <Render />).
    • Asset Loading: Use specialized hooks like useModel(src) or useSplat(src) to load assets.
    • Materials: Create materials using the useMaterial() hook.
    • Data Formats: Use numeric arrays for colors and vectors:
      • Colors: [1, 0, 0]
      • Position/rotation/scale: [0, 0, 0]
  8. Render a basic 3D scene with @playcanvas/react

    main

    You can render 3D objects by composing <Application>, <Entity>, and component-based elements like <Camera /> and <Render />. The <Application> component serves as the root for the PlayCanvas engine within your React tree.

    import { Application, Entity } from '@playcanvas/react';
    import { Camera, Render } from '@playcanvas/react/components';
    import { OrbitControls } from '@playcanvas/react/scripts';
    
    export function AssetViewer() {
      return (
        <Application>
          <Entity position={[0, 2, 0]}>
            <Camera />
            <OrbitControls />
          </Entity>
          <Render type="sphere"/>
        </Application>
      );
    }