webgl_lines_dashed.html 4.0 KB

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