|
| 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