@@ -15,6 +15,8 @@ class VR {
1515 let camera = null ;
1616 let renderer = null ;
1717
18+ const intersectables = [ ] ;
19+
1820 this . currentSession = null ;
1921
2022 const onSessionStarted = async ( session ) => {
@@ -29,6 +31,24 @@ class VR {
2931 mesh . rotation . y = - 0.5 ;
3032 group . add ( mesh ) ;
3133
34+ intersectables . push ( mesh ) ;
35+
36+ // controllers
37+
38+ const controller1 = renderer . xr . getController ( 0 ) ;
39+ controller1 . addEventListener ( 'select' , onSelect ) ;
40+ group . add ( controller1 ) ;
41+
42+ const controller2 = renderer . xr . getController ( 1 ) ;
43+ controller2 . addEventListener ( 'selectstart' , onSelect ) ;
44+ group . add ( controller2 ) ;
45+
46+ const geometry = new THREE . BufferGeometry ( ) ;
47+ geometry . setFromPoints ( [ new THREE . Vector3 ( 0 , 0 , 0 ) , new THREE . Vector3 ( 0 , 0 , - 5 ) ] ) ;
48+
49+ controller1 . add ( new THREE . Line ( geometry ) ) ;
50+ controller2 . add ( new THREE . Line ( geometry ) ) ;
51+
3252 //
3353
3454 const controllerModelFactory = new XRControllerModelFactory ( ) ;
@@ -69,6 +89,43 @@ class VR {
6989
7090 } ;
7191
92+ //
93+
94+ function onSelect ( event ) {
95+
96+ const controller = event . target ;
97+
98+ const intersections = getIntersections ( controller ) ;
99+
100+ if ( intersections . length > 0 ) {
101+
102+ const intersection = intersections [ 0 ] ;
103+
104+ const object = intersection . object ;
105+ const uv = intersection . uv ;
106+
107+ object . material . map . click ( uv . x , 1 - uv . y ) ;
108+
109+ }
110+
111+ }
112+
113+ const raycaster = new THREE . Raycaster ( ) ;
114+ const tempMatrix = new THREE . Matrix4 ( ) ;
115+
116+ function getIntersections ( controller ) {
117+
118+ tempMatrix . identity ( ) . extractRotation ( controller . matrixWorld ) ;
119+
120+ raycaster . ray . origin . setFromMatrixPosition ( controller . matrixWorld ) ;
121+ raycaster . ray . direction . set ( 0 , 0 , - 1 ) . applyMatrix4 ( tempMatrix ) ;
122+
123+ return raycaster . intersectObjects ( intersectables ) ;
124+
125+ }
126+
127+ // signals
128+
72129 signals . toggleVR . add ( ( ) => {
73130
74131 if ( this . currentSession === null ) {
0 commit comments