webgl_custom_attributes_particles2.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>three.js webgl - custom attributes [particles][billboards]</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. position: absolute;
  20. top: 0px; width: 100%;
  21. padding: 5px;
  22. z-index:100;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="info"><a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - custom attributes example - particles - billboards</div>
  28. <div id="container"></div>
  29. <script src="../build/Three.js"></script>
  30. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  31. <script type="text/javascript" src="js/Stats.js"></script>
  32. <script type="x-shader/x-vertex" id="vertexshader">
  33. attribute float size;
  34. attribute vec3 ca;
  35. varying vec3 vColor;
  36. void main() {
  37. vColor = ca;
  38. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  39. //gl_PointSize = size;
  40. gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
  41. gl_Position = projectionMatrix * mvPosition;
  42. }
  43. </script>
  44. <script type="x-shader/x-fragment" id="fragmentshader">
  45. uniform vec3 color;
  46. uniform sampler2D texture;
  47. varying vec3 vColor;
  48. void main() {
  49. gl_FragColor = vec4( color * vColor, 1.0 );
  50. gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
  51. }
  52. </script>
  53. <script type="text/javascript">
  54. var renderer, scene, camera, stats;
  55. var sphere, uniforms, attributes;
  56. var vc1;
  57. var WIDTH = window.innerWidth,
  58. HEIGHT = window.innerHeight;
  59. init();
  60. animate();
  61. function init() {
  62. camera = new THREE.Camera( 45, WIDTH / HEIGHT, 1, 10000 );
  63. camera.position.z = 300;
  64. scene = new THREE.Scene();
  65. attributes = {
  66. size: { type: 'f', value: [] },
  67. ca: { type: 'c', value: [] }
  68. };
  69. uniforms = {
  70. amplitude: { type: "f", value: 1.0 },
  71. color: { type: "c", value: new THREE.Color( 0xffffff ) },
  72. texture: { type: "t", value: 0, texture: THREE.ImageUtils.loadTexture( "textures/sprites/disc.png" ) },
  73. };
  74. uniforms.texture.texture.wrapS = uniforms.texture.texture.wrapT = THREE.RepeatWrapping;
  75. var shaderMaterial = new THREE.MeshShaderMaterial( {
  76. uniforms: uniforms,
  77. attributes: attributes,
  78. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  79. fragmentShader: document.getElementById( 'fragmentshader' ).textContent
  80. });
  81. var radius = 100, segments = 68, rings = 38;
  82. var geometry = new THREE.SphereGeometry( radius, segments, rings );
  83. vc1 = geometry.vertices.length;
  84. var geometry2 = new THREE.CubeGeometry( 0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10 );
  85. GeometryUtils.merge( geometry, geometry2 );
  86. sphere = new THREE.ParticleSystem( geometry, shaderMaterial );
  87. sphere.dynamic = true;
  88. sphere.sortParticles = true;
  89. var vertices = sphere.geometry.vertices;
  90. var values_size = attributes.size.value;
  91. var values_color = attributes.ca.value;
  92. for( var v = 0; v < vertices.length; v++ ) {
  93. values_size[ v ] = 10;
  94. values_color[ v ] = new THREE.Color( 0xffffff );
  95. if ( v < vc1 ) {
  96. values_color[ v ].setHSV( 0.01 + 0.1 * ( v / vc1 ), 0.99, ( vertices[ v ].position.y + radius ) / ( 2 *radius ) );
  97. } else {
  98. values_size[ v ] = 40;
  99. values_color[ v ].setHSV( 0.6, 0.75, 0.5 + vertices[ v ].position.y / ( 0.8 * radius ) );
  100. }
  101. }
  102. scene.addChild( sphere );
  103. renderer = new THREE.WebGLRenderer( { clearColor: 0x000000, clearAlpha: 1 } );
  104. renderer.setSize( WIDTH, HEIGHT );
  105. var container = document.getElementById( 'container' );
  106. container.appendChild( renderer.domElement );
  107. stats = new Stats();
  108. stats.domElement.style.position = 'absolute';
  109. stats.domElement.style.top = '0px';
  110. container.appendChild( stats.domElement );
  111. }
  112. function cap( x, a, b ) {
  113. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  114. }
  115. var i, value;
  116. function animate() {
  117. requestAnimationFrame( animate );
  118. render();
  119. stats.update();
  120. }
  121. function render() {
  122. var time = new Date().getTime() * 0.005;
  123. sphere.rotation.y = 0.02 * time;
  124. sphere.rotation.z = 0.02 * time;
  125. for( i = 0; i < attributes.size.value.length; i++ ) {
  126. if ( i < vc1 )
  127. attributes.size.value[ i ] = 16 + 12 * Math.sin( 0.1 * i + time );
  128. }
  129. attributes.size.needsUpdate = true;
  130. renderer.render( scene, camera );
  131. }
  132. </script>
  133. </body>
  134. </html>