webgl_lines_dashed.html 4.3 KB

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