geometry_birds.html 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <html>
  2. <head>
  3. <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
  4. <title>three.js - geometry - birds</title>
  5. <style type="text/css">
  6. body {
  7. color: #808080;
  8. font-family:Monospace;
  9. font-size:13px;
  10. text-align:center;
  11. background-color: #ffffff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px; width: 100%;
  18. padding: 5px;
  19. }
  20. </style
  21. </head>
  22. <body>
  23. <div id="container"></div>
  24. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - birds demo</div>
  25. <script type="text/javascript" src="js/Stats.js"></script>
  26. <script type="text/javascript" src="../build/Three.js"></script>
  27. <script type="text/javascript" src="geometry/Bird.js"></script>
  28. <script type="text/javascript">
  29. // Based on http://www.openprocessing.org/visuals/?visualID=6910
  30. var Boid = function() {
  31. var vector = new THREE.Vector3(),
  32. _acceleration, _width = 500, _height = 500, _depth = 200, _goal, _neighborhoodRadius = 100,
  33. _maxSpeed = 4, _maxSteerForce = 0.1, _avoidWalls = false;
  34. this.position = new THREE.Vector3();
  35. this.velocity = new THREE.Vector3();
  36. _acceleration = new THREE.Vector3();
  37. this.setGoal = function ( target ) {
  38. _goal = target;
  39. }
  40. this.setAvoidWalls = function ( value ) {
  41. _avoidWalls = value;
  42. }
  43. this.setWorldSize = function ( width, height, depth ) {
  44. _width = width;
  45. _height = height;vector
  46. _depth = depth;
  47. }
  48. this.run = function ( boids ) {
  49. if ( _avoidWalls ) {
  50. vector.set( - _width, this.position.y, this.position.z );
  51. vector = this.avoid( vector );
  52. vector.multiplyScalar( 5 );
  53. _acceleration.addSelf( vector );
  54. vector.set( _width, this.position.y, this.position.z );
  55. vector = this.avoid( vector );
  56. vector.multiplyScalar( 5 );
  57. _acceleration.addSelf( vector );
  58. vector.set( this.position.x, - _height, this.position.z );
  59. vector = this.avoid( vector );
  60. vector.multiplyScalar( 5 );
  61. _acceleration.addSelf( vector );
  62. vector.set( this.position.x, _height, this.position.z );
  63. vector = this.avoid( vector );
  64. vector.multiplyScalar( 5 );
  65. _acceleration.addSelf( vector );
  66. vector.set( this.position.x, this.position.y, - _depth );
  67. vector = this.avoid( vector );
  68. vector.multiplyScalar( 5 );
  69. _acceleration.addSelf( vector );
  70. vector.set( this.position.x, this.position.y, _depth );
  71. vector = this.avoid( vector );
  72. vector.multiplyScalar( 5 );
  73. _acceleration.addSelf( vector );
  74. }/* else {
  75. this.checkBounds();
  76. }
  77. */
  78. if ( Math.random() > 0.5 ) {
  79. this.flock( boids );
  80. }
  81. this.move();
  82. }
  83. this.flock = function ( boids ) {
  84. if ( _goal ) {
  85. _acceleration.addSelf( this.reach( _goal, 0.005 ) );
  86. }
  87. _acceleration.addSelf( this.alignment( boids ) );
  88. _acceleration.addSelf( this.cohesion( boids ) );
  89. _acceleration.addSelf( this.separation( boids ) );
  90. }
  91. this.move = function () {
  92. this.velocity.addSelf( _acceleration );
  93. var l = this.velocity.length();
  94. if ( l > _maxSpeed ) {
  95. this.velocity.divideScalar( l / _maxSpeed );
  96. }
  97. this.position.addSelf( this.velocity );
  98. _acceleration.set( 0, 0, 0 );
  99. }
  100. this.checkBounds = function () {
  101. if ( this.position.x > _width ) this.position.x = - _width;
  102. if ( this.position.x < - _width ) this.position.x = _width;
  103. if ( this.position.y > _height ) this.position.y = - _height;
  104. if ( this.position.y < - _height ) this.position.y = _height;
  105. if ( this.position.z > _depth ) this.position.z = - _depth;
  106. if ( this.position.z < - _depth ) this.position.z = _depth;
  107. }
  108. //
  109. this.avoid = function ( target ) {
  110. var steer = new THREE.Vector3();
  111. steer.copy( this.position );
  112. steer.subSelf( target );
  113. steer.multiplyScalar( 1 / this.position.distanceToSquared( target ) );
  114. return steer;
  115. }
  116. this.repulse = function ( target ) {
  117. var distance = this.position.distanceTo( target );
  118. if ( distance < 150 ) {
  119. var steer = new THREE.Vector3();
  120. steer.sub( this.position, target );
  121. steer.multiplyScalar( 0.5 / distance );
  122. _acceleration.addSelf( steer );
  123. }
  124. }
  125. this.reach = function ( target, amount ) {
  126. var steer = new THREE.Vector3();
  127. steer.sub( target, this.position );
  128. steer.multiplyScalar( amount );
  129. return steer;
  130. }
  131. this.alignment = function ( boids ) {
  132. var boid, velSum = new THREE.Vector3(),
  133. count = 0;
  134. for ( var i = 0, il = boids.length; i < il; i++ ) {
  135. if ( Math.random() > 0.6 ) continue;
  136. boid = boids[ i ];
  137. distance = boid.position.distanceTo( this.position );
  138. if ( distance > 0 && distance <= _neighborhoodRadius ) {
  139. velSum.addSelf( boid.velocity );
  140. count++;
  141. }
  142. }
  143. if ( count > 0 ) {
  144. velSum.divideScalar( count );
  145. var l = velSum.length();
  146. if ( l > _maxSteerForce ) {
  147. velSum.divideScalar( l / _maxSteerForce );
  148. }
  149. }
  150. return velSum;
  151. }
  152. this.cohesion = function ( boids ) {
  153. var boid, distance,
  154. posSum = new THREE.Vector3(),
  155. steer = new THREE.Vector3(),
  156. count = 0;
  157. for ( var i = 0, il = boids.length; i < il; i ++ ) {
  158. if ( Math.random() > 0.6 ) continue;
  159. boid = boids[ i ];
  160. distance = boid.position.distanceTo( this.position );
  161. if ( distance > 0 && distance <= _neighborhoodRadius ) {
  162. posSum.addSelf( boid.position );
  163. count++;
  164. }
  165. }
  166. if ( count > 0 ) {
  167. posSum.divideScalar( count );
  168. }
  169. steer.sub( posSum, this.position );
  170. var l = steer.length();
  171. if ( l > _maxSteerForce ) {
  172. steer.divideScalar( l / _maxSteerForce );
  173. }
  174. return steer;
  175. }
  176. this.separation = function ( boids ) {
  177. var boid, distance,
  178. posSum = new THREE.Vector3(),
  179. repulse = new THREE.Vector3();
  180. for ( var i = 0, il = boids.length; i < il; i ++ ) {
  181. if ( Math.random() > 0.6 ) continue;
  182. boid = boids[ i ];
  183. distance = boid.position.distanceTo( this.position );
  184. if ( distance > 0 && distance <= _neighborhoodRadius ) {
  185. repulse.sub( this.position, boid.position );
  186. repulse.normalize();
  187. repulse.divideScalar( distance );
  188. posSum.addSelf( repulse );
  189. }
  190. }
  191. return posSum;
  192. }
  193. }
  194. </script>
  195. <script type="text/javascript">
  196. var SCREEN_WIDTH = window.innerWidth,
  197. SCREEN_HEIGHT = window.innerHeight,
  198. SCREEN_WIDTH_HALF = SCREEN_WIDTH / 2,
  199. SCREEN_HEIGHT_HALF = SCREEN_HEIGHT / 2;
  200. var camera, scene, renderer,
  201. birds, bird;
  202. var boid, boids;
  203. var stats;
  204. init();
  205. function init() {
  206. camera = new THREE.Camera( 75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  207. camera.position.z = 450;
  208. scene = new THREE.Scene();
  209. birds = [];
  210. boids = [];
  211. for ( var i = 0; i < 200; i ++ ) {
  212. boid = boids[ i ] = new Boid();
  213. boid.position.x = Math.random() * 400 - 200;
  214. boid.position.y = Math.random() * 400 - 200;
  215. boid.position.z = Math.random() * 400 - 200;
  216. boid.velocity.x = Math.random() * 2 - 1;
  217. boid.velocity.y = Math.random() * 2 - 1;
  218. boid.velocity.z = Math.random() * 2 - 1;
  219. boid.setAvoidWalls( true );
  220. boid.setWorldSize( 500, 500, 400 );
  221. bird = birds[ i ] = new THREE.Mesh( new Bird(), new THREE.MeshColorFillMaterial( Math.random() * 0xffffff ) );
  222. bird.phase = Math.floor( Math.random() * 62.83 );
  223. bird.position = boids[ i ].position;
  224. bird.doubleSided = true;
  225. // bird.scale.x = bird.scale.y = bird.scale.z = 10;
  226. scene.addObject( bird );
  227. }
  228. renderer = new THREE.CanvasRenderer();
  229. // renderer.autoClear = false;
  230. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  231. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  232. document.body.appendChild( renderer.domElement );
  233. stats = new Stats();
  234. stats.domElement.style.position = 'absolute';
  235. stats.domElement.style.left = '0px';
  236. stats.domElement.style.top = '0px';
  237. document.getElementById( 'container' ).appendChild(stats.domElement);
  238. setInterval( loop, 1000 / 200 );
  239. }
  240. function onDocumentMouseMove( event ) {
  241. var vector = new THREE.Vector3( event.clientX - SCREEN_WIDTH_HALF, - event.clientY + SCREEN_HEIGHT_HALF, 0 );
  242. for ( var i = 0, il = boids.length; i < il; i++ ) {
  243. boid = boids[ i ];
  244. vector.z = boid.position.z;
  245. boid.repulse( vector );
  246. }
  247. }
  248. function loop() {
  249. for ( var i = 0, il = birds.length; i < il; i++ ) {
  250. boid = boids[ i ];
  251. boid.run( boids );
  252. bird = birds[ i ];
  253. color = bird.material[0].color;
  254. color.r = color.g = color.b = ( 500 - bird.position.z ) / 1000;
  255. color.updateStyleString();
  256. bird.rotation.y = Math.atan2( - boid.velocity.z, boid.velocity.x );
  257. bird.rotation.z = Math.asin( boid.velocity.y / boid.velocity.length() );
  258. bird.phase = ( bird.phase + ( Math.max( 0, bird.rotation.z ) + 0.1 ) ) % 62.83;
  259. bird.geometry.vertices[ 5 ].position.y = bird.geometry.vertices[ 4 ].position.y = Math.sin( bird.phase ) * 5;
  260. }
  261. renderer.render( scene, camera );
  262. stats.update();
  263. }
  264. </script>
  265. </body>
  266. </html>