Skip to content

Commit 30ffb95

Browse files
committed
docs: update basic example to represent solar system
1 parent f31f269 commit 30ffb95

3 files changed

Lines changed: 121 additions & 90 deletions

File tree

playground/App.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { A, Route, Router } from "@solidjs/router"
22
import type { ParentProps } from "solid-js"
33
import * as THREE from "three"
4-
import { Canvas, extend, T } from "../src/index.ts"
5-
import { Basic } from "./Basic.tsx"
4+
import { extend, T } from "../src/index.ts"
5+
import { Basic } from "./examples/Basic.tsx"
66
import "./index.css"
77

88
extend(THREE)
@@ -44,16 +44,7 @@ function Layout(props: ParentProps) {
4444
Home
4545
</A>
4646
</nav>
47-
<Canvas
48-
style={{ width: "100vw", height: "100vh" }}
49-
camera={{ position: new THREE.Vector3(0, 0, 10) }}
50-
onClick={event => console.debug("canvas clicked", event)}
51-
onClickMissed={event => console.debug("canvas click missed", event)}
52-
onPointerLeave={event => console.debug("canvas pointer leave", event)}
53-
onPointerEnter={event => console.debug("canvas pointer enter", event)}
54-
>
55-
{props.children}
56-
</Canvas>
47+
{props.children}
5748
</>
5849
)
5950
}

playground/Basic.tsx

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

playground/examples/Basic.tsx

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import { createContext, createSignal, Show, useContext, type ParentProps, type Ref } from "solid-js"
2+
import * as THREE from "three"
3+
import { Canvas, T, useFrame } from "../../src/index.ts"
4+
5+
const HoverContext = createContext<{
6+
hovered: () => string | null
7+
setHovered: (name: string | null) => void
8+
}>()
9+
10+
function useHover() {
11+
const context = useContext(HoverContext)
12+
if (!context) throw new Error("useHover must be used within HoverProvider")
13+
return context
14+
}
15+
16+
function CelestialBody(
17+
props: ParentProps<{
18+
ref?: Ref<THREE.Object3D>
19+
name: string
20+
radius: number
21+
color: string
22+
emissive?: string
23+
position?: [number, number, number]
24+
rotation?: [number, number, number]
25+
}>,
26+
) {
27+
const [hovered, setHovered] = createSignal(false)
28+
29+
return (
30+
<T.Mesh
31+
ref={props.ref}
32+
position={props.position || [0, 0, 0]}
33+
rotation={props.rotation || [0, 0, 0]}
34+
onPointerEnter={e => {
35+
setHovered(true)
36+
}}
37+
onPointerLeave={e => {
38+
setHovered(false)
39+
}}
40+
>
41+
<T.SphereGeometry args={[props.radius, 32, 32]} />
42+
<T.MeshBasicMaterial color={props.color} />
43+
{props.children}
44+
45+
{/* Outline sphere - only visible when hovered */}
46+
<Show when={hovered()}>
47+
<T.Mesh>
48+
<T.SphereGeometry args={[props.radius + 0.1, 32, 32]} />
49+
<T.MeshBasicMaterial color="red" side={THREE.BackSide} />
50+
</T.Mesh>
51+
</Show>
52+
</T.Mesh>
53+
)
54+
}
55+
56+
export function Basic() {
57+
const [hovered, setHovered] = createSignal<string | null>(null)
58+
let earthRef: THREE.Object3D = null!
59+
let moonRef: THREE.Object3D = null!
60+
61+
return (
62+
<Canvas
63+
style={{ width: "100vw", height: "100vh" }}
64+
camera={{ position: new THREE.Vector3(0, 0, 15) }}
65+
onClick={event => console.debug("canvas clicked", event)}
66+
onClickMissed={event => console.debug("canvas click missed", event)}
67+
onPointerLeave={event => console.debug("canvas pointer leave", event)}
68+
onPointerEnter={event => console.debug("canvas pointer enter", event)}
69+
>
70+
{(() => {
71+
useFrame(state => {
72+
// Earth orbits around sun in an ellipse
73+
const time = state.clock.getElapsedTime()
74+
const a = 4 // semi-major axis
75+
const b = 3 // semi-minor axis
76+
earthRef.position.x = a * Math.cos(time * 0.125)
77+
earthRef.position.z = b * Math.sin(time * 0.125)
78+
79+
// Moon orbits around earth
80+
moonRef.position.x = 1.5 * Math.cos(time * 0.5)
81+
moonRef.position.z = 1.5 * Math.sin(time * 0.5)
82+
})
83+
return null!
84+
})()}
85+
<HoverContext.Provider value={{ hovered, setHovered }}>
86+
<T.AmbientLight intensity={0.2} />
87+
<T.PointLight position={[0, 0, 0]} intensity={2} />
88+
89+
{/* Sun */}
90+
<CelestialBody
91+
name="sun"
92+
radius={1.5}
93+
color="#FDB813"
94+
emissive="#FDB813"
95+
rotation={[0.5, 0.5, 0.5]}
96+
>
97+
{/* Earth system */}
98+
<CelestialBody
99+
ref={earthRef!}
100+
name="earth"
101+
radius={0.5}
102+
color="#1E90FF"
103+
emissive="#4169E1"
104+
>
105+
{/* Moon */}
106+
<CelestialBody
107+
ref={moonRef!}
108+
name="moon"
109+
radius={0.2}
110+
color="#C0C0C0"
111+
emissive="#FFFFFF"
112+
/>
113+
</CelestialBody>
114+
</CelestialBody>
115+
</HoverContext.Provider>
116+
</Canvas>
117+
)
118+
}

0 commit comments

Comments
 (0)