webgl_particles_shapes.html 16 KB

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