|
| 1 | +--- |
| 2 | +title: Building a Scene |
| 3 | +--- |
| 4 | + |
| 5 | +import { Application, Entity } from '@playcanvas/react'; |
| 6 | +import { Camera, Render, Light, Collision } from '@playcanvas/react/components'; |
| 7 | +import { useState, lazy } from 'react'; |
| 8 | +import Tabs from '@theme/Tabs'; |
| 9 | +import TabItem from '@theme/TabItem'; |
| 10 | +import InteractiveSource from '!!raw-loader!@site/src/components/playcanvas-react-example'; |
| 11 | +import CodeBlock from '@theme/CodeBlock'; |
| 12 | +import BrowserOnly from '@docusaurus/BrowserOnly'; |
| 13 | + |
| 14 | +export const LazyInteractive = lazy(() => import('@site/src/components/playcanvas-react-example')); |
| 15 | + |
| 16 | +Let's build a simple 3D scene step by step using PlayCanvas React. We'll create a basic scene with a lit sphere and see it running live in your browser. |
| 17 | + |
| 18 | +## Starting Point |
| 19 | + |
| 20 | +First, let's create the basic structure of our application using the `Application` component: |
| 21 | + |
| 22 | +```jsx |
| 23 | +import { Application } from '@playcanvas/react' |
| 24 | + |
| 25 | +export default function App() { |
| 26 | + return ( |
| 27 | + <Application> |
| 28 | + {/* Your 3D scene will go here */} |
| 29 | + </Application> |
| 30 | + ) |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +This creates an empty 3D scene. However, we can't see anything rendered yet. We need a camera and some content. |
| 35 | + |
| 36 | +## Adding a Camera |
| 37 | + |
| 38 | +To view our scene, we need a camera. In PlayCanvas React, we use `Entity` components as containers and attach component behaviors like `Camera`: |
| 39 | + |
| 40 | +```jsx |
| 41 | +import { Application, Entity } from '@playcanvas/react' |
| 42 | +import { Camera } from '@playcanvas/react/components' |
| 43 | + |
| 44 | +export default function App() { |
| 45 | + return ( |
| 46 | + <Application> |
| 47 | + <Entity name="camera" position={[0, 0, 5]}> |
| 48 | + <Camera /> |
| 49 | + </Entity> |
| 50 | + </Application> |
| 51 | + ) |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +We've added a camera entity positioned 5 units down the positive Z axis. By default, a camera looks down the negative Z axis so our camera is now looking at the origin. The rendered scene shows a solid grey color (the clear color of the camera). |
| 56 | + |
| 57 | +## Adding a Light |
| 58 | + |
| 59 | +Let's add a directional light to illuminate our scene: |
| 60 | + |
| 61 | +```jsx |
| 62 | +import { Application, Entity } from '@playcanvas/react' |
| 63 | +import { Camera, Light } from '@playcanvas/react/components' |
| 64 | + |
| 65 | +export default function App() { |
| 66 | + return ( |
| 67 | + <Application> |
| 68 | + <Entity name="camera" position={[0, 0, 5]}> |
| 69 | + <Camera /> |
| 70 | + </Entity> |
| 71 | + <Entity name="light" rotation={[45, 45, 0]}> |
| 72 | + <Light type="directional" /> |
| 73 | + </Entity> |
| 74 | + </Application> |
| 75 | + ) |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +The light is rotated to shine at an angle, which will create more interesting shading on our objects. |
| 80 | + |
| 81 | +## Adding an Object |
| 82 | + |
| 83 | +Now let's add a sphere to our scene using the `Render` component: |
| 84 | + |
| 85 | +<Tabs> |
| 86 | +<TabItem value="code" label="Code"> |
| 87 | + |
| 88 | +```jsx |
| 89 | +import { Application, Entity } from '@playcanvas/react' |
| 90 | +import { Camera, Light, Render } from '@playcanvas/react/components' |
| 91 | + |
| 92 | +export default function App() { |
| 93 | + return ( |
| 94 | + <Application> |
| 95 | + <Entity name="camera" position={[0, 0, 5]}> |
| 96 | + <Camera /> |
| 97 | + </Entity> |
| 98 | + <Entity name="light" rotation={[45, 45, 0]}> |
| 99 | + <Light type="directional" /> |
| 100 | + </Entity> |
| 101 | + <Entity name="sphere"> |
| 102 | + <Render type="sphere" /> |
| 103 | + </Entity> |
| 104 | + </Application> |
| 105 | + ) |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +</TabItem> |
| 110 | +<TabItem value="demo" label="Live Demo"> |
| 111 | + |
| 112 | +export const CompleteScene = () => { |
| 113 | + return ( |
| 114 | + <div style={{height: '400px', width: '100%', position: 'relative'}}> |
| 115 | + <Application> |
| 116 | + <Entity name="camera" position={[0, 0, 5]}> |
| 117 | + <Camera clearColor="#1a1a1a" /> |
| 118 | + </Entity> |
| 119 | + <Entity name="light" rotation={[45, 45, 0]}> |
| 120 | + <Light type="directional" intensity={1} /> |
| 121 | + </Entity> |
| 122 | + <Entity name="sphere"> |
| 123 | + <Render type="sphere" /> |
| 124 | + </Entity> |
| 125 | + </Application> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +<CompleteScene /> |
| 131 | + |
| 132 | +</TabItem> |
| 133 | +</Tabs> |
| 134 | + |
| 135 | +You should now see a white sphere in the center of your screen! |
| 136 | + |
| 137 | +## Making it Interactive |
| 138 | + |
| 139 | +One of the great advantages of using React is how easy it is to add interactivity. Let's make our sphere respond to clicks. |
| 140 | + |
| 141 | +**Click on the Demo Tab to view the results.** |
| 142 | + |
| 143 | +<Tabs> |
| 144 | + <TabItem default value="code" label="Code"> |
| 145 | + <CodeBlock language="jsx">{InteractiveSource}</CodeBlock> |
| 146 | + </TabItem> |
| 147 | + <TabItem value="demo" label="Demo" className='example-demo'> |
| 148 | + <BrowserOnly> |
| 149 | + {() => <LazyInteractive/>} |
| 150 | + </BrowserOnly> |
| 151 | + </TabItem> |
| 152 | +</Tabs> |
| 153 | + |
| 154 | +## Key Differences from Web Components |
| 155 | + |
| 156 | +When building scenes with PlayCanvas React compared to [Web Components](/user-manual/web-components/): |
| 157 | + |
| 158 | +1. **Component Structure**: Instead of HTML tags like `<pc-app>`, we use React components like `<Application>` |
| 159 | +2. **Props vs Attributes**: We use React props with camelCase (e.g., `clearColor`) instead of HTML attributes |
| 160 | +3. **Event Handling**: We can use React's event system directly (e.g., `onClick`) |
| 161 | +4. **State Management**: We can leverage React hooks like `useState` for dynamic behavior |
| 162 | +5. **Type Safety**: Full TypeScript support with type safety. |
| 163 | + |
| 164 | +## Next Steps |
| 165 | + |
| 166 | +Now that you've built your first scene, try: |
| 167 | + |
| 168 | +- Adding more objects with different shapes (`box`, `cylinder`, `cone`, etc.) |
| 169 | +- Experimenting with different light types (`point`, `spot`) |
| 170 | +- Adding movement with the `OrbitControls` script |
| 171 | +- Exploring physics by adding `RigidBody` and `Collision` components |
| 172 | + |
| 173 | +Now you have the basics, see the [documentation](https://playcanvas-react.vercel.app/docs) for more examples. |
0 commit comments