webgl_lines_dashed.html 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. THREE.ColorManagement.enabled = true;
  28. let renderer, scene, camera, stats;
  29. const objects = [];
  30. const WIDTH = window.innerWidth, HEIGHT = window.innerHeight;
  31. init();
  32. animate();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 60, WIDTH / HEIGHT, 1, 200 );
  35. camera.position.z = 150;
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0x111111 );
  38. scene.fog = new THREE.Fog( 0x111111, 150, 200 );
  39. const subdivisions = 6;
  40. const recursion = 1;
  41. const points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7 );
  42. const spline = new THREE.CatmullRomCurve3( points );
  43. const samples = spline.getPoints( points.length * subdivisions );
  44. const geometrySpline = new THREE.BufferGeometry().setFromPoints( samples );
  45. const line = new THREE.Line( geometrySpline, new THREE.LineDashedMaterial( { color: 0xffffff, dashSize: 1, gapSize: 0.5 } ) );
  46. line.computeLineDistances();
  47. objects.push( line );
  48. scene.add( line );
  49. const geometryBox = box( 50, 50, 50 );
  50. const lineSegments = new THREE.LineSegments( geometryBox, new THREE.LineDashedMaterial( { color: 0xffaa00, dashSize: 3, gapSize: 1 } ) );
  51. lineSegments.computeLineDistances();
  52. objects.push( lineSegments );
  53. scene.add( lineSegments );
  54. renderer = new THREE.WebGLRenderer( { antialias: true } );
  55. renderer.setPixelRatio( window.devicePixelRatio );
  56. renderer.setSize( WIDTH, HEIGHT );
  57. const container = document.getElementById( 'container' );
  58. container.appendChild( renderer.domElement );
  59. stats = new Stats();
  60. container.appendChild( stats.dom );
  61. //
  62. window.addEventListener( 'resize', onWindowResize );
  63. }
  64. function box( width, height, depth ) {
  65. width = width * 0.5,
  66. height = height * 0.5,
  67. depth = depth * 0.5;
  68. const geometry = new THREE.BufferGeometry();
  69. const position = [];
  70. position.push(
  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. width, - height, depth
  95. );
  96. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  97. return geometry;
  98. }
  99. function onWindowResize() {
  100. camera.aspect = window.innerWidth / window.innerHeight;
  101. camera.updateProjectionMatrix();
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. }
  104. function animate() {
  105. requestAnimationFrame( animate );
  106. render();
  107. stats.update();
  108. }
  109. function render() {
  110. const time = Date.now() * 0.001;
  111. scene.traverse( function ( object ) {
  112. if ( object.isLine ) {
  113. object.rotation.x = 0.25 * time;
  114. object.rotation.y = 0.25 * time;
  115. }
  116. } );
  117. renderer.render( scene, camera );
  118. }
  119. </script>
  120. </body>
  121. </html>