webgl_particles_dynamic.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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.Plane( 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.Camera();
  168. effect.camera.projectionMatrix = THREE.Matrix4.makeOrtho( SCREEN_WIDTH / - 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, -10000, 10000 );
  169. effect.camera.position.z = 100;
  170. effect.texture = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, { minFilter: THREE.LinearFilter, magFilter: THREE.NearestFilter, format: THREE.RGBFormat } );
  171. var heatUniforms = {
  172. "map": { type: "t", value:0, texture: effect.texture },
  173. "screenWidth": { type: "f", value: SCREEN_WIDTH },
  174. "screenHeight": { type: "f", value: SCREEN_HEIGHT },
  175. "sampleDistance": { type: "f", value: 0.94 },
  176. "waveFactor": { type: "f", value: 0.00125 }
  177. };
  178. effect.materialHeat = new THREE.MeshShaderMaterial( {
  179. uniforms: heatUniforms,
  180. vertexShader: [
  181. "varying vec2 vUv;",
  182. "void main() {",
  183. "vUv = vec2( uv.x, 1.0 - uv.y );",
  184. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  185. "}"
  186. ].join("\n"),
  187. fragmentShader: [
  188. "uniform float screenWidth;",
  189. "uniform float screenHeight;",
  190. "uniform float sampleDistance;",
  191. "uniform float waveFactor;",
  192. "uniform sampler2D map;",
  193. "varying vec2 vUv;",
  194. "void main() {",
  195. "vec4 color, org, tmp, add;",
  196. "float sample_dist, f;",
  197. "vec2 vin;",
  198. "vec2 uv = vUv;",
  199. "add += color = org = texture2D( map, uv );",
  200. "vin = ( uv - vec2( 0.5 ) ) * vec2( 1.4 );",
  201. "sample_dist = dot( vin, vin ) * 2.0;",
  202. "f = ( waveFactor * 100.0 + sample_dist ) * sampleDistance * 4.0;",
  203. "vec2 sampleSize = vec2( 1.0 / screenWidth, 1.0 / screenHeight ) * vec2(f);",
  204. "add += tmp = texture2D( map, uv + vec2(0.111964, 0.993712) * sampleSize );",
  205. "if( tmp.b < color.b ) color = tmp;",
  206. "add += tmp = texture2D( map, uv + vec2(0.846724, 0.532032) * sampleSize );",
  207. "if( tmp.b < color.b ) color = tmp;",
  208. "add += tmp = texture2D( map, uv + vec2(0.943883, -0.330279) * sampleSize );",
  209. "if( tmp.b < color.b ) color = tmp;",
  210. "add += tmp = texture2D( map, uv + vec2(0.330279, -0.943883) * sampleSize );",
  211. "if( tmp.b < color.b ) color = tmp;",
  212. "add += tmp = texture2D( map, uv + vec2(-0.532032, -0.846724) * sampleSize );",
  213. "if( tmp.b < color.b ) color = tmp;",
  214. "add += tmp = texture2D( map, uv + vec2(-0.993712, -0.111964) * sampleSize );",
  215. "if( tmp.b < color.b ) color = tmp;",
  216. "add += tmp = texture2D( map, uv + vec2(-0.707107, 0.707107) * sampleSize );",
  217. "if( tmp.b < color.b ) color = tmp;",
  218. "color = color * vec4( 2.0 ) - ( add / vec4( 8.0 ) );",
  219. "color = color + ( add / vec4(8.0) - color ) * ( vec4(1.0) - vec4(sample_dist * 0.5) );",
  220. "gl_FragColor = vec4( color.rgb * color.rgb * vec3( 0.95 ) + color.rgb, 1.0 );",
  221. "}"
  222. ].join("\n")
  223. } );
  224. effect.quad = new THREE.Mesh( new THREE.Plane( SCREEN_WIDTH, SCREEN_HEIGHT ), effect.materialHeat );
  225. effect.quad.position.z = -500;
  226. effect.scene.addObject( effect.quad );
  227. }
  228. function initPostprocessingBloom( effect ) {
  229. effect.type = "bloomnoise";
  230. effect.scene = new THREE.Scene();
  231. effect.camera = new THREE.Camera();
  232. effect.camera.projectionMatrix = THREE.Matrix4.makeOrtho( SCREEN_WIDTH / - 2, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / - 2, -10000, 10000 );
  233. effect.camera.position.z = 100;
  234. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBFormat };
  235. effect.rtTexture1 = new THREE.WebGLRenderTarget( SCREEN_WIDTH, SCREEN_HEIGHT, pars );
  236. effect.rtTexture2 = new THREE.WebGLRenderTarget( 256, 512, pars );
  237. effect.rtTexture3 = new THREE.WebGLRenderTarget( 512, 256, pars );
  238. var screen_shader = THREE.ShaderUtils.lib["screen"];
  239. var screen_uniforms = THREE.UniformsUtils.clone( screen_shader.uniforms );
  240. screen_uniforms["tDiffuse"].texture = effect.rtTexture3;
  241. screen_uniforms["opacity"].value = 0.75;
  242. effect.materialScreen = new THREE.MeshShaderMaterial( {
  243. uniforms: screen_uniforms,
  244. vertexShader: screen_shader.vertexShader,
  245. fragmentShader: screen_shader.fragmentShader,
  246. blending: THREE.AdditiveBlending,
  247. transparent: true
  248. } );
  249. var convolution_shader = THREE.ShaderUtils.lib["convolution"];
  250. var convolution_uniforms = THREE.UniformsUtils.clone( convolution_shader.uniforms );
  251. effect.blurx = new THREE.Vector2( 0.001953125, 0.0 ),
  252. effect.blury = new THREE.Vector2( 0.0, 0.001953125 );
  253. convolution_uniforms["tDiffuse"].texture = effect.rtTexture1;
  254. convolution_uniforms["uImageIncrement"].value = effect.blurx;
  255. convolution_uniforms["cKernel"].value = THREE.ShaderUtils.buildKernel( 4.0 );
  256. effect.materialConvolution = new THREE.MeshShaderMaterial( {
  257. uniforms: convolution_uniforms,
  258. vertexShader: "#define KERNEL_SIZE 25.0\n" + convolution_shader.vertexShader,
  259. fragmentShader: "#define KERNEL_SIZE 25\n" + convolution_shader.fragmentShader
  260. } );
  261. var film_shader = THREE.ShaderUtils.lib["film"];
  262. var film_uniforms = THREE.UniformsUtils.clone( film_shader.uniforms );
  263. film_uniforms["tDiffuse"].texture = effect.rtTexture1;
  264. effect.materialFilm = new THREE.MeshShaderMaterial( { uniforms: film_uniforms, vertexShader: film_shader.vertexShader, fragmentShader: film_shader.fragmentShader } );
  265. effect.materialFilm.uniforms.grayscale.value = 0;
  266. effect.materialFilm.uniforms.nIntensity.value = 0.5;
  267. effect.materialFilm.uniforms.sIntensity.value = 0.5;
  268. effect.materialFilm.uniforms.sCount.value = 1448;
  269. effect.quad = new THREE.Mesh( new THREE.Plane( SCREEN_WIDTH, SCREEN_HEIGHT ), effect.materialConvolution );
  270. effect.quad.position.z = -500;
  271. effect.scene.addObject( effect.quad );
  272. }
  273. var j, jl, cm, data, vertices, vertices_tmp, vl, d, vt,
  274. time, oldTime, delta;
  275. function animate() {
  276. requestAnimationFrame( animate );
  277. render();
  278. //stats.update();
  279. }
  280. function render() {
  281. if ( ! oldTime ) {
  282. oldTime = new Date().getTime();
  283. }
  284. time = new Date().getTime();
  285. delta = 0.01 * ( time - oldTime );
  286. oldTime = time;
  287. delta = delta < 2 ? delta : 2;
  288. parent.rotation.y += -0.02 * delta;
  289. for( j = 0, jl = clonemeshes.length; j < jl; j++ ) {
  290. cm = clonemeshes[ j ];
  291. cm.mesh.rotation.y += -0.1 * delta * cm.speed;
  292. }
  293. for( j = 0, jl = meshes.length; j < jl; j++ ) {
  294. data = meshes[ j ];
  295. mesh = data.mesh;
  296. vertices = data.vertices;
  297. vertices_tmp = data.vertices_tmp;
  298. vl = data.vl;
  299. if ( ! data.dynamic ) continue;
  300. if ( data.start > 0 ) {
  301. data.start -= 1;
  302. } else {
  303. if ( !data.started ) {
  304. data.direction = -1;
  305. data.started = true;
  306. }
  307. }
  308. for ( i = 0; i < vl; i ++ ) {
  309. p = vertices[ i ].position;
  310. vt = vertices_tmp[ i ];
  311. // falling down
  312. if ( data.direction < 0 ) {
  313. // 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 ] );
  314. // if ( d < 200 ) {
  315. if ( p.y > 0 ) {
  316. // p.y += data.direction * data.speed * delta;
  317. p.x += 1.5 * ( 0.50 - Math.random() ) * data.speed * delta;
  318. p.y += 3.0 * ( 0.25 - Math.random() ) * data.speed * delta;
  319. p.z += 1.5 * ( 0.50 - Math.random() ) * data.speed * delta;
  320. } else {
  321. if ( ! vt[ 3 ] ) {
  322. vt[ 3 ] = 1;
  323. data.down += 1;
  324. }
  325. }
  326. }
  327. // rising up
  328. if ( data.direction > 0 ) {
  329. //if ( p.y < vertices_tmp[ i ][ 1 ] ) {
  330. // p.y += data.direction * data.speed * delta;
  331. d = Math.abs( p.x - vt[ 0 ] ) + Math.abs( p.y - vt[ 1 ] ) + Math.abs( p.z - vt[ 2 ] );
  332. if ( d > 1 ) {
  333. p.x += - ( p.x - vt[ 0 ] ) / d * data.speed * delta * ( 0.85 - Math.random() );
  334. p.y += - ( p.y - vt[ 1 ] ) / d * data.speed * delta * ( 1 + Math.random() );
  335. p.z += - ( p.z - vt[ 2 ] ) / d * data.speed * delta * ( 0.85 - Math.random() );
  336. } else {
  337. if ( ! vt[ 4 ] ) {
  338. vt[ 4 ] = 1;
  339. data.up += 1;
  340. }
  341. }
  342. }
  343. }
  344. // all down
  345. if ( data.down == vl ) {
  346. if ( data.delay == 0 ) {
  347. data.direction = 1;
  348. data.speed = 10;
  349. data.down = 0;
  350. data.delay = 320;
  351. for ( i = 0; i < vl; i ++ ) {
  352. vertices_tmp[ i ][ 3 ] = 0;
  353. }
  354. } else {
  355. data.delay -= 1;
  356. }
  357. }
  358. // all up
  359. if ( data.up == vl ) {
  360. if ( data.delay == 0 ) {
  361. data.direction = -1;
  362. data.speed = 35;
  363. data.up = 0;
  364. data.delay = 120;
  365. for ( i = 0; i < vl; i ++ ) {
  366. vertices_tmp[ i ][ 4 ] = 0;
  367. }
  368. } else {
  369. data.delay -= 1;
  370. }
  371. }
  372. mesh.geometry.__dirtyVertices = true;
  373. }
  374. renderer.clear();
  375. // BLOOM
  376. // Render scene into texture
  377. renderer.render( scene, camera, postprocessing1.rtTexture1, true );
  378. // Render quad with blured scene into texture (convolution pass 1)
  379. postprocessing1.quad.materials[ 0 ] = postprocessing1.materialConvolution;
  380. postprocessing1.materialConvolution.uniforms.tDiffuse.texture = postprocessing1.rtTexture1;
  381. postprocessing1.materialConvolution.uniforms.uImageIncrement.value = postprocessing1.blurx;
  382. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing1.rtTexture2, true );
  383. // Render quad with blured scene into texture (convolution pass 2)
  384. postprocessing1.materialConvolution.uniforms.tDiffuse.texture = postprocessing1.rtTexture2;
  385. postprocessing1.materialConvolution.uniforms.uImageIncrement.value = postprocessing1.blury;
  386. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing1.rtTexture3, true );
  387. // Render original scene with superimposed blur to texture
  388. postprocessing1.quad.materials[ 0 ] = postprocessing1.materialScreen;
  389. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing1.rtTexture1, false );
  390. // NOISE + SCANLINES
  391. postprocessing1.materialFilm.uniforms.time.value += 0.01;
  392. postprocessing1.quad.materials[ 0 ] = postprocessing1.materialFilm;
  393. renderer.render( postprocessing1.scene, postprocessing1.camera, postprocessing2.texture, true );
  394. // FOCUS
  395. renderer.render( postprocessing2.scene, postprocessing2.camera );
  396. }
  397. </script>
  398. </body>
  399. </html>