canvas_geometry_birds.html 9.5 KB

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