webgl_lines_dashed.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. <style>
  8. body {
  9. color: #ffffff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. font-weight: bold;
  14. background-color: #000000;
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. color: #fff;
  20. position: absolute;
  21. top: 0px; width: 100%;
  22. padding: 5px;
  23. z-index:100;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - dashed lines example</div>
  29. <div id="container"></div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/geometries/hilbert3D.js"></script>
  32. <script src="js/Detector.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  36. var renderer, scene, camera, stats;
  37. var objects = [];
  38. var WIDTH = window.innerWidth, HEIGHT = window.innerHeight;
  39. init();
  40. animate();
  41. function init() {
  42. camera = new THREE.PerspectiveCamera( 60, WIDTH / HEIGHT, 1, 200 );
  43. camera.position.z = 150;
  44. scene = new THREE.Scene();
  45. scene.background = new THREE.Color( 0x111111 );
  46. scene.fog = new THREE.Fog( 0x111111, 150, 200 );
  47. var subdivisions = 6;
  48. var recursion = 1;
  49. var points = hilbert3D( new THREE.Vector3( 0, 0, 0 ), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7 );
  50. var spline = new THREE.CatmullRomCurve3( points );
  51. var samples = spline.getPoints( points.length * subdivisions );
  52. var geometrySpline = new THREE.Geometry().setFromPoints( samples );
  53. var line = new THREE.Line( geometrySpline, new THREE.LineDashedMaterial( { color: 0xffffff, dashSize: 1, gapSize: 0.5 } ) );
  54. line.computeLineDistances();
  55. objects.push( line );
  56. scene.add( line );
  57. var geometryCube = cube( 50 );
  58. var lineSegments = new THREE.LineSegments( geometryCube, new THREE.LineDashedMaterial( { color: 0xffaa00, dashSize: 3, gapSize: 1, linewidth: 2 } ) );
  59. lineSegments.computeLineDistances();
  60. objects.push( lineSegments );
  61. scene.add( lineSegments );
  62. renderer = new THREE.WebGLRenderer( { antialias: true } );
  63. renderer.setPixelRatio( window.devicePixelRatio );
  64. renderer.setSize( WIDTH, HEIGHT );
  65. var container = document.getElementById( 'container' );
  66. container.appendChild( renderer.domElement );
  67. stats = new Stats();
  68. container.appendChild( stats.dom );
  69. //
  70. window.addEventListener( 'resize', onWindowResize, false );
  71. }
  72. function cube( size ) {
  73. var h = size * 0.5;
  74. var geometry = new THREE.BufferGeometry();
  75. var position = [];
  76. position.push(
  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. -h, -h, h,
  95. -h, h, -h,
  96. -h, h, h,
  97. h, h, -h,
  98. h, h, h,
  99. h, -h, -h,
  100. h, -h, h
  101. );
  102. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  103. return geometry;
  104. }
  105. function onWindowResize() {
  106. camera.aspect = window.innerWidth / window.innerHeight;
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. render();
  113. stats.update();
  114. }
  115. function render() {
  116. var time = Date.now() * 0.001;
  117. scene.traverse( function( object ) {
  118. if ( object.isLine ) {
  119. object.rotation.x = 0.25 * time;
  120. object.rotation.y = 0.25 * time;
  121. }
  122. } );
  123. renderer.render( scene, camera );
  124. }
  125. </script>
  126. </body>
  127. </html>