webgpu_lines_fat.html 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. <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 WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  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 { Line2NodeMaterial, LineDashedNodeMaterial, LineBasicNodeMaterial, color } from 'three/nodes';
  29. import { Line2 } from 'three/addons/lines/Line2.js';
  30. import { LineGeometry } from 'three/addons/lines/LineGeometry.js';
  31. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  32. let line, renderer, scene, camera, camera2, controls, backgroundNode;
  33. let line1;
  34. let matLine, matLineBasic, matLineDashed;
  35. let stats;
  36. let gui;
  37. // viewport
  38. let insetWidth;
  39. let insetHeight;
  40. init();
  41. animate();
  42. function init() {
  43. if ( WebGPU.isAvailable() === false ) {
  44. document.body.appendChild( WebGPU.getErrorMessage() );
  45. throw new Error( 'No WebGPU support' );
  46. }
  47. renderer = new WebGPURenderer( { antialias: true } );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setClearColor( 0x000000, 0.0 );
  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.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( 12 * points.length );
  67. const point = new THREE.Vector3();
  68. const lineColor = 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. lineColor.setHSL( t, 1.0, 0.5, THREE.SRGBColorSpace );
  74. colors.push( lineColor.r, lineColor.g, lineColor.b );
  75. }
  76. // Line2 ( LineGeometry, LineMaterial )
  77. const geometry = new LineGeometry();
  78. geometry.setPositions( positions );
  79. geometry.setColors( colors );
  80. geometry.instanceCount = positions.length / 3 - 1;
  81. matLine = new Line2NodeMaterial( {
  82. color: 0xffffff,
  83. linewidth: 5, // in world units with size attenuation, pixels otherwise
  84. vertexColors: true,
  85. dashed: false,
  86. alphaToCoverage: true,
  87. } );
  88. line = new Line2( geometry, matLine );
  89. line.computeLineDistances();
  90. line.scale.set( 1, 1, 1 );
  91. scene.add( line );
  92. const geo = new THREE.BufferGeometry();
  93. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  94. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  95. matLineBasic = new LineBasicNodeMaterial( { vertexColors: true } );
  96. matLineDashed = new LineDashedNodeMaterial( { vertexColors: true, scale: 2, dashSize: 1, gapSize: 1 } );
  97. line1 = new THREE.Line( geo, matLineBasic );
  98. line1.computeLineDistances();
  99. line1.visible = false;
  100. scene.add( line1 );
  101. //
  102. window.addEventListener( 'resize', onWindowResize );
  103. onWindowResize();
  104. stats = new Stats();
  105. document.body.appendChild( stats.dom );
  106. initGui();
  107. }
  108. function onWindowResize() {
  109. camera.aspect = window.innerWidth / window.innerHeight;
  110. camera.updateProjectionMatrix();
  111. renderer.setSize( window.innerWidth, window.innerHeight );
  112. insetWidth = window.innerHeight / 4; // square
  113. insetHeight = window.innerHeight / 4;
  114. camera2.aspect = insetWidth / insetHeight;
  115. camera2.updateProjectionMatrix();
  116. }
  117. function animate() {
  118. requestAnimationFrame( animate );
  119. stats.update();
  120. // main scene
  121. renderer.setClearColor( 0x000000, 0 );
  122. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  123. renderer.autoClear = true;
  124. scene.backgroundNode = null;
  125. renderer.render( scene, camera );
  126. // inset scene
  127. renderer.clearDepth(); // important!
  128. renderer.setScissorTest( true );
  129. renderer.setScissor( 20, 20, insetWidth, insetHeight );
  130. renderer.setViewport( 20, 20, insetWidth, insetHeight );
  131. camera2.position.copy( camera.position );
  132. camera2.quaternion.copy( camera.quaternion );
  133. renderer.autoClear = false;
  134. scene.backgroundNode = backgroundNode;
  135. renderer.render( scene, camera2 );
  136. renderer.setScissorTest( false );
  137. }
  138. //
  139. function initGui() {
  140. gui = new GUI();
  141. const param = {
  142. 'line type': 0,
  143. 'world units': false,
  144. 'width': 5,
  145. 'alphaToCoverage': true,
  146. 'dashed': false,
  147. 'dash offset': 0,
  148. 'dash scale': 1,
  149. 'dash / gap': 1
  150. };
  151. gui.add( param, 'line type', { 'LineGeometry': 0, '"line-strip"': 1 } ).onChange( function ( val ) {
  152. switch ( val ) {
  153. case 0:
  154. line.visible = true;
  155. line1.visible = false;
  156. break;
  157. case 1:
  158. line.visible = false;
  159. line1.visible = true;
  160. break;
  161. }
  162. } );
  163. gui.add( param, 'world units' ).onChange( function ( val ) {
  164. matLine.worldUnits = val;
  165. matLine.needsUpdate = true;
  166. } );
  167. gui.add( param, 'width', 1, 10 ).onChange( function ( val ) {
  168. matLine.linewidth = val;
  169. } );
  170. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  171. matLine.alphaToCoverage = val;
  172. } );
  173. gui.add( param, 'dashed' ).onChange( function ( val ) {
  174. matLine.dashed = val;
  175. line1.material = val ? matLineDashed : matLineBasic;
  176. } );
  177. gui.add( param, 'dash scale', 0.5, 2, 0.1 ).onChange( function ( val ) {
  178. matLine.scale = val;
  179. matLineDashed.scale = val;
  180. } );
  181. gui.add( param, 'dash offset', 0, 5, 0.1 ).onChange( function ( val ) {
  182. matLine.dashOffset = val;
  183. matLineDashed.dashOffset = val;
  184. } );
  185. gui.add( param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 } ).onChange( function ( val ) {
  186. switch ( val ) {
  187. case 0:
  188. matLine.dashSize = 2;
  189. matLine.gapSize = 1;
  190. matLineDashed.dashSize = 2;
  191. matLineDashed.gapSize = 1;
  192. break;
  193. case 1:
  194. matLine.dashSize = 1;
  195. matLine.gapSize = 1;
  196. matLineDashed.dashSize = 1;
  197. matLineDashed.gapSize = 1;
  198. break;
  199. case 2:
  200. matLine.dashSize = 1;
  201. matLine.gapSize = 2;
  202. matLineDashed.dashSize = 1;
  203. matLineDashed.gapSize = 2;
  204. break;
  205. }
  206. } );
  207. }
  208. </script>
  209. </body>
  210. </html>