Three.js Particles of various sizes

别等时光非礼了梦想. 提交于 2019-12-13 18:19:05

问题


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. The texture for each particle is created by drawing a canvas. By using a ParticleSystem all the particles are the same color and size. Creating a ParticleSystem for each particle is very inefficient. Is there an efficient way to handle this?


回答1:


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



来源:https://stackoverflow.com/questions/18257929/three-js-particles-of-various-sizes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!