three.js 箭头ArrowHelper的实践应用

news/2024/7/24 13:12:07 标签: javascript, 开发语言, ecmascript

效果:

代码:

<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>


http://www.niftyadmin.cn/n/5370764.html

相关文章

Rust 初体验2

变量类型 Rust 语言的变量数据类型&#xff0c;主要包括整型、浮点型、字符、布尔型、元组、数组、字符串、枚举、结构体和可变变量等。 fn main() { // 整型 let integer: i32 100; println!("整型: {}", integer); // 浮点型 let floating_point: f64 3.1…

测试管理_利用python连接禅道数据库并自动统计bug数据到钉钉群

测试管理_利用python连接禅道数据库并统计bug数据到钉钉 这篇不多赘述&#xff0c;直接上代码文件。 另文章基础参考博文&#xff1a;参考博文 加以我自己的需求优化而成。 统计的前提 以下代码统计的前提是禅道的提bug流程应规范化 bug未解决不删除bug未关闭不删除 db_…

Flink从入门到实践(一):Flink入门、Flink部署

文章目录 系列文章索引一、快速上手1、导包2、求词频demo&#xff08;1&#xff09;要读取的数据&#xff08;2&#xff09;demo1&#xff1a;批处理&#xff08;离线处理&#xff09;&#xff08;3&#xff09;demo2 - lambda优化&#xff1a;批处理&#xff08;离线处理&…

flutter 国内源

Flutter 在中国由于网络原因&#xff0c;从官方默认的国外源下载Dart包和Flutter SDK可能会比较慢或者不稳定。为了加速依赖包的获取与Flutter SDK的安装&#xff0c;可以使用国内镜像源。以下是一些国内常用的Flutter和Dart包镜像源&#xff1a; 清华大学开源软件镜像站 Flu…

react中的diff算法

diff算法 对于React团队发现在日常开发中对于更新组件的频率&#xff0c;会比新增和删除的频率更高&#xff0c;所以在diff算法里&#xff0c;判断更新的优先级会更高。对于Vue2的diff算法使用了双指针&#xff0c;React的diff算法没有使用双指针&#xff0c;是因为更新的jsx对…

图形学:Transform矩阵(3维 2维) 平移,旋转,缩放

0. 简介 在图形学领域中&#xff0c;Transform矩阵&#xff08;变换矩阵&#xff09;是一种表示图形对象在二维或三维空间中的位置、方向和大小变化的数学工具。它们用于执行各种图形变换&#xff0c;如平移、旋转、缩放。Transform矩阵通常表示为一个二维或三维矩阵&#xff…

C#面:什么是Code-Behind技术

Code-Behind技术是一种在Web开发中常用的技术&#xff0c;它将前端页面与后端代码分离&#xff0c;使得前端页面的设计和后端代码的逻辑处理可以分别进行。在Code-Behind模式下&#xff0c;前端页面通常是一个标记语言&#xff08;如HTML或ASPX&#xff09;&#xff0c;而后端代…

寒假作业7

sql语句 创建表格 create table 表名 &#xff08;字段名 数据类型&#xff0c;字段名 数据类型&#xff09; create table if not exists 表名 &#xff08;字段名 数据类型&#xff0c; 字段名 数据类型&#xff09; 删除表格 drop table 表名&#xff1b; 插入记录 全字…