webgpu_instance_points.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - points instanced</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 Points</span>
  14. </div>
  15. <small>
  16. Rendering instanced sprites with TSL to emulate wide points.
  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 { color, storage, Fn, instancedBufferAttribute, instanceIndex, sin, time, float, uniform, shapeCircle, mix, vec3 } from 'three/tsl';
  32. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  35. let renderer, scene, camera, camera2, controls, backgroundNode;
  36. let material;
  37. let effectController;
  38. // viewport
  39. let insetWidth;
  40. let insetHeight;
  41. // compute
  42. let computeSize;
  43. init();
  44. async function init() {
  45. scene = new THREE.Scene();
  46. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  47. camera.position.set( - 40, 0, 60 );
  48. camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
  49. camera2.position.copy( camera.position );
  50. backgroundNode = color( 0x222222 );
  51. effectController = {
  52. pulseSpeed: uniform( 6 ),
  53. minWidth: uniform( 6 ),
  54. maxWidth: uniform( 20 )
  55. };
  56. // Position and THREE.Color Data
  57. const points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  58. const spline = new THREE.CatmullRomCurve3( points );
  59. const divisions = Math.round( 4 * points.length );
  60. const point = new THREE.Vector3();
  61. const pointColor = new THREE.Color();
  62. const positions = [];
  63. const colors = [];
  64. const sizes = new Float32Array( divisions );
  65. for ( let i = 0, l = divisions; i < l; i ++ ) {
  66. const t = i / l;
  67. spline.getPoint( t, point );
  68. positions.push( point.x, point.y, point.z );
  69. pointColor.setHSL( t, 1.0, 0.5, THREE.SRGBColorSpace );
  70. colors.push( pointColor.r, pointColor.g, pointColor.b );
  71. sizes[ i ] = 10.0;
  72. }
  73. // Instanced Points
  74. const positionAttribute = new THREE.InstancedBufferAttribute( new Float32Array( positions ), 3 );
  75. const colorsAttribute = new THREE.InstancedBufferAttribute( new Float32Array( colors ), 3 );
  76. const instanceSizeBufferAttribute = new THREE.StorageInstancedBufferAttribute( sizes, 1 );
  77. const instanceSizeStorage = storage( instanceSizeBufferAttribute, 'float', instanceSizeBufferAttribute.count );
  78. computeSize = Fn( () => {
  79. const { pulseSpeed, minWidth, maxWidth } = effectController;
  80. const relativeTime = time.add( float( instanceIndex ) );
  81. const sizeFactor = sin( relativeTime.mul( pulseSpeed ) ).add( 1 ).div( 2 );
  82. instanceSizeStorage.element( instanceIndex ).assign( sizeFactor.mul( maxWidth.sub( minWidth ) ).add( minWidth ) );
  83. } )().compute( divisions );
  84. // Material / Sprites
  85. const attributeRange = instancedBufferAttribute( instanceSizeBufferAttribute );
  86. const pointColors = mix( vec3( 0.0 ), instancedBufferAttribute( colorsAttribute ), attributeRange.div( float( effectController.maxWidth ) ) );
  87. material = new THREE.PointsNodeMaterial( {
  88. colorNode: pointColors,
  89. opacityNode: shapeCircle(),
  90. positionNode: instancedBufferAttribute( positionAttribute ),
  91. // rotationNode: time,
  92. sizeNode: instancedBufferAttribute( instanceSizeBufferAttribute ),
  93. // size: 40, // in pixels units
  94. vertexColors: true,
  95. sizeAttenuation: false,
  96. alphaToCoverage: true
  97. } );
  98. const instancedPoints = new THREE.Sprite( material );
  99. instancedPoints.count = divisions;
  100. scene.add( instancedPoints );
  101. // Renderer / Controls
  102. renderer = new THREE.WebGPURenderer( { antialias: true } );
  103. renderer.setPixelRatio( window.devicePixelRatio );
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. renderer.setAnimationLoop( animate );
  106. renderer.inspector = new Inspector();
  107. document.body.appendChild( renderer.domElement );
  108. controls = new OrbitControls( camera, renderer.domElement );
  109. controls.enableDamping = true;
  110. controls.minDistance = 10;
  111. controls.maxDistance = 500;
  112. window.addEventListener( 'resize', onWindowResize );
  113. onWindowResize();
  114. // GUI
  115. const gui = renderer.inspector.createParameters( 'Settings' );
  116. gui.add( material, 'alphaToCoverage' );
  117. gui.add( effectController.minWidth, 'value', 1, 30, 1 ).name( 'minWidth' );
  118. gui.add( effectController.maxWidth, 'value', 2, 30, 1 ).name( 'maxWidth' );
  119. gui.add( effectController.pulseSpeed, 'value', 1, 20, 0.1 ).name( 'pulseSpeed' );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. insetWidth = window.innerHeight / 4; // square
  126. insetHeight = window.innerHeight / 4;
  127. camera2.aspect = insetWidth / insetHeight;
  128. camera2.updateProjectionMatrix();
  129. }
  130. function animate() {
  131. // compute
  132. renderer.compute( computeSize );
  133. // main scene
  134. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  135. controls.update();
  136. renderer.autoClear = true;
  137. scene.backgroundNode = null;
  138. renderer.render( scene, camera );
  139. // inset scene
  140. const posY = window.innerHeight - insetHeight - 20;
  141. renderer.clearDepth(); // important!
  142. renderer.setScissorTest( true );
  143. renderer.setScissor( 20, posY, insetWidth, insetHeight );
  144. renderer.setViewport( 20, posY, insetWidth, insetHeight );
  145. camera2.position.copy( camera.position );
  146. camera2.quaternion.copy( camera.quaternion );
  147. renderer.autoClear = false;
  148. scene.backgroundNode = backgroundNode;
  149. renderer.render( scene, camera2 );
  150. renderer.setScissorTest( false );
  151. }
  152. //
  153. </script>
  154. </body>
  155. </html>
粤ICP备19079148号