webgpu_lines_fat.html 7.3 KB

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