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" rel="noopener">three.js</a> - custom attributes example - particles</div>
  30. <div id="container"></div>
  31. <script src="../build/three.js"></script>
  32. <script src="js/WebGL.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script type="x-shader/x-vertex" id="vertexshader">
  35. attribute float size;
  36. attribute vec3 customColor;
  37. varying vec3 vColor;
  38. void main() {
  39. vColor = customColor;
  40. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  41. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  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>
  55. if ( WEBGL.isWebGLAvailable() === false ) {
  56. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  57. }
  58. var renderer, scene, camera, stats;
  59. var sphere;
  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. color: { value: new THREE.Color( 0xffffff ) },
  96. texture: { value: new THREE.TextureLoader().load( "textures/sprites/spark1.png" ) }
  97. },
  98. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  99. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  100. blending: THREE.AdditiveBlending,
  101. depthTest: false,
  102. transparent: true
  103. } );
  104. //
  105. sphere = new THREE.Points( geometry, material );
  106. scene.add( sphere );
  107. //
  108. renderer = new THREE.WebGLRenderer();
  109. renderer.setPixelRatio( window.devicePixelRatio );
  110. renderer.setSize( WIDTH, HEIGHT );
  111. var container = document.getElementById( 'container' );
  112. container.appendChild( renderer.domElement );
  113. stats = new Stats();
  114. container.appendChild( stats.dom );
  115. //
  116. window.addEventListener( 'resize', onWindowResize, false );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. }
  123. function animate() {
  124. requestAnimationFrame( animate );
  125. render();
  126. stats.update();
  127. }
  128. function render() {
  129. var time = Date.now() * 0.005;
  130. sphere.rotation.z = 0.01 * time;
  131. var geometry = sphere.geometry;
  132. var attributes = geometry.attributes;
  133. for ( var i = 0; i < attributes.size.array.length; i ++ ) {
  134. attributes.size.array[ i ] = 14 + 13 * Math.sin( 0.1 * i + time );
  135. }
  136. attributes.size.needsUpdate = true;
  137. renderer.render( scene, camera );
  138. }
  139. </script>
  140. </body>
  141. </html>