drawing-lines.html 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Drawing Lines</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Drawing Lines">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Drawing Lines</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>
  29. Let's say you want to draw a line or a circle, not a wireframe `Mesh`.
  30. First we need to set up the renderer, scene and camera (see the Creating a scene page).
  31. </p>
  32. <p>Here is the code that we will use:</p>
  33. <pre class="prettyprint notranslate lang-js" translate="no">
  34. const renderer = new THREE.WebGLRenderer();
  35. renderer.setSize( window.innerWidth, window.innerHeight );
  36. document.body.appendChild( renderer.domElement );
  37. const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  38. camera.position.set( 0, 0, 100 );
  39. camera.lookAt( 0, 0, 0 );
  40. const scene = new THREE.Scene();
  41. </pre>
  42. <p>Next thing we will do is define a material. For lines we have to use `LineBasicMaterial` or `LineDashedMaterial`.</p>
  43. <pre class="prettyprint notranslate lang-js" translate="no">
  44. //create a blue LineBasicMaterial
  45. const material = new THREE.LineBasicMaterial( { color: 0x0000ff } );
  46. </pre>
  47. <p>
  48. After material we will need a geometry with some vertices:
  49. </p>
  50. <pre class="prettyprint notranslate lang-js" translate="no">
  51. const points = [];
  52. points.push( new THREE.Vector3( - 10, 0, 0 ) );
  53. points.push( new THREE.Vector3( 0, 10, 0 ) );
  54. points.push( new THREE.Vector3( 10, 0, 0 ) );
  55. const geometry = new THREE.BufferGeometry().setFromPoints( points );
  56. </pre>
  57. <p>Note that lines are drawn between each consecutive pair of vertices, but not between the first and last (the line is not closed.)</p>
  58. <p>Now that we have points for two lines and a material, we can put them together to form a line.</p>
  59. <pre class="prettyprint notranslate lang-js" translate="no">
  60. const line = new THREE.Line( geometry, material );
  61. </pre>
  62. <p>All that's left is to add it to the scene and call `renderer.render()`.</p>
  63. <pre class="prettyprint notranslate lang-js" translate="no">
  64. scene.add( line );
  65. renderer.render( scene, camera );
  66. </pre>
  67. <p>You should now be seeing an arrow pointing upwards, made from two blue lines.</p>
  68. </div>
  69. </div>
  70. </div>
  71. <script src="../resources/prettify.js"></script>
  72. <script src="../resources/lesson.js"></script>
  73. </body></html>
粤ICP备19079148号