webgl_buffergeometry_custom_attributes_particles.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffer geometry custom attributes - particles</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. color: #ffffff;
  10. background-color: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 0px;
  17. width: 100%;
  18. padding: 5px;
  19. font-family: Monospace;
  20. font-size: 13px;
  21. text-align: center;
  22. font-weight: bold;
  23. }
  24. a {
  25. color: #fff;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container"></div>
  31. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - buffergeometry custom attributes - particles</div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script type="x-shader/x-vertex" id="vertexshader">
  36. attribute float size;
  37. attribute vec3 customColor;
  38. varying vec3 vColor;
  39. void main() {
  40. vColor = customColor;
  41. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  42. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  43. gl_Position = projectionMatrix * mvPosition;
  44. }
  45. </script>
  46. <script type="x-shader/x-fragment" id="fragmentshader">
  47. uniform vec3 color;
  48. uniform sampler2D texture;
  49. varying vec3 vColor;
  50. void main() {
  51. gl_FragColor = vec4( color * vColor, 1.0 );
  52. gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
  53. }
  54. </script>
  55. <script>
  56. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  57. var renderer, scene, camera, stats;
  58. var particleSystem, uniforms, geometry;
  59. var particles = 100000;
  60. var WIDTH = window.innerWidth;
  61. var HEIGHT = window.innerHeight;
  62. init();
  63. animate();
  64. function init() {
  65. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 10000 );
  66. camera.position.z = 300;
  67. scene = new THREE.Scene();
  68. uniforms = {
  69. color: { value: new THREE.Color( 0xffffff ) },
  70. texture: { value: new THREE.TextureLoader().load( "textures/sprites/spark1.png" ) }
  71. };
  72. var shaderMaterial = new THREE.ShaderMaterial( {
  73. uniforms: uniforms,
  74. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  75. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  76. blending: THREE.AdditiveBlending,
  77. depthTest: false,
  78. transparent: true
  79. });
  80. var radius = 200;
  81. geometry = new THREE.BufferGeometry();
  82. var positions = new Float32Array( particles * 3 );
  83. var colors = new Float32Array( particles * 3 );
  84. var sizes = new Float32Array( particles );
  85. var color = new THREE.Color();
  86. for ( var i = 0, i3 = 0; i < particles; i ++, i3 += 3 ) {
  87. positions[ i3 + 0 ] = ( Math.random() * 2 - 1 ) * radius;
  88. positions[ i3 + 1 ] = ( Math.random() * 2 - 1 ) * radius;
  89. positions[ i3 + 2 ] = ( Math.random() * 2 - 1 ) * radius;
  90. color.setHSL( i / particles, 1.0, 0.5 );
  91. colors[ i3 + 0 ] = color.r;
  92. colors[ i3 + 1 ] = color.g;
  93. colors[ i3 + 2 ] = color.b;
  94. sizes[ i ] = 20;
  95. }
  96. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  97. geometry.addAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
  98. geometry.addAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
  99. particleSystem = new THREE.Points( geometry, shaderMaterial );
  100. scene.add( particleSystem );
  101. renderer = new THREE.WebGLRenderer();
  102. renderer.setPixelRatio( window.devicePixelRatio );
  103. renderer.setSize( WIDTH, HEIGHT );
  104. var container = document.getElementById( 'container' );
  105. container.appendChild( renderer.domElement );
  106. stats = new Stats();
  107. container.appendChild( stats.dom );
  108. //
  109. window.addEventListener( 'resize', onWindowResize, false );
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. }
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. render();
  119. stats.update();
  120. }
  121. function render() {
  122. var time = Date.now() * 0.005;
  123. particleSystem.rotation.z = 0.01 * time;
  124. var sizes = geometry.attributes.size.array;
  125. for ( var i = 0; i < particles; i++ ) {
  126. sizes[ i ] = 10 * ( 1 + Math.sin( 0.1 * i + time ) );
  127. }
  128. geometry.attributes.size.needsUpdate = true;
  129. renderer.render( scene, camera );
  130. }
  131. </script>
  132. </body>
  133. </html>