2
0

Drawing-lines.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <div>
  12. <p>
  13. Let's say you want to draw a line or a circle, not a wireframe [page:Mesh].
  14. First we need to set up the [page:WebGLRenderer renderer], [page:Scene scene] and camera (see the Creating a scene page).
  15. </p>
  16. <p>Here is the code that we will use:</p>
  17. <code>
  18. const renderer = new THREE.WebGLRenderer();
  19. renderer.setSize( window.innerWidth, window.innerHeight );
  20. document.body.appendChild( renderer.domElement );
  21. const camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 500 );
  22. camera.position.set( 0, 0, 100 );
  23. camera.lookAt( 0, 0, 0 );
  24. const scene = new THREE.Scene();
  25. </code>
  26. <p>Next thing we will do is define a material. For lines we have to use [page:LineBasicMaterial] or [page:LineDashedMaterial].</p>
  27. <code>
  28. //create a blue LineBasicMaterial
  29. const material = new THREE.LineBasicMaterial( { color: 0x0000ff } );
  30. </code>
  31. <p>
  32. After material we will need a geometry with some vertices:
  33. </p>
  34. <code>
  35. const points = [];
  36. points.push( new THREE.Vector3( - 10, 0, 0 ) );
  37. points.push( new THREE.Vector3( 0, 10, 0 ) );
  38. points.push( new THREE.Vector3( 10, 0, 0 ) );
  39. const geometry = new THREE.BufferGeometry().setFromPoints( points );
  40. </code>
  41. <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>
  42. <p>Now that we have points for two lines and a material, we can put them together to form a line.</p>
  43. <code>
  44. const line = new THREE.Line( geometry, material );
  45. </code>
  46. <p>All that's left is to add it to the scene and call [page:WebGLRenderer.render render].</p>
  47. <code>
  48. scene.add( line );
  49. renderer.render( scene, camera );
  50. </code>
  51. <p>You should now be seeing an arrow pointing upwards, made from two blue lines.</p>
  52. </div>
  53. </body>
  54. </html>