webgl_lines_dashed.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. <!-- Import maps polyfill -->
  12. <!-- Remove this when import maps will be widely supported -->
  13. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import * as GeometryUtils from 'three/addons/utils/GeometryUtils.js';
  26. let renderer, scene, camera, stats;
  27. const objects = [];
  28. const WIDTH = window.innerWidth, HEIGHT = window.innerHeight;
  29. init();
  30. animate();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 60, WIDTH / HEIGHT, 1, 200 );
  33. camera.position.z = 150;
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0x111111 );
  36. scene.fog = new THREE.Fog( 0x111111, 150, 200 );
  37. const subdivisions = 6;
  38. const recursion = 1;
  39. const points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7 );
  40. const spline = new THREE.CatmullRomCurve3( points );
  41. const samples = spline.getPoints( points.length * subdivisions );
  42. const geometrySpline = new THREE.BufferGeometry().setFromPoints( samples );
  43. const line = new THREE.Line( geometrySpline, new THREE.LineDashedMaterial( { color: 0xffffff, dashSize: 1, gapSize: 0.5 } ) );
  44. line.computeLineDistances();
  45. objects.push( line );
  46. scene.add( line );
  47. const geometryBox = box( 50, 50, 50 );
  48. const lineSegments = new THREE.LineSegments( geometryBox, new THREE.LineDashedMaterial( { color: 0xffaa00, dashSize: 3, gapSize: 1 } ) );
  49. lineSegments.computeLineDistances();
  50. objects.push( lineSegments );
  51. scene.add( lineSegments );
  52. renderer = new THREE.WebGLRenderer( { antialias: true } );
  53. renderer.setPixelRatio( window.devicePixelRatio );
  54. renderer.setSize( WIDTH, HEIGHT );
  55. document.body.appendChild( renderer.domElement );
  56. stats = new Stats();
  57. document.body.appendChild( stats.dom );
  58. //
  59. window.addEventListener( 'resize', onWindowResize );
  60. }
  61. function box( width, height, depth ) {
  62. width = width * 0.5,
  63. height = height * 0.5,
  64. depth = depth * 0.5;
  65. const geometry = new THREE.BufferGeometry();
  66. const position = [];
  67. position.push(
  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. width, height, depth,
  90. width, - height, - depth,
  91. width, - height, depth
  92. );
  93. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  94. return geometry;
  95. }
  96. function onWindowResize() {
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. }
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. render();
  104. stats.update();
  105. }
  106. function render() {
  107. const time = Date.now() * 0.001;
  108. scene.traverse( function ( object ) {
  109. if ( object.isLine ) {
  110. object.rotation.x = 0.25 * time;
  111. object.rotation.y = 0.25 * time;
  112. }
  113. } );
  114. renderer.render( scene, camera );
  115. }
  116. </script>
  117. </body>
  118. </html>