-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
193 lines (137 loc) · 5.43 KB
/
index.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
class App {
constructor() {
this.onXRFrame = this.onXRFrame.bind(this);
this.onEnterAR = this.onEnterAR.bind(this);
this.init();
}
async init() {
if (navigator.xr) {
navigator.xr.isSessionSupported('immersive-ar').then(
(isSupported) => {
if(!isSupported) alert("immersive ar not supported");
}
);
} else {
alert("WebXR not enabled");
return;
}
window.addEventListener('click', this.onEnterAR);
}
async onEnterAR() {
const outputCanvas = document.createElement('canvas');
navigator.xr
.requestSession('immersive-ar')
.then(xrSession => {
this.session = xrSession;
console.log('requestSession immersive-ar ok');
xrSession.addEventListener(
'end',
this.onXRSessionEnded.bind(this)
);
document.body.appendChild(outputCanvas);
this.onSessionStarted();
})
.catch(error => {
console.warn('requestSession immersive-ar error: ', error);
this.onNoXRDevice();
});
}
onXRSessionEnded() {
console.log('onXRSessionEnded');
if (this.renderer) {
this.renderer.vr.setSession(null);
}
}
async onSessionStarted() {
this.renderer = new THREE.WebGLRenderer({
alpha: false,
preserveDrawingBuffer: false
});
this.gl = this.renderer.getContext();
// this.renderer.vr === new WebXRManager(...) -> https://github.com/mrdoob/three.js/blob/dev/src/renderers/webvr/WebXRManager.js
this.renderer.vr.enabled = true;
this.XRReferenceSpaceType = 'local';
this.renderer.vr.setReferenceSpaceType(this.XRReferenceSpaceType);
this.renderer.vr.setSession(this.session);
this.session.baseLayer = new XRWebGLLayer(this.session, this.gl);
this.scene = new THREE.Scene();
var material = new THREE.MeshPhysicalMaterial();
var geometry = new THREE.SphereBufferGeometry( 5, 32, 32 );
for(var i=0; i < 50; i++){
var mesh = new THREE.Mesh(geometry, material.clone());
mesh.position.set(2 + Math.random() * 10, 2 + Math.random() * 10, 2 + Math.random() * 10);
mesh.material.color = new THREE.Color("hsl("+Math.random()+", 66%, 66%)");
this.scene.add(mesh);
}
var light = new THREE.DirectionalLight( 0xffffff, 0.5 );
light.position.set(100, 100, 100);
this.scene.add(light);
this.camera = new THREE.PerspectiveCamera();
this.camera.matrixAutoUpdate = false;
var cameraL = new PerspectiveCamera();
cameraL.matrixAutoUpdate = false;
var hipd = 0.02;
cameraL.position.x = -hipd;
var cameraR = new PerspectiveCamera();
cameraR.matrixAutoUpdate = false;
cameraR.position.x = hipd;
this.cameraL = cameraL;
this.cameraR = cameraR;
this.camera.add(cameraL);
this.camera.add(cameraR);
this.scene.add(camera);
this.frameOfRef = await this.session.requestReferenceSpace('local');
this.tick();
}
tick() {
this.rafId = this.session.requestAnimationFrame(this.onXRFrame);
}
onXRFrame(time, frame) {
const { session } = frame;
const pose =
'getDevicePose' in frame
? frame.getDevicePose(this.frameOfRef)
: frame.getViewerPose(this.frameOfRef);
// Queue up the next frame
this.tick();
if (pose == null) {
return;
}
for (const view of frame.getViewerPose(this.frameOfRef).views) {
const viewport = session.renderState.baseLayer.getViewport(view);
this.camera.projectionMatrix.fromArray(view.projectionMatrix);
const viewMatrix = new THREE.Matrix4().fromArray(
view.transform.inverse.matrix
);
this.camera.matrix.getInverse(viewMatrix);
this.camera.updateMatrixWorld(true);
this.cameraL.updateMatrixWorld(true);
this.cameraR.updateMatrixWorld(true);
var fov = 2 * atan(1/view.projectionMatrix.elements[5]) * 180 / PI;
var near = view.projectionMatrix.elements[14] / (view.projectionMatrix.elements[10] - 1.0);
var far = view.projectionMatrix.elements[14] / (view.projectionMatrix.elements[10] + 1.0);
var aspect = 0.5 * viewport.width / viewport.height;
[this.cameraL, this.cameraR].forEach(function(c) {
c.fov = fov;
c.near = near;
c.far = far;
c.aspect = aspect;
});
this.renderer.setViewport(
viewport.x,
viewport.y,
viewport.width / 2,
viewport.height
);
this.renderer.render(this.scene, this.cameraL);
this.renderer.setViewport(
viewport.width / 2,
viewport.y,
viewport.width / 2,
viewport.height
);
this.renderer.render(this.scene, this.cameraR);
}
}
}
window.app = new App();