1- import { useXRPlanes , XRSpace , XRPlaneModel } from " @react-three/xr" ;
2- import { createContext , useContext , useState } from " react" ;
3- import { DoubleSide , FrontSide , Mesh } from " three" ;
1+ import { useXRPlanes , XRPlaneModel , XRSpace } from ' @react-three/xr' ;
2+ import { createContext , useContext , useState } from ' react' ;
3+ import { DoubleSide , Mesh } from ' three' ;
44
55type PlaneMeshes = {
6- [ label : string ] : Mesh [ ]
6+ [ label : string ] : Mesh [ ] ;
77} ;
88
99interface EnvironmentContextType {
10- planeMeshes : PlaneMeshes ;
11- sunlightIntensity : number ;
12- setSunlightIntensity : ( intensity : number ) => void ;
10+ planeMeshes : PlaneMeshes ;
11+ sunlightIntensity : number ;
12+ setSunlightIntensity : ( intensity : number ) => void ;
1313}
1414
1515export const EnvironmentContext = createContext < EnvironmentContextType > ( {
16- planeMeshes : { } ,
17- sunlightIntensity : 1 ,
18- setSunlightIntensity : ( intensity : number ) => { } ,
16+ planeMeshes : { } ,
17+ sunlightIntensity : 1 ,
18+ setSunlightIntensity : ( ) => { } ,
1919} ) ;
2020
2121export const useEnvironmentContext = ( ) => {
22- return useContext ( EnvironmentContext ) ;
23- }
22+ return useContext ( EnvironmentContext ) ;
23+ } ;
2424
2525export const Environment = ( { children } : { children : React . ReactNode } ) => {
26- const planes = useXRPlanes ( ) ;
27- const planeMeshes : PlaneMeshes = { } ;
28- const [ sunlightIntensity , setSunlightIntensity ] = useState < number > ( 1 ) ;
29-
30- return (
31- < EnvironmentContext . Provider value = { { planeMeshes, sunlightIntensity, setSunlightIntensity } } >
32- < group >
33- { planes . map ( ( plane , index ) => {
34- const label = plane . semanticLabel ?? 'other' ;
35- if ( ! planeMeshes [ label ] ) {
36- planeMeshes [ label ] = [ ] ;
37- }
38-
39- return (
40- < XRSpace key = { label + index } space = { plane . planeSpace } >
41- < XRPlaneModel
42- renderOrder = { - 1 }
43- plane = { plane }
44- receiveShadow = { true }
45- >
46- < shadowMaterial
47- side = { DoubleSide }
48- shadowSide = { DoubleSide }
49- transparent = { true }
50- opacity = { 0.6 * sunlightIntensity }
51- />
52- </ XRPlaneModel >
53- < XRPlaneModel
54- ref = { ( mesh : Mesh | null ) => {
55- if ( mesh ) {
56- planeMeshes [ label ] . push ( mesh ) ;
57- }
58- } }
59- renderOrder = { - 1 }
60- plane = { plane }
61- position = { [ 0 , 0.01 , 0 ] }
62- >
63- < meshBasicMaterial
64- colorWrite = { false }
65- side = { DoubleSide }
66- />
67- </ XRPlaneModel >
68- </ XRSpace >
69- ) ;
70- } ) }
71- { children }
72- </ group >
73- </ EnvironmentContext . Provider >
74- ) ;
75- } ;
26+ const planes = useXRPlanes ( ) ;
27+ const planeMeshes : PlaneMeshes = { } ;
28+ const [ sunlightIntensity , setSunlightIntensity ] = useState < number > ( 1 ) ;
29+
30+ return (
31+ < EnvironmentContext . Provider value = { { planeMeshes, sunlightIntensity, setSunlightIntensity } } >
32+ < group >
33+ { planes . map ( ( plane , index ) => {
34+ const label = plane . semanticLabel ?? 'other' ;
35+ if ( ! planeMeshes [ label ] ) {
36+ planeMeshes [ label ] = [ ] ;
37+ }
38+
39+ return (
40+ < XRSpace key = { label + index } space = { plane . planeSpace } >
41+ < XRPlaneModel renderOrder = { - 1 } plane = { plane } receiveShadow = { true } >
42+ < shadowMaterial side = { DoubleSide } shadowSide = { DoubleSide } transparent = { true } opacity = { 0.6 * sunlightIntensity } />
43+ </ XRPlaneModel >
44+ < XRPlaneModel
45+ ref = { ( mesh : Mesh | null ) => {
46+ if ( mesh ) {
47+ planeMeshes [ label ] . push ( mesh ) ;
48+ }
49+ } }
50+ renderOrder = { - 1 }
51+ plane = { plane }
52+ position = { [ 0 , 0.01 , 0 ] }
53+ >
54+ < meshBasicMaterial colorWrite = { false } side = { DoubleSide } />
55+ </ XRPlaneModel >
56+ </ XRSpace >
57+ ) ;
58+ } ) }
59+ { children }
60+ </ group >
61+ </ EnvironmentContext . Provider >
62+ ) ;
63+ } ;
0 commit comments