当前位置:   article > 正文

three.js 箭头ArrowHelper的实践应用

three.js 箭头ArrowHelper的实践应用

效果

代码:

  1. <template>
  2. <div>
  3. <el-container>
  4. <el-main>
  5. <div class="box-card-left">
  6. <div id="threejs" style="border: 1px solid red"></div>
  7. </div>
  8. </el-main>
  9. </el-container>
  10. </div>
  11. </template>
  12. <script>
  13. // 引入轨道控制器扩展库OrbitControls.js
  14. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  15. export default {
  16. data() {
  17. return {
  18. sphereGeometry: null,
  19. group: null,
  20. camera: null,
  21. mesh: null,
  22. renderer: null,
  23. requestAnimationFrame_time: null,
  24. B: null,
  25. lengthVal: 0,
  26. normalizeVal: null,
  27. css3DRenderer: null,
  28. };
  29. },
  30. mounted() {
  31. this.name = this.$route.query.name;
  32. this.init();
  33. },
  34. methods: {
  35. goBack() {
  36. this.$router.go(-1);
  37. },
  38. init() {
  39. // 创建场景对象
  40. this.scene = new this.$three.Scene();
  41. // 调用方法创建点模型 A
  42. this.createPoint([0,40,0]);
  43. // 调用方法创建点模型 B
  44. this.createPoint([50,0,0]);
  45. this.createBox();
  46. // 创建环境光对象
  47. const ambientLight = new this.$three.AmbientLight(0xffffff,0.8);
  48. this.scene.add(ambientLight);
  49. // 创建箭头对象
  50. /**
  51. * ArrowHelper(dir : Vector3, origin : Vector3, length : Number, hex : Number, headLength : Number, headWidth : Number )
  52. dir -- 基于箭头原点的方向. 必须为单位向量.
  53. origin -- 箭头的原点.
  54. length -- 箭头的长度. 默认为 1.
  55. hex -- 定义的16进制颜色值. 默认为 0xffff00.
  56. headLength -- 箭头头部(锥体)的长度. 默认为箭头长度的0.2倍(0.2 * length).
  57. headWidth -- The width of the head of the arrow. Default is 0.2 * headLength.
  58. */
  59. /**
  60. * 计算箭头需要的参数;箭头是从A指向B
  61. */
  62. const A = new this.$three.Vector3(0,40,0);
  63. const B = new this.$three.Vector3(50,0,0);
  64. // 箭头方向的单位向量
  65. const dir = B.clone().sub(A).normalize();
  66. // 箭头原点 是 A
  67. const origin = A;
  68. // 箭头长度---就是 A 点到 B 点的距离;使用 length()方法可以计算得到
  69. const length = B.clone().sub(A).length();
  70. const hex = 0xffddaa;
  71. const arrowHelper = new this.$three.ArrowHelper(dir, origin, length, hex);
  72. this.scene.add(arrowHelper);
  73. // 创建坐标轴辅助对象
  74. const axesHelper = new this.$three.AxesHelper(200);
  75. this.scene.add(axesHelper);
  76. // 创建相机对象
  77. this.camera = new this.$three.PerspectiveCamera();
  78. this.camera.position.set(150,150,150);
  79. this.camera.lookAt(0,0,0);
  80. // 创建渲染器对象
  81. this.renderer = new this.$three.WebGLRenderer();
  82. this.renderer.setSize(1000,800);
  83. this.renderer.render(this.scene, this.camera);
  84. window.document.getElementById("threejs").appendChild(this.renderer.domElement);
  85. const controls = new OrbitControls(this.camera, this.renderer.domElement);
  86. controls.addEventListener("change", e => {
  87. this.renderer.render(this.scene, this.camera);
  88. })
  89. },
  90. /**
  91. * 创建点模型的方法,
  92. * point_position: 数组类型,数组里有且只有三个元素,
  93. * */
  94. createPoint(point_position) {
  95. // 创建缓存几何体对象
  96. const bufferGeometry = new this.$three.BufferGeometry();
  97. // 创建类型化数组来存放顶点数据
  98. const vectors = new Float32Array(point_position);
  99. // 创建缓存属性来格式化顶点数据
  100. const bufferAttribute = new this.$three.BufferAttribute(vectors,3);
  101. // 设置缓存几何体的位置属性
  102. bufferGeometry.setAttribute("position", bufferAttribute);
  103. // 创建点材质对象
  104. const material = new this.$three.PointsMaterial({
  105. color: 0x99dd,
  106. size: 10
  107. });
  108. // 创建点模型对象
  109. const point = new this.$three.Points(bufferGeometry, material);
  110. this.scene.add(point);
  111. },
  112. createBox() {
  113. const geometry = new this.$three.BoxGeometry(50, 50, 50);
  114. const material = new this.$three.MeshLambertMaterial({
  115. color: 0x00ffff,
  116. });
  117. const mesh = new this.$three.Mesh(geometry, material);
  118. const p = mesh.geometry.attributes.position; // 顶点坐标集合
  119. const n = mesh.geometry.attributes.normal; // 顶点法线数据集合
  120. // 顶点数量
  121. const count = p.count;
  122. for(let i = 0; i < count; i ++) {
  123. // 该向量是单位向量了
  124. const dir = new this.$three.Vector3(n.getX(i), n.getY(i), n.getZ(i));
  125. // 箭头起点
  126. const origin = new this.$three.Vector3(p.getX(i), p.getY(i), p.getZ(i));
  127. const arrowHelper = new this.$three.ArrowHelper(dir, origin, 20);
  128. mesh.add(arrowHelper);
  129. }
  130. // mesh模型沿着 z 轴正向移动 50
  131. mesh.translateZ(50);
  132. this.scene.add(mesh);
  133. }
  134. },
  135. };
  136. </script>
  137. <style lang="less" scoped>
  138. .box-card-left {
  139. display: flex;
  140. align-items: flex-start;
  141. flex-direction: row;
  142. width: 100%;
  143. .box-right {
  144. img {
  145. width: 500px;
  146. user-select: none;
  147. }
  148. }
  149. }
  150. </style>

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