经纬志
首页 / three.js / API 文档

LineBasicMaterial

先看结论

基础线材质。给 Line、LineLoop、LineSegments 使用,只支持单像素宽的线,没有光照。

最后验证 2026-11-03 · three.js 0.160.0 · 更新 2026-10-29 · 引用本文
new THREE.LineBasicMaterial(parameters)

构造参数

参数默认说明
color0xffffff线颜色
linewidth1线宽,绝大多数平台不支持大于 1
linecap'round'线端样式,仅在支持宽线的平台生效
linejoin'round'拐角样式,同上
vertexColorsfalse读取几何体的 color 属性
fogtrue是否受场景雾影响
transparentfalse透明排序
opacity1透明度

示例

const geometry = new THREE.BufferGeometry().setFromPoints([
  new THREE.Vector3(-5, 0, 0),
  new THREE.Vector3(0, 3, 0),
  new THREE.Vector3(5, 0, 0)
]);

const material = new THREE.LineBasicMaterial({
  color: 0x4aa3df,
  transparent: true,
  opacity: 0.9
});

const line = new THREE.Line(geometry, material);
scene.add(line);

linewidth 为什么无效

WebGL 的 gl.lineWidth 在多数实现中只接受 1, ANGLE 与部分驱动直接忽略大于 1 的值。three.js 保留了该参数, 但实际渲染仍是单像素线。这不是配置问题,而是图形接口的限制。

需要粗线时

官方附加模块提供 Line2 与 LineMaterial, 用几何面片模拟线,可设置像素宽度、虚线、端点与拐角:

import { Line2 } from 'three/addons/lines/Line2.js';
import { LineGeometry } from 'three/addons/lines/LineGeometry.js';
import { LineMaterial } from 'three/addons/lines/LineMaterial.js';

const lineGeometry = new LineGeometry();
lineGeometry.setPositions([-5, 0, 0, 0, 3, 0, 5, 0, 0]);

const lineMaterial = new LineMaterial({
  color: 0x4aa3df,
  linewidth: 3,          // 单位是像素
  worldUnits: false
});
lineMaterial.resolution.set(window.innerWidth, window.innerHeight);

const thick = new Line2(lineGeometry, lineMaterial);
thick.computeLineDistances();
scene.add(thick);

线段与闭合线

对象连接方式
Line首尾依次相连,不闭合
LineLoop末尾顶点连回第一个,自动闭合
LineSegments每两个顶点一组,互不相连

线宽单位在附加模块里是像素。LineMaterial.linewidth 需要配合 resolution 使用,窗口尺寸变化时要同步更新, 否则线宽会随画布缩放而变粗或变细。

引用本文

经纬志(2026)。《LineBasicMaterial》。https://www.jingweizhi.com/three/api/line-basic-material.html(访问日期 2026-11-03)。