webgl_lines_dashed.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. var renderer, scene, camera, stats;
  17. var objects = [];
  18. var 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. var subdivisions = 6;
  28. var recursion = 1;
  29. var points = GeometryUtils.hilbert3D( new THREE.Vector3( 0, 0, 0 ), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7 );
  30. var spline = new THREE.CatmullRomCurve3( points );
  31. var samples = spline.getPoints( points.length * subdivisions );
  32. var geometrySpline = new THREE.BufferGeometry().setFromPoints( samples );
  33. var 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. var geometryCube = cube( 50 );
  38. var lineSegments = new THREE.LineSegments( geometryCube, 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. var container = document.getElementById( 'container' );
  46. container.appendChild( renderer.domElement );
  47. stats = new Stats();
  48. container.appendChild( stats.dom );
  49. //
  50. window.addEventListener( 'resize', onWindowResize, false );
  51. }
  52. function cube( size ) {
  53. var h = size * 0.5;
  54. var geometry = new THREE.BufferGeometry();
  55. var position = [];
  56. position.push(
  57. - h, - h, - h,
  58. - h, h, - h,
  59. - h, h, - h,
  60. h, h, - h,
  61. h, h, - h,
  62. h, - h, - h,
  63. h, - h, - h,
  64. - h, - h, - h,
  65. - h, - h, h,
  66. - h, h, h,
  67. - h, h, h,
  68. h, h, h,
  69. h, h, h,
  70. h, - h, h,
  71. h, - h, h,
  72. - h, - h, h,
  73. - h, - h, - h,
  74. - h, - h, h,
  75. - h, h, - h,
  76. - h, h, h,
  77. h, h, - h,
  78. h, h, h,
  79. h, - h, - h,
  80. h, - h, h
  81. );
  82. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  83. return geometry;
  84. }
  85. function onWindowResize() {
  86. camera.aspect = window.innerWidth / window.innerHeight;
  87. camera.updateProjectionMatrix();
  88. renderer.setSize( window.innerWidth, window.innerHeight );
  89. }
  90. function animate() {
  91. requestAnimationFrame( animate );
  92. render();
  93. stats.update();
  94. }
  95. function render() {
  96. var time = Date.now() * 0.001;
  97. scene.traverse( function ( object ) {
  98. if ( object.isLine ) {
  99. object.rotation.x = 0.25 * time;
  100. object.rotation.y = 0.25 * time;
  101. }
  102. } );
  103. renderer.render( scene, camera );
  104. }
  105. </script>
  106. </body>
  107. </html>