webgpu_instance_points.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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> - instanced 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 InstancedPoints from 'three/addons/objects/InstancedPoints.js';
  30. import InstancedPointsGeometry from 'three/addons/geometries/InstancedPointsGeometry.js';
  31. import { color, InstancedPointsNodeMaterial } from 'three/nodes';
  32. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  33. let renderer, scene, camera, camera2, controls, backgroundNode;
  34. let material;
  35. let stats;
  36. let gui;
  37. // viewport
  38. let insetWidth;
  39. let insetHeight;
  40. init();
  41. function init() {
  42. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  43. document.body.appendChild( WebGPU.getErrorMessage() );
  44. throw new Error( 'No WebGPU or WebGL2 support' );
  45. }
  46. renderer = new WebGPURenderer( { antialias: true } );
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( window.innerWidth, window.innerHeight );
  49. renderer.setAnimationLoop( animate );
  50. document.body.appendChild( renderer.domElement );
  51. scene = new THREE.Scene();
  52. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  53. camera.position.set( - 40, 0, 60 );
  54. camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
  55. camera2.position.copy( camera.position );
  56. controls = new OrbitControls( camera, renderer.domElement );
  57. controls.enableDamping = true;
  58. controls.minDistance = 10;
  59. controls.maxDistance = 500;
  60. backgroundNode = color( 0x222222 );
  61. // Position and THREE.Color Data
  62. const positions = [];
  63. const colors = [];
  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. for ( let i = 0, l = divisions; i < l; i ++ ) {
  70. const t = i / l;
  71. spline.getPoint( t, point );
  72. positions.push( point.x, point.y, point.z );
  73. pointColor.setHSL( t, 1.0, 0.5, THREE.SRGBColorSpace );
  74. colors.push( pointColor.r, pointColor.g, pointColor.b );
  75. }
  76. // Instanced Points
  77. const geometry = new InstancedPointsGeometry();
  78. geometry.setPositions( positions );
  79. geometry.setColors( colors );
  80. geometry.instanceCount = positions.length / 3; // this should not be necessary
  81. material = new InstancedPointsNodeMaterial( {
  82. color: 0xffffff,
  83. pointWidth: 10, // in pixel units
  84. vertexColors: true,
  85. alphaToCoverage: true,
  86. } );
  87. const instancedPoints = new InstancedPoints( geometry, material );
  88. instancedPoints.scale.set( 1, 1, 1 );
  89. scene.add( instancedPoints );
  90. //
  91. window.addEventListener( 'resize', onWindowResize );
  92. onWindowResize();
  93. stats = new Stats();
  94. document.body.appendChild( stats.dom );
  95. initGui();
  96. }
  97. function onWindowResize() {
  98. camera.aspect = window.innerWidth / window.innerHeight;
  99. camera.updateProjectionMatrix();
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. insetWidth = window.innerHeight / 4; // square
  102. insetHeight = window.innerHeight / 4;
  103. camera2.aspect = insetWidth / insetHeight;
  104. camera2.updateProjectionMatrix();
  105. }
  106. function animate() {
  107. stats.update();
  108. // main scene
  109. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  110. controls.update();
  111. renderer.autoClear = true;
  112. scene.backgroundNode = null;
  113. renderer.render( scene, camera );
  114. // inset scene
  115. renderer.clearDepth(); // important!
  116. renderer.setScissorTest( true );
  117. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  118. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  119. camera2.position.copy( camera.position );
  120. camera2.quaternion.copy( camera.quaternion );
  121. renderer.autoClear = false;
  122. scene.backgroundNode = backgroundNode;
  123. renderer.render( scene, camera2 );
  124. renderer.setScissorTest( false );
  125. }
  126. //
  127. function initGui() {
  128. gui = new GUI();
  129. const param = {
  130. 'width': 10,
  131. 'alphaToCoverage': true,
  132. };
  133. gui.add( param, 'width', 1, 20, 1 ).onChange( function ( val ) {
  134. material.pointWidth = val;
  135. } );
  136. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  137. material.alphaToCoverage = val;
  138. } );
  139. }
  140. </script>
  141. </body>
  142. </html>