canvas_geometry_birds.html 9.3 KB

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