2
0

webgl_custom_attributes_points.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. position: absolute;
  21. top: 0px; width: 100%;
  22. padding: 5px;
  23. z-index:100;
  24. }
  25. a { color: #ff0000 }
  26. </style>
  27. </head>
  28. <body>
  29. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - custom attributes example - particles</div>
  30. <div id="container"></div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script type="x-shader/x-vertex" id="vertexshader">
  35. uniform float amplitude;
  36. attribute float size;
  37. attribute vec3 customColor;
  38. varying vec3 vColor;
  39. void main() {
  40. vColor = customColor;
  41. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  42. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  43. gl_Position = projectionMatrix * mvPosition;
  44. }
  45. </script>
  46. <script type="x-shader/x-fragment" id="fragmentshader">
  47. uniform vec3 color;
  48. uniform sampler2D texture;
  49. varying vec3 vColor;
  50. void main() {
  51. gl_FragColor = vec4( color * vColor, 1.0 );
  52. gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
  53. }
  54. </script>
  55. <script>
  56. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  57. var renderer, scene, camera, stats;
  58. var sphere;
  59. var noise = [];
  60. var WIDTH = window.innerWidth;
  61. var HEIGHT = window.innerHeight;
  62. init();
  63. animate();
  64. function init() {
  65. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 10000 );
  66. camera.position.z = 300;
  67. scene = new THREE.Scene();
  68. var amount = 100000;
  69. var radius = 200;
  70. var positions = new Float32Array( amount * 3 );
  71. var colors = new Float32Array( amount * 3 );
  72. var sizes = new Float32Array( amount );
  73. var vertex = new THREE.Vector3();
  74. var color = new THREE.Color( 0xffffff );
  75. for ( var i = 0; i < amount; i ++ ) {
  76. vertex.x = ( Math.random() * 2 - 1 ) * radius;
  77. vertex.y = ( Math.random() * 2 - 1 ) * radius;
  78. vertex.z = ( Math.random() * 2 - 1 ) * radius;
  79. vertex.toArray( positions, i * 3 );
  80. if ( vertex.x < 0 ) {
  81. color.setHSL( 0.5 + 0.1 * ( i / amount ), 0.7, 0.5 );
  82. } else {
  83. color.setHSL( 0.0 + 0.1 * ( i / amount ), 0.9, 0.5 );
  84. }
  85. color.toArray( colors, i * 3 );
  86. sizes[ i ] = 10;
  87. }
  88. var geometry = new THREE.BufferGeometry();
  89. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  90. geometry.addAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
  91. geometry.addAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
  92. //
  93. var material = new THREE.ShaderMaterial( {
  94. uniforms: {
  95. amplitude: { value: 1.0 },
  96. color: { value: new THREE.Color( 0xffffff ) },
  97. texture: { value: new THREE.TextureLoader().load( "textures/sprites/spark1.png" ) }
  98. },
  99. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  100. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  101. blending: THREE.AdditiveBlending,
  102. depthTest: false,
  103. transparent: true
  104. });
  105. //
  106. sphere = new THREE.Points( geometry, material );
  107. scene.add( sphere );
  108. //
  109. renderer = new THREE.WebGLRenderer();
  110. renderer.setPixelRatio( window.devicePixelRatio );
  111. renderer.setSize( WIDTH, HEIGHT );
  112. var container = document.getElementById( 'container' );
  113. container.appendChild( renderer.domElement );
  114. stats = new Stats();
  115. container.appendChild( stats.dom );
  116. //
  117. window.addEventListener( 'resize', onWindowResize, false );
  118. }
  119. function onWindowResize() {
  120. camera.aspect = window.innerWidth / window.innerHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. }
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. render();
  127. stats.update();
  128. }
  129. function render() {
  130. var time = Date.now() * 0.005;
  131. sphere.rotation.z = 0.01 * time;
  132. var geometry = sphere.geometry;
  133. var attributes = geometry.attributes;
  134. for ( var i = 0; i < attributes.size.array.length; i++ ) {
  135. attributes.size.array[ 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>