Drawing-lines.html 2.2 KB

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