webgl_particles_dynamic.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles - dynamic - postprocessing</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. color: #fff;
  9. font-family:Monospace;
  10. font-size:13px;
  11. text-align:center;
  12. font-weight: bold;
  13. background-color: #000;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. color:#fff;
  19. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. }
  23. a { color: red; }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="container"></div>
  28. <div id="info">
  29. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl dynamic particles + postprocessing
  30. - models by <a href="http://sketchup.google.com/3dwarehouse/details?mid=2c6fd128fca34052adc5f5b98d513da1" target="_blank">Reallusion</a>
  31. <a href="http://sketchup.google.com/3dwarehouse/details?mid=f526cc4abf7cb68d76cab47c765b7255" target="_blank">iClone</a>,
  32. <a href="http://artist-3d.com/free_3d_models/dnm/model_disp.php?uid=1129" target="_blank">Troyano</a>
  33. </div>
  34. <script type="text/javascript" src="../build/Three.js"></script>
  35. <script type="text/javascript" src="js/Stats.js"></script>
  36. <script type="text/javascript" src="js/Detector.js"></script>
  37. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  38. <script type="text/javascript">
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var SCREEN_HEIGHT = window.innerHeight;
  41. var SCREEN_WIDTH = window.innerWidth;
  42. var container, stats;
  43. var camera, scene, renderer, mesh, directionalLight;
  44. var parent,
  45. meshes = [], clonemeshes = [];
  46. var p;
  47. var aloader, bloader;
  48. var total = 0, totaln = 0;
  49. var postprocessing1 = {};
  50. var postprocessing2 = {};
  51. init();
  52. animate();
  53. function init() {
  54. container = document.getElementById( 'container' );
  55. camera = new THREE.Camera( 20, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 50000 );
  56. camera.position.z = 7000;
  57. camera.position.y = 700;
  58. scene = new THREE.Scene();
  59. scene.fog = new THREE.FogExp2( 0x000104, 0.0000675 );
  60. directionalLight = new THREE.DirectionalLight( 0xffffff );
  61. directionalLight.position.set( 0, 0, 1 );
  62. directionalLight.position.normalize();
  63. scene.addLight( directionalLight );
  64. initPostprocessingBloom( postprocessing1 );
  65. initPostprocessingFocus( postprocessing2 );
  66. aloader = new THREE.JSONLoader( );
  67. bloader = new THREE.BinaryLoader( true );
  68. document.body.appendChild( bloader.statusDomElement );
  69. aloader.load( { model: "obj/terrain.js", callback: function( geometry ) {
  70. createMesh( geometry, scene, 16.8, -11000, -200, -5000, 0x00ff44, false );
  71. createMesh( geometry, scene, 16.8, 11000, -200, -15000, 0x00ff33, false );
  72. createMesh( geometry, scene, 16.8, 0, -200, -15000, 0x00ff33, false );
  73. createMesh( geometry, scene, 16.8, 0, -200, 15000, 0x00ff33, false );
  74. createMesh( geometry, scene, 16.8, 11000, -200, 15000, 0x00ff22, false );
  75. createMesh( geometry, scene, 16.8, -11000, -200, 5000, 0x00ff11, false );
  76. createMesh( geometry, scene, 16.8, 13000, -200, 5000, 0x00ff55, false );
  77. createMesh( geometry, scene, 16.8, 13000, -200, -5000, 0x00ff66, false );
  78. } } );
  79. bloader.load( { model: "obj/veyron/VeyronNoUv_bin.js", callback: function( geometry ) { createMesh( geometry, scene, 6.8, 2200, -200, -100, 0x0055ff, false ) } } );
  80. bloader.load( { model: "obj/female02/Female02_bin.js", callback: function( geometry ) {
  81. createMesh( geometry, scene, 4.05, -1000, -350, 0, 0xffdd44, true );
  82. createMesh( geometry, scene, 4.05, 0, -350, 0, 0xffffff, true );
  83. createMesh( geometry, scene, 4.05, 1000, -350, 400, 0xff4422, true );
  84. createMesh( geometry, scene, 4.05, 250, -350, 1500, 0xff9955, true );
  85. createMesh( geometry, scene, 4.05, 250, -350, 2500, 0xff77dd, true );
  86. } } );
  87. bloader.load( { model: "obj/male02/Male02_bin.js", callback: function( geometry ) {
  88. createMesh( geometry, scene, 4.05, -500, -350, 600, 0xff7744, true );
  89. createMesh( geometry, scene, 4.05, 500, -350, 0, 0xff5522, true );
  90. createMesh( geometry, scene, 4.05, -250, -350, 1500, 0xff9922, true );
  91. createMesh( geometry, scene, 4.05, -250, -350, -1500, 0xff99ff, true );
  92. } } );
  93. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1, antialias: false } );
  94. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  95. renderer.autoClear = false;
  96. renderer.sortObjects = false;
  97. container.appendChild( renderer.domElement );
  98. renderer.setClearColor( scene.fog.color, 1 );
  99. stats = new Stats();
  100. stats.domElement.style.position = 'absolute';
  101. stats.domElement.style.top = '0px';
  102. //container.appendChild( stats.domElement );
  103. parent = new THREE.Object3D();
  104. scene.addObject( parent );
  105. var grid = new THREE.ParticleSystem( new THREE.PlaneGeometry( 15000, 15000, 64, 64 ), new THREE.ParticleBasicMaterial( { color: 0xff0000, size: 10 } ) );
  106. grid.rotation.x = 1.57;
  107. grid.position.y = -400;
  108. parent.addChild( grid );
  109. totaln += 1;
  110. total += grid.geometry.vertices.length;
  111. }
  112. function createMesh( originalGeometry, scene, scale, x, y, z, color, dynamic ) {
  113. var i, c;
  114. var vertices = originalGeometry.vertices;
  115. var vl = vertices.length;
  116. var geometry = new THREE.Geometry();
  117. var vertices_tmp = [];
  118. for( i = 0; i < vl; i++ ) {
  119. p = vertices[ i ].position;
  120. geometry.vertices[ i ] = new THREE.Vertex( p.clone() );
  121. vertices_tmp[ i ] = [ p.x, p.y, p.z, 0, 0 ];
  122. }
  123. var clones = [
  124. [ 6000, 0, -4000 ],
  125. [ 5000, 0, 0 ],
  126. [ 1000, 0, 5000 ],
  127. [ 1000, 0, -5000 ],
  128. [ 4000, 0, 2000 ],
  129. [ -4000, 0, 1000 ],
  130. [ -5000, 0, -5000 ],
  131. [ 0, 0, 0 ]
  132. ];
  133. if ( dynamic ) {
  134. for( i = 0; i < clones.length; i++ ) {
  135. c = ( i < clones.length -1 ) ? 0x252525 : color;
  136. mesh = new THREE.ParticleSystem( geometry, new THREE.ParticleBasicMaterial( { size: 3, color: c } ) );
  137. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  138. mesh.position.x = x + clones[ i ][ 0 ];
  139. mesh.position.y = y + clones[ i ][ 1 ];
  140. mesh.position.z = z + clones[ i ][ 2 ];
  141. parent.addChild( mesh );
  142. clonemeshes.push( { mesh: mesh, speed: 0.5 + Math.random() } );
  143. }
  144. totaln += clones.length;
  145. total += clones.length * vl;
  146. } else {
  147. mesh = new THREE.ParticleSystem( geometry, new THREE.ParticleBasicMaterial( { size: 3, color: color } ) );
  148. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  149. mesh.position.x = x;
  150. mesh.position.y = y;
  151. mesh.position.z = z;
  152. parent.addChild( mesh );
  153. totaln += 1;
  154. total += vl;
  155. }
  156. bloader.statusDomElement.style.display = "none";
  157. meshes.push( { mesh: mesh, vertices: geometry.vertices, vertices_tmp: vertices_tmp, vl: vl,
  158. down: 0, up: 0, direction: 0, speed: 35, delay: Math.floor( 200 + 200 * Math.random() ),
  159. started: false, start: Math.floor( 100 + 200 * Math.random() ),
  160. dynamic: dynamic,
  161. bb: geometry.boundingBox } );
  162. console.log( total, totaln );
  163. }
  164. function initPostprocessingFocus( effect ) {
  165. effect.type = "focus";
  166. effect.scene = new THREE.Scene();
  167. effect.camera = new THREE.OrthoCamera( SCREEN_WIDTH / - 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, -10000, 10000 );
  168. effect.camera.position.z = 100;
  169. effect.texture = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat } );
  170. var heatUniforms = {
  171. "map": { type: "t", value:0, texture: effect.texture },
  172. "screenWidth": { type: "f", value: SCREEN_WIDTH },
  173. "screenHeight": { type: "f", value: SCREEN_HEIGHT },
  174. "sampleDistance": { type: "f", value: 0.94 },
  175. "waveFactor": { type: "f", value: 0.00125 }
  176. };
  177. effect.materialHeat = new THREE.MeshShaderMaterial( {
  178. uniforms: heatUniforms,
  179. vertexShader: [
  180. "varying vec2 vUv;",
  181. "void main() {",
  182. "vUv = vec2( uv.x, 1.0 - uv.y );",
  183. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  184. "}"
  185. ].join("\n"),
  186. fragmentShader: [
  187. "uniform float screenWidth;",
  188. "uniform float screenHeight;",
  189. "uniform float sampleDistance;",
  190. "uniform float waveFactor;",
  191. "uniform sampler2D map;",
  192. "varying vec2 vUv;",
  193. "void main() {",
  194. "vec4 color, org, tmp, add;",
  195. "float sample_dist, f;",
  196. "vec2 vin;",
  197. "vec2 uv = vUv;",
  198. "add += color = org = texture2D( map, uv );",
  199. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  200. "sample_dist = dot( vin, vin ) * 2.0;",
  201. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  202. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2(f);",
  203. "add += tmp = texture2D( map, uv + vec2(0.111964, 0.993712) * sampleSize );",
  204. "if( tmp.b < color.b ) color = tmp;",
  205. "add += tmp = texture2D( map, uv + vec2(0.846724, 0.532032) * sampleSize );",
  206. "if( tmp.b < color.b ) color = tmp;",
  207. "add += tmp = texture2D( map, uv + vec2(0.943883, -0.330279) * sampleSize );",
  208. "if( tmp.b < color.b ) color = tmp;",
  209. "add += tmp = texture2D( map, uv + vec2(0.330279, -0.943883) * sampleSize );",
  210. "if( tmp.b < color.b ) color = tmp;",
  211. "add += tmp = texture2D( map, uv + vec2(-0.532032, -0.846724) * sampleSize );",
  212. "if( tmp.b < color.b ) color = tmp;",
  213. "add += tmp = texture2D( map, uv + vec2(-0.993712, -0.111964) * sampleSize );",
  214. "if( tmp.b < color.b ) color = tmp;",
  215. "add += tmp = texture2D( map, uv + vec2(-0.707107, 0.707107) * sampleSize );",
  216. "if( tmp.b < color.b ) color = tmp;",
  217. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  218. "color = color + ( add / vec4(8.0) - color ) * ( vec4(1.0) - vec4(sample_dist * 0.5) );",
  219. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  220. "}"
  221. ].join("\n")
  222. } );
  223. effect.quad = new THREE.Mesh( new THREE.PlaneGeometry( SCREEN_WIDTH, SCREEN_HEIGHT ), effect.materialHeat );
  224. effect.quad.position.z = -500;
  225. effect.scene.addObject( effect.quad );
  226. }
  227. function initPostprocessingBloom( effect ) {
  228. effect.type = "bloomnoise";
  229. effect.scene = new THREE.Scene();
  230. effect.camera = new THREE.OrthoCamera( SCREEN_WIDTH / - 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, -10000, 10000 );
  231. effect.camera.position.z = 100;
  232. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  233. effect.rtTexture1 = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, pars );
  234. effect.rtTexture2 = new THREE.WebGLRenderTarget( 256, 512, pars );
  235. effect.rtTexture3 = new THREE.WebGLRenderTarget( 512, 256, pars );
  236. var screen_shader = THREE.ShaderUtils.lib["screen"];
  237. var screen_uniforms = THREE.UniformsUtils.clone( screen_shader.uniforms );
  238. screen_uniforms["tDiffuse"].texture = effect.rtTexture3;
  239. screen_uniforms["opacity"].value = 0.75;
  240. effect.materialScreen = new THREE.MeshShaderMaterial( {
  241. uniforms: screen_uniforms,
  242. vertexShader: screen_shader.vertexShader,
  243. fragmentShader: screen_shader.fragmentShader,
  244. blending: THREE.AdditiveBlending,
  245. transparent: true
  246. } );
  247. var convolution_shader = THREE.ShaderUtils.lib["convolution"];
  248. var convolution_uniforms = THREE.UniformsUtils.clone( convolution_shader.uniforms );
  249. effect.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  250. effect.blury = new THREE.Vector2( 0.0, 0.001953125 );
  251. convolution_uniforms["tDiffuse"].texture = effect.rtTexture1;
  252. convolution_uniforms["uImageIncrement"].value = effect.blurx;
  253. convolution_uniforms["cKernel"].value = THREE.ShaderUtils.buildKernel( 4.0 );
  254. effect.materialConvolution = new THREE.MeshShaderMaterial( {
  255. uniforms: convolution_uniforms,
  256. vertexShader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertexShader,
  257. fragmentShader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragmentShader
  258. } );
  259. var film_shader = THREE.ShaderUtils.lib["film"];
  260. var film_uniforms = THREE.UniformsUtils.clone( film_shader.uniforms );
  261. film_uniforms["tDiffuse"].texture = effect.rtTexture1;
  262. effect.materialFilm = new THREE.MeshShaderMaterial( { uniforms: film_uniforms, vertexShader: film_shader.vertexShader, fragmentShader: film_shader.fragmentShader } );
  263. effect.materialFilm.uniforms.grayscale.value = 0;
  264. effect.materialFilm.uniforms.nIntensity.value = 0.5;
  265. effect.materialFilm.uniforms.sIntensity.value = 0.5;
  266. effect.materialFilm.uniforms.sCount.value = 1448;
  267. effect.quad = new THREE.Mesh( new THREE.PlaneGeometry( SCREEN_WIDTH, SCREEN_HEIGHT ), effect.materialConvolution );
  268. effect.quad.position.z = -500;
  269. effect.scene.addObject( effect.quad );
  270. }
  271. var j, jl, cm, data, vertices, vertices_tmp, vl, d, vt,
  272. time, oldTime, delta;
  273. function animate() {
  274. requestAnimationFrame( animate );
  275. render();
  276. //stats.update();
  277. }
  278. function render() {
  279. if ( ! oldTime ) {
  280. oldTime = new Date().getTime();
  281. }
  282. time = new Date().getTime();
  283. delta = 0.01 * ( time - oldTime );
  284. oldTime = time;
  285. delta = delta < 2 ? delta : 2;
  286. parent.rotation.y += -0.02 * delta;
  287. for( j = 0, jl = clonemeshes.length; j < jl; j++ ) {
  288. cm = clonemeshes[ j ];
  289. cm.mesh.rotation.y += -0.1 * delta * cm.speed;
  290. }
  291. for( j = 0, jl = meshes.length; j < jl; j++ ) {
  292. data = meshes[ j ];
  293. mesh = data.mesh;
  294. vertices = data.vertices;
  295. vertices_tmp = data.vertices_tmp;
  296. vl = data.vl;
  297. if ( ! data.dynamic ) continue;
  298. if ( data.start > 0 ) {
  299. data.start -= 1;
  300. } else {
  301. if ( !data.started ) {
  302. data.direction = -1;
  303. data.started = true;
  304. }
  305. }
  306. for ( i = 0; i < vl; i ++ ) {
  307. p = vertices[ i ].position;
  308. vt = vertices_tmp[ i ];
  309. // falling down
  310. if ( data.direction < 0 ) {
  311. // var d = Math.abs( p.x - vertices_tmp[ i ][ 0 ] ) + Math.abs( p.y - vertices_tmp[ i ][ 1 ] ) + Math.abs( p.z - vertices_tmp[ i ][ 2 ] );
  312. // if ( d < 200 ) {
  313. if ( p.y > 0 ) {
  314. // p.y += data.direction * data.speed * delta;
  315. p.x += 1.5 * ( 0.50 - Math.random() ) * data.speed * delta;
  316. p.y += 3.0 * ( 0.25 - Math.random() ) * data.speed * delta;
  317. p.z += 1.5 * ( 0.50 - Math.random() ) * data.speed * delta;
  318. } else {
  319. if ( ! vt[ 3 ] ) {
  320. vt[ 3 ] = 1;
  321. data.down += 1;
  322. }
  323. }
  324. }
  325. // rising up
  326. if ( data.direction > 0 ) {
  327. //if ( p.y < vertices_tmp[ i ][ 1 ] ) {
  328. // p.y += data.direction * data.speed * delta;
  329. d = Math.abs( p.x - vt[ 0 ] ) + Math.abs( p.y - vt[ 1 ] ) + Math.abs( p.z - vt[ 2 ] );
  330. if ( d > 1 ) {
  331. p.x += - ( p.x - vt[ 0 ] ) / d * data.speed * delta * ( 0.85 - Math.random() );
  332. p.y += - ( p.y - vt[ 1 ] ) / d * data.speed * delta * ( 1 + Math.random() );
  333. p.z += - ( p.z - vt[ 2 ] ) / d * data.speed * delta * ( 0.85 - Math.random() );
  334. } else {
  335. if ( ! vt[ 4 ] ) {
  336. vt[ 4 ] = 1;
  337. data.up += 1;
  338. }
  339. }
  340. }
  341. }
  342. // all down
  343. if ( data.down == vl ) {
  344. if ( data.delay == 0 ) {
  345. data.direction = 1;
  346. data.speed = 10;
  347. data.down = 0;
  348. data.delay = 320;
  349. for ( i = 0; i < vl; i ++ ) {
  350. vertices_tmp[ i ][ 3 ] = 0;
  351. }
  352. } else {
  353. data.delay -= 1;
  354. }
  355. }
  356. // all up
  357. if ( data.up == vl ) {
  358. if ( data.delay == 0 ) {
  359. data.direction = -1;
  360. data.speed = 35;
  361. data.up = 0;
  362. data.delay = 120;
  363. for ( i = 0; i < vl; i ++ ) {
  364. vertices_tmp[ i ][ 4 ] = 0;
  365. }
  366. } else {
  367. data.delay -= 1;
  368. }
  369. }
  370. mesh.geometry.__dirtyVertices = true;
  371. }
  372. renderer.clear();
  373. // BLOOM
  374. // Render scene into texture
  375. renderer.render( scene, camera, postprocessing1.rtTexture1, true );
  376. // Render quad with blured scene into texture (convolution pass 1)
  377. postprocessing1.quad.materials[ 0 ] = postprocessing1.materialConvolution;
  378. postprocessing1.materialConvolution.uniforms.tDiffuse.texture = postprocessing1.rtTexture1;
  379. postprocessing1.materialConvolution.uniforms.uImageIncrement.value = postprocessing1.blurx;
  380. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing1.rtTexture2, true );
  381. // Render quad with blured scene into texture (convolution pass 2)
  382. postprocessing1.materialConvolution.uniforms.tDiffuse.texture = postprocessing1.rtTexture2;
  383. postprocessing1.materialConvolution.uniforms.uImageIncrement.value = postprocessing1.blury;
  384. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing1.rtTexture3, true );
  385. // Render original scene with superimposed blur to texture
  386. postprocessing1.quad.materials[ 0 ] = postprocessing1.materialScreen;
  387. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing1.rtTexture1, false );
  388. // NOISE + SCANLINES
  389. postprocessing1.materialFilm.uniforms.time.value += 0.01;
  390. postprocessing1.quad.materials[ 0 ] = postprocessing1.materialFilm;
  391. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing2.texture, true );
  392. // FOCUS
  393. renderer.render( postprocessing2.scene, postprocessing2.camera );
  394. }
  395. </script>
  396. </body>
  397. </html>