1
0

webgl_custom_attributes_points.html 4.4 KB

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