webgl_custom_attributes_points.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - custom attributes example - particles</div>
  11. <div id="container"></div>
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. attribute float size;
  14. attribute vec3 customColor;
  15. varying vec3 vColor;
  16. void main() {
  17. vColor = customColor;
  18. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  19. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  20. gl_Position = projectionMatrix * mvPosition;
  21. }
  22. </script>
  23. <script type="x-shader/x-fragment" id="fragmentshader">
  24. uniform vec3 color;
  25. uniform sampler2D pointTexture;
  26. varying vec3 vColor;
  27. void main() {
  28. gl_FragColor = vec4( color * vColor, 1.0 );
  29. gl_FragColor = gl_FragColor * texture2D( pointTexture, gl_PointCoord );
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from '../build/three.module.js';
  34. import Stats from './jsm/libs/stats.module.js';
  35. var renderer, scene, camera, stats;
  36. var sphere;
  37. var WIDTH = window.innerWidth;
  38. var HEIGHT = window.innerHeight;
  39. init();
  40. animate();
  41. function init() {
  42. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 10000 );
  43. camera.position.z = 300;
  44. scene = new THREE.Scene();
  45. var amount = 100000;
  46. var radius = 200;
  47. var positions = new Float32Array( amount * 3 );
  48. var colors = new Float32Array( amount * 3 );
  49. var sizes = new Float32Array( amount );
  50. var vertex = new THREE.Vector3();
  51. var color = new THREE.Color( 0xffffff );
  52. for ( var i = 0; i < amount; i ++ ) {
  53. vertex.x = ( Math.random() * 2 - 1 ) * radius;
  54. vertex.y = ( Math.random() * 2 - 1 ) * radius;
  55. vertex.z = ( Math.random() * 2 - 1 ) * radius;
  56. vertex.toArray( positions, i * 3 );
  57. if ( vertex.x < 0 ) {
  58. color.setHSL( 0.5 + 0.1 * ( i / amount ), 0.7, 0.5 );
  59. } else {
  60. color.setHSL( 0.0 + 0.1 * ( i / amount ), 0.9, 0.5 );
  61. }
  62. color.toArray( colors, i * 3 );
  63. sizes[ i ] = 10;
  64. }
  65. var geometry = new THREE.BufferGeometry();
  66. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  67. geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
  68. geometry.setAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
  69. //
  70. var material = new THREE.ShaderMaterial( {
  71. uniforms: {
  72. color: { value: new THREE.Color( 0xffffff ) },
  73. pointTexture: { value: new THREE.TextureLoader().load( "textures/sprites/spark1.png" ) }
  74. },
  75. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  76. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  77. blending: THREE.AdditiveBlending,
  78. depthTest: false,
  79. transparent: true
  80. } );
  81. //
  82. sphere = new THREE.Points( geometry, material );
  83. scene.add( sphere );
  84. //
  85. renderer = new THREE.WebGLRenderer();
  86. renderer.setPixelRatio( window.devicePixelRatio );
  87. renderer.setSize( WIDTH, HEIGHT );
  88. var container = document.getElementById( 'container' );
  89. container.appendChild( renderer.domElement );
  90. stats = new Stats();
  91. container.appendChild( stats.dom );
  92. //
  93. window.addEventListener( 'resize', onWindowResize, false );
  94. }
  95. function onWindowResize() {
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function animate() {
  101. requestAnimationFrame( animate );
  102. render();
  103. stats.update();
  104. }
  105. function render() {
  106. var time = Date.now() * 0.005;
  107. sphere.rotation.z = 0.01 * time;
  108. var geometry = sphere.geometry;
  109. var attributes = geometry.attributes;
  110. for ( var i = 0; i < attributes.size.array.length; i ++ ) {
  111. attributes.size.array[ i ] = 14 + 13 * Math.sin( 0.1 * i + time );
  112. }
  113. attributes.size.needsUpdate = true;
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>