three.js 箭头ArrowHelper的实践应用

效果:

代码:

<template>
  <div>
    <el-container>
      <el-main>
        <div class="box-card-left">
          <div id="threejs" style="border: 1px solid red"></div>
        </div>
      </el-main>
    </el-container>
  </div>
</template>
<script>
// 引入轨道控制器扩展库OrbitControls.js
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
export default {
  data() {
    return {
      sphereGeometry: null,
      group: null,
      camera: null,
      mesh: null,
      renderer: null,
      requestAnimationFrame_time: null,
      B: null,
      lengthVal: 0,
      normalizeVal: null,
      css3DRenderer: null,
    };
  },
  mounted() {
    this.name = this.$route.query.name;
    this.init();
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    init() {
      // 创建场景对象
      this.scene = new this.$three.Scene();
      // 调用方法创建点模型 A
      this.createPoint([0,40,0]);
      // 调用方法创建点模型 B
      this.createPoint([50,0,0]);
      this.createBox();
      // 创建环境光对象
      const ambientLight = new this.$three.AmbientLight(0xffffff,0.8);
      this.scene.add(ambientLight);
      // 创建箭头对象
      /**
       * ArrowHelper(dir : Vector3, origin : Vector3, length : Number, hex : Number, headLength : Number, headWidth : Number )
          dir -- 基于箭头原点的方向. 必须为单位向量.
          origin -- 箭头的原点.
          length -- 箭头的长度. 默认为 1.
          hex -- 定义的16进制颜色值. 默认为 0xffff00.
          headLength -- 箭头头部(锥体)的长度. 默认为箭头长度的0.2倍(0.2 * length).
          headWidth -- The width of the head of the arrow. Default is 0.2 * headLength.
       */
      /**
       * 计算箭头需要的参数;箭头是从A指向B
       */
      const A = new this.$three.Vector3(0,40,0);
      const B = new this.$three.Vector3(50,0,0);
      // 箭头方向的单位向量
      const dir = B.clone().sub(A).normalize();
      // 箭头原点 是 A
      const origin = A;
      // 箭头长度---就是 A 点到 B  点的距离;使用 length()方法可以计算得到
      const length = B.clone().sub(A).length();
      const hex = 0xffddaa;
      const arrowHelper = new this.$three.ArrowHelper(dir, origin, length, hex);
      this.scene.add(arrowHelper);
      // 创建坐标轴辅助对象
      const axesHelper = new this.$three.AxesHelper(200);
      this.scene.add(axesHelper);
      // 创建相机对象
      this.camera = new this.$three.PerspectiveCamera();
      this.camera.position.set(150,150,150);
      this.camera.lookAt(0,0,0);
      // 创建渲染器对象
      this.renderer = new this.$three.WebGLRenderer();
      this.renderer.setSize(1000,800);
      this.renderer.render(this.scene, this.camera);
      window.document.getElementById("threejs").appendChild(this.renderer.domElement);
      
      const controls = new OrbitControls(this.camera, this.renderer.domElement);
      controls.addEventListener("change", e => {
        this.renderer.render(this.scene, this.camera);
      })
    },
    /**
     * 创建点模型的方法,
     * point_position: 数组类型,数组里有且只有三个元素,
     *  */
    createPoint(point_position) {
      // 创建缓存几何体对象
      const bufferGeometry = new this.$three.BufferGeometry();
      // 创建类型化数组来存放顶点数据
      const vectors = new Float32Array(point_position);
      // 创建缓存属性来格式化顶点数据
      const bufferAttribute = new this.$three.BufferAttribute(vectors,3);
      // 设置缓存几何体的位置属性
      bufferGeometry.setAttribute("position", bufferAttribute);
      // 创建点材质对象
      const material = new this.$three.PointsMaterial({
        color: 0x99dd,
        size: 10
      });
      // 创建点模型对象
      const point = new this.$three.Points(bufferGeometry, material);
      this.scene.add(point);
    },
    createBox() {
      const geometry = new this.$three.BoxGeometry(50, 50, 50);
      const material = new this.$three.MeshLambertMaterial({
        color: 0x00ffff,
      });
      const mesh = new this.$three.Mesh(geometry, material);
      const p = mesh.geometry.attributes.position; // 顶点坐标集合
      const n = mesh.geometry.attributes.normal; // 顶点法线数据集合
      // 顶点数量
      const count = p.count;
      for(let i = 0; i < count; i ++) {
        // 该向量是单位向量了
        const dir = new this.$three.Vector3(n.getX(i), n.getY(i), n.getZ(i));
        // 箭头起点
        const origin = new this.$three.Vector3(p.getX(i), p.getY(i), p.getZ(i));
        const arrowHelper = new this.$three.ArrowHelper(dir, origin, 20);
        mesh.add(arrowHelper);
      }
      // mesh模型沿着 z 轴正向移动 50
      mesh.translateZ(50);
      this.scene.add(mesh);
    }
  },
};
</script>
<style lang="less" scoped>
.box-card-left {
  display: flex;
  align-items: flex-start;
  flex-direction: row;

  width: 100%;

  .box-right {
    img {
      width: 500px;
      user-select: none;
    }
  }
}
</style>

相关推荐

  1. three.js捋文档记录笔记(五):threejs引入

    2024-02-09 11:56:02       14 阅读
  2. 编程上箭头怎么打:全面解析与实用技巧

    2024-02-09 11:56:02       8 阅读
  3. 箭头函数this指向问题

    2024-02-09 11:56:02       54 阅读
  4. 箭头函数与普通函数差异

    2024-02-09 11:56:02       35 阅读
  5. 箭头函数和普通函数区别

    2024-02-09 11:56:02       13 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-02-09 11:56:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-02-09 11:56:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-02-09 11:56:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-02-09 11:56:02       20 阅读

热门阅读

  1. 开源大型语言模型概览:多语种支持与中文专注

    2024-02-09 11:56:02       32 阅读
  2. openssl3.2 - exp - RAND_bytes_ex

    2024-02-09 11:56:02       28 阅读
  3. 程序设计进阶——C语言(翁凯版)第五周

    2024-02-09 11:56:02       32 阅读
  4. 初识c++之基础认识

    2024-02-09 11:56:02       27 阅读
  5. C++:栈的作用域

    2024-02-09 11:56:02       31 阅读
  6. 怎么进入docker容器并执行

    2024-02-09 11:56:02       27 阅读
  7. Python笔记(九)

    2024-02-09 11:56:02       22 阅读
  8. OpenHarmony轻量级内核-LiteOS-M

    2024-02-09 11:56:02       31 阅读
  9. MongoDB聚合: $sort

    2024-02-09 11:56:02       29 阅读
  10. 编程中“游戏心切”心态的影响及其对策探讨

    2024-02-09 11:56:02       27 阅读