Skip to content

Commit ec9c7df

Browse files
🚚 R3f first application
1 parent 77119fe commit ec9c7df

File tree

10 files changed

+124
-137
lines changed

10 files changed

+124
-137
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
node_modules
1+
.vercel

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
},
1414
"dependencies": {
1515
"react": "18.2",
16-
"react-dom": "18.2"
16+
"react-dom": "18.2",
17+
"@react-three/fiber": "^8.15.8",
18+
"three": "^0.158.0"
1719
}
18-
}
20+
}

src/App.jsx

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/Clicker.jsx

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/CustomObject.jsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useEffect, useRef, useMemo } from 'react'
2+
import * as THREE from 'three'
3+
4+
export default function CustomObject()
5+
{
6+
const geometryRef = useRef()
7+
8+
const verticesCount = 10 * 3
9+
10+
const positions = useMemo(() =>
11+
{
12+
const positions = new Float32Array(verticesCount * 3)
13+
14+
for(let i = 0; i < verticesCount * 3; i++)
15+
positions[i] = (Math.random() - 0.5) * 3
16+
17+
return positions
18+
}, [])
19+
20+
useEffect(() =>
21+
{
22+
geometryRef.current.computeVertexNormals()
23+
}, [])
24+
25+
return <mesh>
26+
<bufferGeometry ref={ geometryRef }>
27+
<bufferAttribute
28+
attach="attributes-position"
29+
count={ verticesCount }
30+
itemSize={ 3 }
31+
array={ positions }
32+
/>
33+
</bufferGeometry>
34+
<meshStandardMaterial color="red" side={ THREE.DoubleSide } />
35+
</mesh>
36+
}

src/Experience.jsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { useThree, extend, useFrame } from '@react-three/fiber'
2+
import { useRef } from 'react'
3+
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'
4+
import CustomObject from './CustomObject.jsx'
5+
6+
extend({ OrbitControls })
7+
8+
export default function Experience()
9+
{
10+
const { camera, gl } = useThree()
11+
12+
const cubeRef = useRef()
13+
const groupRef = useRef()
14+
15+
useFrame((state, delta) =>
16+
{
17+
// const angle = state.clock.elapsedTime
18+
// state.camera.position.x = Math.sin(angle) * 8
19+
// state.camera.position.z = Math.cos(angle) * 8
20+
// state.camera.lookAt(0, 0, 0)
21+
22+
cubeRef.current.rotation.y += delta
23+
// groupRef.current.rotation.y += delta
24+
})
25+
26+
return <>
27+
<orbitControls args={ [ camera, gl.domElement ] } />
28+
29+
<directionalLight position={ [ 1, 2, 3 ] } intensity={ 4.5 } />
30+
<ambientLight intensity={ 1.5 } />
31+
32+
<group ref={ groupRef }>
33+
<mesh position-x={ - 2 }>
34+
<sphereGeometry />
35+
<meshStandardMaterial color="orange" />
36+
</mesh>
37+
38+
<mesh ref={ cubeRef } rotation-y={ Math.PI * 0.25 } position-x={ 2 } scale={ 1.5 }>
39+
<boxGeometry />
40+
<meshStandardMaterial color="mediumpurple" />
41+
</mesh>
42+
</group>
43+
44+
<mesh position-y={ - 1 } rotation-x={ - Math.PI * 0.5 } scale={ 10 }>
45+
<planeGeometry />
46+
<meshStandardMaterial color="greenyellow" />
47+
</mesh>
48+
49+
<CustomObject />
50+
</>
51+
}

src/People.jsx

Lines changed: 0 additions & 34 deletions
This file was deleted.

src/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8">
55
<meta http-equiv="X-UA-Compatible" content="IE=edge">
66
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7-
<title>42 - First React Application</title>
7+
<title>43 - First React Three Fiber Application</title>
88
</head>
99
<body>
1010
<div id="root"></div>

src/index.jsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1-
import { createRoot } from 'react-dom/client'
2-
import App from './App.jsx'
1+
import './style.css'
2+
import ReactDOM from 'react-dom/client'
3+
import { Canvas } from '@react-three/fiber'
4+
import Experience from './Experience.jsx'
5+
import * as THREE from 'three'
36

4-
const root = createRoot(document.querySelector('#root'))
7+
const root = ReactDOM.createRoot(document.querySelector('#root'))
58

69
root.render(
7-
<App clickersCount={ 3 }>
8-
<h1>My First React App</h1>
9-
<h2>And a fancy subtitle</h2>
10-
</App>
10+
<Canvas
11+
gl={ {
12+
antialias: true,
13+
toneMapping: THREE.ACESFilmicToneMapping,
14+
// outputColorSpace: THREE.SRGBColorSpace
15+
} }
16+
camera={ {
17+
fov: 45,
18+
near: 0.1,
19+
far: 200,
20+
position: [ 3, 2, 6 ]
21+
} }
22+
>
23+
<Experience />
24+
</Canvas>
1125
)

src/style.css

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
.purpleSquare
1+
html,
2+
body,
3+
#root
24
{
3-
width: 200px;
4-
height: 200px;
5-
background-color: mediumpurple;
5+
position: fixed;
6+
top: 0;
7+
left: 0;
8+
width: 100%;
9+
height: 100%;
10+
overflow: hidden;
11+
background: LightSkyBlue;
612
}

0 commit comments

Comments
 (0)