webgpu_lines_fat.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - lines - 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 lines</div>
  12. <!-- Import maps polyfill -->
  13. <!-- Remove this when import maps will be widely supported -->
  14. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/",
  20. "three/nodes": "./jsm/nodes/Nodes.js"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  27. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. import { Line2NodeMaterial, LineDashedNodeMaterial, LineBasicNodeMaterial, color } from 'three/nodes';
  32. import { Line2 } from 'three/addons/lines/Line2.js';
  33. import { LineGeometry } from 'three/addons/lines/LineGeometry.js';
  34. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  35. let line, renderer, scene, camera, camera2, controls, backgroundNode;
  36. let line1;
  37. let matLine, matLineBasic, matLineDashed;
  38. let stats;
  39. let gui;
  40. // viewport
  41. let insetWidth;
  42. let insetHeight;
  43. init();
  44. animate();
  45. function init() {
  46. if ( WebGPU.isAvailable() === false ) {
  47. document.body.appendChild( WebGPU.getErrorMessage() );
  48. throw new Error( 'No WebGPU support' );
  49. }
  50. renderer = new WebGPURenderer( { antialias: true } );
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setClearColor( 0x000000, 0.0 );
  53. renderer.setSize( window.innerWidth, window.innerHeight );
  54. document.body.appendChild( renderer.domElement );
  55. scene = new THREE.Scene();
  56. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  57. camera.position.set( - 40, 0, 60 );
  58. camera2 = new THREE.PerspectiveCamera( 40, 1, 1, 1000 );
  59. camera2.position.copy( camera.position );
  60. controls = new OrbitControls( camera, renderer.domElement );
  61. controls.minDistance = 10;
  62. controls.maxDistance = 500;
  63. backgroundNode = color( 0x222222 );
  64. // Position and THREE.Color Data
  65. const positions = [];
  66. const colors = [];
  67. const points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 20.0, 1, 0, 1, 2, 3, 4, 5, 6, 7 );
  68. const spline = new THREE.CatmullRomCurve3( points );
  69. const divisions = Math.round( 12 * points.length );
  70. const point = new THREE.Vector3();
  71. const lineColor = new THREE.Color();
  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. lineColor.setHSL( t, 1.0, 0.5, THREE.SRGBColorSpace );
  77. colors.push( lineColor.r, lineColor.g, lineColor.b );
  78. }
  79. // Line2 ( LineGeometry, LineMaterial )
  80. const geometry = new LineGeometry();
  81. geometry.setPositions( positions );
  82. geometry.setColors( colors );
  83. geometry.instanceCount = positions.length / 3 - 1;
  84. matLine = new Line2NodeMaterial( {
  85. color: 0xffffff,
  86. linewidth: 5, // in world units with size attenuation, pixels otherwise
  87. vertexColors: true,
  88. dashed: false,
  89. alphaToCoverage: true,
  90. } );
  91. line = new Line2( geometry, matLine );
  92. line.computeLineDistances();
  93. line.scale.set( 1, 1, 1 );
  94. scene.add( line );
  95. const geo = new THREE.BufferGeometry();
  96. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  97. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  98. matLineBasic = new LineBasicNodeMaterial( { vertexColors: true } );
  99. matLineDashed = new LineDashedNodeMaterial( { vertexColors: true, scale: 2, dashSize: 1, gapSize: 1 } );
  100. line1 = new THREE.Line( geo, matLineBasic );
  101. line1.computeLineDistances();
  102. line1.visible = false;
  103. scene.add( line1 );
  104. //
  105. window.addEventListener( 'resize', onWindowResize );
  106. onWindowResize();
  107. stats = new Stats();
  108. document.body.appendChild( stats.dom );
  109. initGui();
  110. }
  111. function onWindowResize() {
  112. camera.aspect = window.innerWidth / window.innerHeight;
  113. camera.updateProjectionMatrix();
  114. renderer.setSize( window.innerWidth, window.innerHeight );
  115. insetWidth = window.innerHeight / 4; // square
  116. insetHeight = window.innerHeight / 4;
  117. camera2.aspect = insetWidth / insetHeight;
  118. camera2.updateProjectionMatrix();
  119. }
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. stats.update();
  123. // main scene
  124. renderer.setClearColor( 0x000000, 0 );
  125. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  126. renderer.autoClear = true;
  127. scene.backgroundNode = null;
  128. renderer.render( scene, camera );
  129. // inset scene
  130. renderer.clearDepth(); // important!
  131. renderer.setScissorTest( true );
  132. renderer.setScissor( 20, window.innerHeight - insetHeight - 20, insetWidth, insetHeight );
  133. renderer.setViewport( 20, window.innerHeight - insetHeight - 20, insetWidth, insetHeight );
  134. camera2.position.copy( camera.position );
  135. camera2.quaternion.copy( camera.quaternion );
  136. renderer.autoClear = false;
  137. scene.backgroundNode = backgroundNode;
  138. renderer.render( scene, camera2 );
  139. renderer.setScissorTest( false );
  140. }
  141. //
  142. function initGui() {
  143. gui = new GUI();
  144. const param = {
  145. 'line type': 0,
  146. 'world units': false,
  147. 'width': 5,
  148. 'alphaToCoverage': true,
  149. 'dashed': false,
  150. 'dash offset': 0,
  151. 'dash scale': 1,
  152. 'dash / gap': 1
  153. };
  154. gui.add( param, 'line type', { 'LineGeometry': 0, '"line-strip"': 1 } ).onChange( function ( val ) {
  155. switch ( val ) {
  156. case 0:
  157. line.visible = true;
  158. line1.visible = false;
  159. break;
  160. case 1:
  161. line.visible = false;
  162. line1.visible = true;
  163. break;
  164. }
  165. } );
  166. gui.add( param, 'world units' ).onChange( function ( val ) {
  167. matLine.worldUnits = val;
  168. matLine.needsUpdate = true;
  169. } );
  170. gui.add( param, 'width', 1, 10 ).onChange( function ( val ) {
  171. matLine.linewidth = val;
  172. } );
  173. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  174. matLine.alphaToCoverage = val;
  175. } );
  176. gui.add( param, 'dashed' ).onChange( function ( val ) {
  177. matLine.dashed = val;
  178. line1.material = val ? matLineDashed : matLineBasic;
  179. } );
  180. gui.add( param, 'dash scale', 0.5, 2, 0.1 ).onChange( function ( val ) {
  181. matLine.scale = val;
  182. matLineDashed.scale = val;
  183. } );
  184. gui.add( param, 'dash offset', 0, 5, 0.1 ).onChange( function ( val ) {
  185. matLine.dashOffset = val;
  186. matLineDashed.dashOffset = val;
  187. } );
  188. gui.add( param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 } ).onChange( function ( val ) {
  189. switch ( val ) {
  190. case 0:
  191. matLine.dashSize = 2;
  192. matLine.gapSize = 1;
  193. matLineDashed.dashSize = 2;
  194. matLineDashed.gapSize = 1;
  195. break;
  196. case 1:
  197. matLine.dashSize = 1;
  198. matLine.gapSize = 1;
  199. matLineDashed.dashSize = 1;
  200. matLineDashed.gapSize = 1;
  201. break;
  202. case 2:
  203. matLine.dashSize = 1;
  204. matLine.gapSize = 2;
  205. matLineDashed.dashSize = 1;
  206. matLineDashed.gapSize = 2;
  207. break;
  208. }
  209. } );
  210. }
  211. </script>
  212. </body>
  213. </html>