简体   繁体   English

Three.js各种尺寸的颗粒

[英]Three.js Particles of various sizes

I'm new to three.js and am trying to figure out the best way to add a 1000 particles each being a different size and color. 我是three.js的新手,我正试图找出添加1000个粒子的最佳方法,每个粒子的大小和颜色都不同。 The texture for each particle is created by drawing a canvas. 通过绘制画布创建每个粒子的纹理。 By using a ParticleSystem all the particles are the same color and size. 通过使用ParticleSystem,所有粒子的颜色和大小都相同。 Creating a ParticleSystem for each particle is very inefficient. 为每个粒子创建粒子系统的效率非常低。 Is there an efficient way to handle this? 有没有一种有效的方法来处理这个问题?

The best way to go about this, in my experience, is to create a customized shader material; 根据我的经验,最好的方法是创建一个定制的着色器材料; you can then include attributes, which are properties that vary from particle to particle. 然后,您可以包含属性,这些属性是从粒子到粒子的不同属性。 Your shader code would look something like this: 您的着色器代码看起来像这样:

Vertex shader: 顶点着色器:

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;
}

Fragment shader: 片段着色器:

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 );
}

For a similar live example, check out: 有关类似的实例,请查看:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM