@@ -2,11 +2,12 @@ import React, { useEffect, useRef } from 'react';
22import { Scene as BabylonScene , SceneOptions , WebXRDefaultExperienceOptions , HavokPlugin , Vector3 , Nullable , Camera } from '@babylonjs/core' ;
33import { GUI3DManager } from '@babylonjs/gui' ;
44import HavokPhysics from '@babylonjs/havok' ;
5- import { SceneContext , EngineContextType } from './hooks ' ;
5+ import { SceneContext , EngineStore , Store , createBabylonStore } from './store ' ;
66import { RootContainer } from '@types' ;
77import Reactylon from '../reconciler' ;
88import { type ContextBridge , useContextBridge } from 'its-fine' ;
99import { type CameraProps } from '@props' ;
10+ import { type StoreApi } from 'zustand' ;
1011
1112type SceneProps = React . PropsWithChildren < {
1213 /**
@@ -25,17 +26,19 @@ type SceneProps = React.PropsWithChildren<{
2526 * @internal
2627 * This prop is only for internal use and should not be passed to this component.
2728 */
28- _context ?: EngineContextType ;
29+ _context ?: EngineStore ;
2930} > ;
3031
3132//FIXME: replace global var with a singleton Manager
3233export let activeScene : BabylonScene | null = null ;
3334
3435export const Scene : React . FC < SceneProps > = ( { children, sceneOptions, onSceneReady, isGui3DManager, xrDefaultExperienceOptions, physicsOptions, _context, ...rest } ) => {
35- const { engine, isMultipleCanvas, isMultipleScene } = _context as EngineContextType ;
36+ const { engine, isMultipleCanvas, isMultipleScene } = _context as EngineStore ;
3637 const rootContainer = useRef < Nullable < RootContainer > > ( null ) ;
3738 const isFirstRender = useRef ( false ) ;
3839
40+ const store = useRef < StoreApi < Store > > ( ) ;
41+
3942 // Returns a bridged context provider that forwards context
4043 const Bridge : ContextBridge = useContextBridge ( ) ;
4144
@@ -125,13 +128,17 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea
125128 /* --------------------------------------------------------------------------------------- */
126129 /* RECONCILER
127130 ------------------------------------------------------------------------------------------ */
128- rootContainer . current = {
131+ store . current = createBabylonStore ( {
129132 engine,
130133 scene,
131134 canvas,
132135 isMultipleCanvas,
133136 isMultipleScene,
134137 xrExperience,
138+ } ) ;
139+
140+ rootContainer . current = {
141+ ...store . current . getState ( ) ,
135142 metadata : {
136143 children : [ ] ,
137144 } ,
@@ -140,7 +147,7 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea
140147 // Renders children with bridged context into a secondary renderer
141148 Reactylon . render (
142149 < Bridge >
143- < SceneContext . Provider value = { { engine , isMultipleCanvas , isMultipleScene , scene , xrExperience , canvas } } > { children } </ SceneContext . Provider >
150+ < SceneContext . Provider value = { store . current } > { children } </ SceneContext . Provider >
144151 </ Bridge > ,
145152 rootContainer . current ! ,
146153 ) ;
@@ -156,16 +163,17 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea
156163
157164 useEffect ( ( ) => {
158165 if ( ! isFirstRender . current ) {
159- const { scene, xrExperience, canvas } = rootContainer . current ! ;
160- // Renders children with bridged context into a secondary renderer
161- Reactylon . render (
162- < Bridge >
163- < SceneContext . Provider value = { { engine, isMultipleCanvas, isMultipleScene, scene, xrExperience, canvas } } > { children } </ SceneContext . Provider >
164- </ Bridge > ,
165- rootContainer . current ! ,
166- ) ;
167- } else {
168- isFirstRender . current = false ;
166+ if ( store . current ) {
167+ // Renders children with bridged context into a secondary renderer
168+ Reactylon . render (
169+ < Bridge >
170+ < SceneContext . Provider value = { store . current } > { children } </ SceneContext . Provider >
171+ </ Bridge > ,
172+ rootContainer . current ! ,
173+ ) ;
174+ } else {
175+ isFirstRender . current = false ;
176+ }
169177 }
170178 } ) ;
171179
0 commit comments