This repository has been archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy paththree-demo-complete.html
167 lines (128 loc) · 5.4 KB
/
three-demo-complete.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Building VR interactives with Three.js</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<style>
html, body { margin:0; padding:0; overflow:hidden;}
</style>
</head>
<body>
<script>
WebVRConfig = {
// Forces availability of VR mode.
FORCE_ENABLE_VR: true,
};
</script>
<!-- Assets we'll need - Three.js, controls, and the webVR manager and polyfill -->
<script src="js/es6-promise.min.js"></script>
<script src="js/three.min.js"></script>
<script src="js/three.flycontrols.js"></script>
<script src="js/three.terrainloader.js"></script>
<script src="js/VRControls.js"></script>
<script src="js/VREffect.js"></script>
<script src="js/webvr-polyfill.js"></script>
<script src="js/webvr-manager.js"></script>
<script>
// Width and height of the browser window
var WINDOW_WIDTH = window.innerWidth;
var WINDOW_HEIGHT = window.innerHeight;
// Width and height of the surface we're going to create
var WORLD_WIDTH = 2000;
var WORLD_HEIGHT = 1900;
// Where our lights and cameras will go
var scene = new THREE.Scene();
// Keeps track of time
var clock = new THREE.Clock();
// How we will see the scene
var camera = new THREE.PerspectiveCamera(75, WINDOW_WIDTH / WINDOW_HEIGHT, 1, 5000);
// Position the camera slightly above and in front of the scene
camera.position.set(0, -199, 75);
camera.up = new THREE.Vector3(0,0,1);
// Look at the center of the scene
camera.lookAt(scene.position);
// Think of the renderer as the engine that drives the scene
var renderer = new THREE.WebGLRenderer({antialias: true});
// Set the pixel ratio of the screen (for high DPI screens)
renderer.setPixelRatio(window.devicePixelRatio);
// Set the background of the scene to a orange/red
renderer.setClearColor(0xffd4a6);
// Set renderer to the size of the window
renderer.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Append the renderer to the DOM
document.body.appendChild( renderer.domElement );
// Apply VR stereo rendering to renderer
var effect = new THREE.VREffect(renderer);
effect.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
var manager = new WebVRManager(renderer, effect);
// URL to our DEM resource
var terrainURL = "data/Gale_HRSC_DEM_50m_300x285.bin";
// Utility to load the DEM data
var terrainLoader = new THREE.TerrainLoader();
// We'll need this later
var surface;
// Create the plane geometry
var geometry = new THREE.PlaneGeometry(WORLD_WIDTH, WORLD_HEIGHT, 299, 284);
// The terrainLoader loads the DEM file and defines a function to be called when the file is successfully downloaded.
terrainLoader.load(terrainURL, function(data){
// Adjust each vertex in the plane to correspond to the height value in the DEM file.
for (var i = 0, l = geometry.vertices.length; i < l; i++) {
geometry.vertices[i].z = data[i] / 65535 * 100;
}
var textureLoader = new THREE.TextureLoader();
var textureURL = "data/Gale_texture_high_4096px.jpg";
// This goes inside the TerrainLoader callback function
textureLoader.load(textureURL, function(texture) {
var material = new THREE.MeshLambertMaterial({
map: texture,
});
// This goes in the TextureLoader callback
// Create the surface mesh and add it to the scene
surface = new THREE.Mesh(geometry, material);
scene.add(surface);
});
});
// Lights!
var dirLight = new THREE.DirectionalLight( 0xffffff, 0.75);
dirLight.position.set( -1, 1, 1).normalize();
var ambiLight = new THREE.AmbientLight(0x999999);
// Add the lights to the scene
scene.add(ambiLight);
scene.add(dirLight);
// Detect mobile devices in the user agent
var is_mobile= /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
// Conditionally load VR or Fly controls, based on whether we're on a mobile device
if (is_mobile) {
var controls = new THREE.VRControls(camera);
} else {
// WASD-style movement controls
var controls = new THREE.FlyControls(camera);
// Disable automatic forward movement
controls.autoForward = false;
// Click and drag to look around with the mouse
controls.dragToLook = true;
// Movement and roll speeds, adjust these and see what happens!
controls.movementSpeed = 20;
controls.rollSpeed = Math.PI / 12;
}
// Render loop
// This should go at the bottom of the script.
function render() {
// Get the difference from when the clock was last updated and update the controls based on that value.
var delta = clock.getDelta();
controls.update(delta);
// Update the scene through the manager.
manager.render(scene, camera);
// Call the render function again
requestAnimationFrame( render );
}
render();
</script>
</body>
</html>