Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 3a8678f

Browse files
committed
Add 'three-fiber' example
1 parent 7fbe01e commit 3a8678f

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

examples/three-fiber/aleph.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Config } from 'aleph/types'
2+
3+
export default (): Config => ({
4+
ssr: false
5+
})

examples/three-fiber/app.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, { ComponentType } from 'react'
2+
import './style/index.css'
3+
4+
export default function App({ Page, pageProps }: { Page: ComponentType<any>, pageProps: any }) {
5+
return (
6+
<main>
7+
<head>
8+
<meta name="viewport" content="width=device-width" />
9+
</head>
10+
<Page {...pageProps} />
11+
</main>
12+
)
13+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { useFrame, MeshProps } from 'https://esm.sh/@react-three/fiber'
2+
import React, { useRef, useState } from 'react'
3+
4+
export function Box(props: MeshProps) {
5+
// Set up state for the hovered and active state
6+
const [hovered, setHover] = useState(false)
7+
const [active, setActive] = useState(false)
8+
9+
// This reference will give us direct access to the mesh
10+
const ref = useRef<any>()
11+
12+
// Rotate mesh every frame, this is outside of React without overhead
13+
useFrame(() => {
14+
ref.current.rotation.x = ref.current.rotation.y += 0.01
15+
})
16+
17+
return (
18+
<mesh
19+
{...props}
20+
ref={ref}
21+
scale={active ? 1.5 : 1}
22+
onClick={() => setActive(!active)}
23+
onPointerOver={() => setHover(true)}
24+
onPointerOut={() => setHover(false)}>
25+
<boxGeometry args={[1, 1, 1]} />
26+
<meshStandardMaterial color={hovered ? 'hotpink' : 'orange'} />
27+
</mesh>
28+
)
29+
}

examples/three-fiber/pages/index.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Canvas } from 'https://esm.sh/@react-three/fiber'
2+
import React from 'react'
3+
import { Box } from '../components/Box.tsx'
4+
5+
export default function Home() {
6+
return (
7+
<Canvas>
8+
<ambientLight intensity={0.5} />
9+
<spotLight position={[10, 10, 10]} angle={0.15} penumbra={1} />
10+
<pointLight position={[-10, -10, -10]} />
11+
<Box position={[-1.2, 0, 0]} />
12+
<Box position={[1.2, 0, 0]} />
13+
</Canvas>
14+
)
15+
}

examples/three-fiber/style/index.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
* {
2+
margin: 0;
3+
padding: 0;
4+
border: none;
5+
outline: none;
6+
font: inherit;
7+
font-size: 100%;
8+
vertical-align: baseline;
9+
background: transparent;
10+
}
11+
12+
body {
13+
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
14+
font-size: 16px;
15+
}
16+
17+
main {
18+
width: 100vw;
19+
height: 100vh;
20+
}

0 commit comments

Comments
 (0)