webgpu_lines_fat.html 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. <meta property="og:title" content="three.js webgpu - fat lines">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_lines_fat.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lines_fat.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank">three.js</a> - fat lines</div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.webgpu.js",
  20. "three/webgpu": "../build/three.webgpu.js",
  21. "three/tsl": "../build/three.tsl.js",
  22. "three/addons/": "./jsm/"
  23. }
  24. }
  25. </script>
  26. <script type="module">
  27. import * as THREE from 'three/webgpu';
  28. import { color } from 'three/tsl';
  29. import Stats from 'three/addons/libs/stats.module.js';
  30. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  31. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  32. import { Line2 } from 'three/addons/lines/webgpu/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. function init() {
  45. renderer = new THREE.WebGPURenderer( { antialias: true } );
  46. renderer.setPixelRatio( window.devicePixelRatio );
  47. renderer.setClearColor( 0x000000 );
  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( 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. matLine = new THREE.Line2NodeMaterial( {
  81. color: 0xffffff,
  82. linewidth: 5, // in world units with size attenuation, pixels otherwise
  83. vertexColors: true,
  84. dashed: false,
  85. alphaToCoverage: false,
  86. } );
  87. line = new Line2( geometry, matLine );
  88. line.computeLineDistances();
  89. line.scale.set( 1, 1, 1 );
  90. scene.add( line );
  91. // THREE.Line ( THREE.BufferGeometry, THREE.LineBasicNodeMaterial ) - rendered with gl.LINE_STRIP
  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 THREE.LineBasicNodeMaterial( { vertexColors: true } );
  96. matLineDashed = new THREE.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. stats.update();
  119. // main scene
  120. renderer.setClearColor( 0x000000 );
  121. renderer.setViewport( 0, 0, window.innerWidth, window.innerHeight );
  122. controls.update();
  123. renderer.autoClear = true;
  124. scene.backgroundNode = null;
  125. renderer.render( scene, camera );
  126. // inset scene
  127. const posY = window.innerHeight - insetHeight - 20;
  128. renderer.clearDepth(); // important!
  129. renderer.setScissorTest( true );
  130. renderer.setScissor( 20, posY, insetWidth, insetHeight );
  131. renderer.setViewport( 20, posY, 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': 10,
  146. 'alphaToCoverage': false,
  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. if ( val ) {
  168. widthController.name( 'width (world units)' ).min( 0.1 ).max( 0.5 ).setValue( 0.5 );
  169. } else {
  170. widthController.name( 'width (pixels)' ).min( 1 ).max( 10 ).setValue( 10 );
  171. }
  172. } );
  173. const widthController = gui.add( param, 'width', 1, 10 ).name( 'width (pixels)' ).onChange( function ( val ) {
  174. matLine.linewidth = val;
  175. } );
  176. gui.add( param, 'alphaToCoverage' ).onChange( function ( val ) {
  177. matLine.alphaToCoverage = val;
  178. } );
  179. gui.add( param, 'dashed' ).onChange( function ( val ) {
  180. matLine.dashed = val;
  181. line1.material = val ? matLineDashed : matLineBasic;
  182. } );
  183. gui.add( param, 'dash scale', 0.5, 2, 0.1 ).onChange( function ( val ) {
  184. matLine.scale = val;
  185. matLineDashed.scale = val;
  186. } );
  187. gui.add( param, 'dash offset', 0, 5, 0.1 ).onChange( function ( val ) {
  188. matLine.dashOffset = val;
  189. matLineDashed.dashOffset = val;
  190. } );
  191. gui.add( param, 'dash / gap', { '2 : 1': 0, '1 : 1': 1, '1 : 2': 2 } ).onChange( function ( val ) {
  192. switch ( val ) {
  193. case 0:
  194. matLine.dashSize = 2;
  195. matLine.gapSize = 1;
  196. matLineDashed.dashSize = 2;
  197. matLineDashed.gapSize = 1;
  198. break;
  199. case 1:
  200. matLine.dashSize = 1;
  201. matLine.gapSize = 1;
  202. matLineDashed.dashSize = 1;
  203. matLineDashed.gapSize = 1;
  204. break;
  205. case 2:
  206. matLine.dashSize = 1;
  207. matLine.gapSize = 2;
  208. matLineDashed.dashSize = 1;
  209. matLineDashed.gapSize = 2;
  210. break;
  211. }
  212. } );
  213. }
  214. </script>
  215. </body>
  216. </html>
粤ICP备19079148号