webgl_custom_attributes_particles.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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://github.com/mrdoob/three.js" target="_blank">three.js</a> - custom attributes example - particles</div>
  33. <div id="container"></div>
  34. <script src="../build/Three.js"></script>
  35. <script src="js/RequestAnimationFrame.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/Stats.js"></script>
  38. <script type="x-shader/x-vertex" id="vertexshader">
  39. uniform float amplitude;
  40. attribute float size;
  41. attribute vec3 customColor;
  42. varying vec3 vColor;
  43. void main() {
  44. vColor = customColor;
  45. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  46. //gl_PointSize = size;
  47. gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
  48. gl_Position = projectionMatrix * mvPosition;
  49. }
  50. </script>
  51. <script type="x-shader/x-fragment" id="fragmentshader">
  52. uniform vec3 color;
  53. uniform sampler2D texture;
  54. varying vec3 vColor;
  55. void main() {
  56. gl_FragColor = vec4( color * vColor, 1.0 );
  57. gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
  58. }
  59. </script>
  60. <script>
  61. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  62. var renderer, scene, camera, stats;
  63. var sphere, uniforms, attributes;
  64. var noise = [];
  65. var WIDTH = window.innerWidth,
  66. HEIGHT = window.innerHeight;
  67. init();
  68. animate();
  69. function init() {
  70. camera = new THREE.Camera( 40, WIDTH / HEIGHT, 1, 10000 );
  71. camera.position.z = 300;
  72. scene = new THREE.Scene();
  73. attributes = {
  74. size: { type: 'f', value: [] },
  75. customColor: { type: 'c', value: [] }
  76. };
  77. uniforms = {
  78. amplitude: { type: "f", value: 1.0 },
  79. color: { type: "c", value: new THREE.Color( 0xffffff ) },
  80. texture: { type: "t", value: 0, texture: 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. var geometry = new THREE.Geometry();
  93. for ( var i = 0; i < 100000; i++ ) {
  94. vector = new THREE.Vector3( Math.random() * 2 - 1, Math.random() * 2 - 1, Math.random() * 2 - 1 );
  95. vector.multiplyScalar( radius );
  96. geometry.vertices.push( new THREE.Vertex( vector ) );
  97. }
  98. sphere = new THREE.ParticleSystem( geometry, shaderMaterial );
  99. sphere.dynamic = true;
  100. //sphere.sortParticles = true;
  101. var vertices = sphere.geometry.vertices;
  102. var values_size = attributes.size.value;
  103. var values_color = attributes.customColor.value;
  104. for( var v = 0; v < vertices.length; v++ ) {
  105. values_size[ v ] = 10;
  106. values_color[ v ] = new THREE.Color( 0xffaa00 );
  107. if ( vertices[ v ].position.x < 0 )
  108. values_color[ v ].setHSV( 0.5 + 0.1 * ( v / vertices.length ), 0.7, 0.9 );
  109. else
  110. values_color[ v ].setHSV( 0.0 + 0.1 * ( v / vertices.length ), 0.9, 0.9 );
  111. }
  112. scene.add( sphere );
  113. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1 } );
  114. renderer.setSize( WIDTH, HEIGHT );
  115. var container = document.getElementById( 'container' );
  116. container.appendChild( renderer.domElement );
  117. stats = new Stats();
  118. stats.domElement.style.position = 'absolute';
  119. stats.domElement.style.top = '0px';
  120. container.appendChild( stats.domElement );
  121. }
  122. function cap( x, a, b ) {
  123. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  124. }
  125. var i, value;
  126. function animate() {
  127. requestAnimationFrame( animate );
  128. render();
  129. stats.update();
  130. }
  131. function render() {
  132. var time = new Date().getTime() * 0.005;
  133. sphere.rotation.z = 0.01 * time;
  134. for( i = 0; i < attributes.size.value.length; i++ ) {
  135. attributes.size.value[ i ] = 14 + 13 * Math.sin( 0.1 * i + time );
  136. }
  137. attributes.size.needsUpdate = true;
  138. renderer.render( scene, camera );
  139. }
  140. </script>
  141. </body>
  142. </html>