webgpu_instance_sprites.html 4.1 KB

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