canvas_geometry_birds.html 9.6 KB

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