-
Notifications
You must be signed in to change notification settings - Fork 10
/
example40.html
182 lines (147 loc) · 5.31 KB
/
example40.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
<!DOCTYPE html>
<html>
<head>
<title>Example 40 - Path controls</title>
<style>
body{
margin: 0;
overflow: hidden;
}
#stats { /* Align stats top-left */
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- JavaScript libraries -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!-- NBBBB we're using three.js r58 -->
<script src="http://cdnjs.cloudflare.com/ajax/libs/three.js/r58/three.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/stats.js/r11/Stats.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.5/dat.gui.min.js"></script>
<script src="assets/libs/PathControls.js"></script>
<!-- Javascript code that runs our Three.js examples -->
<script>
// once everything is loaded, we run our Three.js stuff.
$(function () {
var container, stats;
var camera, controls, scene, renderer;
var cross;
var clock = new THREE.Clock();
initStats();
init();
animate();
function generatePoints (loop_points, n_loops, h_loop, r) {
var points = [];
var n_points = n_loops * loop_points;
var i = 0, j = 0;
var alpha = (2 * Math.PI) / loop_points;
var h_step = h_loop / n_points;
var h = 0;
for (i = 0; i < n_loops; i += 1) {
for (j = 0; j < loop_points; j +=1) {
points.push([
r * Math.cos(alpha*j), // x
h, // y
r * Math.sin(alpha*j) // z
]);
h += h_step;
}
}
return points;
}
function init() {
var points = generatePoints(16,8,42,12);
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
controls = new THREE.PathControls( camera );
controls.waypoints = points;
controls.duration = 80
controls.useConstantSpeed = true;
controls.lookSpeed = 0.2;
controls.lookVertical = true;
controls.lookHorizontal = true;
controls.verticalAngleMap = { srcRange: [ 0, 2 * Math.PI ], dstRange: [ 1.1, 3.8 ] };
controls.horizontalAngleMap = { srcRange: [ 0, 2 * Math.PI ], dstRange: [ 0.3, Math.PI - 0.3 ] };
controls.lon = 180;
controls.init();
// world
scene = new THREE.Scene();
scene.fog = new THREE.FogExp2( 0xcccccc, 0.002 );
scene.add(controls.animationParent);
// add some objects to understand movements
var geometry = new THREE.CylinderGeometry( 0, 10, 30, 4, 1 );
var material = new THREE.MeshLambertMaterial( { color:0xffffff, shading: THREE.FlatShading } );
for ( var i = 0; i < 500; i ++ ) {
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = ( Math.random() - 0.5 ) * 1000;
mesh.position.y = ( Math.random() - 0.5 ) * 1000;
mesh.position.z = ( Math.random() - 0.5 ) * 1000;
mesh.updateMatrix();
mesh.matrixAutoUpdate = false;
scene.add( mesh );
}
var mesh = new THREE.Mesh(
new THREE.CubeGeometry(3,42,3),
new THREE.MeshLambertMaterial({color: 0x12d434})
);
mesh.position.y = 21;
scene.add(mesh);
var plane = new THREE.Mesh(
new THREE.PlaneGeometry(20,20),
new THREE.MeshBasicMaterial({color: 0x565656})
);
plane.rotation.x = -Math.PI/2;
scene.add(plane);
// add some objects to trace path
var pathGeometry = new THREE.Geometry();
points.forEach(function (point) {
var s = new THREE.Mesh(
new THREE.SphereGeometry(0.1),
new THREE.MeshLambertMaterial({color: 0xff0000})
);
s.position.set(point[0], point[1], point[2]);
scene.add(s);
pathGeometry.vertices.push(new THREE.Vector3(point[0],point[1],point[2]));
});
var path = new THREE.Line(pathGeometry, new THREE.LineBasicMaterial({color: 0xff0000}));
scene.add(path);
// lights
light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 1, 1, 1 );
scene.add( light );
light = new THREE.DirectionalLight( 0x002288 );
light.position.set( -1, -1, -1 );
scene.add( light );
light = new THREE.AmbientLight( 0x222222 );
scene.add( light );
// renderer
renderer = new THREE.WebGLRenderer( { antialias: false } );
renderer.setClearColor( scene.fog.color, 1 );
renderer.setSize( window.innerWidth, window.innerHeight );
$('body').append( renderer.domElement );
// start animation
controls.animation.play( true, 0 );
}
function animate() {
stats.update();
requestAnimationFrame( animate );
render();
}
function render() {
var delta = clock.getDelta();
THREE.AnimationHandler.update( delta );
controls.update( delta );
renderer.render( scene, camera );
}
function initStats() {
stats = new Stats();
stats.setMode(0); // 0: fps, 1: ms
$('body').append(stats.domElement);
return stats;
}
});
</script>
</body>
</html>