-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp1.js
108 lines (108 loc) · 3.72 KB
/
app1.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
(_=>{
'use strict';
const polyfill = navigator.xr ? null : new WebXRPolyfill();
const cvs = document.createElement('canvas');
const xrButton = new XRDeviceButton({
onRequestSession: device => device.requestSession({exclusive: true}).then(session => {
xrButton.setSession(session);
session.addEventListener('end', e => {
xrButton.setSession(null)
cvs.style.display = 'none'
});
start(session);
}),
onEndSession: session => session.end()
});
[cvs, xrButton.domElement].forEach(el => document.body.appendChild(el));
if(navigator.xr){
navigator.xr.requestDevice()
.then(device => device.supportsSession({exclusive: true}).then(_ => xrButton.setDevice(device)));
}
const start = session => {
const start = isOK => {
if(!isOK) return console.log('error');
cvs.style.display = 'block'
const world = RedWorld();
const scene = RedScene(redGL);
const renderer = RedRenderer();
const camL = RedCamera(), camR = RedCamera();
camL.fov = 90
camR.fov = 90
redGL.world = world;
renderer.world = redGL.world;
camL.autoUpdateMatrix = camR.autoUpdateMatrix = false;
camL.x = camL.y = camL.z = 10
camL.lookAt(0, 1, 0)
camR.x = camR.y = camR.z = 10
camL.lookAt(0, 1, 0)
const tUUID = +RedGL.makeUUID()
const tLeftViewName = 'left' + tUUID
const tRightViewName = 'right' + tUUID
world.addView(RedView(tLeftViewName, scene, camL));
RedView(tLeftViewName).setSize('50%', '100%');
RedView(tLeftViewName).setLocation('0%', '0%');
world.addView(RedView('right' + tUUID, scene, camR));
RedView(tRightViewName).setSize('50%', '100%');
RedView(tRightViewName).setLocation('50%', '0%');
const setScene = function () {
let i = 10
let tMesh;
let tMat = RedColorPhongTextureMaterial(redGL)
let tGeo = RedSphere(redGL, 0.1)
let testDLight;
testDLight = RedDirectionalLight(redGL, '#fff')
testDLight.x = 3
testDLight.y = 3
testDLight.z = 3
scene.addLight(testDLight)
while ( i-- ) {
tMesh = RedMesh(redGL, tGeo, tMat)
tMesh.x = Math.sin(Math.PI * 2 / 10 * i) * 3
tMesh.y = Math.cos(Math.PI * 2 / 10 * i) * 3
tMesh.z = -10
scene.addChild(tMesh)
}
tMesh = RedMesh(redGL, RedSphere(redGL, 1, 32, 32, 32), tMat)
tMesh.z = -10
scene.addChild(tMesh)
// scene.grid = RedGrid(redGL);
scene.skyBox =
RedSkyBox(redGL, [
'asset/cubemap/posx.png',
'asset/cubemap/negx.png',
'asset/cubemap/posy.png',
'asset/cubemap/negy.png',
'asset/cubemap/negz.png',
'asset/cubemap/posz.png'
]);
}
setScene()
redGL.fullMode = false
session.baseLayer = new XRWebGLLayer(session, redGL.gl);
session.requestFrameOfReference('eyeLevel').then(frameOfRef => {
const onframe = (t, frame) => {
const session = frame.session;
const pose = frame.getDevicePose(frameOfRef);
t = performance.now()
if ( pose ) {
redGL.gl.bindFramebuffer(redGL.gl.FRAMEBUFFER, session.baseLayer.framebuffer);
redGL.gl.clear(redGL.gl.COLOR_BUFFER_BIT | redGL.gl.DEPTH_BUFFER_BIT);
for ( const view of frame.views ) {
const viewport = session.baseLayer.getViewport(view);
const cam = viewport.x == 0 ? camL : camR;
const viewName = viewport.x == 0 ? tLeftViewName : tRightViewName
RedView(viewName).setSize(viewport.width, viewport.height)
RedView(viewName).setLocation(viewport.x, viewport.y)
cam.perspectiveMTX = view.projectionMatrix;
cam.matrix = pose.getViewMatrix(view);
}
renderer.render(redGL, t);
}
session.requestAnimationFrame(onframe);
}
session.requestAnimationFrame(onframe);
});
};
const redGL = RedGL(document.querySelector('canvas'), start, {compatibleXRDevice: session.device});
};
})();