-
Notifications
You must be signed in to change notification settings - Fork 1
/
three-vr-example.js
108 lines (88 loc) · 2.92 KB
/
three-vr-example.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
const canvasSketch = require('canvas-sketch');
global.THREE = require('three');
global.WEBVR = require('./js/WebVR.js');
require('three/examples/js/controls/OrbitControls');
require('three/examples/js/geometries/BoxLineGeometry');
const settings = {
context: 'webgl',
attributes: { antialias: true }
};
const sketch = ({ context }) => {
let activeCamera;
const resetCameraPosition = () => {
controls.target.set(0, 1, 0);
camera.position.set(1.5, 1.8, 1.5);
}
const render = (time) => {
isPresentingVr = renderer.vr.getDevice() && renderer.vr.isPresenting();
if (isPresentingVr) {
activeCamera = vrCamera;
} else {
activeCamera = camera;
controls.update();
}
mesh.rotation.y += 0.01;
renderer.render(scene, activeCamera);
}
const onVrPresentChange = (e) => {
if (e.display.isPresenting) {
// toggle vr as to not conflict with OrbitControls.
renderer.vr.enabled = true;
} else {
// toggle vr as to not conflict with OrbitControls.
renderer.vr.enabled = false;
resetCameraPosition();
}
}
const renderer = new THREE.WebGLRenderer(context);
renderer.setClearColor('#082337', 1);
// Use three.js renderer to drive render loop.
renderer.setAnimationLoop(render);
// Adds Enter VR button
document.body.appendChild(WEBVR.createButton(renderer));
const scene = new THREE.Scene();
// setup standard camera and use 2D mouse and touch OrbitControls.
const camera = activeCamera = new THREE.PerspectiveCamera(70, 1, 0.1, 1000);
const controls = new THREE.OrbitControls(camera, context.canvas);
controls.maxPolarAngle = Math.PI / 2;
// setup a specific VR camera and put it on dolly so we can move it around.
const vrDolly = new THREE.Group();
const vrCamera = new THREE.PerspectiveCamera();
vrDolly.add(vrCamera);
vrDolly.position.set(0, 0, 2);
scene.add(vrDolly)
// lights
scene.add(new THREE.AmbientLight('#cfcfcf'));
const light = new THREE.DirectionalLight('#fff', 1);
light.position.set(-10, 10, 10);
scene.add(light);
// scene
const room = new THREE.LineSegments(
new THREE.BoxLineGeometry(5, 5, 5, 10, 10, 10),
new THREE.LineBasicMaterial({ color: '#134D79' }));
room.geometry.translate(0, 2.5, 0);
scene.add(room);
const mesh = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshPhysicalMaterial({
color: '#FF0044',
roughness: 0.80,
flatShading: true
}));
mesh.position.set(0, 1, 0);
scene.add(mesh);
resetCameraPosition();
window.addEventListener('vrdisplaypresentchange', onVrPresentChange);
return {
resize ({ pixelRatio, viewportWidth, viewportHeight }) {
renderer.setPixelRatio(pixelRatio);
renderer.setSize(viewportWidth, viewportHeight);
camera.aspect = viewportWidth / viewportHeight;
camera.updateProjectionMatrix();
},
unload () {
renderer.dispose();
}
};
};
canvasSketch(sketch, settings);