1
1
import { Canvas , useFrame , useThree } from '@react-three/fiber'
2
2
import { createXRStore , XR } from '@react-three/xr'
3
- import { useEffect , useMemo , useRef } from 'react'
4
- import { Group , Mesh } from 'three'
5
- import { useHandle } from '@react-three/handle'
3
+ import { useEffect , useRef } from 'react'
4
+ import { BufferGeometry , Mesh , Vector3 } from 'three'
5
+ import { Handle , HandleTarget , useHandle } from '@react-three/handle'
6
6
import { forwardHtmlEvents } from '@pmndrs/pointer-events'
7
+ import { Environment } from '@react-three/drei'
7
8
8
9
const store = createXRStore ( )
9
10
@@ -19,15 +20,15 @@ export function App() {
19
20
>
20
21
< SwitchToXRPointerEvents />
21
22
< XR store = { store } >
22
- < ambientLight />
23
+ < Environment preset = "city" />
23
24
< directionalLight position = { [ 1 , 1 , 1 ] } />
24
25
< Cube />
25
26
< Cube />
26
27
{ /*<Smoke count={100} maxSize={0.3} minSize={0.1} spawnRate={10} speed={0.1} />
27
28
<TeleportTarget onTeleport={setPosition}>
28
29
<mesh scale={[10, 1, 10]} position={[0, -0.5, 0]}>
29
30
<boxGeometry />
30
- <meshBasicMaterial color="green" />
31
+ <meshStandardMaterial color="green" />
31
32
</mesh>
32
33
</TeleportTarget>*/ }
33
34
</ XR >
@@ -37,123 +38,54 @@ export function App() {
37
38
}
38
39
39
40
function Cube ( ) {
40
- const ref = useRef < Mesh > ( null )
41
- const xHandleRef = useRef < Mesh > ( null )
42
- const yHandleRef = useRef < Mesh > ( null )
43
- const zHandleRef = useRef < Mesh > ( null )
44
- useHandle ( ref , {
45
- apply : ( state ) => {
46
- const mesh = ref . current
47
- if ( mesh == null ) {
48
- return
49
- }
50
- mesh . scale . x = state . current . scale . x
51
- console . log ( state . current . scale . toArray ( ) )
52
- if ( xHandleRef . current != null ) {
53
- xHandleRef . current . scale . x = 0.1 / mesh . scale . x
54
- }
55
- if ( yHandleRef . current != null ) {
56
- yHandleRef . current . scale . x = 0.1 / mesh . scale . x
57
- }
58
- if ( zHandleRef . current != null ) {
59
- zHandleRef . current . scale . x = 0.1 / mesh . scale . x
60
- }
61
- } ,
62
- handle : xHandleRef ,
63
- translate : 'as-scale' ,
64
- scale : 'x' ,
65
- } )
66
- useHandle ( ref , {
67
- apply : ( state ) => {
68
- const mesh = ref . current
69
- if ( mesh == null ) {
70
- return
71
- }
72
- mesh . scale . y = state . current . scale . y
73
- if ( xHandleRef . current != null ) {
74
- xHandleRef . current . scale . y = 0.1 / mesh . scale . y
75
- }
76
- if ( yHandleRef . current != null ) {
77
- yHandleRef . current . scale . y = 0.1 / mesh . scale . y
78
- }
79
- if ( zHandleRef . current != null ) {
80
- zHandleRef . current . scale . y = 0.1 / mesh . scale . y
81
- }
82
- } ,
83
- handle : yHandleRef ,
84
- translate : 'as-scale' ,
85
- scale : 'y' ,
86
- } )
87
- useHandle ( ref , {
88
- apply : ( state ) => {
89
- const mesh = ref . current
90
- if ( mesh == null ) {
91
- return
92
- }
93
- mesh . rotation . x = state . current . rotation . x
94
- } ,
95
- handle : zHandleRef ,
96
- translate : 'as-rotate' ,
97
- rotate : 'x' ,
98
- } )
99
- useHandle ( ref , {
100
- apply : ( state ) => {
101
- ref . current ?. position . copy ( state . current . position )
102
- ref . current ?. quaternion . copy ( state . current . quaternion )
103
- if ( state . current . pointerAmount > 1 ) {
104
- ref . current ?. scale . copy ( state . current . scale )
105
- if ( ref . current != null ) {
106
- xHandleRef . current ?. scale . setScalar ( 0.1 ) . divide ( ref . current . scale )
107
- yHandleRef . current ?. scale . setScalar ( 0.1 ) . divide ( ref . current . scale )
108
- zHandleRef . current ?. scale . setScalar ( 0.1 ) . divide ( ref . current . scale )
109
- }
110
- }
111
- } ,
112
- translate : {
113
- z : false ,
114
- } ,
115
- } )
116
41
return (
117
- < group rotation-y = { Math . PI / 4 } position-y = { - 2 } >
118
- < mesh rotation-order = "XZY" scale = { 1 } pointerEventsType = { { deny : 'touch' } } ref = { ref } >
119
- < boxGeometry />
120
- < meshPhongMaterial color = "red" />
121
- < mesh
122
- pointerEventsOrder = { 1 }
123
- renderOrder = { 1 }
124
- scale = { 0.1 }
125
- position-x = { 0.7 }
126
- pointerEventsType = { { deny : 'touch' } }
127
- ref = { xHandleRef }
128
- >
129
- < boxGeometry />
130
- < meshBasicMaterial depthTest = { false } color = "blue" />
131
- </ mesh >
42
+ < group position-y = { - 2 } >
43
+ < HandleTarget >
44
+ < Handle >
45
+ < mesh rotation-order = "XZY" scale = { 1 } pointerEventsType = { { deny : 'touch' } } >
46
+ < boxGeometry />
47
+ < meshStandardMaterial color = "red" />
48
+ < Handle scale = "x" translate = "as-scale" >
49
+ < mesh
50
+ pointerEventsOrder = { 1 }
51
+ renderOrder = { 1 }
52
+ scale = { 0.1 }
53
+ position-x = { 0.7 }
54
+ pointerEventsType = { { deny : 'touch' } }
55
+ >
56
+ < boxGeometry />
57
+ < meshStandardMaterial depthTest = { false } color = "blue" />
58
+ </ mesh >
59
+ </ Handle >
132
60
133
- < mesh
134
- pointerEventsOrder = { 1 }
135
- renderOrder = { 1 }
136
- scale = { 0.1 }
137
- position-y = { 0.7 }
138
- pointerEventsType = { { deny : 'touch' } }
139
- ref = { yHandleRef }
140
- >
141
- < boxGeometry />
142
- < meshBasicMaterial depthTest = { false } color = "yellow" />
143
- </ mesh >
61
+ < Handle translate = "as-rotate" rotate = "x" >
62
+ < mesh
63
+ pointerEventsOrder = { 1 }
64
+ renderOrder = { 1 }
65
+ scale = { 0.1 }
66
+ position-y = { 0.7 }
67
+ pointerEventsType = { { deny : 'touch' } }
68
+ >
69
+ < boxGeometry />
70
+ < meshStandardMaterial depthTest = { false } color = "yellow" />
71
+ </ mesh >
72
+ </ Handle >
144
73
145
- < mesh
146
- pointerEventsOrder = { 1 }
147
- renderOrder = { 1 }
148
- scale = { 0.1 }
149
- position-z = { 0.7 }
150
- pointerEventsType = { { deny : 'touch' } }
151
- ref = { zHandleRef }
152
- >
153
- < boxGeometry />
154
- < meshBasicMaterial depthTest = { false } color = "green" />
155
- </ mesh >
156
- </ mesh >
74
+ < Handle translate = "as-rotate-and-scale" >
75
+ < mesh
76
+ pointerEventsOrder = { 1 }
77
+ renderOrder = { 1 }
78
+ scale = { 0.1 }
79
+ position-z = { 0.7 }
80
+ pointerEventsType = { { deny : 'touch' } }
81
+ >
82
+ < boxGeometry />
83
+ < meshStandardMaterial depthTest = { false } color = "green" />
84
+ </ mesh >
85
+ </ Handle >
86
+ </ mesh >
87
+ </ Handle >
88
+ </ HandleTarget >
157
89
</ group >
158
90
)
159
91
}
0 commit comments