webgl_particles_shapes.html 17 KB

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