首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >粒子系统-通过脚本设置发射器的网格

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

Stack Overflow用户
提问于 2014-08-26 06:52:53
回答 1查看 6.5K关注 0票数 1

如何在“粒子系统”(Shuriken)组件中设置粒子发射网格?在谷歌搜索只为遗留粒子系统返回我的教程。

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-26 07:59:07

..。

UP1。哦,我看到下面的评论。不,您不能改变网格形状从脚本,但一旦我看到了解决这个问题的解决办法。它是关于将空网格设置为ParticleSystem发射形状,然后从脚本更改网格几何图形。

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

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

代码语言:javascript
复制
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
            );
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25499739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档