-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbird.js
137 lines (103 loc) · 2.98 KB
/
bird.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
/*
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 Bird = function () {
var scope = this;
THREE.Geometry.call( this );
// Body (0-4)
v( 5, 0, 0 );
v( - 3, - 1, 0.5 );
v( - 5, 0, 0 );
v( - 3, - 1, - 0.5 );
v( 4, 0.5, 0 );
// Wings (5-10)
v( 0, 2, - 6 );
v( 0, 2, 6 );
v( 2, 0, 0 );
v( - 3, 0, 0 );
v( 1, 1, - 3 );
v( 1, 1, 3 );
// Tail (11-13)
v( - 8, 0.5, 1 );
v( - 9, 1, 0 );
v( - 8, 0.5, -1 );
// Body
//f3( 0, 2, 1 );
//f3( 0, 3, 2 );
//f3( 0, 4, 2 );
//f3( 0, 8, 2 );
f3( 0, 1, 4 );
f3( 2, 1, 4 );
f3( 0, 3, 4 );
f3( 2, 3, 4 );
// Wings
f3( 8, 9, 7 );
f3( 5, 8, 9 );
f3( 6, 10, 8 );
f3( 10, 7, 8 );
// Tail
f3( 11, 8, 12);
f3( 12, 8, 13);
f3( 2, 8, 12 );
this.computeFaceNormals();
function v( x, y, z ) {
scope.vertices.push( new THREE.Vector3( x, y, z ) );
}
function f3( a, b, c ) {
scope.faces.push( new THREE.Face3( a, b, c ) );
}
}
Bird.prototype = Object.create( THREE.Geometry.prototype );
function flap( bird ) {
phase = bird.phase;
function sign(x) {
return x && x / Math.abs( x );
}
h = Math.sin( phase );
w = Math.cos( phase );
lag = Math.sin( phase - 1 );
lagw = Math.cos( phase - 0.3 );
if (sign(lagw) < 0) {
bird.geometry.vertices[ 5 ].z = lagw * 3 - 6;
bird.geometry.vertices[ 6 ].z = - lagw * 3 + 6;
} else {
bird.geometry.vertices[ 5 ].z = lagw * 1 - 6;
bird.geometry.vertices[ 6 ].z = - lagw * 1 + 6;
}
bird.geometry.vertices[ 9 ].z = w * 1 - 3;
bird.geometry.vertices[ 10 ].z = - w * 1 + 3;
bird.geometry.vertices[ 9 ].x = bird.geometry.vertices[ 10 ].x = w * 1 + 2;
bird.geometry.vertices[ 9 ].x = bird.geometry.vertices[ 10 ].x = lagw * 1 + 2;
if (sign(h) > 0) {
bird.geometry.vertices[ 9 ].y = bird.geometry.vertices[ 10 ].y = h * 4;
} else {
bird.geometry.vertices[ 9 ].y = bird.geometry.vertices[ 10 ].y = h * 3;
}
if (sign(lag) > 0) {
bird.geometry.vertices[ 5 ].y = bird.geometry.vertices[ 6 ].y = lag * 5 + 1;
} else {
bird.geometry.vertices[ 5 ].y = bird.geometry.vertices[ 6 ].y = lag * 6 + 1;
}
// Bird butt movement
bird.geometry.vertices[ 8 ].y = - h * 0.3;
bird.geometry.vertices[ 2 ].y = - h * 0.2;
bird.geometry.vertices[ 12 ].y = h * 0.25;
bird.geometry.vertices[ 11 ].y = bird.geometry.vertices[ 13 ].y = - lagw * 0.15;
bird.geometry.vertices[ 1 ].y = bird.geometry.vertices[ 3 ].y = - h * 0.2 - 1;
}
function orientate( bird, boid ) {
// Move
bird.position.copy( boid.position );
// Yaw
bird.rotation.y = Math.atan2( - boid.averageVelocity.z, boid.averageVelocity.x );
// Pitch
bird.rotation.z = Math.asin( boid.averageVelocity.y / boid.averageVelocity.length() );
// Bank
bird.rotation.x = boid.averageBank;
// Yaw and Pitch before Banking
bird.rotation.order = 'YZX';
}