【问题标题】:Three.js Particles of various sizesThree.js 各种大小的粒子
【发布时间】:2013-08-15 17:13:02
【问题描述】:

我是 three.js 的新手,我正在尝试找出添加 1000 个粒子的最佳方法,每个粒子具有不同的大小和颜色。每个粒子的纹理都是通过绘制画布创建的。通过使用粒子系统,所有粒子的颜色和大小都相同。为每个粒子创建一个 ParticleSystem 效率非常低。有没有有效的方法来处理这个问题?

【问题讨论】:

标签: javascript three.js particles


【解决方案1】:

根据我的经验,最好的方法是创建自定义着色器材质;然后,您可以包含属性,这些属性是因粒子而异的属性。您的着色器代码如下所示:

顶点着色器:

attribute vec3 customColor;
attribute float customSize;
varying vec3 vColor;
void main() 
{
    vColor = customColor; // set color associated to vertex; use later in fragment shader
    vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
    gl_PointSize = customSize * ( 300.0 / length( mvPosition.xyz ) );
    gl_Position = projectionMatrix * mvPosition;
}

片段着色器:

uniform sampler2D texture;
varying vec3 vColor; // colors associated to vertices; assigned by vertex shader
void main() 
{
    // calculates a color for the particle
    gl_FragColor = vec4( vColor, 1.0 );
    // sets particle texture to desired color
    gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
}

对于类似的现场示例,请查看:

http://stemkoski.github.io/Three.js/ParticleSystem-Attributes.html

【讨论】:

  • 另见基于@lee-stemkoski 工作的ShaderParticleEngine
  • +1:由于巧妙地使用了 GPU,上述评论中的链接运行速度提高了 10 倍。
  • 同时,threejs 不再支持 material-attributes,它们现在在几何中,而且,Geometry.colors 是一个可以填充的标准数组,导致属性color 传递给着色器。
猜你喜欢
  • 2012-06-22
  • 2014-01-26
  • 2012-06-28
  • 2013-08-28
  • 2015-06-24
  • 1970-01-01
  • 2011-11-15
  • 2014-06-29
  • 2013-07-12
相关资源
最近更新 更多