03-threejs画线

一、新建项目

1、命令窗口输入以下代码,我的项目名字是drawline,大家可自行填写名称

vue create drawline

2、下载three.js
在项目的命令窗口中输入

npm install three --force

二、项目中使用

1、挂载点

新建Base.js文件,用来编写three.js文件,抛出一个类,这个类是用来挂载dom节点的

export class Line{
  dom = null;
  constructor(dom){
    this.dom = dom
  }
}

2、使用

在组件中引入这个类,并挂载DOM节点

<template>
  <div class="hello">
    <div id="line" ref="line"></div>
  </div>
</template>

<script>
import { Line } from './js/Base';
export default {
  name: 'HelloWorld',
  data() {
    return {
      Line: null,
    };
  },
  mounted() {
    this.Line = new Line(this.$refs.line);
  },
};
</script>

<style scoped>
#line {
  width: 400px;
  height: 400px;
  margin: 0 auto;
  border: 1px solid red;
}
</style>

3、构建渲染器,相机和场景

1、引入

在Base.js中引入场景、相机和渲染器,

import { Scene,PerspectiveCamera,WebGL1Renderer} from 'three'

2、实例化

1、渲染器

// 创建渲染器
 let render = new WebGL1Renderer({antialias:true})
 // 设置渲染器大小
 render.setSize(dom.offsetWidth,dom.offsetHeight,true)

2、场景

   let scene = new Scene()
   this.scene = scene

3、相机

 // 实例化相机
    let camera = new PerspectiveCamera(45,dom.offsetWidth/dom.offsetHeight,1,1000)
    // 设置相机位置
    camera.position.set(50,50,50)
    // 设置相机看向位置
    camera.lookAt(new Vector3(0,0,0))
    // 设置相机自身方向
    camera.up = new Vector3(0,1,0)

4、 渲染器渲染相机和场景

 render.render(scene,camera)
 let animate = ()=>{
    render.render(scene,camera)
    requestAnimationFrame(animate)
  }
  animate()

5、在Base.js中写一个方法,用来添加线条

  addModal(...moadl) {
    moadl.forEach(elem => {
      this.scene.add(elem)  // 场景添加模型
    })
  }

4、新建一个modal.js,用来放模型

1、引入三维向量Vector3,材质LineBasicMaterial,BufferGeometry

import { Vector3,LineBasicMaterial,BufferGeometry } from "three";

2、抛出一个线条数组,并将创建一个线条数组

export const lines = []
const poinst = []

3、先添加点到lines数组

poinst.push(new Vector3(-10,0,0));
poinst.push(new Vector3(0,10,0));
poinst.push(new Vector3(10,0,0));

4、创建几何体并设置点

const geometry = new BufferGeometry().setFromPoints(poinst);

5、设置材质-为蓝色的线条

const material = new LineBasicMaterial({color:0x0000ff})

6、创建线条对象,并将几何体和材质传递到构造函数中

export const line = new Line(geometry,material)

7、将线条添加到线条数组中去

lines.push(line)

5、组件中引入和使用

1、引入

import { lines } from './js/modal';

2、传入线条数组

  this.Line.addModal(...lines);

3、这个时候可以看到项目中有条蓝色的线条,但是背景颜色是黑色的,我们可以在Base.js中设置一下颜色

 render.setClearColor('rgb(65, 184, 131)')  // 设置渲染器的颜色

三、效果

在这里插入图片描述

四、总体代码

1、组件代码(HelloWorld.vue)

<template>
  <div class="hello">
    <div id="line" ref="line"></div>
  </div>
</template>

<script>
import { Line } from './js/Base';
import { lines } from './js/modal';
export default {
  name: 'HelloWorld',
  data() {
    return {
      Line: null,
    };
  },
  mounted() {
    this.Line = new Line(this.$refs.line);
    this.Line.addModal(...lines);
  },
};
</script>

<style scoped>
#line {
  width: 400px;
  height: 400px;
  margin: 0 auto;
  border: 1px solid red;
}
</style>

2、Base.js代码

import { Scene,PerspectiveCamera,WebGL1Renderer,Vector3} from 'three'
export class Line{
  dom = null;
  constructor(dom){

    // 创建渲染器
    let render = new WebGL1Renderer({antialias:true})
    // 设置渲染器大小
    render.setSize(dom.offsetWidth,dom.offsetHeight,true)
    dom.appendChild(render.domElement)  // 将渲染器挂载到dom
    // 实例化场景
    let scene = new Scene()

    // 实例化相机
    let camera = new PerspectiveCamera(45,dom.offsetWidth/dom.offsetHeight,1,1000)
    // 设置相机位置
    camera.position.set(50,50,50)
    // 设置相机看向位置
    camera.lookAt(new Vector3(0,0,0))
    // 设置相机自身方向
    camera.up = new Vector3(0,1,0)

    //   渲染器渲染相机和场景
    render.render(scene,camera)
    render.setClearColor('rgb(65, 184, 131)')  // 设置渲染器的颜色

  //  挂载场景到实例上
  this.scene = scene





    this.dom = dom
    // 渲染器渲染场景和相机
  let animate = ()=>{
    render.render(scene,camera)
    requestAnimationFrame(animate);
  }
  animate()
  }
   /**
   * 向场景中添加模型
   * @param  {...any} moadl 模型列表
   */
  addModal(...moadl) {
    moadl.forEach(elem => {
      this.scene.add(elem)  // 场景添加模型
    })
  }
}

3、modal.js代码

import { Vector3,LineBasicMaterial,BufferGeometry,Line } from "three";
//
export const lines = []
const poinst = []

// 先添加点到poinst数组
poinst.push(new Vector3(-10,0,0));
poinst.push(new Vector3(0,10,0));
poinst.push(new Vector3(10,0,0));
// 创建几何体并设置点
const geometry = new BufferGeometry().setFromPoints(poinst);
// 设置材质
const material = new LineBasicMaterial({color:0x0000ff})
// 创建线条对象,并将几何体和材质传递到构造函数中
export const line = new Line(geometry,material)
// export const Line = new BufferGeometry().setFromPoints(poinst)


lines.push(line)
  • 20
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Three.js是一个用于创建和展示3D图形的JavaScript库。它提供了一系列功能强大的工具和API,可以轻松地在Web浏览器中创建交互式的3D场景和动画效果。 要使用Three.js来画线,你可以按照以下步骤进行操作: 1. 创建一个场景(Scene)对象,用于容纳所有的3D元素。 2. 创建一个相机(Camera)对象,用于定义观察者的视角和位置。 3. 创建一个渲染器(Renderer)对象,用于将场景渲染到HTML页面上。 4. 创建一个几何体(Geometry)对象,用于定义线的形状和位置。 5. 创建一个材质(Material)对象,用于定义线的颜色和外观。 6. 创建一个线条(Line)对象,将几何体和材质结合起来。 7. 将线条添加到场景中。 8. 渲染场景,将结果显示在HTML页面上。 下面是一个简单的示例代码,演示了如何使用Three.js来画一条线: ```javascript // 创建场景 var scene = new THREE.Scene(); // 创建相机 var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.z = 5; // 创建渲染器 var renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); // 创建几何体 var geometry = new THREE.Geometry(); geometry.vertices.push(new THREE.Vector3(-2, 0, 0)); geometry.vertices.push(new THREE.Vector3(0, 2, 0)); geometry.vertices.push(new THREE.Vector3(2, 0, 0)); // 创建材质 var material = new THREE.LineBasicMaterial({ color: 0x00ff00 }); // 创建线条 var line = new THREE.Line(geometry, material); // 将线条添加到场景中 scene.add(line); // 渲染场景 function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); ``` 这段代码创建了一个场景,一个相机和一个渲染器。然后,它创建了一个几何体,定义了线的形状和位置。接着,它创建了一个材质,定义了线的颜色和外观。最后,它创建了一个线条,并将线条添加到场景中。通过调用`renderer.render(scene, camera)`来渲染场景,将结果显示在HTML页面上。 希望这个简单的示例能够帮助你理解如何使用Three.js来画线

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值