1
0

webgpu_lines_fat_raycasting.html 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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 timer;
  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. timer = new THREE.Timer();
  79. timer.connect( document );
  80. renderer = new THREE.WebGPURenderer( { antialias: true, alpha: true, trackTimestamp: true } );
  81. renderer.setPixelRatio( window.devicePixelRatio );
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. renderer.setClearColor( 0x000000, 0.0 );
  84. renderer.setAnimationLoop( animate );
  85. renderer.inspector = new Inspector();
  86. document.body.appendChild( renderer.domElement );
  87. scene = new THREE.Scene();
  88. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  89. camera.position.set( - 40, 0, 60 );
  90. controls = new OrbitControls( camera, renderer.domElement );
  91. controls.minDistance = 10;
  92. controls.maxDistance = 500;
  93. const sphereGeometry = new THREE.SphereGeometry( 0.25, 8, 4 );
  94. const sphereInterMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, depthTest: false } );
  95. const sphereOnLineMaterial = new THREE.MeshBasicMaterial( { color: 0x00ff00, depthTest: false } );
  96. sphereInter = new THREE.Mesh( sphereGeometry, sphereInterMaterial );
  97. sphereOnLine = new THREE.Mesh( sphereGeometry, sphereOnLineMaterial );
  98. sphereInter.visible = false;
  99. sphereOnLine.visible = false;
  100. sphereInter.renderOrder = 10;
  101. sphereOnLine.renderOrder = 10;
  102. scene.add( sphereInter );
  103. scene.add( sphereOnLine );
  104. // Position and THREE.Color Data
  105. const positions = [];
  106. const colors = [];
  107. const points = [];
  108. for ( let i = - 50; i < 50; i ++ ) {
  109. const t = i / 3;
  110. points.push( new THREE.Vector3( t * Math.sin( 2 * t ), t, t * Math.cos( 2 * t ) ) );
  111. }
  112. const spline = new THREE.CatmullRomCurve3( points );
  113. const divisions = Math.round( 3 * points.length );
  114. const point = new THREE.Vector3();
  115. const color = new THREE.Color();
  116. for ( let i = 0, l = divisions; i < l; i ++ ) {
  117. const t = i / l;
  118. spline.getPoint( t, point );
  119. positions.push( point.x, point.y, point.z );
  120. color.setHSL( t, 1.0, 0.5, THREE.SRGBColorSpace );
  121. colors.push( color.r, color.g, color.b );
  122. }
  123. const lineGeometry = new LineGeometry();
  124. lineGeometry.setPositions( positions );
  125. lineGeometry.setColors( colors );
  126. const segmentsGeometry = new LineSegmentsGeometry();
  127. segmentsGeometry.setPositions( positions );
  128. segmentsGeometry.setColors( colors );
  129. segments = new LineSegments2( segmentsGeometry, matLine );
  130. segments.computeLineDistances();
  131. segments.scale.set( 1, 1, 1 );
  132. scene.add( segments );
  133. thresholdSegments = new LineSegments2( segmentsGeometry, matThresholdLine );
  134. thresholdSegments.computeLineDistances();
  135. thresholdSegments.scale.set( 1, 1, 1 );
  136. scene.add( thresholdSegments );
  137. line = new Line2( lineGeometry, matLine );
  138. line.computeLineDistances();
  139. line.scale.set( 1, 1, 1 );
  140. scene.add( line );
  141. thresholdLine = new Line2( lineGeometry, matThresholdLine );
  142. thresholdLine.computeLineDistances();
  143. thresholdLine.scale.set( 1, 1, 1 );
  144. scene.add( thresholdLine );
  145. const geo = new THREE.BufferGeometry();
  146. geo.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ) );
  147. geo.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 3 ) );
  148. //
  149. switchLine( params[ 'line type' ] );
  150. //
  151. document.addEventListener( 'pointermove', onPointerMove );
  152. window.addEventListener( 'resize', onWindowResize );
  153. onWindowResize();
  154. initGui();
  155. }
  156. function onWindowResize() {
  157. camera.aspect = window.innerWidth / window.innerHeight;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( window.innerWidth, window.innerHeight );
  160. }
  161. function onPointerMove( event ) {
  162. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  163. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  164. }
  165. async function animate() {
  166. timer.update();
  167. const delta = timer.getDelta();
  168. const obj = line.visible ? line : segments;
  169. thresholdLine.position.copy( line.position );
  170. thresholdLine.quaternion.copy( line.quaternion );
  171. thresholdSegments.position.copy( segments.position );
  172. thresholdSegments.quaternion.copy( segments.quaternion );
  173. if ( params.animate ) {
  174. line.rotation.y += delta * 0.1;
  175. segments.rotation.y = line.rotation.y;
  176. }
  177. raycaster.setFromCamera( pointer, camera );
  178. const intersects = raycaster.intersectObject( obj );
  179. if ( intersects.length > 0 ) {
  180. sphereInter.visible = true;
  181. sphereOnLine.visible = true;
  182. sphereInter.position.copy( intersects[ 0 ].point );
  183. sphereOnLine.position.copy( intersects[ 0 ].pointOnLine );
  184. const index = intersects[ 0 ].faceIndex;
  185. const colors = obj.geometry.getAttribute( 'instanceColorStart' );
  186. color.fromBufferAttribute( colors, index );
  187. sphereInter.material.color.copy( color ).offsetHSL( 0.3, 0, 0 );
  188. sphereOnLine.material.color.copy( color ).offsetHSL( 0.7, 0, 0 );
  189. renderer.domElement.style.cursor = 'crosshair';
  190. } else {
  191. sphereInter.visible = false;
  192. sphereOnLine.visible = false;
  193. renderer.domElement.style.cursor = '';
  194. }
  195. renderer.render( scene, camera );
  196. }
  197. //
  198. function switchLine( val ) {
  199. switch ( val ) {
  200. case 0:
  201. line.visible = true;
  202. thresholdLine.visible = true;
  203. segments.visible = false;
  204. thresholdSegments.visible = false;
  205. break;
  206. case 1:
  207. line.visible = false;
  208. thresholdLine.visible = false;
  209. segments.visible = true;
  210. thresholdSegments.visible = true;
  211. break;
  212. }
  213. }
  214. function initGui() {
  215. gui = renderer.inspector.createParameters( 'Settings' );
  216. gui.add( params, 'line type', { 'LineGeometry': 0, 'LineSegmentsGeometry': 1 } ).onChange( function ( val ) {
  217. switchLine( val );
  218. } );
  219. gui.add( params, 'world units' ).onChange( function ( val ) {
  220. matLine.worldUnits = val;
  221. matLine.needsUpdate = true;
  222. matThresholdLine.worldUnits = val;
  223. matThresholdLine.needsUpdate = true;
  224. } );
  225. gui.add( params, 'visualize threshold' ).onChange( function ( val ) {
  226. matThresholdLine.visible = val;
  227. } );
  228. gui.add( params, 'width', 1, 10 ).onChange( function ( val ) {
  229. matLine.linewidth = val;
  230. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  231. } );
  232. gui.add( params, 'alphaToCoverage' ).onChange( function ( val ) {
  233. matLine.alphaToCoverage = val;
  234. } );
  235. gui.add( params, 'threshold', 0, 10 ).onChange( function ( val ) {
  236. raycaster.params.Line2.threshold = val;
  237. matThresholdLine.linewidth = matLine.linewidth + raycaster.params.Line2.threshold;
  238. } );
  239. gui.add( params, 'translation', 0, 10 ).onChange( function ( val ) {
  240. line.position.x = val;
  241. segments.position.x = val;
  242. } );
  243. gui.add( params, 'animate' );
  244. }
  245. </script>
  246. </body>
  247. </html>
粤ICP备19079148号