webgl_custom_attributes_points.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. <!-- Import maps polyfill -->
  33. <!-- Remove this when import maps will be widely supported -->
  34. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  35. <script type="importmap">
  36. {
  37. "imports": {
  38. "three": "../build/three.module.js"
  39. }
  40. }
  41. </script>
  42. <script type="module">
  43. import * as THREE from 'three';
  44. import Stats from './jsm/libs/stats.module.js';
  45. let renderer, scene, camera, stats;
  46. let sphere;
  47. const WIDTH = window.innerWidth;
  48. const HEIGHT = window.innerHeight;
  49. init();
  50. animate();
  51. function init() {
  52. camera = new THREE.PerspectiveCamera( 40, WIDTH / HEIGHT, 1, 10000 );
  53. camera.position.z = 300;
  54. scene = new THREE.Scene();
  55. const amount = 100000;
  56. const radius = 200;
  57. const positions = new Float32Array( amount * 3 );
  58. const colors = new Float32Array( amount * 3 );
  59. const sizes = new Float32Array( amount );
  60. const vertex = new THREE.Vector3();
  61. const color = new THREE.Color( 0xffffff );
  62. for ( let i = 0; i < amount; i ++ ) {
  63. vertex.x = ( Math.random() * 2 - 1 ) * radius;
  64. vertex.y = ( Math.random() * 2 - 1 ) * radius;
  65. vertex.z = ( Math.random() * 2 - 1 ) * radius;
  66. vertex.toArray( positions, i * 3 );
  67. if ( vertex.x < 0 ) {
  68. color.setHSL( 0.5 + 0.1 * ( i / amount ), 0.7, 0.5 );
  69. } else {
  70. color.setHSL( 0.0 + 0.1 * ( i / amount ), 0.9, 0.5 );
  71. }
  72. color.toArray( colors, i * 3 );
  73. sizes[ i ] = 10;
  74. }
  75. const geometry = new THREE.BufferGeometry();
  76. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  77. geometry.setAttribute( 'customColor', new THREE.BufferAttribute( colors, 3 ) );
  78. geometry.setAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
  79. //
  80. const material = new THREE.ShaderMaterial( {
  81. uniforms: {
  82. color: { value: new THREE.Color( 0xffffff ) },
  83. pointTexture: { value: new THREE.TextureLoader().load( 'textures/sprites/spark1.png' ) }
  84. },
  85. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  86. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  87. blending: THREE.AdditiveBlending,
  88. depthTest: false,
  89. transparent: true
  90. } );
  91. //
  92. sphere = new THREE.Points( geometry, material );
  93. scene.add( sphere );
  94. //
  95. renderer = new THREE.WebGLRenderer();
  96. renderer.setPixelRatio( window.devicePixelRatio );
  97. renderer.setSize( WIDTH, HEIGHT );
  98. const container = document.getElementById( 'container' );
  99. container.appendChild( renderer.domElement );
  100. stats = new Stats();
  101. container.appendChild( stats.dom );
  102. //
  103. window.addEventListener( 'resize', onWindowResize );
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. render();
  113. stats.update();
  114. }
  115. function render() {
  116. const time = Date.now() * 0.005;
  117. sphere.rotation.z = 0.01 * time;
  118. const geometry = sphere.geometry;
  119. const attributes = geometry.attributes;
  120. for ( let i = 0; i < attributes.size.array.length; i ++ ) {
  121. attributes.size.array[ i ] = 14 + 13 * Math.sin( 0.1 * i + time );
  122. }
  123. attributes.size.needsUpdate = true;
  124. renderer.render( scene, camera );
  125. }
  126. </script>
  127. </body>
  128. </html>