粒子系统 - 通过脚本设置发射器的网格

时间:2014-08-26 06:52:53

标签: unity3d mesh particle-system

如何在"粒子系统"(手里剑)组件中设置粒子发射网格?在谷歌搜索只返回传统粒子系统的教程。

UPD: @gamedevelopmentgerm回答了我的问题:

  

如果要在运行时控制粒子系统的所有方面,则必须使用旧粒子。

1 个答案:

答案 0 :(得分:2)

...

UP1。哦,我看到下面的评论。不,虽然我已经看到针对此问题的解决方法,但您无法从脚本更改网格形状。这是关于将空网格设置为粒子系统发射形状,然后从脚本中更改网格几何体。

UP2。好的,我看到更多细节。如果你想模拟火焰传播,那么我建议你考虑ParticleSystem.Emit方法而不是任何发射形状。我推荐它,因为使用ParticleSystem.Emit,您将能够以更灵活的方式控制发射形状。

实际上,您也可以使用ParticleSystem.Emit从不同的网格实例中发射粒子。

using UnityEngine;
using System.Collections;

[RequireComponent( typeof( ParticleSystem ) )]
public class CustomParticleSystem : MonoBehaviour 
{
    #region Properties
    public Mesh[] EmissionShapes;
    public float  EmissionShapeSwitchSpeed = 0.5f;
    public float  EmissionRate = 10f;
    #endregion

    float          _shapeIndex = 0f;
    float          _timeToEmission = 0f;
    ParticleSystem _particleSystem;

    float EmissionPeriod { get { return 1f / EmissionRate; } }

    void Awake () 
    {
        _particleSystem = GetComponent<ParticleSystem>();
        _particleSystem.emissionRate = 0f;
        _timeToEmission = EmissionPeriod;
    }

    void Start () 
    {   
    }

    void Update () 
    {
        _shapeIndex += EmissionShapeSwitchSpeed * Time.deltaTime;
        if( _shapeIndex > EmissionShapes.Length-1 ) _shapeIndex -= (EmissionShapes.Length-1);

        _timeToEmission -= Time.deltaTime;
        if( _timeToEmission <= 0f )
        {
            _timeToEmission = EmissionPeriod - _timeToEmission;

            Mesh currentShape = EmissionShapes[(int)_shapeIndex];

            int triangleIndex = Random.Range( 0, currentShape.triangles.Length/3 );

            int vertexIndex0 = currentShape.triangles[triangleIndex*3];
            int vertexIndex1 = currentShape.triangles[triangleIndex*3+1];
            int vertexIndex2 = currentShape.triangles[triangleIndex*3+2];

            Vector3 v0 = currentShape.vertices[vertexIndex0];
            Vector3 v1 = currentShape.vertices[vertexIndex1];
            Vector3 v2 = currentShape.vertices[vertexIndex2];

            Vector3 n0 = currentShape.normals[vertexIndex0];
            Vector3 n1 = currentShape.normals[vertexIndex1];
            Vector3 n2 = currentShape.normals[vertexIndex2];

            float u = Random.Range( 0f, 1f );
            float v = Random.Range( 0f, 1f );
            float w = Random.Range( 0f, 1f );
            float uvw = u+v+w;
            u /= uvw;
            v /= uvw;
            w /= uvw;

            Vector3 randomPosition = v0*u + v1*v + v2*w;
            Vector3 normalAtRandomPosition = n0*u + n1*v + n2*w;

            randomPosition = this.transform.localToWorldMatrix.MultiplyPoint( randomPosition );
            normalAtRandomPosition = this.transform.localToWorldMatrix.MultiplyVector( normalAtRandomPosition );

            _particleSystem.Emit( 
                randomPosition,
                normalAtRandomPosition * _particleSystem.startSpeed, 
                _particleSystem.startSize,
                _particleSystem.startLifetime,
                _particleSystem.startColor
            );
        }
    }
}