@@ -11,8 +11,12 @@ class VR {
1111 const signals = editor . signals ;
1212
1313 let group = null ;
14+
15+ let camera = null ;
1416 let renderer = null ;
1517
18+ const intersectables = [ ] ;
19+
1620 this . currentSession = null ;
1721
1822 const onSessionStarted = async ( session ) => {
@@ -27,6 +31,24 @@ class VR {
2731 mesh . rotation . y = - 0.5 ;
2832 group . add ( mesh ) ;
2933
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+
3052 //
3153
3254 const controllerModelFactory = new XRControllerModelFactory ( ) ;
@@ -41,16 +63,20 @@ class VR {
4163
4264 }
4365
66+ camera = editor . camera . clone ( ) ;
67+
4468 group . visible = true ;
4569
4670 this . currentSession = session ;
4771 this . currentSession . addEventListener ( 'end' , onSessionEnded ) ;
4872
4973 await renderer . xr . setSession ( this . currentSession ) ;
5074
51- }
75+ } ;
76+
77+ const onSessionEnded = async ( ) => {
5278
53- const onSessionEnded = async ( ) => {
79+ editor . camera . copy ( camera ) ;
5480
5581 group . visible = false ;
5682
@@ -63,6 +89,43 @@ class VR {
6389
6490 } ;
6591
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+
66129 signals . toggleVR . add ( ( ) => {
67130
68131 if ( this . currentSession === null ) {
0 commit comments