1
0

webgpu_instance_sprites.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - instance sprites</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">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - instance sprites
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/tsl": "../build/three.webgpu.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { uniform, timerGlobal, instanceIndex, instancedBufferAttribute } from 'three/tsl';
  27. let camera, scene, renderer, stats, material;
  28. let mouseX = 0, mouseY = 0;
  29. let windowHalfX = window.innerWidth / 2;
  30. let windowHalfY = window.innerHeight / 2;
  31. init();
  32. function init() {
  33. camera = new THREE.PerspectiveCamera( 55, window.innerWidth / window.innerHeight, 2, 2000 );
  34. camera.position.z = 1000;
  35. scene = new THREE.Scene();
  36. scene.fog = new THREE.FogExp2( 0x000000, 0.001 );
  37. // positions
  38. const count = 10000;
  39. const positions = [];
  40. for ( let i = 0; i < count; i ++ ) {
  41. positions.push( 2000 * Math.random() - 1000, 2000 * Math.random() - 1000, 2000 * Math.random() - 1000 );
  42. }
  43. const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 );
  44. // texture
  45. const map = new THREE.TextureLoader().load( 'textures/sprites/snowflake1.png' );
  46. map.colorSpace = THREE.SRGBColorSpace;
  47. // material
  48. const timer = timerGlobal();
  49. material = new THREE.SpriteNodeMaterial( { sizeAttenuation: true, map, alphaMap: map, alphaTest: 0.1, transparent: true } );
  50. material.color.setHSL( 1.0, 0.3, 0.7, THREE.SRGBColorSpace );
  51. material.positionNode = instancedBufferAttribute( positionAttribute );
  52. material.rotationNode = timer.add( instanceIndex ).sin();
  53. material.scaleNode = uniform( 15 );
  54. // sprites
  55. const particles = new THREE.Sprite( material );
  56. particles.count = count;
  57. scene.add( particles );
  58. //
  59. renderer = new THREE.WebGPURenderer();
  60. renderer.setPixelRatio( window.devicePixelRatio );
  61. renderer.setSize( window.innerWidth, window.innerHeight );
  62. renderer.setAnimationLoop( animate );
  63. document.body.appendChild( renderer.domElement );
  64. //
  65. stats = new Stats();
  66. document.body.appendChild( stats.dom );
  67. //
  68. const gui = new GUI();
  69. gui.add( material, 'sizeAttenuation' ).onChange( function () {
  70. material.needsUpdate = true;
  71. material.scaleNode.value = material.sizeAttenuation ? 15 : 0.03;
  72. } );
  73. gui.open();
  74. //
  75. document.body.style.touchAction = 'none';
  76. document.body.addEventListener( 'pointermove', onPointerMove );
  77. //
  78. window.addEventListener( 'resize', onWindowResize );
  79. }
  80. function onWindowResize() {
  81. windowHalfX = window.innerWidth / 2;
  82. windowHalfY = window.innerHeight / 2;
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. }
  87. function onPointerMove( event ) {
  88. if ( event.isPrimary === false ) return;
  89. mouseX = event.clientX - windowHalfX;
  90. mouseY = event.clientY - windowHalfY;
  91. }
  92. //
  93. function animate() {
  94. render();
  95. stats.update();
  96. }
  97. function render() {
  98. const time = Date.now() * 0.00005;
  99. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  100. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  101. camera.lookAt( scene.position );
  102. const h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
  103. material.color.setHSL( h, 0.5, 0.5 );
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>