webgpu_lines_fat.html 7.6 KB

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