webgl_custom_attributes_particles.html 4.5 KB

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