webgpu_points_fat.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - points - fat</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> - fat points</div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js",
  16. "three/addons/": "./jsm/",
  17. "three/nodes": "./jsm/nodes/Nodes.js"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  24. import WebGL from 'three/addons/capabilities/WebGL.js';
  25. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  28. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  29. import { FatPoints } from 'three/addons/points/FatPoints.js';
  30. //import { FatPointsNodeMaterial } from 'three/addons/points/FatPointsNodeMaterial.js'; // why not this, instead?
  31. import { FatPointsGeometry } from 'three/addons/points/FatPointsGeometry.js';
  32. import { color, FatPointsNodeMaterial } from 'three/nodes';
  33. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  34. let renderer, scene, camera, camera2, controls, backgroundNode;
  35. let material;
  36. let stats;
  37. let gui;
  38. // viewport
  39. let insetWidth;
  40. let insetHeight;
  41. init();
  42. animate();
  43. function init() {
  44. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  45. document.body.appendChild( WebGPU.getErrorMessage() );
  46. throw new Error( 'No WebGPU or WebGL2 support' );
  47. }
  48. renderer = new WebGPURenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. document.body.appendChild( renderer.domElement );
  52. scene = new THREE.Scene();
  53. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  54. camera.position.set( - 40, 0, 60 );
  55. camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
  56. camera2.position.copy( camera.position );
  57. controls = new OrbitControls( camera, renderer.domElement );
  58. controls.enableDamping = true;
  59. controls.minDistance = 10;
  60. controls.maxDistance = 500;
  61. backgroundNode = color( 0x222222 );
  62. // Position and THREE.Color Data
  63. const positions = [];
  64. const colors = [];
  65. const points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  66. const spline = new THREE.CatmullRomCurve3( points );
  67. const divisions = Math.round( 4 * points.length );
  68. const point = new THREE.Vector3();
  69. const pointColor = new THREE.Color();
  70. for ( let i = 0, l = divisions; i < l; i ++ ) {
  71. const t = i / l;
  72. spline.getPoint( t, point );
  73. positions.push( point.x, point.y, point.z );
  74. pointColor.setHSL( t, 1.0, 0.5, THREE.SRGBColorSpace );
  75. colors.push( pointColor.r, pointColor.g, pointColor.b );
  76. }
  77. // Fat Points
  78. const geometry = new FatPointsGeometry();
  79. geometry.setPositions( positions );
  80. geometry.setColors( colors );
  81. geometry.instanceCount = positions.length / 3; // this should not be necessary
  82. material = new FatPointsNodeMaterial( {
  83. color: 0xffffff,
  84. pointWidth: 10, // in pixel units
  85. vertexColors: true,
  86. alphaToCoverage: true,
  87. } );
  88. const fatPoints = new FatPoints( geometry, material );
  89. fatPoints.scale.set( 1, 1, 1 );
  90. scene.add( fatPoints );
  91. //
  92. window.addEventListener( 'resize', onWindowResize );
  93. onWindowResize();
  94. stats = new Stats();
  95. document.body.appendChild( stats.dom );
  96. initGui();
  97. }
  98. function onWindowResize() {
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. camera.updateProjectionMatrix();
  101. renderer.setSize( window.innerWidth, window.innerHeight );
  102. insetWidth = window.innerHeight / 4; // square
  103. insetHeight = window.innerHeight / 4;
  104. camera2.aspect = insetWidth / insetHeight;
  105. camera2.updateProjectionMatrix();
  106. }
  107. function animate() {
  108. requestAnimationFrame( animate );
  109. stats.update();
  110. // main scene
  111. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  112. controls.update();
  113. renderer.autoClear = true;
  114. scene.backgroundNode = null;
  115. renderer.render( scene, camera );
  116. // inset scene
  117. renderer.clearDepth(); // important!
  118. renderer.setScissorTest( true );
  119. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  120. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  121. camera2.position.copy( camera.position );
  122. camera2.quaternion.copy( camera.quaternion );
  123. renderer.autoClear = false;
  124. scene.backgroundNode = backgroundNode;
  125. renderer.render( scene, camera2 );
  126. renderer.setScissorTest( false );
  127. }
  128. //
  129. function initGui() {
  130. gui = new GUI();
  131. const param = {
  132. 'width': 10,
  133. 'alphaToCoverage': true,
  134. };
  135. gui.add( param, 'width', 1, 20, 1 ).onChange( function ( val ) {
  136. material.pointWidth = val;
  137. } );
  138. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  139. material.alphaToCoverage = val;
  140. } );
  141. }
  142. </script>
  143. </body>
  144. </html>