webgpu_instance_sprites.html 4.4 KB

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