返回介绍

手册

参考

示例

开发者参考

MeshSurfaceSampler

发布于 2021-07-10 14:14:20 字数 2823 浏览 1175 评论 0 收藏 0

Utility class for sampling weighted random points on the surface of a mesh.

Weighted sampling is useful for effects like heavier foliage growth in certain areas of terrain, or concentrated particle emissions from specific parts of a mesh. Vertex weights may be written programmatically, or painted by hand as vertex colors in 3D tools like Blender.

代码示例

// 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

Constructor

MeshSurfaceSampler( mesh : Mesh )

mesh — Surface mesh from which to sample.

Creates a new MeshSurfaceSampler. If the input geometry is indexed, a non-indexed copy is made. After construction, the sampler is not able to return samples until build is called.

Methods

.setWeightAttribute ( name : String ) : this

Specifies a vertex attribute to be used as a weight when sampling from the surface. Faces with higher weights are more likely to be sampled, and those with weights of zero will not be sampled at all. For vector attributes, only .x is used in sampling.

If no weight attribute is selected, sampling is randomly distributed by area.

.build () : this

Processes the input geometry and prepares to return samples. Any configuration of the geometry or sampler must occur before this method is called. Time complexity is O(n) for a surface with n faces.

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

Selects a random point on the surface of the input geometry, returning the position and optionally the normal vector and color at that point. Time complexity is O(log n) for a surface with n faces.

Source

examples/jsm/math/MeshSurfaceSampler.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文