webgpu_instance_points.html 6.5 KB

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