webgl_particles_shapes.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles - shapes</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. font-family: Monospace;
  10. color: #aaa;
  11. background-color: #000;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script src="../build/three.min.js"></script>
  19. <script src="js/libs/stats.min.js"></script>
  20. <script src="js/libs/tween.min.js"></script>
  21. <script src="js/Sparks.js"></script>
  22. <script src="js/shaders/CopyShader.js"></script>
  23. <script src="js/shaders/FilmShader.js"></script>
  24. <script src="js/shaders/FocusShader.js"></script>
  25. <script src="js/shaders/HorizontalBlurShader.js"></script>
  26. <script src="js/shaders/TriangleBlurShader.js"></script>
  27. <script src="js/shaders/VerticalBlurShader.js"></script>
  28. <script src="js/postprocessing/EffectComposer.js"></script>
  29. <script src="js/postprocessing/RenderPass.js"></script>
  30. <script src="js/postprocessing/ShaderPass.js"></script>
  31. <script src="js/postprocessing/MaskPass.js"></script>
  32. <script src="js/postprocessing/BloomPass.js"></script>
  33. <script src="js/postprocessing/FilmPass.js"></script>
  34. <!-- load the font file from canvas-text -->
  35. <script src="fonts/helvetiker_regular.typeface.js"></script>
  36. <script type="x-shader/x-vertex" id="vertexshader">
  37. attribute float size;
  38. attribute vec3 pcolor;
  39. varying vec3 vColor;
  40. void main() {
  41. vColor = pcolor;
  42. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  43. gl_PointSize = size * ( 200.0 / length( mvPosition.xyz ) );
  44. gl_Position = projectionMatrix * mvPosition;
  45. }
  46. </script>
  47. <script type="x-shader/x-fragment" id="fragmentshader">
  48. uniform sampler2D texture;
  49. varying vec3 vColor;
  50. void main() {
  51. vec4 outColor = texture2D( texture, gl_PointCoord );
  52. gl_FragColor = outColor * vec4( vColor, 1.0 );
  53. }
  54. </script>
  55. <script>
  56. var container, stats;
  57. var camera, scene, renderer;
  58. var text, plane;
  59. var speed = 50;
  60. var pointLight;
  61. var targetRotation = 0;
  62. var targetRotationOnMouseDown = 0;
  63. var mouseX = 0;
  64. var mouseXOnMouseDown = 0;
  65. var windowHalfX = window.innerWidth / 2;
  66. var windowHalfY = window.innerHeight / 2;
  67. var delta = 1, clock = new THREE.Clock();
  68. var heartShape, particleCloud, sparksEmitter, emitterPos;
  69. var _rotation = 0;
  70. var timeOnShapePath = 0;
  71. var composer;
  72. var effectBlurX, effectBlurY, hblur, vblur;
  73. init();
  74. animate();
  75. function init() {
  76. container = document.createElement( 'div' );
  77. document.body.appendChild( container );
  78. var info = document.createElement( 'div' );
  79. info.style.position = 'absolute';
  80. info.style.top = '10px';
  81. info.style.width = '100%';
  82. info.style.textAlign = 'center';
  83. info.innerHTML = 'Three.js - simple particle systems with shapes by <a href="http://www.lab4games.net/zz85/blog">zz85</a><br>Move your mouse. Click to pause/resume.';
  84. container.appendChild( info );
  85. // CAMERA
  86. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 2000 );
  87. camera.position.set( 0, 150, 400 );
  88. // SCENE
  89. scene = new THREE.Scene();
  90. // LIGHTS
  91. var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
  92. directionalLight.position.set( 0, -1, 1 );
  93. directionalLight.position.normalize();
  94. scene.add( directionalLight );
  95. pointLight = new THREE.PointLight( 0xffffff, 2, 300 );
  96. pointLight.position.set( 0, 0, 0 );
  97. scene.add( pointLight );
  98. // TEXT
  99. var theText = "THREE.JS";
  100. // Get text from hash
  101. var hash = document.location.hash.substr( 1 );
  102. if ( hash.length !== 0 ) {
  103. theText = hash;
  104. }
  105. var textMaterialFront = new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, opacity: 0.95 } );
  106. var textMaterialSide = new THREE.MeshLambertMaterial( { color: 0xffffff } );
  107. var text3d = new THREE.TextGeometry( theText, {
  108. size: 70,
  109. height: 25,
  110. curveSegments: 4,
  111. font: "helvetiker",
  112. bevelEnabled: true,
  113. bevelThickness: 2,
  114. bevelSize: 2,
  115. material: 0,
  116. extrudeMaterial: 1
  117. });
  118. text3d.materials = [ textMaterialFront, textMaterialSide ];
  119. text3d.computeVertexNormals();
  120. text3d.computeBoundingBox();
  121. var centerOffset = -0.5 * ( text3d.boundingBox.max.x - text3d.boundingBox.min.x );
  122. text = new THREE.Mesh( text3d, new THREE.MeshFaceMaterial() );
  123. // Potentially, we can extract the vertices or faces of the text to generate particles too.
  124. // Geo > Vertices > Position
  125. text.position.x = centerOffset;
  126. text.position.y = 130;
  127. text.position.z = -50;
  128. text.rotation.x = 0;
  129. text.rotation.y = Math.PI * 2;
  130. parent = new THREE.Object3D();
  131. parent.add( text );
  132. scene.add( parent );
  133. ///// Create particle objects for Three.js
  134. var particlesLength = 70000;
  135. var particles = new THREE.Geometry();
  136. function newpos( x, y, z ) {
  137. return new THREE.Vector3( x, y, z );
  138. }
  139. var Pool = {
  140. __pools: [],
  141. // Get a new Vector
  142. get: function() {
  143. if ( this.__pools.length > 0 ) {
  144. return this.__pools.pop();
  145. }
  146. console.log( "pool ran out!" )
  147. return null;
  148. },
  149. // Release a vector back into the pool
  150. add: function( v ) {
  151. this.__pools.push( v );
  152. }
  153. };
  154. for ( i = 0; i < particlesLength; i ++ ) {
  155. particles.vertices.push( newpos( Math.random() * 200 - 100, Math.random() * 100 + 150, Math.random() * 50 ) );
  156. Pool.add( i );
  157. }
  158. // Create pools of vectors
  159. attributes = {
  160. size: { type: 'f', value: [] },
  161. pcolor: { type: 'c', value: [] }
  162. };
  163. var sprite = generateSprite() ;
  164. texture = new THREE.Texture( sprite );
  165. texture.needsUpdate = true;
  166. uniforms = {
  167. texture: { type: "t", value: texture }
  168. };
  169. // PARAMETERS
  170. // Steadycounter
  171. // Life
  172. // Opacity
  173. // Hue Speed
  174. // Movement Speed
  175. function generateSprite() {
  176. var canvas = document.createElement( 'canvas' );
  177. canvas.width = 128;
  178. canvas.height = 128;
  179. var context = canvas.getContext( '2d' );
  180. // Just a square, doesnt work too bad with blur pp.
  181. // context.fillStyle = "white";
  182. // context.strokeStyle = "white";
  183. // context.fillRect(0, 0, 63, 63) ;
  184. // Heart Shapes are not too pretty here
  185. // var x = 4, y = 0;
  186. // context.save();
  187. // context.scale(8, 8); // Scale so canvas render can redraw within bounds
  188. // context.beginPath();
  189. // context.bezierCurveTo( x + 2.5, y + 2.5, x + 2.0, y, x, y );
  190. // context.bezierCurveTo( x - 3.0, y, x - 3.0, y + 3.5,x - 3.0,y + 3.5 );
  191. // context.bezierCurveTo( x - 3.0, y + 5.5, x - 1.0, y + 7.7, x + 2.5, y + 9.5 );
  192. // context.bezierCurveTo( x + 6.0, y + 7.7, x + 8.0, y + 5.5, x + 8.0, y + 3.5 );
  193. // context.bezierCurveTo( x + 8.0, y + 3.5, x + 8.0, y, x + 5.0, y );
  194. // context.bezierCurveTo( x + 3.5, y, x + 2.5, y + 2.5, x + 2.5, y + 2.5 );
  195. // context.closePath();
  196. context.beginPath();
  197. context.arc( 64, 64, 60, 0, Math.PI * 2, false) ;
  198. context.closePath();
  199. context.lineWidth = 0.5; //0.05
  200. context.stroke();
  201. context.restore();
  202. var gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  203. gradient.addColorStop( 0, 'rgba(255,255,255,1)' );
  204. gradient.addColorStop( 0.2, 'rgba(255,255,255,1)' );
  205. gradient.addColorStop( 0.4, 'rgba(200,200,200,1)' );
  206. gradient.addColorStop( 1, 'rgba(0,0,0,1)' );
  207. context.fillStyle = gradient;
  208. context.fill();
  209. return canvas;
  210. }
  211. var shaderMaterial = new THREE.ShaderMaterial( {
  212. uniforms: uniforms,
  213. attributes: attributes,
  214. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  215. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  216. blending: THREE.AdditiveBlending,
  217. depthWrite: false,
  218. transparent: true
  219. });
  220. particleCloud = new THREE.ParticleSystem( particles, shaderMaterial );
  221. particleCloud.dynamic = true;
  222. //particleCloud.sortParticles = true;
  223. var vertices = particleCloud.geometry.vertices;
  224. var values_size = attributes.size.value;
  225. var values_color = attributes.pcolor.value;
  226. for( var v = 0; v < vertices.length; v ++ ) {
  227. values_size[ v ] = 50;
  228. values_color[ v ] = new THREE.Color( 0xffffff );
  229. values_color[ v ].setHSV( 0, 0, 0 );
  230. particles.vertices[ v ].set( Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY );
  231. }
  232. parent.add( particleCloud );
  233. particleCloud.y = 800;
  234. // Create Particle Systems
  235. // EMITTER STUFF
  236. // Heart
  237. var x = 0, y = 0;
  238. heartShape = new THREE.Shape();
  239. heartShape.moveTo( x + 25, y + 25 );
  240. heartShape.bezierCurveTo( x + 25, y + 25, x + 20, y, x, y );
  241. heartShape.bezierCurveTo( x - 30, y, x - 30, y + 35,x - 30,y + 35 );
  242. heartShape.bezierCurveTo( x - 30, y + 55, x - 10, y + 77, x + 25, y + 95 );
  243. heartShape.bezierCurveTo( x + 60, y + 77, x + 80, y + 55, x + 80, y + 35 );
  244. heartShape.bezierCurveTo( x + 80, y + 35, x + 80, y, x + 50, y );
  245. heartShape.bezierCurveTo( x + 35, y, x + 25, y + 25, x + 25, y + 25 );
  246. var hue = 0;
  247. var setTargetParticle = function() {
  248. var target = Pool.get();
  249. values_size[ target ] = Math.random() * 200 + 100;
  250. return target;
  251. };
  252. var onParticleCreated = function( p ) {
  253. var position = p.position;
  254. p.target.position = position;
  255. var target = p.target;
  256. if ( target ) {
  257. //console.log(target,particles.vertices[target]);
  258. //values_size[target]
  259. //values_color[target]
  260. hue += 0.0003 * delta;
  261. if ( hue > 1 ) hue -= 1;
  262. // TODO Create a PointOnShape Action/Zone in the particle engine
  263. timeOnShapePath += 0.00035 * delta;
  264. if ( timeOnShapePath > 1 ) timeOnShapePath -= 1;
  265. var pointOnShape = heartShape.getPointAt( timeOnShapePath );
  266. emitterpos.x = pointOnShape.x * 5 - 100;
  267. emitterpos.y = -pointOnShape.y * 5 + 400;
  268. //pointLight.position.copy( emitterpos );
  269. pointLight.position.x = emitterpos.x;
  270. pointLight.position.y = emitterpos.y;
  271. pointLight.position.z = 100;
  272. particles.vertices[ target ] = p.position;
  273. values_color[ target ].setHSV( hue, 0.8, 0.15 );
  274. pointLight.color.setHSV( hue, 0.8, 0.95 );
  275. };
  276. };
  277. var onParticleDead = function( particle ) {
  278. var target = particle.target;
  279. if ( target ) {
  280. // Hide the particle
  281. values_color[ target ].setHSV( 0, 0, 0 );
  282. particles.vertices[ target ].set( Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY, Number.POSITIVE_INFINITY );
  283. // Mark particle system as available by returning to pool
  284. Pool.add( particle.target );
  285. }
  286. };
  287. var engineLoopUpdate = function() {
  288. };
  289. sparksEmitter = new SPARKS.Emitter( new SPARKS.SteadyCounter( 500 ) );
  290. emitterpos = new THREE.Vector3( 0, 0, 0 );
  291. sparksEmitter.addInitializer( new SPARKS.Position( new SPARKS.PointZone( emitterpos ) ) );
  292. sparksEmitter.addInitializer( new SPARKS.Lifetime( 1, 15 ));
  293. sparksEmitter.addInitializer( new SPARKS.Target( null, setTargetParticle ) );
  294. sparksEmitter.addInitializer( new SPARKS.Velocity( new SPARKS.PointZone( new THREE.Vector3( 0, -5, 1 ) ) ) );
  295. // TOTRY Set velocity to move away from centroid
  296. sparksEmitter.addAction( new SPARKS.Age() );
  297. sparksEmitter.addAction( new SPARKS.Accelerate( 0, 0, -50 ) );
  298. sparksEmitter.addAction( new SPARKS.Move() );
  299. sparksEmitter.addAction( new SPARKS.RandomDrift( 90, 100, 2000 ) );
  300. sparksEmitter.addCallback( "created", onParticleCreated );
  301. sparksEmitter.addCallback( "dead", onParticleDead );
  302. sparksEmitter.start();
  303. //sparksEmitter.addCallback("loopUpdated", engineLoopUpdate);
  304. //sparksEmitter.addCallback("updated", function(p) {
  305. // var target = particle.target;
  306. // if (target) {
  307. // // update energy properties
  308. // //values_size[target] = Math.random()*100;
  309. // }
  310. //});
  311. //
  312. // End Particles
  313. renderer = new THREE.WebGLRenderer();
  314. renderer.setSize( window.innerWidth, window.innerHeight );
  315. renderer.setClearColorHex( 0x000000, 1 );
  316. container.appendChild( renderer.domElement );
  317. stats = new Stats();
  318. stats.domElement.style.position = 'absolute';
  319. stats.domElement.style.top = '0px';
  320. container.appendChild( stats.domElement );
  321. // POST PROCESSING
  322. var effectFocus = new THREE.ShaderPass( THREE.FocusShader );
  323. var effectCopy = new THREE.ShaderPass( THREE.CopyShader );
  324. effectFilm = new THREE.FilmPass( 0.5, 0.25, 2048, false );
  325. var shaderBlur = THREE.TriangleBlurShader;
  326. effectBlurX = new THREE.ShaderPass( shaderBlur, 'texture' );
  327. effectBlurY = new THREE.ShaderPass( shaderBlur, 'texture' );
  328. var radius = 15;
  329. var blurAmountX = radius / window.innerWidth;
  330. var blurAmountY = radius / window.innerHeight;
  331. hblur = new THREE.ShaderPass( THREE.HorizontalBlurShader );
  332. vblur = new THREE.ShaderPass( THREE.VerticalBlurShader);
  333. hblur.uniforms[ 'h' ].value = 1 / window.innerWidth;
  334. vblur.uniforms[ 'v' ].value = 1 / window.innerHeight;
  335. effectBlurX.uniforms[ 'delta' ].value = new THREE.Vector2( blurAmountX, 0 );
  336. effectBlurY.uniforms[ 'delta' ].value = new THREE.Vector2( 0, blurAmountY );
  337. effectFocus.uniforms[ 'sampleDistance' ].value = 0.99; //0.94
  338. effectFocus.uniforms[ 'waveFactor' ].value = 0.003; //0.00125
  339. var renderScene = new THREE.RenderPass( scene, camera );
  340. composer = new THREE.EffectComposer( renderer );
  341. composer.addPass( renderScene );
  342. composer.addPass( hblur );
  343. composer.addPass( vblur );
  344. //composer.addPass( effectBlurX );
  345. //composer.addPass( effectBlurY );
  346. //composer.addPass( effectCopy );
  347. //composer.addPass( effectFocus );
  348. //composer.addPass( effectFilm );
  349. vblur.renderToScreen = true;
  350. effectBlurY.renderToScreen = true;
  351. effectFocus.renderToScreen = true;
  352. effectCopy.renderToScreen = true;
  353. effectFilm.renderToScreen = true;
  354. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  355. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  356. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  357. //
  358. window.addEventListener( 'resize', onWindowResize, false );
  359. }
  360. function onWindowResize() {
  361. windowHalfX = window.innerWidth / 2;
  362. windowHalfY = window.innerHeight / 2;
  363. camera.aspect = window.innerWidth / window.innerHeight;
  364. camera.updateProjectionMatrix();
  365. renderer.setSize( window.innerWidth, window.innerHeight );
  366. //
  367. hblur.uniforms[ 'h' ].value = 1 / window.innerWidth;
  368. vblur.uniforms[ 'v' ].value = 1 / window.innerHeight;
  369. var radius = 15;
  370. var blurAmountX = radius / window.innerWidth;
  371. var blurAmountY = radius / window.innerHeight;
  372. effectBlurX.uniforms[ 'delta' ].value = new THREE.Vector2( blurAmountX, 0 );
  373. effectBlurY.uniforms[ 'delta' ].value = new THREE.Vector2( 0, blurAmountY );
  374. composer.reset();
  375. }
  376. //
  377. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  378. function onDocumentMouseDown( event ) {
  379. event.preventDefault();
  380. mouseXOnMouseDown = event.clientX - windowHalfX;
  381. targetRotationOnMouseDown = targetRotation;
  382. if ( sparksEmitter.isRunning() ) {
  383. sparksEmitter.stop();
  384. } else {
  385. sparksEmitter.start();
  386. }
  387. }
  388. function onDocumentMouseMove( event ) {
  389. mouseX = event.clientX - windowHalfX;
  390. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
  391. }
  392. function onDocumentTouchStart( event ) {
  393. if ( event.touches.length === 1 ) {
  394. event.preventDefault();
  395. mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
  396. targetRotationOnMouseDown = targetRotation;
  397. }
  398. }
  399. function onDocumentTouchMove( event ) {
  400. if ( event.touches.length === 1 ) {
  401. event.preventDefault();
  402. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  403. targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
  404. }
  405. }
  406. //
  407. function animate() {
  408. requestAnimationFrame( animate );
  409. render();
  410. stats.update();
  411. }
  412. function render() {
  413. delta = speed * clock.getDelta();
  414. particleCloud.geometry.verticesNeedUpdate = true;
  415. attributes.size.needsUpdate = true;
  416. attributes.pcolor.needsUpdate = true;
  417. // Pretty cool effect if you enable this
  418. //particleCloud.rotation.y += 0.05;
  419. parent.rotation.y += ( targetRotation - parent.rotation.y ) * 0.05;
  420. renderer.clear();
  421. //renderer.render( scene, camera );
  422. composer.render( 0.1 );
  423. }
  424. </script>
  425. </body>
  426. </html>