webgpu_lines_fat.html 7.6 KB

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