forked from mrdoob/three.js
/
Viewport.VR.js
194 lines (123 loc) · 4.9 KB
/
Viewport.VR.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import * as THREE from 'three';
import { HTMLMesh } from 'three/addons/interactive/HTMLMesh.js';
import { InteractiveGroup } from 'three/addons/interactive/InteractiveGroup.js';
import { XRControllerModelFactory } from 'three/addons/webxr/XRControllerModelFactory.js';
class VR {
constructor( editor ) {
const signals = editor.signals;
let group = null;
let camera = null;
let renderer = null;
let video = null;
const intersectables = [];
this.currentSession = null;
const onSessionStarted = async ( session ) => {
const sidebar = document.getElementById( 'sidebar' );
sidebar.style.width = '300px';
sidebar.style.height = '700px';
//
if ( group === null ) {
group = new InteractiveGroup( renderer );
editor.sceneHelpers.add( group );
const mesh = new HTMLMesh( sidebar );
mesh.position.set( 1, 1.5, - 0.5 );
mesh.rotation.y = - 0.5;
mesh.scale.setScalar( 2 );
group.add( mesh );
intersectables.push( mesh );
video = document.getElementById( 'video' );
video.style.display = 'none';
document.body.appendChild( video );
const texture = new THREE.VideoTexture( video );
texture.colorSpace = THREE.SRGBColorSpace;
const planeGeometry = new THREE.PlaneGeometry( 9, 16 );
planeGeometry.scale( 0.075, 0.075, 0.075 );
const material = new THREE.MeshBasicMaterial( { map: texture } );
const planeMesh = new THREE.Mesh( planeGeometry, material );
planeMesh.position.set( 0, 1.5, - 0.75 );
group.add( planeMesh );
intersectables.push( planeMesh );
// Grab
// Store the initial offset between the planeMesh and the controller
let offset = new THREE.Vector3();
// Add 'mousedown' event listener to the planeMesh
planeMesh.addEventListener('mousedown', function (event) {
offset.copy(planeMesh.position).sub(controller1.position); // Update the offset
planeMesh.parent = controller1; // Set the parent of planeMesh to controller1
});
// Add 'mousemove' event listener to the planeMesh
planeMesh.addEventListener('mousemove', function (event) {
if (planeMesh.parent === controller1) { // Check if the parent is set to controller1
const { x, y, z } = controller1.position.clone().add(offset);
planeMesh.position.set(x, y, z); // Update the position of the planeMesh
}
});
// Add 'mouseup' event listener to the planeMesh
planeMesh.addEventListener('mouseup', function () {
planeMesh.parent = group; // Set the parent back to group
});
// controllers
const geometry = new THREE.BufferGeometry();
geometry.setFromPoints( [ new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 0, - 5 ) ] );
const controller1 = renderer.xr.getController( 0 );
controller1.add( new THREE.Line( geometry ) );
group.add( controller1 );
const controller2 = renderer.xr.getController( 1 );
controller2.add( new THREE.Line( geometry ) );
group.add( controller2 );
//
const controllerModelFactory = new XRControllerModelFactory();
const controllerGrip1 = renderer.xr.getControllerGrip( 0 );
controllerGrip1.add( controllerModelFactory.createControllerModel( controllerGrip1 ) );
group.add( controllerGrip1 );
const controllerGrip2 = renderer.xr.getControllerGrip( 1 );
controllerGrip2.add( controllerModelFactory.createControllerModel( controllerGrip2 ) );
group.add( controllerGrip2 );
}
camera = editor.camera.clone();
group.visible = true;
this.currentSession = session;
this.currentSession.addEventListener( 'end', onSessionEnded );
await renderer.xr.setSession( this.currentSession );
if ( navigator.mediaDevices && navigator.mediaDevices.getUserMedia ) {
const constraints = { video: { width: 1280, height: 720, facingMode: 'user' } };
navigator.mediaDevices.getUserMedia( constraints )
.then( stream => {
video.srcObject = stream;
video.play();
} )
.catch( error => {
console.error( 'Unable to access the camera/webcam.', error );
} );
} else {
console.error( 'MediaDevices interface not available.' );
}
};
const onSessionEnded = async () => {
const sidebar = document.getElementById( 'sidebar' );
sidebar.style.width = '';
sidebar.style.height = '';
//
editor.camera.copy( camera );
group.visible = false;
this.currentSession.removeEventListener( 'end', onSessionEnded );
this.currentSession = null;
await renderer.xr.setSession( null );
signals.exitedVR.dispatch();
};
// signals
signals.toggleVR.add( () => {
if ( this.currentSession === null ) {
const sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor' ] };
navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted );
} else {
this.currentSession.end();
}
} );
signals.rendererCreated.add( ( value ) => {
renderer = value;
renderer.xr.enabled = true;
} );
}
}
export { VR };