1- import * as React from 'react'
2- import { Vector3 } from 'three'
31import { Meta , StoryObj } from '@storybook/react-vite'
42
53import { Setup } from '../Setup'
6- import { useTurntable } from '../useTurntable'
74
8- import { Box , Center , useGLTF } from '../../src'
9- import { ComponentProps } from 'react'
5+ import { Box , Center , Cylinder , Gltf } from '../../src'
6+ import { Ref , useMemo , useState } from 'react'
7+ import { Box3 , Vector3 } from 'three'
108
119export default {
1210 title : 'Staging/Center' ,
1311 component : Center ,
1412 decorators : [
1513 ( Story ) => (
16- < Setup cameraPosition = { new Vector3 ( 0 , 0 , - 10 ) } >
14+ < Setup cameraPosition = { new Vector3 ( 2 , 2 , 2 ) } >
1715 < Story />
1816 </ Setup >
1917 ) ,
@@ -22,30 +20,130 @@ export default {
2220
2321type Story = StoryObj < typeof Center >
2422
25- const SimpleExample = ( props : ComponentProps < typeof Center > ) => {
26- const { scene } = useGLTF ( 'LittlestTokyo.glb' )
23+ function LittlestTokyo ( { catMeshRef } : { catMeshRef ?: Ref < THREE . Mesh > } ) {
24+ return (
25+ < group >
26+ < Gltf src = "LittlestTokyo.glb" scale = { 0.002 } />
27+
28+ { catMeshRef && (
29+ // we draw a box around the cat
30+ < Box ref = { catMeshRef } position = { [ 0.095 , 0.35 , 0.25 ] } args = { [ 0.2 , 0.2 , 0.2 ] } >
31+ < meshStandardMaterial color = "green" transparent opacity = { 0.5 } />
32+ </ Box >
33+ ) }
34+ </ group >
35+ )
36+ }
37+
38+ //
39+
40+ /**
41+ * `children` are centered, by default at (0,0,0)
42+ */
43+ export const St1 = {
44+ render : ( ) => (
45+ < >
46+ < axesHelper />
47+ < Center >
48+ < group
49+ position = { [ 100 , 100 , 100 ] } // whatever inner position
50+ >
51+ < LittlestTokyo />
52+ </ group >
53+ </ Center >
54+ </ >
55+ ) ,
56+ name : 'Default' ,
57+ } satisfies Story
58+
59+ //
60+
61+ /**
62+ * if `position` is set, children are centered at that position
63+ */
64+ export const St2 = {
65+ render : ( ) => (
66+ < >
67+ < axesHelper />
68+ < Center position = { [ 0 , 1 , 0 ] } >
69+ < group
70+ position = { [ 100 , 100 , 100 ] } // whatever inner position
71+ >
72+ < LittlestTokyo />
73+ </ group >
74+ </ Center >
75+ </ >
76+ ) ,
77+ name : '[position]' ,
78+ } satisfies Story
2779
28- const ref = useTurntable ( )
80+ //
81+
82+ /**
83+ * At `top` of centered position
84+ */
85+ export const St3 = {
86+ render : ( ) => (
87+ < >
88+ < axesHelper />
89+ < Center position = { [ 0 , 1 , 0 ] } top >
90+ < group position = { [ 100 , 100 , 100 ] } >
91+ < LittlestTokyo />
92+ </ group >
93+ </ Center >
94+ </ >
95+ ) ,
96+ name : '[position][top]' ,
97+ } satisfies Story
98+
99+ //
100+
101+ function St4Scene ( ) {
102+ const [ catMesh , setCatMesh ] = useState < THREE . Mesh | null > ( null )
29103
30104 return (
31- < Center position = { [ 5 , 5 , 10 ] } { ...props } >
32- < Box args = { [ 10 , 10 , 10 ] } >
33- < meshNormalMaterial wireframe />
34- </ Box >
35- < primitive ref = { ref } object = { scene } scale = { [ 0.01 , 0.01 , 0.01 ] } />
36- </ Center >
105+ < >
106+ < axesHelper />
107+ < Center object = { catMesh } >
108+ < group position = { [ 100 , 100 , 100 ] } >
109+ < LittlestTokyo catMeshRef = { setCatMesh } />
110+ </ group >
111+ </ Center >
112+ </ >
37113 )
38114}
39115
40- function CenterScene ( props : ComponentProps < typeof Center > ) {
116+ /**
117+ * An inner `object` can be specified: its bounding box will be used to center (instead of the one of `children`, by default).
118+ */
119+
120+ export const St4 = {
121+ render : ( ) => < St4Scene /> ,
122+ name : '[object]' ,
123+ } satisfies Story
124+
125+ //
126+
127+ function St5Scene ( ) {
128+ const [ catMesh , setCatMesh ] = useState < THREE . Mesh | null > ( null )
129+
41130 return (
42- < React . Suspense fallback = { null } >
43- < SimpleExample { ...props } />
44- </ React . Suspense >
131+ < >
132+ < axesHelper />
133+ < Center object = { catMesh } position = { [ 0 , 1 , 0 ] } top >
134+ < group position = { [ 100 , 100 , 100 ] } >
135+ < LittlestTokyo catMeshRef = { setCatMesh } />
136+ </ group >
137+ </ Center >
138+ </ >
45139 )
46140}
47141
48- export const CenterSt = {
49- render : ( args ) => < CenterScene { ...args } /> ,
50- name : 'Default' ,
142+ /**
143+ * Inner "cat mesh" centered at `top` of (0,1,0) position
144+ */
145+
146+ export const St5 = {
147+ render : ( ) => < St5Scene /> ,
148+ name : '[object][position][top]' ,
51149} satisfies Story
0 commit comments