webgl_custom_attributes_particles2.html 4.6 KB

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