three.js MeshSurfaceSampler

2023-02-16 17:51 更新

用于对网格表面上的加权随机点进行采样的实用程序类。

加权采样对于某些地形区域中较重的树叶生长或网格特定部分的集中粒子发射等效果很有用。顶点权重可以编程方式编写,也可以在 Blender 等 3D 工具中手工绘制为顶点颜色。

代码示例

// Create a sampler for a Mesh surface.
const sampler = new MeshSurfaceSampler( surfaceMesh )
	.setWeightAttribute( 'color' )
	.build();

const sampleMesh = new THREE.InstancedMesh( sampleGeometry, sampleMaterial, 100 );

const _position = new THREE.Vector3();
const _matrix = new THREE.Matrix4();

// Sample randomly from the surface, creating an instance of the sample
// geometry at each sample point.
for ( let i = 0; i < 100; i ++ ) {

	sampler.sample( _position );

	_matrix.makeTranslation( _position.x, _position.y, _position.z );

	mesh.setMatrixAt( i, _matrix );

}

mesh.instanceMatrix.needsUpdate = true;

scene.add( mesh );

例子

webgl_instancing_scatter

构造函数

MeshSurfaceSampler( mesh : Mesh )

mesh — 从中采样的表面网格。

创建一个新的 MeshSurfaceSampler。如果输入几何体被索引,则创建一个非索引副本。构建之后,采样器无法返回样本,直到调用构建。

方法

.setWeightAttribute ( name : String ) : this

指定从表面采样时用作权重的顶点属性。权重较高的人脸更有可能被采样,而权重为零的人脸则根本不会被采样。对于矢量属性,仅 .x 用于采样。

如果未选择权重属性,则采样按区域随机分布。

.build () : this

处理输入几何并准备返回样本。几何或采样器的任何配置都必须在调用此方法之前进行。对于具有 n 个面的表面,时间复杂度为 O(n)。

.sample ( targetPosition : Vector3, targetNormal : Vector3, targetColor : Color ) : this

在输入几何体的表面上选择一个随机点,返回该点的位置以及可选的法向量和颜色。对于具有 n 个面的表面,时间复杂度为 O(log n)。

源码

examples/jsm/math/MeshSurfaceSampler.js


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号