-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.js
138 lines (114 loc) · 3.26 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Taming Macaws
Adapted from http://threejs.org/examples/#canvas_geometry_birds
Available on https://github.com/ongmingyang/macaws
Ong Ming Yang <[email protected]>
2014
*/
var SCREEN_WIDTH = window.innerWidth,
SCREEN_HEIGHT = window.innerHeight,
TOTAL_BIRDS = 15;
var camera, scene, renderer, light, plane,
bird, birds, boid, boids, obstacles;
var controls, gridHelper;
// Initialize vectors used in render loop
var centripetal = new THREE.Vector3(),
home = new THREE.Vector3(),
origin = new THREE.Vector3( 0, 30, 0 ),
up = new THREE.Vector3( 0, 1, 0 );
init();
render();
function init() {
birds = [];
boids = [];
obstacles = [];
// Camera
camera = new THREE.PerspectiveCamera( 45, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
camera.position.set( 150, 200, 300 );
camera.lookAt( 0, 0, 0 );
// Scene
scene = new THREE.Scene();
for ( var i = 0; i < TOTAL_BIRDS; i++ ) {
// Boids
boid = boids[i] = new Boid(Math.random()*200-100, Math.random()*50, Math.random()*200-100);
// Birds
geometry = new Bird();
material = new THREE.MeshPhongMaterial({
color: randomColour(),
side: THREE.DoubleSide
//wireframe: true
});
bird = birds[i] = new THREE.Mesh( geometry, material );
bird.castShadow = true;
bird.receiveShadow = true;
bird.phase = Math.random() * 62.83;
scene.add( bird );
}
// Add terrain features
addTerrain( scene );
// Renderer
renderer = new THREE.WebGLRenderer({
antialias: true
});
renderer.setClearColor( 0xffffff );
renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
renderer.shadowMapEnabled = true;
document.body.appendChild( renderer.domElement );
// Browser controls
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.minDistance = 100;
controls.maxDistance = 800;
controls.maxPolarAngle = Math.PI / 2 - 0.1;
controls.noKeys = true;
controls.noPan = true;
window.addEventListener( 'resize', onWindowResize, false );
}
function randomColour() {
switch (Math.ceil(Math.random()*4)) {
case 1:
return 0x3366ff;
break;
case 2:
return 0x66ff33;
break;
case 3:
return 0xff6633;
break;
default:
return 0x6633ff;
}
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function render() {
requestAnimationFrame(render);
renderer.render(scene, camera);
// Render birds
for ( var i = 0; i < birds.length; i++ ) {
boid = boids[i];
// Creates clockwise motion instinct
centripetal.crossVectors( boid.velocity, up );
centripetal.divideScalar(100);
// Creates homing instinct
home.subVectors( origin, boid.position );
home.divideScalar( 50000 );
// Add boid forces
boid.keepBounded();
boid.flock( boids );
for ( var j = 0; j < obstacles.length; j++ ) {
boid.repulse( obstacles[j] );
}
boid.acceleration.add( centripetal );
boid.acceleration.add( home );
bird = birds[i];
bird.geometry.verticesNeedUpdate = true;
bird.phase = (bird.phase + 0.1) % 62.83; // 20 pi
// Compute movements
orientate( bird, boid );
boid.move();
flap(bird);
}
}