当前位置: 首页 > news >正文

Three.js 顶点颜色教程

顶点颜色 ·Vertex Color· ▶ 在线运行案例

  • 案例合集:三维可视化功能案例(threehub.cn)
  • 开源仓库github地址:https://github.com/z2586300277/three-cesium-examples
  • 400个案例代码:网盘链接

你将学到什么

  • geometry.attributes.color为每个顶点指定 RGB
  • 材质vertexColors: true启用顶点插值着色
  • GPU 如何在三角面内插值顶点颜色

效果说明

三角形的每个顶点颜色不同,面片内部呈现平滑渐变(GPU 对三个顶点颜色做重心坐标插值)。

核心概念

顶点属性 pipeline

attributes.position → 顶点位置

attributes.color → 顶点颜色 (RGB, 0~1) attributes.normal → 法线(光照计算) attributes.uv → 纹理坐标

const colors = new Float32Array([

1, 1, 1, // 顶点0 白 1, 1, 0, // 顶点1 黄 1, 1, 1, // 顶点2 白 1, 0, 0, // 顶点3 红 0, 1, 1, // 顶点4 青 0, 0, 1, // 顶点5 蓝 ]); geometry.attributes.color = new THREE.BufferAttribute(colors, 3);

vertexColors

材质必须开启vertexColors: true(或传入THREE.VertexColors常量,旧版 API),否则只用material.color

new THREE.PointsMaterial({

vertexColors: true, // 使用 attributes.color size: 10.0 });

插值原理:三角形内任意点的颜色 = 三个顶点颜色按距离加权平均。这是 GLSL varying 变量的基础机制,自定义 shader 同样适用。

实现步骤

  • 定义 position + color 两组 BufferAttribute
  • PointsMaterial 开启 vertexColors
  • 分别用 Points 和 Mesh 展示(本案例两者共用 material)
  • 代码要点

    import * as THREE from 'three'; import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

    const scene = new THREE.Scene();

    // 网格模型Mesh其实就一个一个三角形(面)拼接构成 const geometry = new THREE.BufferGeometry(); const vertices = new Float32Array([ 0, 0, 0, 50, 0, 0, 50, 0, 50,

    0, 0, 0, 0, 0, 50, 50, 0, 50, ]);

    geometry.attributes.position = new THREE.BufferAttribute(vertices, 3);

    const colors = new Float32Array([ 1, 1, 1, //顶点1颜色 1, 1, 0, //顶点2颜色 1, 1, 1, //顶点3颜色 1, 0, 0, //顶点4颜色 0, 1, 1, //顶点5颜色 0, 0, 1, //顶点6颜色 ]); geometry.attributes.color = new THREE.BufferAttribute(colors, 3)

    // 点颜色 const material = new THREE.PointsMaterial({ vertexColors: colors, size: 10.0 }) const points = new THREE.Points(geometry, material) scene.add(points)

    // 网格颜色 const mesh = new THREE.Mesh(geometry, material) scene.add(mesh)

    // AxesHelper const axesHelper = new THREE.AxesHelper(150); scene.add(axesHelper);

    // 相机 const camera = new THREE.PerspectiveCamera(); //相机 camera.position.set(200, 200, 200); //相机位置 camera.lookAt(0, 0, 0); //相机观察位置

    // 渲染器 const renderer = new THREE.WebGLRenderer(); // 创建渲染器 const box = document.getElementById('box'); renderer.setSize(box.clientWidth, box.clientHeight); //渲染区域 renderer.render(scene, camera); //执行渲染 box.appendChild(renderer.domElement);;

    const controls = new OrbitControls(camera, renderer.domElement); controls.addEventListener('change', function () { renderer.render(scene, camera); });

    完整源码:GitHub

    小结

    • 本文提供顶点颜色完整 Three.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
    • 更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
http://www.jsqmd.com/news/1117342/

相关文章:

  • 【仅限首批内测者开放】AI原生开发流程SOP v3.2(含Git提交规范/AI生成代码审计checklist/责任追溯机制)——来自20年技术委员会的强制落地建议
  • 鸿蒙原生 ArkTS 布局深度解析:Swiper 无限循环 —— 首尾无缝衔接的实现与原理
  • 【AI工具组合黄金法则】:20年实战验证的7步工作流重构法,效率提升300%的私密框架
  • 小红书内容采集神器:XHS-Downloader批量下载工具完全指南
  • 十堰网红火锅实测测评|科学避坑就餐选型指南
  • 通往AGI的具身之路——TVA自适应协同进化系统(5)
  • ACS CMxa2C00TN8DBNNNNNN0NN交流相驱动电源模块
  • Tomcat漏洞复现实战:从原理到加固的完整指南
  • 我用 Rust 写了个 AI 媒体管家:Gliding Horse 赋能 media_agent,目标是让 ComfyUI 工作流彻底自动化
  • 国产编程大模型实测:Kimi、MiniMax、Qwen、GLM五大场景硬核对比
  • 高效D类音频放大系统设计与实现
  • AD74412R与PIC18F26K20在工业自动化中的硬件设计与优化
  • Bullet Constraints Builder:终极Blender建筑坍塌模拟插件完全指南
  • 如何免费解锁IDM完整版:简单实用的激活脚本使用教程
  • PIC18F87K22与DS28EC20的1-Wire EEPROM存储方案
  • 工业4-20mA电流环系统设计与DAC161S997应用解析
  • DBeaver驱动包架构深度解析:构建企业级数据库连接管理解决方案
  • IndexTTS2 WebUI防御CC攻击实战:360网站卫士配置与防护策略详解
  • ChatGPT赋能自媒体创作全链路(含Prompt工程+合规审核+数据归因)——2024唯一经工信部备案验证的合规流程
  • MuleSoft企业级AI编排:让大语言模型合规、可审计、可运维
  • 遗传算法工业落地实战:破解早熟收敛与算子失效
  • PX4多旋翼无人机集群协同控制:深入解析分布式算法与通信机制
  • 多轴机床故障难诊断?LabVIEW+CompactRIO三层架构实现毫秒级预警
  • 6款论文降AI率软件横评:100%AI率清零,这款好用不心疼
  • 猫抓Cat-Catch:浏览器扩展的架构演进哲学与技术决策树分析
  • 飞书文档转Markdown终极指南:三步告别文档迁移烦恼
  • 5分钟搞定:DDrawCompat终极指南,让Windows 10/11经典游戏重获新生
  • 中国AI的工业数据闭环:从算力竞赛到物理世界锚定
  • 代码大模型选型指南:Claude 3.5 Sonnet与GPT-4o实战对比
  • Linux下fast.ai第一课环境搭建实战:CUDA驱动、conda依赖与GPU验证全指南