-
Notifications
You must be signed in to change notification settings - Fork 0
/
vr.js
117 lines (93 loc) · 2.88 KB
/
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
import * as THREE from 'three';
import {VRButton} from 'three/addons/webxr/VRButton.js';
import {GLTFLoader} from 'three/addons/loaders/GLTFLoader.js';
import {OrbitControls} from 'three/addons/controls/OrbitControls.js';
let camera;
let renderer;
let scene;
init();
animate();
function init() {
const canvas = document.querySelector('#c');
renderer = new THREE.WebGLRenderer({canvas});
//enable webXR
renderer.xr.enabled = true;
renderer.xr.setReferenceSpaceType( 'local' );
document.body.appendChild(VRButton.createButton(renderer));
scene = new THREE.Scene();
//light
const color = 0xFFFFFF;
const intensity = 0.5;
//main light
const light = new THREE.DirectionalLight(color, intensity);
light.position.set(-40, 30, 1);
light.castShadow = true;
scene.add(light);
const light2 = new THREE.DirectionalLight(color, intensity);
light2.position.set(-40, -30, 1);
light2.castShadow = true;
scene.add(light2);
//ambient light
const ambientLight = new THREE.AmbientLight(0x4B4B4B, 0.5);
scene.add(ambientLight);
//camera
const fov = 75;
const aspect = 2; // the canvas default
const near = 0.1;
const far = 1000;
camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
//fix bug where camera pos does not match vr camera pos
const _camera = new THREE.Object3D();
_camera.position.set(0.2, 1.1, -0.70);
_camera.rotation.y = Math.PI;
scene.add(_camera);
_camera.add(camera);
//add skybox
var skybox = new THREE.SphereGeometry( 500, 60, 40 );
skybox.scale( - 1, 1, 1 );
var skyboxmaterial = new THREE.MeshBasicMaterial( {
map: new THREE.TextureLoader().load( './resources/panorama.png' )
} );
var skyboxmesh = new THREE.Mesh( skybox, skyboxmaterial );
scene.add( skyboxmesh );
//add train model
loadModel();
//controller for PC camera
const controls = new OrbitControls( _camera, renderer.domElement );
controls.rotateSpeed *= -1;
controls.target.set(0.2, 1.1, -0.7001);
controls.update();
}
function resizeRendererToDisplaySize(renderer) {
const canvas = renderer.domElement;
const width = canvas.clientWidth;
const height = canvas.clientHeight;
const needResize = canvas.width !== width || canvas.height !== height;
if (needResize) {
renderer.setSize(width, height, false);
}
return needResize;
}
function render(time) {
time *= 0.001;
if (resizeRendererToDisplaySize(renderer)) {
const canvas = renderer.domElement;
camera.aspect = canvas.clientWidth / canvas.clientHeight;
camera.updateProjectionMatrix();
}
renderer.render(scene, camera);
}
function animate() {
renderer.setAnimationLoop( render );
}
function loadModel() {
const loader = new GLTFLoader();
loader.load('./resources/train.gltf', (gltf) => {
gltf.scene.traverse(c => {
c.castShadow = true;
c.receiveShadow = true;
});
gltf.scene.scale.set(1,1,1);
scene.add(gltf.scene);
});
}