1
1
import * as THREE from "three" ;
2
- import { useEffect , useState } from "react" ;
2
+ import { useCallback , useEffect , useState } from "react" ;
3
3
import { useFrame , useThree } from "@react-three/fiber" ;
4
4
import { useController , useXR } from "@react-three/xr" ;
5
5
import nipplejs from "nipplejs" ;
6
-
7
- import FaceText from "./components/FaceText" ;
6
+ import { folder , useControls } from "leva" ;
8
7
9
8
type Vec2 = { x : number ; y : number } ;
10
9
11
- type GamepadsProps = {
12
- nipples : boolean ;
13
- } ;
14
- function Gamepads ( { nipples = true } : GamepadsProps ) {
15
- const [ leftpad ] = useState < Vec2 > ( { x : 0 , y : 0 } ) ;
16
- const [ rightpad ] = useState < Vec2 > ( { x : 0 , y : 0 } ) ;
10
+ function Gamepads ( ) {
11
+ const [ gui , setGui ] = useControls ( ( ) => ( {
12
+ Gamepads : folder (
13
+ {
14
+ leftpad : { x : 0 , y : 0 } ,
15
+ rightpad : { x : 0 , y : 0 } ,
16
+ nipples : false ,
17
+ }
18
+ // { collapsed: true }
19
+ ) ,
20
+ } ) ) ;
21
+
22
+ const leftpad = gui . leftpad ;
23
+ const setLeftpad = useCallback (
24
+ ( v : typeof leftpad ) => setGui ( { leftpad : v } ) ,
25
+ [ setGui ]
26
+ ) ;
27
+
28
+ const rightpad = gui . rightpad ;
29
+ const setRightpad = useCallback (
30
+ ( v : typeof rightpad ) => setGui ( { rightpad : v } ) ,
31
+ [ setGui ]
32
+ ) ;
17
33
18
34
const player = useXR ( ( state ) => state . player ) ;
19
35
20
- const [ text1 , setText1 ] = useState ( "left" ) ;
21
- const [ text2 , setText2 ] = useState ( "right" ) ;
22
-
23
36
//
24
37
// Update `player` position and rotation
25
38
//
@@ -30,8 +43,6 @@ function Gamepads({ nipples = true }: GamepadsProps) {
30
43
} ;
31
44
32
45
useFrame ( ( ) => {
33
- setText1 ( `leftpad: [ ${ leftpad . x . toFixed ( 3 ) } , ${ leftpad . y . toFixed ( 3 ) } ]` ) ;
34
-
35
46
player . position . add (
36
47
new THREE . Vector3 (
37
48
leftpad . x * sensitivity . left . x ,
@@ -40,21 +51,17 @@ function Gamepads({ nipples = true }: GamepadsProps) {
40
51
) . applyEuler ( player . rotation )
41
52
) ;
42
53
43
- setText2 (
44
- `rightpad: [ ${ rightpad . x . toFixed ( 3 ) } , ${ rightpad . y . toFixed ( 3 ) } ]`
45
- ) ;
46
54
player . rotation . y -= rightpad . x * sensitivity . right . x ;
47
55
player . position . y -= rightpad . y * sensitivity . right . y ;
48
56
} ) ;
49
57
50
58
return (
51
59
< >
52
- < FaceText position = { [ 0 , 6 , 0 ] } > { text1 } </ FaceText >
53
- < FaceText position = { [ 0 , 5 , 0 ] } > { text2 } </ FaceText >
54
-
55
- < NormalGamepad leftpad = { leftpad } rightpad = { rightpad } />
56
- < XRGamepads leftpad = { leftpad } rightpad = { rightpad } />
57
- { nipples && < NipplesGamepads leftpad = { leftpad } rightpad = { rightpad } /> }
60
+ < NormalGamepad setLeftpad = { setLeftpad } setRightpad = { setRightpad } />
61
+ < XRGamepads setLeftpad = { setLeftpad } setRightpad = { setRightpad } />
62
+ { gui . nipples && (
63
+ < NipplesGamepads setLeftpad = { setLeftpad } setRightpad = { setRightpad } />
64
+ ) }
58
65
</ >
59
66
) ;
60
67
}
@@ -68,11 +75,11 @@ export default Gamepads;
68
75
//
69
76
70
77
function NormalGamepad ( {
71
- leftpad ,
72
- rightpad ,
78
+ setLeftpad ,
79
+ setRightpad ,
73
80
} : {
74
- leftpad : Vec2 ;
75
- rightpad : Vec2 ;
81
+ setLeftpad : ( v : Vec2 ) => void ;
82
+ setRightpad : ( v : Vec2 ) => void ;
76
83
} ) {
77
84
const [ gamepadIndex , setGamepadIndex ] = useState < number | null > ( null ) ;
78
85
@@ -106,11 +113,8 @@ function NormalGamepad({
106
113
const gp = navigator . getGamepads ( ) [ gamepadIndex ] ;
107
114
if ( ! gp ) return ;
108
115
109
- leftpad . x = threshold ( gp . axes [ 0 ] ) ;
110
- leftpad . y = threshold ( gp . axes [ 1 ] ) ;
111
-
112
- rightpad . x = threshold ( gp . axes [ 2 ] ) ;
113
- rightpad . y = threshold ( gp . axes [ 3 ] ) ;
116
+ setLeftpad ( { x : threshold ( gp . axes [ 0 ] ) , y : threshold ( gp . axes [ 1 ] ) } ) ;
117
+ setRightpad ( { x : threshold ( gp . axes [ 2 ] ) , y : threshold ( gp . axes [ 3 ] ) } ) ;
114
118
} ) ;
115
119
116
120
return < > </ > ;
@@ -120,7 +124,13 @@ function NormalGamepad({
120
124
// XR Gamepads (see: https://github.com/pmndrs/react-xr/issues/218)
121
125
//
122
126
123
- function XRGamepads ( { leftpad, rightpad } : { leftpad : Vec2 ; rightpad : Vec2 } ) {
127
+ function XRGamepads ( {
128
+ setLeftpad,
129
+ setRightpad,
130
+ } : {
131
+ setLeftpad : ( v : Vec2 ) => void ;
132
+ setRightpad : ( v : Vec2 ) => void ;
133
+ } ) {
124
134
const leftController = useController ( "left" ) ;
125
135
const rightController = useController ( "right" ) ;
126
136
@@ -130,15 +140,19 @@ function XRGamepads({ leftpad, rightpad }: { leftpad: Vec2; rightpad: Vec2 }) {
130
140
if ( leftController ) {
131
141
const XRLeftGamepad = leftController . inputSource ?. gamepad ;
132
142
133
- leftpad . x = XRLeftGamepad ?. axes [ 2 ] || 0 ;
134
- leftpad . y = XRLeftGamepad ?. axes [ 3 ] || 0 ;
143
+ setLeftpad ( {
144
+ x : XRLeftGamepad ?. axes [ 2 ] || 0 ,
145
+ y : XRLeftGamepad ?. axes [ 3 ] || 0 ,
146
+ } ) ;
135
147
}
136
148
137
149
if ( rightController ) {
138
150
const XRRightGamepad = rightController . inputSource ?. gamepad ;
139
151
140
- rightpad . x = XRRightGamepad ?. axes [ 2 ] || 0 ;
141
- rightpad . y = XRRightGamepad ?. axes [ 3 ] || 0 ;
152
+ setRightpad ( {
153
+ x : XRRightGamepad ?. axes [ 2 ] || 0 ,
154
+ y : XRRightGamepad ?. axes [ 3 ] || 0 ,
155
+ } ) ;
142
156
}
143
157
} ) ;
144
158
@@ -152,11 +166,11 @@ function XRGamepads({ leftpad, rightpad }: { leftpad: Vec2; rightpad: Vec2 }) {
152
166
//
153
167
154
168
function NipplesGamepads ( {
155
- leftpad ,
156
- rightpad ,
169
+ setLeftpad ,
170
+ setRightpad ,
157
171
} : {
158
- leftpad : Vec2 ;
159
- rightpad : Vec2 ;
172
+ setLeftpad : ( v : Vec2 ) => void ;
173
+ setRightpad : ( v : Vec2 ) => void ;
160
174
} ) {
161
175
const { size, gl } = useThree ( ) ;
162
176
@@ -174,12 +188,10 @@ function NipplesGamepads({
174
188
// Only one joystick
175
189
if ( position . x < size . width / 2 ) {
176
190
// left part of the screen
177
- leftpad . x = vector . x ;
178
- leftpad . y = - vector . y ;
191
+ setLeftpad ( { x : vector . x , y : - vector . y } ) ;
179
192
} else {
180
193
// right part of the screen
181
- rightpad . x = vector . x ;
182
- rightpad . y = - vector . y ;
194
+ setRightpad ( { x : vector . x , y : - vector . y } ) ;
183
195
}
184
196
} else {
185
197
// Two joysticks
@@ -188,20 +200,16 @@ function NipplesGamepads({
188
200
189
201
if ( position . x < otherJoystick . position . x ) {
190
202
// I'm on the left of the other joystick
191
- leftpad . x = vector . x ;
192
- leftpad . y = - vector . y ;
203
+ setLeftpad ( { x : vector . x , y : - vector . y } ) ;
193
204
} else {
194
205
// I'm on the right of the other joystick
195
- rightpad . x = vector . x ;
196
- rightpad . y = - vector . y ;
206
+ setRightpad ( { x : vector . x , y : - vector . y } ) ;
197
207
}
198
208
}
199
209
} ) ;
200
210
manager . on ( "end" , ( evt , nipple ) => {
201
- leftpad . x = 0 ;
202
- leftpad . y = 0 ;
203
- rightpad . x = 0 ;
204
- rightpad . y = 0 ;
211
+ setLeftpad ( { x : 0 , y : 0 } ) ;
212
+ setRightpad ( { x : 0 , y : 0 } ) ;
205
213
} ) ;
206
214
207
215
//
@@ -211,7 +219,7 @@ function NipplesGamepads({
211
219
return ( ) => {
212
220
manager . destroy ( ) ;
213
221
} ;
214
- } , [ gl , size , leftpad , rightpad ] ) ;
222
+ } , [ gl , size , setLeftpad , setRightpad ] ) ;
215
223
216
224
return < > </ > ;
217
225
}
0 commit comments