webgl_custom_attributes_points.html 4.6 KB

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