webgl_lines_dashed.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - dashed 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="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - dashed lines example</div>
  11. <script type="importmap">
  12. {
  13. "imports": {
  14. "three": "../build/three.module.js",
  15. "three/addons/": "./jsm/"
  16. }
  17. }
  18. </script>
  19. <script type="module">
  20. import * as THREE from 'three';
  21. import Stats from 'three/addons/libs/stats.module.js';
  22. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  23. let renderer, scene, camera, stats;
  24. const objects = [];
  25. const WIDTH = window.innerWidth, HEIGHT = window.innerHeight;
  26. init();
  27. function init() {
  28. camera = new THREE.PerspectiveCamera( 60, WIDTH / HEIGHT, 1, 200 );
  29. camera.position.z = 150;
  30. scene = new THREE.Scene();
  31. scene.background = new THREE.Color( 0x111111 );
  32. scene.fog = new THREE.Fog( 0x111111, 150, 200 );
  33. const subdivisions = 6;
  34. const recursion = 1;
  35. const points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7 );
  36. const spline = new THREE.CatmullRomCurve3( points );
  37. const samples = spline.getPoints( points.length * subdivisions );
  38. const geometrySpline = new THREE.BufferGeometry().setFromPoints( samples );
  39. const line = new THREE.Line( geometrySpline, new THREE.LineDashedMaterial( { color: 0xffffff, dashSize: 1, gapSize: 0.5 } ) );
  40. line.computeLineDistances();
  41. objects.push( line );
  42. scene.add( line );
  43. const geometryBox = box( 50, 50, 50 );
  44. const lineSegments = new THREE.LineSegments( geometryBox, new THREE.LineDashedMaterial( { color: 0xffaa00, dashSize: 3, gapSize: 1 } ) );
  45. lineSegments.computeLineDistances();
  46. objects.push( lineSegments );
  47. scene.add( lineSegments );
  48. renderer = new THREE.WebGLRenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( WIDTH, HEIGHT );
  51. renderer.setAnimationLoop( animate );
  52. document.body.appendChild( renderer.domElement );
  53. stats = new Stats();
  54. document.body.appendChild( stats.dom );
  55. //
  56. window.addEventListener( 'resize', onWindowResize );
  57. }
  58. function box( width, height, depth ) {
  59. width = width * 0.5,
  60. height = height * 0.5,
  61. depth = depth * 0.5;
  62. const geometry = new THREE.BufferGeometry();
  63. const position = [];
  64. position.push(
  65. - width, - height, - depth,
  66. - width, height, - depth,
  67. - width, height, - depth,
  68. width, height, - depth,
  69. width, height, - depth,
  70. width, - height, - depth,
  71. width, - height, - depth,
  72. - width, - height, - depth,
  73. - width, - height, depth,
  74. - width, height, depth,
  75. - width, height, depth,
  76. width, height, depth,
  77. width, height, depth,
  78. width, - height, depth,
  79. width, - height, depth,
  80. - width, - height, depth,
  81. - width, - height, - depth,
  82. - width, - height, depth,
  83. - width, height, - depth,
  84. - width, height, depth,
  85. width, height, - depth,
  86. width, height, depth,
  87. width, - height, - depth,
  88. width, - height, depth
  89. );
  90. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  91. return geometry;
  92. }
  93. function onWindowResize() {
  94. camera.aspect = window.innerWidth / window.innerHeight;
  95. camera.updateProjectionMatrix();
  96. renderer.setSize( window.innerWidth, window.innerHeight );
  97. }
  98. function animate() {
  99. render();
  100. stats.update();
  101. }
  102. function render() {
  103. const time = Date.now() * 0.001;
  104. scene.traverse( function ( object ) {
  105. if ( object.isLine ) {
  106. object.rotation.x = 0.25 * time;
  107. object.rotation.y = 0.25 * time;
  108. }
  109. } );
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>