-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_three.html
286 lines (255 loc) · 9.1 KB
/
test_three.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>ThreePad</title>
<meta charset="utf-8">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"/>
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
<meta name="description" content="HTML5 Canvas rendering using Three.js - by Jay Crossler" />
<meta name="keywords" content="html5, canvas, ipad, three.js, ios, android, gestures" />
<meta name="robots" content="index, follow" />
<style type="text/css">
body {
color: #000000;
font-family:Monospace;
font-size:13px;
text-align:center;
background-color: #ff0000;
margin: 0px;
padding:0;
height:100%;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
padding: 5px;
}
#details {
position: absolute;
width: 120px; height: 110px;
color: #ffffff;
background-color: #200;
padding: 3px;
right: 10px; bottom: 10px;
-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)";
filter: alpha(opacity=50);
opacity: 0.7;
}
a, span {
color: #0000ff;
cursor: pointer; cursor: hand;
}
#details a,span {
color: #ffff00;
}
//Handles mouse bouncing and other iPad-specific issues
* {
-webkit-touch-callout: none;
-webkit-text-size-adjust: none;
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-user-select: none;
}
</style>
<script type="text/javascript" src="js/Stats.js"></script>
<script type="text/javascript" src="js/Three.js"></script>
<script type="text/javascript" src="js/CameraControl.js"></script>
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
</head>
<body onload="preloader()">
<div id="container"></div>
<div id="info"></div>
<div id="details">
Cube:<span onclick="init_object_cube();">ADD</span>
<span onclick="kill_object_cube();">DEL</span> <br/>
Planet:<span onclick="init_object_planet();">ADD</span>
<span onclick="kill_object_planet();">DEL</span> <br/>
Points:<span onclick="init_object_points();">ADD</span>
<span onclick="kill_object_points();">DEL</span> <br/>
Line:<span onclick="init_object_line();">ADD</span>
<span onclick="kill_object_line();">DEL</span> <br/><br/>
<a href="?renderer=webgl">WEBGL</a>
<a href="?renderer=canvas">CANVAS</a>
<a href="?renderer=svg">SVG</a>
</div>
<script type="text/javascript">
var container, stats, div_details, div_info;
var camera, cameraControl, scene, renderer, projector;
var option_RenderEngine;
var obj_cube, obj_planet, obj_points, obj_points_list = [], obj_line; //Kept as globals so easy to delete (not a good coding standard)
function preloader() {
init(); //Set up the scene and renderer
animate(); //Start updating the scene as often as possible
}
function init() {
container = document.getElementById( 'container' );
div_details =document.getElementById( 'details' );
div_info= document.getElementById( 'info' );
scene = new THREE.Scene();
init_camera();
init_grid();
init_renderer();
init_stats();
}
//=======================================
function init_object_cube() {
//Set up any objects that should be shown on the scene
var materials = [];
for ( var i = 0; i < 6; i ++ ) {
materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } ) ] );
}
obj_cube = new THREE.Mesh( new Cube( 20, 20, 20, 1, 1, 1, materials ), new THREE.MeshFaceMaterial() );
obj_cube.position.y = 10;
obj_cube.overdraw = true;
obj_cube.isClickable = true;
obj_cube.name = "Cuby";
scene.addObject( obj_cube );
}
function kill_object_cube() {
scene.removeObject(obj_cube);
}
function init_object_planet() {
obj_planet = new THREE.Mesh( new Sphere( 20, 8, 8 ), new THREE.MeshBasicMaterial( { map: ImageUtils.loadTexture( 'textures/low/Earth_surface.jpg' ) } ) );
obj_planet.position.x = 40;
obj_planet.position.y = 10;
obj_planet.overdraw = true;
obj_planet.isClickable = true;
obj_planet.name = "Earth";
scene.addObject(obj_planet);
}
function kill_object_planet() {
scene.removeObject(obj_planet);
}
function init_object_points() {
var material = new THREE.ParticleCircleMaterial( { color: 0xffffff } );
obj_points = new THREE.Geometry();
obj_points_list = [];
//Particles aren't selectable, and don't render under WebGL
for ( var i = 0; i < 20; i ++ ) {
var particle = new THREE.Particle( material );
particle.position.x = Math.random() * 2 - 1;
particle.position.y = Math.random() * 2 - 1;
particle.position.z = Math.random() * 2 - 1;
particle.position.normalize();
particle.position.multiplyScalar( Math.random()*10 + 100 );
particle.scale.x = particle.scale.y = 5;
scene.addObject( particle );
obj_points_list.push(particle);
obj_points.vertices.push( new THREE.Vertex( particle.position ) );
}
}
function kill_object_points() {
for (var l = obj_points_list.length, i = l-1; i >= 0; i--) {
scene.removeObject(obj_points_list[i]);
}
}
function init_object_line() {
obj_line = new THREE.Line( obj_points, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.5 } ) );
scene.addObject( obj_line );
}
function kill_object_line() {
scene.removeObject(obj_line);
}
//=========================================
function init_grid() {
// Grid
var geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( -1 * 200, 0, 0 ) ) );
geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 200, 0, 0 ) ) );
var material = new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.8 } );
var lineSteps = 10;
for ( var i = 0; i <= lineSteps; i ++ ) {
var step = (200 *2) / lineSteps;
var line = new THREE.Line( geometry, material );
line.position.y = 0;
line.position.z = ( i * step ) - 200;
scene.addObject( line );
var line = new THREE.Line( geometry, material );
line.position.x = ( i * step ) - 200;
line.position.y = 0;
line.rotation.y = Math.PI / 2;
scene.addObject( line );
}
console.log('Grid created and added to Scene. SceneObjects now:'+scene.objects.length);
}
function init_camera() {
camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.y = 100;
camera.position.z = 100;
window.addEventListener( 'resize', onWindowResize, false );
projector = new THREE.Projector();
cameraControl = new CameraControlWASD( camera, 10, 0.005, true, false, false, false );
}
function init_stats() {
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
container.appendChild( stats.domElement );
}
function queryString(q) {
hu = window.location.search.substring(1);
gy = hu.split("&");
var result = null;
for (i=0;i<gy.length;i++) {
ft = gy[i].split("=");
if (ft[0] == q) {
result = ft[1]; break;
}
}
return result;
}
function init_renderer() {
var renderer_created = false;
var requested_renderer = queryString("renderer");
if (!requested_renderer) requested_renderer="canvas";
try {
switch(requested_renderer) {
case "webgl": renderer = new THREE.WebGLRenderer(); break;
case "canvas": renderer = new THREE.CanvasRenderer(); break;
case "svg": renderer = new THREE.SVGRenderer(); break;
default: break;
}
option_RenderEngine = requested_renderer;
div_info.innerHTML='Rendering: '+requested_renderer;
renderer_created = true;
} catch (err) {
//Try canvas
console.log("WebGL Rendering failed");
renderer = new THREE.CanvasRenderer();
div_info.innerHTML='Rendering: Canvas';
option_RenderEngine = "canvas";
renderer_created = true;
}
if (renderer_created) {
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
} else {
container.innerHTML = "<br/><br/><br/><h1>3D Rendering could not load</h1>";
console.log("FAIL! No renderer able to load");
}
}
function onWindowResize( event ) {
//When rotated, resize the window and rendering space
var width = window.innerWidth, height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize( width, height );
renderer.domElement.style.width = window.innerWidth + 'px';
renderer.domElement.style.height = window.innerHeight + 'px';
}
function animate() {
//Animate these as often as possible - up to 60fps
cameraControl.update();
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
//Render the updated canvas
renderer.render( scene, camera );
}
</script>
</body>
</html>