diff options
author | chai <chaifix@163.com> | 2020-10-22 08:57:28 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-10-22 08:57:28 +0800 |
commit | 8268e4e308bd110dfea4ad849a7ff74e66951349 (patch) | |
tree | fdfa55d7272092df599c107d4a282357cce325c5 /Assets/Scripts/Effects | |
parent | df84ee7e568fa500fec7b1865b966345b814e68f (diff) |
*残影特效
Diffstat (limited to 'Assets/Scripts/Effects')
-rw-r--r-- | Assets/Scripts/Effects/AfterImageEffects.cs | 2 | ||||
-rw-r--r-- | Assets/Scripts/Effects/CharacterGhostEffect.cs | 127 | ||||
-rw-r--r-- | Assets/Scripts/Effects/CharacterGhostEffect.cs.meta (renamed from Assets/Scripts/Effects/IEffectHandler.cs.meta) | 2 | ||||
-rw-r--r-- | Assets/Scripts/Effects/EffectHandler.cs (renamed from Assets/Scripts/Effects/IEffectHandler.cs) | 36 | ||||
-rw-r--r-- | Assets/Scripts/Effects/EffectHandler.cs.meta | 11 |
5 files changed, 158 insertions, 20 deletions
diff --git a/Assets/Scripts/Effects/AfterImageEffects.cs b/Assets/Scripts/Effects/AfterImageEffects.cs index a19be897..ba269279 100644 --- a/Assets/Scripts/Effects/AfterImageEffects.cs +++ b/Assets/Scripts/Effects/AfterImageEffects.cs @@ -1,4 +1,4 @@ -using UnityEngine;
+ using UnityEngine;
using System.Collections;
using System.Collections.Generic;
/// <summary>
diff --git a/Assets/Scripts/Effects/CharacterGhostEffect.cs b/Assets/Scripts/Effects/CharacterGhostEffect.cs new file mode 100644 index 00000000..d40adc17 --- /dev/null +++ b/Assets/Scripts/Effects/CharacterGhostEffect.cs @@ -0,0 +1,127 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CharacterGhostEffect : EffectHandler +{ + [Tooltip("目标skinMeshRenderer")] + public SkinnedMeshRenderer m_Renderer; + + [Tooltip("如果mesh包含多个submesh,需要每个submesh单独设置一个材质")] + public Material[] Materials; + + [Tooltip("Submesh是否使用同一个材质")] + public bool SubmeshShareMaterial; + + [Tooltip("使用范围内的submesh")] + public bool UseRangedSubmesh; + + [Tooltip("Submesh索引范围")] + public Vector2Int SubmeshRange; + + [Tooltip("是否开启残影效果")] + public bool IsEnable; + + [Tooltip("残影生成的时间间隔")] + public float Interval = 0.1f; + + [Tooltip("残影的生存时间")] + public float LifeTime = 0.5f; + + private float m_Time = 0; + private List<GhostSnapshot> m_Ghosts = new List<GhostSnapshot>(); + + void Update() + { + //if (!IsEnable) + //{ + // if (m_Ghosts.Count > 0) + // UpdateGhost(); // destroy ghosts + // return; + //} + + if (m_Ghosts == null || m_Renderer == null || Materials == null || Materials.Length == 0) + { + IsEnable = false; + return; + } + + m_Time += Time.deltaTime; + + //if(IsEnable) + // CreateGhost(); + + UpdateGhost(); + DrawGhost(); + } + + public void CreateGhost() + { + if(m_Time >= Interval) + { + m_Time -= Interval; + + Mesh mesh = new Mesh(); + m_Renderer.BakeMesh(mesh); + + Matrix4x4 mat = m_Renderer.localToWorldMatrix; + + m_Ghosts.Add(new GhostSnapshot(mesh, mat, Time.realtimeSinceStartup, LifeTime)); + } + } + + void UpdateGhost() + { + for(int i = m_Ghosts.Count - 1; i >= 0 ; --i) + { + GhostSnapshot ghost = m_Ghosts[i]; + float passTime = Time.realtimeSinceStartup - ghost.startTime; + + if (passTime > ghost.lifeTime) + { + m_Ghosts.Remove(ghost); + Destroy(ghost); + continue; + } + } + } + + void DrawGhost() + { + for(int i = 0; i < m_Ghosts.Count; ++ i) + { + GhostSnapshot ghost = m_Ghosts[i]; + int start = 0, end = ghost.mesh.subMeshCount - 1; + if(UseRangedSubmesh) + { + start = SubmeshRange.x; + end = SubmeshRange.y; + } + for (int j = start; j <= end; ++j) + { + Material material; + if (SubmeshShareMaterial) + material = Materials[0]; + else + material = Materials[j]; + Graphics.DrawMesh(ghost.mesh, ghost.matrix, material, gameObject.layer, null, j); + } + } + } +} + +class GhostSnapshot : Object +{ + public Mesh mesh; + public Matrix4x4 matrix; + public float startTime; + public float lifeTime; + + public GhostSnapshot(Mesh mesh, Matrix4x4 mat, float startTime, float lifeTime) + { + this.mesh = mesh; + this.matrix = mat; + this.startTime = startTime; + this.lifeTime = lifeTime; + } +} diff --git a/Assets/Scripts/Effects/IEffectHandler.cs.meta b/Assets/Scripts/Effects/CharacterGhostEffect.cs.meta index 142a3f23..9a1f3a3a 100644 --- a/Assets/Scripts/Effects/IEffectHandler.cs.meta +++ b/Assets/Scripts/Effects/CharacterGhostEffect.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: cb3081a24820b3e438133b2ca6eb5549 +guid: 76b355b49906df24cac5ccfb0de69331 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/Scripts/Effects/IEffectHandler.cs b/Assets/Scripts/Effects/EffectHandler.cs index bdc4c6a0..265a1001 100644 --- a/Assets/Scripts/Effects/IEffectHandler.cs +++ b/Assets/Scripts/Effects/EffectHandler.cs @@ -1,18 +1,18 @@ -using System.Collections;
-using System.Collections.Generic;
-using UnityEngine;
-
-public class IEffectHandler : MonoBehaviour
-{
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
-}
+using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class EffectHandler : MonoBehaviour +{ + // Start is called before the first frame update + void Start() + { + + } + + // Update is called once per frame + void Update() + { + + } +} diff --git a/Assets/Scripts/Effects/EffectHandler.cs.meta b/Assets/Scripts/Effects/EffectHandler.cs.meta new file mode 100644 index 00000000..81ee0c34 --- /dev/null +++ b/Assets/Scripts/Effects/EffectHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 83576b34fc8f74048a5c050c30ab463f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |