Unity角色残影特效

残影特效在网上有很多例子,比如​ ​这个​​,我参考着自己整合了一下,算是整合了一个比较完整且特别简单易用的出来,只需要一个脚本挂上去无需任何设定就能用。

这里只针对SkinnedMeshRenderer的网格(也就是带蒙皮的网格)残影,主要原理是根据设定的间隔时间连续的截取当前SkinnedMeshRenderer的网格数据并使用Graphics.DrawMesh画出网格。


一、首先是我们的残影类,为了能及时的Destroy,所以它最好派生至Object:

登录后复制

class AfterImage : Object{    //残影网格    public Mesh _Mesh;    //残影纹理    public Material _Material;    //残影位置    public Matrix4x4 _Matrix;    //残影透明度    public float _Alpha;    //残影启动时间    public float _StartTime;    //残影保留时间    public float _Duration;    public AfterImage(Mesh mesh, Material material, Matrix4x4 matrix4x4, float alpha, float startTime, float duration)    {        _Mesh = mesh;        _Material = material;        _Matrix = matrix4x4;        _Alpha = alpha;        _StartTime = startTime;        _Duration = duration;    }}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.


属性描述:残影从创建之时起便记录《残影启动时间》,当其生命周期达到或者超过了设定的《残影保留时间》时,该残影即被清除;《残影网格》为残影创建之时从SkinnedMeshRenderer截取而来,保留有当前SkinnedMeshRenderer的网格数据,并依据设定的《残影纹理》在《残影位置》以《残影透明度》DrawMesh。


二、我们再定义一个残影特效类来管理残影:

登录后复制

using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 残影特效/// </summary>public class AfterImageEffects : MonoBehaviour {    //开启残影    public bool _OpenAfterImage;    //残影颜色    public Color _AfterImageColor = Color.black;    //残影的生存时间    public float _SurvivalTime = 1;    //生成残影的间隔时间    public float _IntervalTime = 0.2f;    private float _Time = 0;    //残影初始透明度    [Range(0.1f, 1.0f)]    public float _InitialAlpha = 1.0f;    private List<AfterImage> _AfterImageList;    private SkinnedMeshRenderer _SkinnedMeshRenderer;    void Awake () {        _AfterImageList = new List<AfterImage>();        _SkinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();    }    void Update () {        if (_OpenAfterImage && _AfterImageList != null)        {            if (_SkinnedMeshRenderer == null)            {                _OpenAfterImage = false;                return;            }            _Time += Time.deltaTime;            //生成残影            CreateAfterImage();            //刷新残影            UpdateAfterImage();        }    }}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.


属性描述:《残影的生存时间》为每个残影从创建开始到销毁经历的时间,《生成残影的间隔时间》为残影创建后到下一个残影创建的时间,每个残影都会从《初始透明度》逐渐变化到0透明度并销毁。


生成残影:

登录后复制

/// <summary>    /// 生成残影    /// </summary>    void CreateAfterImage()    {        //生成残影        if (_Time >= _IntervalTime)        {            _Time = 0;            Mesh mesh = new Mesh();            _SkinnedMeshRenderer.BakeMesh(mesh);            Material material = new Material(_SkinnedMeshRenderer.material);            SetMaterialRenderingMode(material, RenderingMode.Fade);            _AfterImageList.Add(new AfterImage(                mesh,                material,                transform.localToWorldMatrix,                _InitialAlpha,                Time.realtimeSinceStartup,                _SurvivalTime));        }    }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.


刷新残影:

登录后复制

/// <summary>    /// 刷新残影    /// </summary>    void UpdateAfterImage()    {        //刷新残影,根据生存时间销毁已过时的残影        for (int i = 0; i < _AfterImageList.Count; i++)        {            float _PassingTime = Time.realtimeSinceStartup - _AfterImageList[i]._StartTime;            if (_PassingTime > _AfterImageList[i]._Duration)            {                _AfterImageList.Remove(_AfterImageList[i]);                Destroy(_AfterImageList[i]);                continue;            }            if (_AfterImageList[i]._Material.HasProperty("_Color"))            {                _AfterImageList[i]._Alpha *= (1 - _PassingTime / _AfterImageList[i]._Duration);                _AfterImageColor.a = _AfterImageList[i]._Alpha;                _AfterImageList[i]._Material.SetColor("_Color", _AfterImageColor);            }            Graphics.DrawMesh(_AfterImageList[i]._Mesh, _AfterImageList[i]._Matrix, _AfterImageList[i]._Material, gameObject.layer);        }    }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.


残影存在透明通道,所以必须要设置纹理的渲染模式为fade模式:

登录后复制

/// <summary>    /// 设置纹理渲染模式    /// </summary>    void SetMaterialRenderingMode(Material material, RenderingMode renderingMode)    {        switch (renderingMode)        {            case RenderingMode.Opaque:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);                material.SetInt("_ZWrite", 1);                material.DisableKeyword("_ALPHATEST_ON");                material.DisableKeyword("_ALPHABLEND_ON");                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = -1;                break;            case RenderingMode.Cutout:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);                material.SetInt("_ZWrite", 1);                material.EnableKeyword("_ALPHATEST_ON");                material.DisableKeyword("_ALPHABLEND_ON");                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = 2450;                break;            case RenderingMode.Fade:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);                material.SetInt("_ZWrite", 0);                material.DisableKeyword("_ALPHATEST_ON");                material.EnableKeyword("_ALPHABLEND_ON");                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = 3000;                break;            case RenderingMode.Transparent:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);                material.SetInt("_ZWrite", 0);                material.DisableKeyword("_ALPHATEST_ON");                material.DisableKeyword("_ALPHABLEND_ON");                material.EnableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = 3000;                break;        }    }1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.


三、效果图如下:

Unity角色残影特效_Material

Unity角色残影特效_unity_02

Unity角色残影特效_残影_03


四、我将完整的源码贴一遍,只需要新建一个脚本AfterImageEffects,复制以下源码到其中,然后挂在有SkinnedMeshRenderer组件的模型上,并勾选OpenAfterImage,运行便可以看到效果:


登录后复制

using UnityEngine;using System.Collections;using System.Collections.Generic;/// <summary>/// 残影特效/// </summary>public class AfterImageEffects : MonoBehaviour {    //开启残影    public bool _OpenAfterImage;    //残影颜色    public Color _AfterImageColor = Color.black;    //残影的生存时间    public float _SurvivalTime = 1;    //生成残影的间隔时间    public float _IntervalTime = 0.2f;    private float _Time = 0;    //残影初始透明度    [Range(0.1f, 1.0f)]    public float _InitialAlpha = 1.0f;    private List<AfterImage> _AfterImageList;    private SkinnedMeshRenderer _SkinnedMeshRenderer;    void Awake () {        _AfterImageList = new List<AfterImage>();        _SkinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();    }    void Update () {        if (_OpenAfterImage && _AfterImageList != null)        {            if (_SkinnedMeshRenderer == null)            {                _OpenAfterImage = false;                return;            }            _Time += Time.deltaTime;            //生成残影            CreateAfterImage();            //刷新残影            UpdateAfterImage();        }    }    /// <summary>    /// 生成残影    /// </summary>    void CreateAfterImage()    {        //生成残影        if (_Time >= _IntervalTime)        {            _Time = 0;            Mesh mesh = new Mesh();            _SkinnedMeshRenderer.BakeMesh(mesh);            Material material = new Material(_SkinnedMeshRenderer.material);            SetMaterialRenderingMode(material, RenderingMode.Fade);            _AfterImageList.Add(new AfterImage(                mesh,                material,                transform.localToWorldMatrix,                _InitialAlpha,                Time.realtimeSinceStartup,                _SurvivalTime));        }    }    /// <summary>    /// 刷新残影    /// </summary>    void UpdateAfterImage()    {        //刷新残影,根据生存时间销毁已过时的残影        for (int i = 0; i < _AfterImageList.Count; i++)        {            float _PassingTime = Time.realtimeSinceStartup - _AfterImageList[i]._StartTime;            if (_PassingTime > _AfterImageList[i]._Duration)            {                _AfterImageList.Remove(_AfterImageList[i]);                Destroy(_AfterImageList[i]);                continue;            }            if (_AfterImageList[i]._Material.HasProperty("_Color"))            {                _AfterImageList[i]._Alpha *= (1 - _PassingTime / _AfterImageList[i]._Duration);                _AfterImageColor.a = _AfterImageList[i]._Alpha;                _AfterImageList[i]._Material.SetColor("_Color", _AfterImageColor);            }            Graphics.DrawMesh(_AfterImageList[i]._Mesh, _AfterImageList[i]._Matrix, _AfterImageList[i]._Material, gameObject.layer);        }    }    /// <summary>    /// 设置纹理渲染模式    /// </summary>    void SetMaterialRenderingMode(Material material, RenderingMode renderingMode)    {        switch (renderingMode)        {            case RenderingMode.Opaque:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);                material.SetInt("_ZWrite", 1);                material.DisableKeyword("_ALPHATEST_ON");                material.DisableKeyword("_ALPHABLEND_ON");                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = -1;                break;            case RenderingMode.Cutout:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero);                material.SetInt("_ZWrite", 1);                material.EnableKeyword("_ALPHATEST_ON");                material.DisableKeyword("_ALPHABLEND_ON");                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = 2450;                break;            case RenderingMode.Fade:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.SrcAlpha);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);                material.SetInt("_ZWrite", 0);                material.DisableKeyword("_ALPHATEST_ON");                material.EnableKeyword("_ALPHABLEND_ON");                material.DisableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = 3000;                break;            case RenderingMode.Transparent:                material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One);                material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha);                material.SetInt("_ZWrite", 0);                material.DisableKeyword("_ALPHATEST_ON");                material.DisableKeyword("_ALPHABLEND_ON");                material.EnableKeyword("_ALPHAPREMULTIPLY_ON");                material.renderQueue = 3000;                break;        }    }}public enum RenderingMode{    Opaque,    Cutout,    Fade,    Transparent,}class AfterImage : Object{    //残影网格    public Mesh _Mesh;    //残影纹理    public Material _Material;    //残影位置    public Matrix4x4 _Matrix;    //残影透明度    public float _Alpha;    //残影启动时间    public float _StartTime;    //残影保留时间    public float _Duration;    public AfterImage(Mesh mesh, Material material, Matrix4x4 matrix4x4, float alpha, float startTime, float duration)    {        _Mesh = mesh;        _Material = material;        _Matrix = matrix4x4;        _Alpha = alpha;        _StartTime = startTime;        _Duration = duration;    }}1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.23.24.25.26.27.28.29.30.31.32.33.34.35.36.37.38.39.40.41.42.43.44.45.46.47.48.49.50.51.52.53.54.55.56.57.58.59.60.61.62.63.64.65.66.67.68.69.70.71.72.73.74.75.76.77.78.79.80.81.82.83.84.85.86.87.88.89.90.91.92.93.94.95.96.97.98.99.100.101.102.103.104.105.106.107.108.109.110.111.112.113.114.115.116.117.118.119.120.121.122.123.124.125.126.127.128.129.130.131.132.133.134.135.136.137.138.139.140.141.142.143.144.145.146.147.148.149.150.151.152.153.154.155.156.157.158.159.160.161.162.163.164.165.166.167.168.169.170.171.172.173.174.175.




免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空