canvas_geometry_birds.html 9.4 KB

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