1
0

webgpu_instance_points.html 6.4 KB

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