webgl_gpgpu_birds.html 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - gpgpu - flocking</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. background-color: #ffffff;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. text-align:center;
  16. cursor: pointer;
  17. }
  18. a {
  19. color:#0078ff;
  20. }
  21. #info {
  22. color: #000;
  23. position: absolute;
  24. top: 10px;
  25. width: 100%;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="info">
  31. <a href="http://threejs.org" target="_blank">three.js</a> - <span id="birds"></span> webgl gpgpu birds<br/>
  32. Select <span id="options"></span> birds<br/>
  33. Move mouse to disturb birds.
  34. </div>
  35. <script src="../build/three.min.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/libs/stats.min.js"></script>
  38. <script src="js/libs/dat.gui.min.js"></script>
  39. <script src="js/SimulatorRenderer.js"></script>
  40. <!--
  41. TODO: If you're reading this, you may wish to improve this example by
  42. - Replacing the custom BirdGeometry with a BufferGeometry?
  43. - Create a better shading for the birds?
  44. - Refactoring the SimulationRenderer to a more generic TextureRenderer / making the GPGPU workflow easier?
  45. -->
  46. <!-- pass through vertex shader -->
  47. <script id="vertexShader" type="x-shader/x-vertex">
  48. void main() {
  49. gl_Position = vec4( position, 1.0 );
  50. }
  51. </script>
  52. <!-- pass through fragment shader -->
  53. <script id="fragmentShader" type="x-shader/x-fragment">
  54. uniform vec2 resolution;
  55. uniform float time;
  56. uniform sampler2D texture;
  57. void main() {
  58. vec2 uv = gl_FragCoord.xy / resolution.xy;
  59. vec3 color = texture2D( texture, uv ).xyz;
  60. gl_FragColor=vec4(color, 1.0);
  61. }
  62. </script>
  63. <!-- end pass through shaders -->
  64. <!-- shader for bird's position -->
  65. <script id="fragmentShaderPosition" type="x-shader/x-fragment">
  66. uniform vec2 resolution;
  67. uniform float time;
  68. uniform float delta;
  69. uniform sampler2D textureVelocity;
  70. uniform sampler2D texturePosition;
  71. void main() {
  72. vec2 uv = gl_FragCoord.xy / resolution.xy;
  73. vec4 tmpPos = texture2D( texturePosition, uv );
  74. vec3 position = tmpPos.xyz;
  75. vec3 velocity = texture2D( textureVelocity, uv ).xyz;
  76. float phase = tmpPos.w;
  77. phase = mod( ( phase + delta +
  78. length( velocity.xz ) * delta * 3. +
  79. max( velocity.y, 0.0 ) * delta * 6. ), 62.83 );
  80. gl_FragColor = vec4( position + velocity * delta * 15. , phase );
  81. }
  82. </script>
  83. <!-- shader for bird's velocity -->
  84. <script id="fragmentShaderVelocity" type="x-shader/x-fragment">
  85. uniform vec2 resolution;
  86. uniform float time;
  87. uniform float testing;
  88. uniform float delta; // about 0.016
  89. uniform float seperationDistance; // 20
  90. uniform float alignmentDistance; // 40
  91. uniform float cohesionDistance; //
  92. uniform float freedomFactor;
  93. uniform vec3 predator;
  94. uniform sampler2D textureVelocity;
  95. uniform sampler2D texturePosition;
  96. const float width = WIDTH;
  97. const float height = WIDTH;
  98. const float PI = 3.141592653589793;
  99. const float PI_2 = PI * 2.0;
  100. // const float VISION = PI * 0.55;
  101. float zoneRadius = 40.0;
  102. float zoneRadiusSquared = zoneRadius * zoneRadius;
  103. float separationThresh = 0.45;
  104. float alignmentThresh = 0.65;
  105. const float UPPER_BOUNDS = 400.0;
  106. const float LOWER_BOUNDS = -UPPER_BOUNDS;
  107. const float SPEED_LIMIT = 9.0;
  108. float rand(vec2 co){
  109. return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);
  110. }
  111. void main() {
  112. zoneRadius = seperationDistance + alignmentDistance + cohesionDistance;
  113. separationThresh = seperationDistance / zoneRadius;
  114. alignmentThresh = ( seperationDistance + alignmentDistance ) / zoneRadius;
  115. zoneRadiusSquared = zoneRadius * zoneRadius;
  116. vec2 uv = gl_FragCoord.xy / resolution.xy;
  117. vec3 birdPosition, birdVelocity;
  118. vec3 selfPosition = texture2D( texturePosition, uv ).xyz;
  119. vec3 selfVelocity = texture2D( textureVelocity, uv ).xyz;
  120. float dist;
  121. vec3 dir; // direction
  122. float distSquared;
  123. float seperationSquared = seperationDistance * seperationDistance;
  124. float cohesionSquared = cohesionDistance * cohesionDistance;
  125. float f;
  126. float percent;
  127. vec3 velocity = selfVelocity;
  128. float limit = SPEED_LIMIT;
  129. dir = predator * UPPER_BOUNDS - selfPosition;
  130. dir.z = 0.;
  131. // dir.z *= 0.6;
  132. dist = length( dir );
  133. distSquared = dist * dist;
  134. float preyRadius = 150.0;
  135. float preyRadiusSq = preyRadius * preyRadius;
  136. // move birds away from predator
  137. if (dist < preyRadius) {
  138. f = ( distSquared / preyRadiusSq - 1.0 ) * delta * 100.;
  139. velocity += normalize( dir ) * f;
  140. limit += 5.0;
  141. }
  142. // if (testing == 0.0) {}
  143. // if ( rand( uv + time ) < freedomFactor ) {}
  144. // Attract flocks to the center
  145. vec3 central = vec3( 0., 0., 0. );
  146. dir = selfPosition - central;
  147. dist = length( dir );
  148. dir.y *= 2.5;
  149. velocity -= normalize( dir ) * delta * 5.;
  150. for (float y=0.0;y<height;y++) {
  151. for (float x=0.0;x<width;x++) {
  152. if (
  153. x == gl_FragCoord.x && y == gl_FragCoord.y) continue;
  154. birdPosition = texture2D( texturePosition,
  155. vec2( x / resolution.x, y / resolution.y ) ).xyz;
  156. dir = birdPosition - selfPosition;
  157. dist = length(dir);
  158. distSquared = dist * dist;
  159. if ( dist > 0.0 && distSquared < zoneRadiusSquared ) {
  160. percent = distSquared / zoneRadiusSquared;
  161. if ( percent < separationThresh ) { // low
  162. // Separation - Move apart for comfort
  163. f = (separationThresh / percent - 1.0) * delta;
  164. velocity -= normalize(dir) * f;
  165. } else if ( percent < alignmentThresh ) { // high
  166. // Alignment - fly the same direction
  167. float threshDelta = alignmentThresh - separationThresh;
  168. float adjustedPercent = ( percent - separationThresh ) / threshDelta;
  169. birdVelocity = texture2D( textureVelocity, vec2(x/resolution.x, y/resolution.y) ).xyz;
  170. f = ( 0.5 - cos( adjustedPercent * PI_2 ) * 0.5 + 0.5 ) * delta;
  171. velocity += normalize(birdVelocity) * f;
  172. } else {
  173. // Attraction / Cohesion - move closer
  174. float threshDelta = 1.0 - alignmentThresh;
  175. float adjustedPercent = ( percent - alignmentThresh ) / threshDelta;
  176. f = ( 0.5 - ( cos( adjustedPercent * PI_2 ) * -0.5 + 0.5 ) ) * delta;
  177. velocity += normalize(dir) * f;
  178. }
  179. }
  180. }
  181. }
  182. // this make tends to fly around than down or up
  183. // if (velocity.y > 0.) velocity.y *= (1. - 0.2 * delta);
  184. // Speed Limits
  185. if ( length( velocity ) > limit ) {
  186. velocity = normalize( velocity ) * limit;
  187. }
  188. gl_FragColor = vec4( velocity, 1.0 );
  189. }
  190. </script>
  191. <script type="x-shader/x-vertex" id="birdVS">
  192. attribute vec2 reference;
  193. attribute float birdVertex;
  194. attribute vec3 birdColor;
  195. uniform sampler2D texturePosition;
  196. uniform sampler2D textureVelocity;
  197. varying vec3 vNormal;
  198. varying vec2 vUv;
  199. varying vec4 vColor;
  200. varying float z;
  201. uniform float time;
  202. void main() {
  203. vNormal = normal;
  204. vec4 tmpPos = texture2D( texturePosition, reference );
  205. vec3 pos = tmpPos.xyz;
  206. vec3 velocity = normalize(texture2D( textureVelocity, reference ).xyz);
  207. vec3 newPosition = position;
  208. if ( birdVertex == 4.0 || birdVertex == 7.0 ) {
  209. // flap wings
  210. newPosition.y = sin( tmpPos.w ) * 5.;
  211. }
  212. newPosition = mat3( modelMatrix ) * newPosition;
  213. velocity.z *= -1.;
  214. float xz = length( velocity.xz );
  215. float xyz = 1.;
  216. float x = sqrt( 1. - velocity.y * velocity.y );
  217. float cosry = velocity.x / xz;
  218. float sinry = velocity.z / xz;
  219. float cosrz = x / xyz;
  220. float sinrz = velocity.y / xyz;
  221. mat3 maty = mat3(
  222. cosry, 0, -sinry,
  223. 0 , 1, 0 ,
  224. sinry, 0, cosry
  225. );
  226. mat3 matz = mat3(
  227. cosrz , sinrz, 0,
  228. -sinrz, cosrz, 0,
  229. 0 , 0 , 1
  230. );
  231. newPosition = maty * matz * newPosition;
  232. newPosition += pos;
  233. z = newPosition.z;
  234. vColor = vec4( birdColor, 1.0 );
  235. gl_Position = projectionMatrix * viewMatrix * vec4( newPosition, 1.0 );
  236. }
  237. </script>
  238. <!-- bird geometry shader -->
  239. <script type="x-shader/x-fragment" id="birdFS">
  240. varying vec3 vNormal;
  241. varying vec2 vUv;
  242. varying vec4 vColor;
  243. varying float z;
  244. uniform vec3 color;
  245. void main() {
  246. // Fake colors for now
  247. float z2 = 0.2 + ( 1000. - z ) / 1000. * vColor.x;
  248. gl_FragColor = vec4( z2, z2, z2, 1. );
  249. // vec3 light = vec3( 0.0, 1.0, 1.0 );
  250. // light = normalize( light );
  251. // float dProd = dot( vNormal, light ) ; //* 0.5 + 0.5;
  252. // vec4 tcolor = vColor;
  253. // vec4 gray = vec4( vec3( tcolor.r * 0.3 + tcolor.g * 0.59 + tcolor.b * 0.11 ), 1.0 );
  254. // gl_FragColor = gray * dProd;
  255. // gl_FragColor = vec4( dProd * tcolor );
  256. }
  257. </script>
  258. <script>
  259. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  260. var hash = document.location.hash.substr( 1 );
  261. if (hash) hash = parseInt(hash, 0);
  262. /* TEXTURE WIDTH FOR SIMULATION */
  263. var WIDTH = hash || 32;
  264. var BIRDS = 1024;
  265. // Custom Geometry
  266. THREE.BirdGeometry = function () {
  267. THREE.Geometry.call( this );
  268. BIRDS = WIDTH * WIDTH;
  269. var verts = this.vertices;
  270. var faces = this.faces;
  271. var uvs = this.faceVertexUvs[ 0 ];
  272. var fi = 0;
  273. for (var f = 0; f<BIRDS; f++ ) {
  274. verts.push(
  275. new THREE.Vector3(0, -0, -20),
  276. new THREE.Vector3(0, 10, -20),
  277. new THREE.Vector3(0, 0, 30)
  278. );
  279. faces.push(new THREE.Face3(
  280. fi++,
  281. fi++,
  282. fi++
  283. ));
  284. uvs.push([
  285. new THREE.Vector2(0, 0),
  286. new THREE.Vector2(0, 1),
  287. new THREE.Vector2(1, 1)
  288. ]);
  289. var wingsSpan = 30;
  290. verts.push(
  291. new THREE.Vector3(0, 0, -20),
  292. new THREE.Vector3(-wingsSpan, 0, 0),
  293. new THREE.Vector3(0, 0, 20)
  294. );
  295. verts.push(
  296. new THREE.Vector3(0, 0, 20),
  297. new THREE.Vector3(wingsSpan, 0, 0),
  298. new THREE.Vector3(0, 0, -20)
  299. );
  300. faces.push(new THREE.Face3(
  301. fi++,
  302. fi++,
  303. fi++
  304. ));
  305. faces.push(new THREE.Face3(
  306. fi++,
  307. fi++,
  308. fi++
  309. ));
  310. uvs.push([
  311. new THREE.Vector2(0, 0),
  312. new THREE.Vector2(0, 1),
  313. new THREE.Vector2(1, 1)
  314. ]);
  315. uvs.push([
  316. new THREE.Vector2(0, 0),
  317. new THREE.Vector2(0, 1),
  318. new THREE.Vector2(1, 1)
  319. ]);
  320. }
  321. this.applyMatrix( new THREE.Matrix4().makeScale( 0.2, 0.2, 0.2 ) );
  322. this.computeCentroids();
  323. this.computeFaceNormals();
  324. this.computeVertexNormals();
  325. }
  326. THREE.BirdGeometry.prototype = Object.create( THREE.Geometry.prototype );
  327. var container, stats;
  328. var camera, scene, renderer, geometry, i, h, color;
  329. var mouseX = 0, mouseY = 0;
  330. var windowHalfX = window.innerWidth / 2;
  331. var windowHalfY = window.innerHeight / 2;
  332. var HEIGHT = WIDTH;
  333. var PARTICLES = WIDTH * WIDTH;
  334. var BOUNDS = 800, BOUNDS_HALF = BOUNDS / 2;
  335. document.getElementById('birds').innerText = PARTICLES;
  336. function change(n) {
  337. location.hash = n;
  338. location.reload();
  339. return false;
  340. }
  341. var options = '';
  342. for (i=1; i<7; i++) {
  343. var j = Math.pow(2, i);
  344. options += '<a href="#" onclick="return change(' + j + ')">' + (j * j) + '</a> ';
  345. }
  346. document.getElementById('options').innerHTML = options;
  347. var debug;
  348. var data, texture;
  349. var spline = new THREE.SplineCurve3();
  350. var timer = 0;
  351. var paused = false;
  352. var last = performance.now();
  353. var delta, now, t = 0;
  354. var simulator;
  355. var flipflop = true;
  356. var rtPosition1, rtPosition2, rtVelocity1, rtVelocity2;
  357. init();
  358. animate();
  359. onMouseDown();
  360. function init() {
  361. container = document.createElement( 'div' );
  362. document.body.appendChild( container );
  363. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  364. camera.position.z = 350;
  365. scene = new THREE.Scene();
  366. scene.fog = new THREE.Fog( 0xffffff, 100, 1000 );
  367. renderer = new THREE.WebGLRenderer();
  368. renderer.setSize( window.innerWidth, window.innerHeight );
  369. container.appendChild( renderer.domElement );
  370. renderer.setClearColor( scene.fog.color, 1 );
  371. renderer.autoClear = true;
  372. ////////
  373. simulator = new SimulatorRenderer(WIDTH, renderer);
  374. var dtPosition = generateDataTexture();
  375. var dtVelocity = generateVelocityTexture();
  376. rtPosition1 = simulator.getRenderTarget();
  377. rtPosition2 = rtPosition1.clone();
  378. rtVelocity1 = rtPosition1.clone();
  379. rtVelocity2 = rtPosition1.clone();
  380. simulator.renderTexture(dtPosition, rtPosition1);
  381. simulator.renderTexture(rtPosition1, rtPosition2);
  382. simulator.renderTexture(dtVelocity, rtVelocity1);
  383. simulator.renderTexture(rtVelocity1, rtVelocity2);
  384. simulator.velocityUniforms.testing.value = 10;
  385. /////////
  386. plane = new THREE.PlaneGeometry( BOUNDS, BOUNDS, 1, 1 );
  387. // new THREE.CubeGeometry( BOUNDS, BOUNDS, BOUNDS),
  388. cube = new THREE.Mesh(
  389. plane,
  390. new THREE.MeshBasicMaterial( {color: 0xdddddd, wireframe: true, depthWrite: false} )
  391. );
  392. cube.rotation.x = -Math.PI / 2;
  393. cube.position.y = -400;
  394. // scene.add(cube);
  395. stats = new Stats();
  396. stats.domElement.style.position = 'absolute';
  397. stats.domElement.style.top = '0px';
  398. container.appendChild( stats.domElement );
  399. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  400. // document.addEventListener( 'mousedown', onMouseDown, false );
  401. document.addEventListener( 'mouseup', onMouseUp, false );
  402. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  403. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  404. //
  405. window.addEventListener( 'resize', onWindowResize, false );
  406. var gui = new dat.GUI();
  407. var effectController = {
  408. seperation: 20.0,
  409. alignment: 20.0,
  410. cohesion: 20.0,
  411. freedom: 0.75
  412. };
  413. var valuesChanger = function() {
  414. simulator.velocityUniforms.seperationDistance.value = effectController.seperation;
  415. simulator.velocityUniforms.alignmentDistance.value = effectController.alignment;
  416. simulator.velocityUniforms.cohesionDistance.value = effectController.cohesion;
  417. simulator.velocityUniforms.freedomFactor.value = effectController.freedom;
  418. };
  419. valuesChanger();
  420. gui.add( effectController, "seperation", 0.0, 100.0, 1.0 ).onChange( valuesChanger );
  421. gui.add( effectController, "alignment", 0.0, 100, 0.001 ).onChange( valuesChanger );
  422. gui.add( effectController, "cohesion", 0.0, 100, 0.025 ).onChange( valuesChanger );
  423. // gui.add( effectController, "freedom", 0.0, 1.0, 0.025 ).onChange( valuesChanger );
  424. gui.close();
  425. initBirds();
  426. // var ambient = new THREE.AmbientLight( 0x444444 );
  427. // scene.add( ambient );
  428. // light = new THREE.DirectionalLight( 0xffffff );
  429. // light.position.set( 1, 1, 1 );
  430. // scene.add( light );
  431. // light = new THREE.DirectionalLight( 0xffffff );
  432. // light.position.set( -1, -1, -1 );
  433. // scene.add( light );
  434. }
  435. function initBirds() {
  436. var geometry = new THREE.BirdGeometry( );
  437. // For Vertex Shaders
  438. birdAttributes = {
  439. index: { type: 'i', value: [] },
  440. birdColor: { type: 'c', value: [] },
  441. reference: { type: 'v2', value: [] },
  442. birdVertex: { type: 'f', value: [] },
  443. };
  444. // For Vertex and Fragment
  445. birdUniforms = {
  446. color: { type: "c", value: new THREE.Color( 0xff2200 ) },
  447. texturePosition: { type: "t", value: null },
  448. textureVelocity: { type: "t", value: null },
  449. time: { type: "f", value: 1.0 },
  450. delta: { type: "f", value: 0.0 },
  451. };
  452. // ShaderMaterial
  453. var shaderMaterial = new THREE.ShaderMaterial( {
  454. uniforms: birdUniforms,
  455. attributes: birdAttributes,
  456. vertexShader: document.getElementById( 'birdVS' ).textContent,
  457. fragmentShader: document.getElementById( 'birdFS' ).textContent,
  458. side: THREE.DoubleSide,
  459. // wireframe: true
  460. });
  461. // geometry.dynamic = false;
  462. var vertices = geometry.vertices;
  463. var birdColors = birdAttributes.birdColor.value;
  464. var references = birdAttributes.reference.value;
  465. var birdVertex = birdAttributes.birdVertex.value;
  466. for( var v = 0; v < vertices.length; v++ ) {
  467. var i = ~~(v / 3);
  468. var x = (i % WIDTH) / WIDTH;
  469. var y = ~~(i / WIDTH) / WIDTH;
  470. birdColors[ v ] = new THREE.Color(
  471. Math.random() * 0xffffff
  472. // ~~(v / 9) / BIRDS * 0xffffff
  473. );
  474. references[ v ] = new THREE.Vector2( x, y );
  475. birdVertex[ v ] = v % 9;
  476. }
  477. // var
  478. birdMesh = new THREE.Mesh( geometry, shaderMaterial );
  479. birdMesh.rotation.y = Math.PI / 2;
  480. birdMesh.sortObjects = false;
  481. birdMesh.matrixAutoUpdate = false;
  482. birdMesh.updateMatrix();
  483. scene.add(birdMesh);
  484. }
  485. function onWindowResize() {
  486. windowHalfX = window.innerWidth / 2;
  487. windowHalfY = window.innerHeight / 2;
  488. camera.aspect = window.innerWidth / window.innerHeight;
  489. camera.updateProjectionMatrix();
  490. renderer.setSize( window.innerWidth, window.innerHeight );
  491. }
  492. function onDocumentMouseMove( event ) {
  493. mouseX = event.clientX - windowHalfX;
  494. mouseY = event.clientY - windowHalfY;
  495. }
  496. function onDocumentTouchStart( event ) {
  497. if ( event.touches.length === 1 ) {
  498. event.preventDefault();
  499. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  500. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  501. }
  502. }
  503. function onDocumentTouchMove( event ) {
  504. if ( event.touches.length === 1 ) {
  505. event.preventDefault();
  506. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  507. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  508. }
  509. }
  510. //
  511. function animate() {
  512. requestAnimationFrame( animate );
  513. render();
  514. stats.update();
  515. }
  516. function render() {
  517. now = performance.now()
  518. delta = (now - last) / 1000;
  519. if (delta > 1) delta = 1; // safety cap on large deltas
  520. last = now;
  521. birdUniforms.time.value = now;
  522. birdUniforms.delta.value = delta;
  523. // if ( paused ) {
  524. // camera.position.x += ( mouseX * 2 - camera.position.x ) * 0.05;
  525. // camera.position.y += ( - mouseY * 2 - camera.position.y ) * 0.05;
  526. // camera.lookAt( scene.position );
  527. // delta = 0.0001;
  528. // }
  529. if (!paused)
  530. if (flipflop) {
  531. simulator.renderVelocity( rtPosition1, rtVelocity1, rtVelocity2, delta );
  532. simulator.renderPosition( rtPosition1, rtVelocity2, rtPosition2, delta );
  533. birdUniforms.texturePosition.value = rtPosition2;
  534. birdUniforms.textureVelocity.value = rtVelocity2;
  535. } else {
  536. simulator.renderVelocity( rtPosition2, rtVelocity2, rtVelocity1, delta );
  537. simulator.renderPosition( rtPosition2, rtVelocity1, rtPosition1, delta );
  538. birdUniforms.texturePosition.value = rtPosition1;
  539. birdUniforms.textureVelocity.value = rtVelocity1;
  540. }
  541. flipflop = !flipflop;
  542. simulator.velocityUniforms.predator.value.set( mouseX / windowHalfX, -mouseY / windowHalfY, 0 );
  543. mouseX = 10000;
  544. mouseY = 10000;
  545. renderer.render( scene, camera );
  546. }
  547. function onMouseDown() {
  548. // simulator.velocityUniforms.testing.value = 0;
  549. }
  550. function onMouseUp() {
  551. // simulator.velocityUniforms.testing.value = 1;
  552. }
  553. function generateDataTexture() {
  554. var x, y, z;
  555. var w = WIDTH, h = WIDTH;
  556. var a = new Float32Array(PARTICLES * 4);
  557. for (var k = 0; k < PARTICLES; k++) {
  558. x = Math.random() * BOUNDS - BOUNDS_HALF;
  559. y = Math.random() * BOUNDS - BOUNDS_HALF;
  560. z = Math.random() * BOUNDS - BOUNDS_HALF;
  561. a[ k*4 + 0 ] = x;
  562. a[ k*4 + 1 ] = y;
  563. a[ k*4 + 2 ] = z;
  564. a[ k*4 + 3 ] = 1;
  565. }
  566. var texture = new THREE.DataTexture( a, WIDTH, WIDTH, THREE.RGBAFormat, THREE.FloatType );
  567. texture.minFilter = THREE.NearestFilter;
  568. texture.magFilter = THREE.NearestFilter;
  569. texture.needsUpdate = true;
  570. texture.flipY = false;
  571. return texture;
  572. }
  573. function generateVelocityTexture() {
  574. var x, y, z;
  575. var w = WIDTH, h = WIDTH;
  576. var a = new Float32Array(PARTICLES * 4);
  577. for (var k = 0; k < PARTICLES; k++) {
  578. x = Math.random() - 0.5;
  579. y = Math.random() - 0.5;
  580. z = Math.random() - 0.5;
  581. a[ k*4 + 0 ] = x * 10;
  582. a[ k*4 + 1 ] = y * 10;
  583. a[ k*4 + 2 ] = z * 10;
  584. a[ k*4 + 3 ] = 1;
  585. }
  586. var texture = new THREE.DataTexture( a, WIDTH, WIDTH, THREE.RGBAFormat, THREE.FloatType );
  587. texture.minFilter = THREE.NearestFilter;
  588. texture.magFilter = THREE.NearestFilter;
  589. texture.needsUpdate = true;
  590. texture.flipY = false;
  591. return texture;
  592. }
  593. </script>
  594. </body>
  595. </html>