当前位置:   article > 正文

three.js绘制直线_three.js连接两个对象画线

three.js连接两个对象画线

先看效果:

 

本文中创建直线的方式用了三种:

方式一:使用 Vector3创建点后,添加到geometry.vertices中(用的普通模型对象)

  1. var p1 = new THREE.Vector3(50,10,20);
  2. var p2 = new THREE.Vector3(10,40,30);
  3. geometry.vertices.push(p1,p2);

方式二:使用 Vector3, LineCurve3 

  1. var p3 = new THREE.Vector3(100,10,10);
  2. var p4 = new THREE.Vector3(10,10,100);
  3. var line2 = new THREE.LineCurve3(p3,p4);
  4. let points = line2.getPoints(100);
  5. geometry.setFromPoints(points);

方式三:使用 Vector2, LineCurve

  1. var p5 = new THREE.Vector2(80,10);
  2. var p6 = new THREE.Vector2(20,80);
  3. var line3 = new THREE.LineCurve(p5,p6);
  4. let points = line3.getPoints(100);
  5. geometry.setFromPoints(points);

 全部代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>绘制二维三维直线</title>
  8. <script src="../three.js"></script>
  9. <script src="../OrbitControls.js"></script>
  10. </head>
  11. <body>
  12. <script>
  13. // 1,创建场景对象
  14. var scene = new THREE.Scene();
  15. // 2,创建普通模型对象
  16. var geometry = new THREE.BufferGeometry();
  17. // getPoints是基类Curve的方法,返回一个vector2对象作为元素组成的数组
  18. // setFromPoints方法从points中提取数据改变几何体的顶点属性vertices
  19. //创建直线的方式有三种:
  20. // 方式一 start (创建三维直线,使用 Vector3)
  21. var p1 = new THREE.Vector3(50,10,20);
  22. var p2 = new THREE.Vector3(10,40,30);
  23. geometry.vertices.push(p1,p2);
  24. // 方式一 end (创建三维直线,使用 Vector3)
  25. // 方式二 start (创建三维直线,使用 Vector3, LineCurve3)
  26. // var p3 = new THREE.Vector3(100,10,10);
  27. // var p4 = new THREE.Vector3(10,10,100);
  28. // var line2 = new THREE.LineCurve3(p3,p4);
  29. // let points = line2.getPoints(100);
  30. // geometry.setFromPoints(points);
  31. // 方式二 end (创建三维直线,使用 Vector3, LineCurve3)
  32. // 方式三 start (创建二维直线,使用 Vector2, LineCurve)
  33. // var p5 = new THREE.Vector2(80,10);
  34. // var p6 = new THREE.Vector2(20,80);
  35. // var line3 = new THREE.LineCurve(p5,p6);
  36. // let points = line3.getPoints(100);
  37. // geometry.setFromPoints(points);
  38. // 方式三 end (创建二维直线,使用 Vector2, LineCurve)
  39. var material = new THREE.LineBasicMaterial({color:0xee00ff});
  40. var line = new THREE.Line(geometry,material);
  41. scene.add(line);
  42. // 3,创建环境光对象
  43. var ambient = new THREE.AmbientLight({color:0xffffff});
  44. scene.add(ambient);
  45. // 4,创建相机对象
  46. var width = window.innerWidth;
  47. var height = window.innerHeight;
  48. var k = width / height;
  49. var s = 200;
  50. var camera = new THREE.OrthographicCamera(-s*k,s*k,s,-s,-2000,2000);
  51. camera.position.set(200,300,200);
  52. camera.lookAt(scene.position);
  53. // 创建轴辅助对象
  54. var axes_helper = new THREE.AxesHelper(500);
  55. scene.add(axes_helper);
  56. // 5,创建渲染器对象
  57. var renderer = new THREE.WebGLRenderer();
  58. renderer.setSize(width,height);
  59. document.body.appendChild(renderer.domElement);
  60. function render(){
  61. renderer.render(scene,camera);
  62. }
  63. render();
  64. // 创建鼠标控制对象
  65. var controls = new THREE.OrbitControls(camera,renderer.domElement);
  66. controls.addEventListener('change',render);
  67. </script>
  68. </body>
  69. </html>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/91487
推荐阅读
相关标签
  

闽ICP备14008679号