1
0

webgpu_lines_fat_raycasting.html 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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_raycasting.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lines_fat_raycasting.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  17. <div class="title-wrapper">
  18. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Fat Lines</span>
  19. </div>
  20. <small>
  21. Raycasting on fat lines. Move mouse to test raycasting.
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { Inspector } from 'three/addons/inspector/Inspector.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { LineSegments2 } from 'three/addons/lines/webgpu/LineSegments2.js';
  39. import { LineSegmentsGeometry } from 'three/addons/lines/LineSegmentsGeometry.js';
  40. import { Line2 } from 'three/addons/lines/webgpu/Line2.js';
  41. import { LineGeometry } from 'three/addons/lines/LineGeometry.js';
  42. //
  43. let line, thresholdLine, segments, thresholdSegments;
  44. let renderer, scene, camera, controls;
  45. let sphereInter, sphereOnLine;
  46. let gui;
  47. let timer;
  48. const color = new THREE.Color();
  49. const pointer = new THREE.Vector2( Infinity, Infinity );
  50. const raycaster = new THREE.Raycaster();
  51. raycaster.params.Line2 = {};
  52. raycaster.params.Line2.threshold = 0;
  53. const matLine = new THREE.Line2NodeMaterial( {
  54. color: 0xffffff,
  55. linewidth: 1, // in world units with size attenuation, pixels otherwise
  56. worldUnits: true,
  57. vertexColors: true,
  58. alphaToCoverage: true,
  59. } );
  60. const matThresholdLine = new THREE.Line2NodeMaterial( {
  61. color: 0xffffff,
  62. linewidth: matLine.linewidth, // in world units with size attenuation, pixels otherwise
  63. worldUnits: true,
  64. // vertexColors: true,
  65. transparent: true,
  66. opacity: 0.2,
  67. depthTest: false,
  68. visible: false,
  69. } );
  70. const params = {
  71. 'line type': 1,
  72. 'world units': matLine.worldUnits,
  73. 'visualize threshold': matThresholdLine.visible,
  74. 'width': matLine.linewidth,
  75. 'alphaToCoverage': matLine.alphaToCoverage,
  76. 'threshold': raycaster.params.Line2.threshold,
  77. 'translation': 0,
  78. 'animate': true
  79. };
  80. init();
  81. function init() {
  82. timer = new THREE.Timer();
  83. timer.connect( document );
  84. renderer = new THREE.WebGPURenderer( { antialias: true, alpha: true, trackTimestamp: true } );
  85. renderer.setPixelRatio( window.devicePixelRatio );
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. renderer.setClearColor( 0x000000 );
  88. renderer.setAnimationLoop( animate );
  89. renderer.inspector = new Inspector();
  90. document.body.appendChild( renderer.domElement );
  91. scene = new THREE.Scene();
  92. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  93. camera.position.set( - 40, 0, 60 );
  94. controls = new OrbitControls( camera, renderer.domElement );
  95. controls.minDistance = 10;
  96. controls.maxDistance = 500;
  97. const sphereGeometry = new THREE.SphereGeometry( 0.25, 8, 4 );
  98. const sphereInterMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, depthTest: false } );
  99. const sphereOnLineMaterial = new THREE.MeshBasicMaterial( { color: 0x00ff00, depthTest: false } );
  100. sphereInter = new THREE.Mesh( sphereGeometry, sphereInterMaterial );
  101. sphereOnLine = new THREE.Mesh( sphereGeometry, sphereOnLineMaterial );
  102. sphereInter.visible = false;
  103. sphereOnLine.visible = false;
  104. sphereInter.renderOrder = 10;
  105. sphereOnLine.renderOrder = 10;
  106. scene.add( sphereInter );
  107. scene.add( sphereOnLine );
  108. // Position and THREE.Color Data
  109. const positions = [];
  110. const colors = [];
  111. const points = [];
  112. for ( let i = - 50; i < 50; i ++ ) {
  113. const t = i / 3;
  114. points.push( new THREE.Vector3( t * Math.sin( 2 * t ), t, t * Math.cos( 2 * t ) ) );
  115. }
  116. const spline = new THREE.CatmullRomCurve3( points );
  117. const divisions = Math.round( 3 * points.length );
  118. const point = new THREE.Vector3();
  119. const color = new THREE.Color();
  120. for ( let i = 0, l = divisions; i < l; i ++ ) {
  121. const t = i / l;
  122. spline.getPoint( t, point );
  123. positions.push( point.x, point.y, point.z );
  124. color.setHSL( t, 1.0, 0.5, THREE.SRGBColorSpace );
  125. colors.push( color.r, color.g, color.b );
  126. }
  127. const lineGeometry = new LineGeometry();
  128. lineGeometry.setPositions( positions );
  129. lineGeometry.setColors( colors );
  130. const segmentsGeometry = new LineSegmentsGeometry();
  131. segmentsGeometry.setPositions( positions );
  132. segmentsGeometry.setColors( colors );
  133. segments = new LineSegments2( segmentsGeometry, matLine );
  134. segments.computeLineDistances();
  135. segments.scale.set( 1, 1, 1 );
  136. scene.add( segments );
  137. thresholdSegments = new LineSegments2( segmentsGeometry, matThresholdLine );
  138. thresholdSegments.computeLineDistances();
  139. thresholdSegments.scale.set( 1, 1, 1 );
  140. scene.add( thresholdSegments );
  141. line = new Line2( lineGeometry, matLine );
  142. line.computeLineDistances();
  143. line.scale.set( 1, 1, 1 );
  144. scene.add( line );
  145. thresholdLine = new Line2( lineGeometry, matThresholdLine );
  146. thresholdLine.computeLineDistances();
  147. thresholdLine.scale.set( 1, 1, 1 );
  148. scene.add( thresholdLine );
  149. const geo = new THREE.BufferGeometry();
  150. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  151. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  152. //
  153. switchLine( params[ 'line type' ] );
  154. //
  155. document.addEventListener( 'pointermove', onPointerMove );
  156. window.addEventListener( 'resize', onWindowResize );
  157. onWindowResize();
  158. initGui();
  159. }
  160. function onWindowResize() {
  161. camera.aspect = window.innerWidth / window.innerHeight;
  162. camera.updateProjectionMatrix();
  163. renderer.setSize( window.innerWidth, window.innerHeight );
  164. }
  165. function onPointerMove( event ) {
  166. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  167. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  168. }
  169. async function animate() {
  170. timer.update();
  171. const delta = timer.getDelta();
  172. const obj = line.visible ? line : segments;
  173. thresholdLine.position.copy( line.position );
  174. thresholdLine.quaternion.copy( line.quaternion );
  175. thresholdSegments.position.copy( segments.position );
  176. thresholdSegments.quaternion.copy( segments.quaternion );
  177. if ( params.animate ) {
  178. line.rotation.y += delta * 0.1;
  179. segments.rotation.y = line.rotation.y;
  180. }
  181. raycaster.setFromCamera( pointer, camera );
  182. const intersects = raycaster.intersectObject( obj );
  183. if ( intersects.length > 0 ) {
  184. sphereInter.visible = true;
  185. sphereOnLine.visible = true;
  186. sphereInter.position.copy( intersects[ 0 ].point );
  187. sphereOnLine.position.copy( intersects[ 0 ].pointOnLine );
  188. const index = intersects[ 0 ].faceIndex;
  189. const colors = obj.geometry.getAttribute( 'instanceColorStart' );
  190. color.fromBufferAttribute( colors, index );
  191. sphereInter.material.color.copy( color ).offsetHSL( 0.3, 0, 0 );
  192. sphereOnLine.material.color.copy( color ).offsetHSL( 0.7, 0, 0 );
  193. renderer.domElement.style.cursor = 'crosshair';
  194. } else {
  195. sphereInter.visible = false;
  196. sphereOnLine.visible = false;
  197. renderer.domElement.style.cursor = '';
  198. }
  199. renderer.render( scene, camera );
  200. }
  201. //
  202. function switchLine( val ) {
  203. switch ( val ) {
  204. case 0:
  205. line.visible = true;
  206. thresholdLine.visible = true;
  207. segments.visible = false;
  208. thresholdSegments.visible = false;
  209. break;
  210. case 1:
  211. line.visible = false;
  212. thresholdLine.visible = false;
  213. segments.visible = true;
  214. thresholdSegments.visible = true;
  215. break;
  216. }
  217. }
  218. function initGui() {
  219. gui = renderer.inspector.createParameters( 'Settings' );
  220. gui.add( params, 'line type', { 'LineGeometry': 0, 'LineSegmentsGeometry': 1 } ).onChange( function ( val ) {
  221. switchLine( val );
  222. } );
  223. gui.add( params, 'world units' ).onChange( function ( val ) {
  224. matLine.worldUnits = val;
  225. matLine.needsUpdate = true;
  226. matThresholdLine.worldUnits = val;
  227. matThresholdLine.needsUpdate = true;
  228. } );
  229. gui.add( params, 'visualize threshold' ).onChange( function ( val ) {
  230. matThresholdLine.visible = val;
  231. } );
  232. gui.add( params, 'width', 1, 10 ).onChange( function ( val ) {
  233. matLine.linewidth = val;
  234. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  235. } );
  236. gui.add( params, 'alphaToCoverage' ).onChange( function ( val ) {
  237. matLine.alphaToCoverage = val;
  238. } );
  239. gui.add( params, 'threshold', 0, 10 ).onChange( function ( val ) {
  240. raycaster.params.Line2.threshold = val;
  241. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  242. } );
  243. gui.add( params, 'translation', 0, 10 ).onChange( function ( val ) {
  244. line.position.x = val;
  245. segments.position.x = val;
  246. } );
  247. gui.add( params, 'animate' );
  248. }
  249. </script>
  250. </body>
  251. </html>
粤ICP备19079148号