webgl_lines_dashed.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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/WebGL.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. if ( WEBGL.isWebGLAvailable() === false ) {
  36. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  37. }
  38. var renderer, scene, camera, stats;
  39. var objects = [];
  40. var WIDTH = window.innerWidth, HEIGHT = window.innerHeight;
  41. init();
  42. animate();
  43. function init() {
  44. camera = new THREE.PerspectiveCamera( 60, WIDTH / HEIGHT, 1, 200 );
  45. camera.position.z = 150;
  46. scene = new THREE.Scene();
  47. scene.background = new THREE.Color( 0x111111 );
  48. scene.fog = new THREE.Fog( 0x111111, 150, 200 );
  49. var subdivisions = 6;
  50. var recursion = 1;
  51. var points = hilbert3D( new THREE.Vector3( 0, 0, 0 ), 25.0, recursion, 0, 1, 2, 3, 4, 5, 6, 7 );
  52. var spline = new THREE.CatmullRomCurve3( points );
  53. var samples = spline.getPoints( points.length * subdivisions );
  54. var geometrySpline = new THREE.BufferGeometry().setFromPoints( samples );
  55. var line = new THREE.Line( geometrySpline, new THREE.LineDashedMaterial( { color: 0xffffff, dashSize: 1, gapSize: 0.5 } ) );
  56. line.computeLineDistances();
  57. objects.push( line );
  58. scene.add( line );
  59. var geometryCube = cube( 50 );
  60. var lineSegments = new THREE.LineSegments( geometryCube, new THREE.LineDashedMaterial( { color: 0xffaa00, dashSize: 3, gapSize: 1 } ) );
  61. lineSegments.computeLineDistances();
  62. objects.push( lineSegments );
  63. scene.add( lineSegments );
  64. renderer = new THREE.WebGLRenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( WIDTH, HEIGHT );
  67. var container = document.getElementById( 'container' );
  68. container.appendChild( renderer.domElement );
  69. stats = new Stats();
  70. container.appendChild( stats.dom );
  71. //
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. }
  74. function cube( size ) {
  75. var h = size * 0.5;
  76. var geometry = new THREE.BufferGeometry();
  77. var position = [];
  78. position.push(
  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. h, -h, -h,
  102. h, -h, h
  103. );
  104. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( position, 3 ) );
  105. return geometry;
  106. }
  107. function onWindowResize() {
  108. camera.aspect = window.innerWidth / window.innerHeight;
  109. camera.updateProjectionMatrix();
  110. renderer.setSize( window.innerWidth, window.innerHeight );
  111. }
  112. function animate() {
  113. requestAnimationFrame( animate );
  114. render();
  115. stats.update();
  116. }
  117. function render() {
  118. var time = Date.now() * 0.001;
  119. scene.traverse( function( object ) {
  120. if ( object.isLine ) {
  121. object.rotation.x = 0.25 * time;
  122. object.rotation.y = 0.25 * time;
  123. }
  124. } );
  125. renderer.render( scene, camera );
  126. }
  127. </script>
  128. </body>
  129. </html>