This repository has been archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app1.html
125 lines (107 loc) · 3.31 KB
/
app1.html
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
<!doctype html>
<html>
<body>
<script src="js/three.js"></script>
<script src="js/GLTFLoader.js"></script>
<script>
(async () => {
const renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
});
renderer.gammaOutput = true;
renderer.gammaFactor = 2.2;
renderer.physicallyCorrectLights = true;
renderer.toneMappingExposure = 1;
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.sortObjects = false;
// renderer.shadowMap.enabled = true;
// renderer.shadowMap.type = THREE.PCFShadowMap;
document.body.appendChild(renderer.domElement);
const scene = new THREE.Scene();
scene.matrixAutoUpdate = false;
const camera = new THREE.PerspectiveCamera(60, window.innerWidth/window.innerHeight, 0.1, 1000);
scene.add(camera);
/* const ambientLight = new THREE.AmbientLight(0x333333, 1);
scene.add(ambientLight); */
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 3);
directionalLight.position.set(10, 30, 10);
scene.add(directionalLight);
const modelMesh = (() => {
const object = new THREE.Object3D();
const loader = new THREE.GLTFLoader();
loader.load('room.glb', function(o) {
o = o.scene;
const map = null;
o.traverse(e => {
if (e.isMesh) {
// e.receiveShadow = true;
// e.castShadow = true;
/* if (Array.isArray(e.material)) {
for (let material of e.material) {
if (material.isMeshBasicMaterial) {
continue;
}
material.envMap = map;
material.needsUpdate = true;
}
} else if (e.material && !e.material.isMeshBasicMaterial) {
e.material.envMap = map;
e.material.needsUpdate = true;
} */
/* e.material.color = new THREE.Color(0.4, 0.4, 0.4);
e.material.metalness = 0.4;
e.material.roughness = 1;
e.material.emissive = new THREE.Color(0.1, 0.1, 0.1); */
}
});
// o.updateMatrixWorld();
object.add(o);
}, undefined, function(e) {
console.error(e);
});
return object;
})();
scene.add(modelMesh);
const lightsaberIframe = document.createElement('xr-iframe');
lightsaberIframe.src = 'lightsaber.html';
lightsaberIframe.id = 'lightsaber';
lightsaberIframe.name = 'Lightsaber';
lightsaberIframe.position = [0, 1, 0];
document.body.appendChild(lightsaberIframe);
const session = await navigator.xr.requestSession();
if (session.layers) {
session.layers.push(renderer.domElement);
session.layers.push(lightsaberIframe);
}
session.requestAnimationFrame((timestamp, frame) => {
renderer.vr.setSession(session, {
frameOfReferenceType: 'stage',
});
const pose = frame.getViewerPose();
const viewport = session.baseLayer.getViewport(pose.views[0]);
// const width = viewport.width;
const height = viewport.height;
const fullWidth = (() => {
let result = 0;
for (let i = 0; i < pose.views.length; i++) {
result += session.baseLayer.getViewport(pose.views[i]).width;
}
return result;
})();
renderer.setSize(fullWidth, height);
renderer.setPixelRatio(1);
renderer.setAnimationLoop(null);
renderer.vr.enabled = true;
renderer.vr.setSession(session);
renderer.vr.setAnimationLoop(animate);
});
function animate() {
renderer.render(scene, camera);
}
// renderer.setAnimationLoop(animate);
})();
</script>
</body>
</html>