webgl_lines_dashed.html 3.7 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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - dashed lines example</div>
  11. <div id="container"></div>
  12. <script type="module">
  13. import {
  14. BufferGeometry,
  15. CatmullRomCurve3,
  16. Color,
  17. Float32BufferAttribute,
  18. Fog,
  19. Line,
  20. LineDashedMaterial,
  21. LineSegments,
  22. PerspectiveCamera,
  23. Scene,
  24. Vector3,
  25. WebGLRenderer
  26. } from "../build/three.module.js";
  27. import Stats from './jsm/libs/stats.module.js';
  28. import { GeometryUtils } from './jsm/utils/GeometryUtils.js';
  29. var renderer, scene, camera, stats;
  30. var objects = [];
  31. var WIDTH = window.innerWidth, HEIGHT = window.innerHeight;
  32. init();
  33. animate();
  34. function init() {
  35. camera = new PerspectiveCamera( 60, WIDTH / HEIGHT, 1, 200 );
  36. camera.position.z = 150;
  37. scene = new Scene();
  38. scene.background = new Color( 0x111111 );
  39. scene.fog = new Fog( 0x111111, 150, 200 );
  40. var subdivisions = 6;
  41. var recursion = 1;
  42. var points = GeometryUtils.hilbert3D( new Vector3( 0, 0, 0 ), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7 );
  43. var spline = new CatmullRomCurve3( points );
  44. var samples = spline.getPoints( points.length * subdivisions );
  45. var geometrySpline = new BufferGeometry().setFromPoints( samples );
  46. var line = new Line( geometrySpline, new LineDashedMaterial( { color: 0xffffff, dashSize: 1, gapSize: 0.5 } ) );
  47. line.computeLineDistances();
  48. objects.push( line );
  49. scene.add( line );
  50. var geometryCube = cube( 50 );
  51. var lineSegments = new LineSegments( geometryCube, new LineDashedMaterial( { color: 0xffaa00, dashSize: 3, gapSize: 1 } ) );
  52. lineSegments.computeLineDistances();
  53. objects.push( lineSegments );
  54. scene.add( lineSegments );
  55. renderer = new WebGLRenderer( { antialias: true } );
  56. renderer.setPixelRatio( window.devicePixelRatio );
  57. renderer.setSize( WIDTH, HEIGHT );
  58. var container = document.getElementById( 'container' );
  59. container.appendChild( renderer.domElement );
  60. stats = new Stats();
  61. container.appendChild( stats.dom );
  62. //
  63. window.addEventListener( 'resize', onWindowResize, false );
  64. }
  65. function cube( size ) {
  66. var h = size * 0.5;
  67. var geometry = new BufferGeometry();
  68. var position = [];
  69. position.push(
  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. h, h, h,
  82. h, h, h,
  83. h, - h, h,
  84. h, - h, h,
  85. - h, - h, h,
  86. - h, - h, - h,
  87. - h, - h, h,
  88. - h, h, - h,
  89. - h, h, h,
  90. h, h, - h,
  91. h, h, h,
  92. h, - h, - h,
  93. h, - h, h
  94. );
  95. geometry.addAttribute( 'position', new 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. var 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>