ThreeJs顶点颜色信息渲染

1、实现效果

线和面都会根据顶点的颜色进行渐变
1、点渲染
在这里插入图片描述
2、线渲染
在这里插入图片描述
3、面渲染
在这里插入图片描述

2、思维导图

在这里插入图片描述

3、实现流程

1、创建一个空几何体
2、创建空几何体的顶点位置数据(类型数组)
3、创建属性缓冲区对象(生成顶点)
4、设置几何体的attributes属性的位置position属性(将顶点赋给空几何体)
5、创建空几何体的顶点位置数据(类型数组)
6、创建属性缓冲区对象(生成RGB颜色)
7、设置几何体的attributes属性的颜色color属性(将颜色赋给空几何体)

4、关键代码

1、点渲染

 /**
     * 创建网格模型
     */
    var geometry = new THREE.BufferGeometry(); //声明一个缓冲几何体对象

    //类型数组创建顶点位置position数据
    var vertices = new Float32Array([
      0, 0, 0, //顶点1坐标
      50, 0, 0, //顶点2坐标
      0, 100, 0, //顶点3坐标

      0, 0, 10, //顶点4坐标
      0, 0, 100, //顶点5坐标
      50, 0, 10, //顶点6坐标
    ]);
    // 创建属性缓冲区对象
    var attribue = new THREE.BufferAttribute(vertices, 3); //3个为一组,作为一个顶点的xyz坐标
    // 设置几何体attributes属性的位置position属性
    geometry.attributes.position = attribue;


    //类型数组创建顶点颜色color数据
    var colors = new Float32Array([
      1, 0, 0, //顶点1颜色
      0, 1, 0, //顶点2颜色
      0, 0, 1, //顶点3颜色

      1, 1, 0, //顶点4颜色
      0, 1, 1, //顶点5颜色
      1, 0, 1, //顶点6颜色
    ]);
    // 设置几何体attributes属性的颜色color属性
    geometry.attributes.color = new THREE.BufferAttribute(colors, 3); //3个为一组,表示一个顶点的颜色数据RGB


    //材质对象
    var material = new THREE.PointsMaterial({
      // 使用顶点颜色数据渲染模型,不需要再定义color属性
      // color: 0xff0000,
      vertexColors: THREE.VertexColors, //以顶点颜色为准
      size: 10.0 //点对象像素尺寸
    });
    // 点渲染模式  点模型对象Points
    var points = new THREE.Points(geometry, material); //点模型对象
    scene.add(points); //点对象添加到场景中

2、线渲染

/**
     * 创建网格模型
     */
    var geometry = new THREE.BufferGeometry(); //声明一个缓冲几何体对象

    //类型数组创建顶点位置position数据
    var vertices = new Float32Array([
      0, 0, 0, //顶点1坐标
      100, 100, 100, //顶点2坐标
    ]);
    // 创建属性缓冲区对象
    var attribue = new THREE.BufferAttribute(vertices, 3); //3个为一组,作为一个顶点的xyz坐标
    // 设置几何体attributes属性的位置position属性
    geometry.attributes.position = attribue;


    //类型数组创建顶点颜色color数据
    var colors = new Float32Array([
      1, 0, 0, //顶点1颜色
      0, 0, 1, //顶点2颜色
    ]);
    // 设置几何体attributes属性的颜色color属性
    geometry.attributes.color = new THREE.BufferAttribute(colors, 3); //3个为一组,表示一个顶点的颜色数据RGB


    //材质对象
    var material = new THREE.LineBasicMaterial({
      // 使用顶点颜色数据渲染模型,不需要再定义color属性
      // color: 0xff0000,
      vertexColors: THREE.VertexColors, //以顶点颜色为准
    });
    // 线条渲染模式  线模型对象Line
    var line = new THREE.Line(geometry, material); //点模型对象
    scene.add(line); //点对象添加到场景中

3、面渲染

  /**
     * 创建网格模型
     */
    var geometry = new THREE.BufferGeometry(); //声明一个缓冲几何体对象

    //类型数组创建顶点位置position数据
    var vertices = new Float32Array([
      0, 0, 0, //顶点1坐标
      50, 0, 0, //顶点2坐标
      0, 100, 0, //顶点3坐标

      0, 0, 10, //顶点4坐标
      0, 0, 100, //顶点5坐标
      50, 0, 10, //顶点6坐标
    ]);
    // 创建属性缓冲区对象
    var attribue = new THREE.BufferAttribute(vertices, 3); //3个为一组,作为一个顶点的xyz坐标
    // 设置几何体attributes属性的位置position属性
    geometry.attributes.position = attribue;


    //类型数组创建顶点颜色color数据
    var colors = new Float32Array([
      1, 0, 0, //顶点1颜色
      0, 1, 0, //顶点2颜色
      0, 0, 1, //顶点3颜色

      1, 1, 0, //顶点4颜色
      0, 1, 1, //顶点5颜色
      1, 0, 1, //顶点6颜色
    ]);
    // 设置几何体attributes属性的颜色color属性
    geometry.attributes.color = new THREE.BufferAttribute(colors, 3); //3个为一组,表示一个顶点的颜色数据RGB


    //材质对象
    var material = new THREE.MeshBasicMaterial({
      // 使用顶点颜色数据渲染模型,不需要再定义color属性
      // color: 0xff0000,
      vertexColors: THREE.VertexColors, //以顶点颜色为准
    });
    // 网格模型  三角面渲染模式
    var mesh = new THREE.Mesh(geometry, material); //网格模型
    scene.add(mesh); //点对象添加到场景中
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Vue项目中使用`THREE.Line`来显示不同颜色顶点,可以按照以下步骤进行: 1. 安装`three`库:在项目的根目录下运行以下命令安装`three`库。 ``` npm install three ``` 2. 在Vue组件中引入`three`库:在需要使用`THREE.Line`的Vue组件中引入`three`库。 ```javascript import * as THREE from 'three'; ``` 3. 定义顶点坐标和颜色数组:定义包含顶点坐标和颜色的数组,例如: ```javascript const positions = [ 0, 0, 0, 1, 1, 1, 2, 2, 2 ]; const colors = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ]; ``` 4. 创建几何体和材质:使用`THREE.BufferGeometry`创建几何体,并将顶点坐标和颜色数组传递给几何体的`position`和`color`属性。然后,使用`THREE.LineBasicMaterial`创建材质,设置`vertexColors`为`THREE.VertexColors`。 ```javascript const geometry = new THREE.BufferGeometry(); geometry.addAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); geometry.addAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); const material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors }); ``` 5. 创建线对象并添加到场景中:使用几何体和材质创建`THREE.Line`对象,并将其添加到场景中。 ```javascript const line = new THREE.Line(geometry, material); scene.add(line); ``` 完整的Vue组件示例代码如下所示: ```vue <template> <div ref="container"></div> </template> <script> import * as THREE from 'three'; export default { mounted() { // 创建场景、相机和渲染器 const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); this.$refs.container.appendChild(renderer.domElement); // 定义顶点坐标和颜色数组 const positions = [ 0, 0, 0, 1, 1, 1, 2, 2, 2 ]; const colors = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ]; // 创建几何体和材质 const geometry = new THREE.BufferGeometry(); geometry.addAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); geometry.addAttribute('color', new THREE.Float32BufferAttribute(colors, 3)); const material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors }); // 创建线对象并添加到场景中 const line = new THREE.Line(geometry, material); scene.add(line); // 设置相机位置 camera.position.z = 5; // 渲染场景 function animate() { requestAnimationFrame(animate); renderer.render(scene, camera); } animate(); } } </script> <style> #container { width: 100%; height: 100%; overflow: hidden; } </style> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值