webgpu_lines_fat_raycasting.html 9.4 KB

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