webgl_points_dynamic.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles - dynamic - postprocessing</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info">
  12. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl dynamic particles + postprocessing<br/>
  13. models by <a href="http://sketchup.google.com/3dwarehouse/details?mid=2c6fd128fca34052adc5f5b98d513da1" target="_blank" rel="noopener">Reallusion</a>
  14. <a href="http://sketchup.google.com/3dwarehouse/details?mid=f526cc4abf7cb68d76cab47c765b7255" target="_blank" rel="noopener">iClone</a>
  15. </div>
  16. <script type="module">
  17. import * as THREE from '../build/three.module.js';
  18. import Stats from './jsm/libs/stats.module.js';
  19. import { EffectComposer } from './jsm/postprocessing/EffectComposer.js';
  20. import { RenderPass } from './jsm/postprocessing/RenderPass.js';
  21. import { ShaderPass } from './jsm/postprocessing/ShaderPass.js';
  22. import { BloomPass } from './jsm/postprocessing/BloomPass.js';
  23. import { FilmPass } from './jsm/postprocessing/FilmPass.js';
  24. import { FocusShader } from './jsm/shaders/FocusShader.js';
  25. import { OBJLoader } from './jsm/loaders/OBJLoader.js';
  26. var camera, scene, renderer, mesh;
  27. var parent, meshes = [], clonemeshes = [];
  28. var composer, effectFocus;
  29. var clock = new THREE.Clock();
  30. var stats;
  31. init();
  32. animate();
  33. function init() {
  34. var container = document.querySelector( '#container' );
  35. camera = new THREE.PerspectiveCamera( 20, window.innerWidth / window.innerHeight, 1, 50000 );
  36. camera.position.set( 0, 700, 7000 );
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0x000104 );
  39. scene.fog = new THREE.FogExp2( 0x000104, 0.0000675 );
  40. camera.lookAt( scene.position );
  41. var loader = new OBJLoader();
  42. loader.load( 'models/obj/male02/male02.obj', function ( object ) {
  43. var positions = combineBuffer( object, 'position' );
  44. createMesh( positions, scene, 4.05, - 500, - 350, 600, 0xff7744 );
  45. createMesh( positions, scene, 4.05, 500, - 350, 0, 0xff5522 );
  46. createMesh( positions, scene, 4.05, - 250, - 350, 1500, 0xff9922 );
  47. createMesh( positions, scene, 4.05, - 250, - 350, - 1500, 0xff99ff );
  48. } );
  49. loader.load( 'models/obj/female02/female02.obj', function ( object ) {
  50. var positions = combineBuffer( object, 'position' );
  51. createMesh( positions, scene, 4.05, - 1000, - 350, 0, 0xffdd44 );
  52. createMesh( positions, scene, 4.05, 0, - 350, 0, 0xffffff );
  53. createMesh( positions, scene, 4.05, 1000, - 350, 400, 0xff4422 );
  54. createMesh( positions, scene, 4.05, 250, - 350, 1500, 0xff9955 );
  55. createMesh( positions, scene, 4.05, 250, - 350, 2500, 0xff77dd );
  56. } );
  57. renderer = new THREE.WebGLRenderer();
  58. renderer.setPixelRatio( window.devicePixelRatio );
  59. renderer.setSize( window.innerWidth, window.innerHeight );
  60. renderer.autoClear = false;
  61. container.appendChild( renderer.domElement );
  62. parent = new THREE.Object3D();
  63. scene.add( parent );
  64. var grid = new THREE.Points( new THREE.PlaneBufferGeometry( 15000, 15000, 64, 64 ), new THREE.PointsMaterial( { color: 0xff0000, size: 10 } ) );
  65. grid.position.y = - 400;
  66. grid.rotation.x = - Math.PI / 2;
  67. parent.add( grid );
  68. // postprocessing
  69. var renderModel = new RenderPass( scene, camera );
  70. var effectBloom = new BloomPass( 0.75 );
  71. var effectFilm = new FilmPass( 0.5, 0.5, 1448, false );
  72. effectFocus = new ShaderPass( FocusShader );
  73. effectFocus.uniforms[ "screenWidth" ].value = window.innerWidth * window.devicePixelRatio;
  74. effectFocus.uniforms[ "screenHeight" ].value = window.innerHeight * window.devicePixelRatio;
  75. composer = new EffectComposer( renderer );
  76. composer.addPass( renderModel );
  77. composer.addPass( effectBloom );
  78. composer.addPass( effectFilm );
  79. composer.addPass( effectFocus );
  80. //stats
  81. stats = new Stats();
  82. container.appendChild( stats.dom );
  83. window.addEventListener( 'resize', onWindowResize, false );
  84. }
  85. function onWindowResize() {
  86. camera.aspect = window.innerWidth / window.innerHeight;
  87. camera.updateProjectionMatrix();
  88. camera.lookAt( scene.position );
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. composer.setSize( window.innerWidth, window.innerHeight );
  91. effectFocus.uniforms[ "screenWidth" ].value = window.innerWidth * window.devicePixelRatio;
  92. effectFocus.uniforms[ "screenHeight" ].value = window.innerHeight * window.devicePixelRatio;
  93. }
  94. function combineBuffer( model, bufferName ) {
  95. let count = 0;
  96. model.traverse( function ( child ) {
  97. if ( child.isMesh ) {
  98. var buffer = child.geometry.attributes[ bufferName ];
  99. count += buffer.array.length;
  100. }
  101. } );
  102. var combined = new Float32Array( count );
  103. let offset = 0;
  104. model.traverse( function ( child ) {
  105. if ( child.isMesh ) {
  106. var buffer = child.geometry.attributes[ bufferName ];
  107. combined.set( buffer.array, offset );
  108. offset += buffer.array.length;
  109. }
  110. } );
  111. return new THREE.BufferAttribute( combined, 3 );
  112. }
  113. function createMesh( positions, scene, scale, x, y, z, color ) {
  114. var geometry = new THREE.BufferGeometry();
  115. geometry.setAttribute( 'position', positions.clone() );
  116. geometry.setAttribute( 'initialPosition', positions.clone() );
  117. geometry.attributes.position.setUsage( THREE.DynamicDrawUsage );
  118. var clones = [
  119. [ 6000, 0, - 4000 ],
  120. [ 5000, 0, 0 ],
  121. [ 1000, 0, 5000 ],
  122. [ 1000, 0, - 5000 ],
  123. [ 4000, 0, 2000 ],
  124. [ - 4000, 0, 1000 ],
  125. [ - 5000, 0, - 5000 ],
  126. [ 0, 0, 0 ]
  127. ];
  128. for ( var i = 0; i < clones.length; i ++ ) {
  129. var c = ( i < clones.length - 1 ) ? 0x252525 : color;
  130. mesh = new THREE.Points( geometry, new THREE.PointsMaterial( { size: 30, color: c } ) );
  131. mesh.scale.x = mesh.scale.y = mesh.scale.z = scale;
  132. mesh.position.x = x + clones[ i ][ 0 ];
  133. mesh.position.y = y + clones[ i ][ 1 ];
  134. mesh.position.z = z + clones[ i ][ 2 ];
  135. parent.add( mesh );
  136. clonemeshes.push( { mesh: mesh, speed: 0.5 + Math.random() } );
  137. }
  138. meshes.push( {
  139. mesh: mesh, verticesDown: 0, verticesUp: 0, direction: 0, speed: 15, delay: Math.floor( 200 + 200 * Math.random() ),
  140. start: Math.floor( 100 + 200 * Math.random() ),
  141. } );
  142. }
  143. function animate() {
  144. requestAnimationFrame( animate );
  145. render();
  146. stats.update();
  147. }
  148. function render() {
  149. var delta = 10 * clock.getDelta();
  150. delta = delta < 2 ? delta : 2;
  151. parent.rotation.y += - 0.02 * delta;
  152. for ( var j = 0; j < clonemeshes.length; j ++ ) {
  153. var cm = clonemeshes[ j ];
  154. cm.mesh.rotation.y += - 0.1 * delta * cm.speed;
  155. }
  156. for ( var j = 0; j < meshes.length; j ++ ) {
  157. var data = meshes[ j ];
  158. var positions = data.mesh.geometry.attributes.position;
  159. var initialPositions = data.mesh.geometry.attributes.initialPosition;
  160. var count = positions.count;
  161. if ( data.start > 0 ) {
  162. data.start -= 1;
  163. } else {
  164. if ( data.direction === 0 ) {
  165. data.direction = - 1;
  166. }
  167. }
  168. for ( var i = 0; i < count; i ++ ) {
  169. var px = positions.getX( i );
  170. var py = positions.getY( i );
  171. var pz = positions.getZ( i );
  172. // falling down
  173. if ( data.direction < 0 ) {
  174. if ( py > 0 ) {
  175. positions.setXYZ(
  176. i,
  177. px + 1.5 * ( 0.50 - Math.random() ) * data.speed * delta,
  178. py + 3.0 * ( 0.25 - Math.random() ) * data.speed * delta,
  179. pz + 1.5 * ( 0.50 - Math.random() ) * data.speed * delta
  180. );
  181. } else {
  182. data.verticesDown += 1;
  183. }
  184. }
  185. // rising up
  186. if ( data.direction > 0 ) {
  187. var ix = initialPositions.getX( i );
  188. var iy = initialPositions.getY( i );
  189. var iz = initialPositions.getZ( i );
  190. var dx = Math.abs( px - ix );
  191. var dy = Math.abs( py - iy );
  192. var dz = Math.abs( pz - iz );
  193. var d = dx + dy + dx;
  194. if ( d > 1 ) {
  195. positions.setXYZ(
  196. i,
  197. px - ( px - ix ) / dx * data.speed * delta * ( 0.85 - Math.random() ),
  198. py - ( py - iy ) / dy * data.speed * delta * ( 1 + Math.random() ),
  199. pz - ( pz - iz ) / dz * data.speed * delta * ( 0.85 - Math.random() )
  200. );
  201. } else {
  202. data.verticesUp += 1;
  203. }
  204. }
  205. }
  206. // all vertices down
  207. if ( data.verticesDown >= count ) {
  208. if ( data.delay <= 0 ) {
  209. data.direction = 1;
  210. data.speed = 5;
  211. data.verticesDown = 0;
  212. data.delay = 320;
  213. } else {
  214. data.delay -= 1;
  215. }
  216. }
  217. // all vertices up
  218. if ( data.verticesUp >= count ) {
  219. if ( data.delay <= 0 ) {
  220. data.direction = - 1;
  221. data.speed = 15;
  222. data.verticesUp = 0;
  223. data.delay = 120;
  224. } else {
  225. data.delay -= 1;
  226. }
  227. }
  228. positions.needsUpdate = true;
  229. }
  230. composer.render( 0.01 );
  231. }
  232. </script>
  233. </body>
  234. </html>