2
0

webgl_buffergeometry_custom_attributes_particles.html 5.1 KB

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