网格表面采样器([name])

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

加权采样对于诸如地形的特定区域内更浓密的植被生长或来自网格特定部分的浓缩粒子排放等效果非常有用。顶点权重可以通过编程方式编写,也可以在 3D 工具(如 Blender)中作为顶点颜色手工绘制。

导入

[name] 是一个附加组件,必须显式导入。请参阅 [link:#manual/introduction/Installation Installation / Addons].

import { MeshSurfaceSampler } from 'three/addons/math/MeshSurfaceSampler.js';

代码示例

// Create a sampler for a Mesh surface. const sampler = new MeshSurfaceSampler( surfaceMesh ) .setWeightAttribute( 'color' ) .build(); const mesh = 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 ); } scene.add( mesh );

例子

[example:webgl_instancing_scatter]

构造函数

[name]( [param:Mesh mesh] )

[page:Mesh mesh] — ​​进行采样的表面网格。

创建一个新的 [name]。如果输入的几何体是索引的,将创建一个非索引的副本。构造后,采样器无法返回样本,直到调用 [page:MeshSurfaceSampler.build build] 方法。

方法

[method:this setWeightAttribute]( [param:String name] )

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

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

[method:this build]()

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

[method:this sample]( [param:Vector3 targetPosition], [param:Vector3 targetNormal], [param:Color targetColor], [param:Vector2 targetUV] )

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

源代码

[link:https://github.com/mrdoob/three.js/blob/master/examples/jsm/math/MeshSurfaceSampler.js examples/jsm/math/MeshSurfaceSampler.js]