|
1 |
| -# Building your own NPC Universe on web browser |
| 1 | +## Getting Started |
2 | 2 |
|
3 |
| -\***\*Prerequisites\*\*** |
| 3 | +### React App Setup: |
4 | 4 |
|
5 |
| -A foundational understanding of [THREE JS](https://threejs.org/) and [React three fiber](https://docs.pmnd.rs/react-three-fiber/getting-started/introduction). Basic scene setup. |
| 5 | +Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects. |
| 6 | + |
| 7 | +```bash |
| 8 | +With NPM: |
| 9 | +$ npm create vite@latest |
| 10 | + |
| 11 | +With Yarn: |
| 12 | +$ yarn create vite |
| 13 | + |
| 14 | +With PNPM: |
| 15 | +$ pnpm create vite |
| 16 | +``` |
| 17 | + |
| 18 | +```bash |
| 19 | +cd my-project |
| 20 | + |
| 21 | +npm install |
| 22 | +npm run dev |
| 23 | +``` |
| 24 | + |
| 25 | +**Installing Dependencies** |
| 26 | + |
| 27 | +```bash |
| 28 | +npm install three @react-three/fiber @react-three/drei @react-three/rapier |
| 29 | +``` |
| 30 | + |
| 31 | +**App.jsx** |
| 32 | + |
| 33 | +```jsx |
| 34 | +import { Canvas } from "@react-three/fiber"; |
| 35 | +import { Experience } from "./components/Experience"; |
| 36 | +import { Loader, OrbitControls, Stats } from "@react-three/drei"; |
| 37 | +import { Physics } from "@react-three/rapier"; |
| 38 | +import { useRef } from "react"; |
| 39 | + |
| 40 | +function App() { |
| 41 | + const testing = false; |
| 42 | + return ( |
| 43 | + <> |
| 44 | + <Canvas shadows camera={{ position: [0, 2, 3], fov: 75 }}> |
| 45 | + <color attach="background" args={["#ececec"]} /> |
| 46 | + {testing ? <Stats /> : null} |
| 47 | + {testing ? <axesHelper args={[2]} /> : null} |
| 48 | + <OrbitControls /> |
| 49 | + <Physics debug={testing ? true : false}> |
| 50 | + <Experience /> |
| 51 | + </Physics> |
| 52 | + </Canvas> |
| 53 | + <Loader /> |
| 54 | + </> |
| 55 | + ); |
| 56 | +} |
| 57 | + |
| 58 | +export default App; |
| 59 | +``` |
| 60 | + |
| 61 | +**Experience.jsx** |
6 | 62 |
|
7 | 63 | ```jsx
|
8 |
| -import * as THREE from 'three' |
9 |
| -import { createRoot } from 'react-dom/client' |
10 |
| -import React, { useRef, useState } from 'react' |
11 |
| -import { Canvas, useFrame } from '@react-three/fiber' |
| 64 | +import { RigidBody } from "@react-three/rapier"; |
| 65 | +import { Platform } from "./Platform"; |
| 66 | +export const Experience = ({ heroRef }) => { |
| 67 | + return ( |
| 68 | + <> |
| 69 | + <ambientLight intensity={0.1} /> |
| 70 | + {/* <OrbitControls /> */} |
| 71 | + <directionalLight position={[0, 20, 20]} intensity={1} /> |
| 72 | + <RigidBody friction={2} colliders="trimesh" type="fixed"> |
| 73 | + <Platform /> |
| 74 | + </RigidBody> |
| 75 | + </> |
| 76 | + ); |
| 77 | +}; |
| 78 | +``` |
12 | 79 |
|
| 80 | +This will render a 3D scene with physics in it. To import 3D models to React Scene we can use the following command: |
13 | 81 |
|
14 |
| -const App = () => { |
| 82 | +```bash |
| 83 | +npx gltfjsx 'path' |
| 84 | +``` |
| 85 | + |
| 86 | +This will convert gltf/glb files to react component which we can render directly. |
| 87 | + |
| 88 | +```jsx |
| 89 | +import React, { useRef } from "react"; |
| 90 | +import { useGLTF, useAnimations } from "@react-three/drei"; |
| 91 | + |
| 92 | +export function Platform(props) { |
| 93 | + const group = useRef(); |
| 94 | + const { nodes, materials, animations } = useGLTF("/platform.glb"); |
| 95 | + const { actions } = useAnimations(animations, group); |
15 | 96 | return (
|
16 |
| - <Canvas |
17 |
| - camera={{ |
18 |
| - position: [5, 5, -5], |
19 |
| - fov: 75, |
20 |
| - }} |
21 |
| - > |
22 |
| - <ambientLight /> |
23 |
| - <pointLight position={[10, 10, 10]} /> |
24 |
| - <mesh> |
25 |
| - <boxGeometry args={[1, 1, 1]} /> |
26 |
| - <meshStandardMaterial color="orange" /> |
27 |
| - </mesh> |
28 |
| - </Canvas> |
29 |
| - ) |
| 97 | + <group ref={group} {...props} dispose={null}> |
| 98 | + <group name="Sketchfab_Scene"> |
| 99 | + <group name="Sketchfab_model" rotation={[-Math.PI / 2, 0, 0]}> |
| 100 | + <group name="root"> |
| 101 | + <group name="GLTF_SceneRootNode" rotation={[Math.PI / 2, 0, 0]}> |
| 102 | + <group name="Sphere_0" rotation={[0, -Math.PI / 2, 0]}> |
| 103 | + <mesh |
| 104 | + name="Object_4" |
| 105 | + geometry={nodes.Object_4.geometry} |
| 106 | + material={materials.Sky1} |
| 107 | + /> |
| 108 | + </group> |
| 109 | + ......... ......... ......... |
| 110 | + <group name="Cube009_41"> |
| 111 | + <mesh |
| 112 | + name="Object_88" |
| 113 | + geometry={nodes.Object_88.geometry} |
| 114 | + material={materials.StairsFirstFloorBaked} |
| 115 | + /> |
| 116 | + </group> |
| 117 | + <group |
| 118 | + name="Cube036_42" |
| 119 | + position={[-12.412, 3.471, -10.386]} |
| 120 | + rotation={[-Math.PI, -0.731, -Math.PI]} |
| 121 | + > |
| 122 | + <mesh |
| 123 | + name="Object_90" |
| 124 | + geometry={nodes.Object_90.geometry} |
| 125 | + material={materials.Stairs_SecondFloorBaked} |
| 126 | + /> |
| 127 | + </group> |
| 128 | + <group name="Cube008_43"> |
| 129 | + <mesh |
| 130 | + name="Object_92" |
| 131 | + geometry={nodes.Object_92.geometry} |
| 132 | + material={materials.StairsFirstFloorBaked} |
| 133 | + /> |
| 134 | + </group> |
| 135 | + <group name="Outer_44" position={[0, 3.259, 0]}> |
| 136 | + <mesh |
| 137 | + name="Object_94" |
| 138 | + geometry={nodes.Object_94.geometry} |
| 139 | + material={materials.Railing} |
| 140 | + /> |
| 141 | + </group> |
| 142 | + <group name="Armature_80"> |
| 143 | + <group name="GLTF_created_0"> |
| 144 | + <primitive object={nodes.GLTF_created_0_rootJoint} /> |
| 145 | + <group name="Inner_79" /> |
| 146 | + <skinnedMesh |
| 147 | + name="Object_99" |
| 148 | + geometry={nodes.Object_99.geometry} |
| 149 | + material={materials.Railing} |
| 150 | + skeleton={nodes.Object_99.skeleton} |
| 151 | + /> |
| 152 | + </group> |
| 153 | + </group> |
| 154 | + </group> |
| 155 | + </group> |
| 156 | + </group> |
| 157 | + </group> |
| 158 | + </group> |
| 159 | + ); |
30 | 160 | }
|
31 | 161 |
|
32 |
| -createRoot(document.getElementById('root')!).render(<App />) |
| 162 | +useGLTF.preload("/platform.glb"); |
33 | 163 | ```
|
| 164 | + |
| 165 | +**Platform Example:** |
| 166 | + |
| 167 | + |
| 168 | +Credits : [Interdimensional Art Gallery - floating platform](https://skfb.ly/o6FLW) [License](https://creativecommons.org/licenses/by/4.0/) |
0 commit comments