您好,登錄后才能下訂單哦!
這篇文章主要講解了Unity實現角色受擊身體邊緣發光特效的方法,內容清晰明了,對此有興趣的小伙伴可以學習一下,相信大家閱讀完之后會有幫助。
游戲中經常需要制作角色受擊打的身體邊緣光效果,本文使用的方法是,給Renderer疊加一個制作好的邊緣光材質球,并通過腳本動態控制邊緣光的漸變效果,表現出受擊后的邊緣光效果
工程結構如下
1 創建一個材質球HittedMatEffect.mat放在Assets/Resources/Material目錄中,使用TransparentRim.shader
注意代碼中用了Resources.Load,所以必須放在這個目錄里,你可以改成別的方式
2 場景中創建一個Sphere(球體),掛上Runner腳本,運行,點擊屏幕任意位置,球體就會表現出受擊的邊緣光效果了
運行效果
代碼
TransparentRim.shader
Shader "Effect/TransparentRim" { Properties{ _RimColor("Rim Color", Color) = (0.5,0.5,0.5,0.5) _InnerColor("Inner Color", Color) = (0.5,0.5,0.5,0.5) _InnerColorPower("Inner Color Power", Range(0.0,1.0)) = 0.5 _RimPower("Rim Power", Range(0.0,5.0)) = 2.5 _AlphaPower("Alpha Rim Power", Range(0.0,8.0)) = 4.0 _AllPower("All Power", Range(0.0, 10.0)) = 1.0 } SubShader{ Tags { "Queue" = "Transparent" } CGPROGRAM #pragma surface surf Lambert alpha struct Input { float3 viewDir; INTERNAL_DATA }; float4 _RimColor; float _RimPower; float _AlphaPower; float _AlphaMin; float _InnerColorPower; float _AllPower; float4 _InnerColor; void surf(Input IN, inout SurfaceOutput o) { half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal)); o.Emission = _RimColor.rgb * pow(rim, _RimPower)*_AllPower + (_InnerColor.rgb * 2 * _InnerColorPower); o.Alpha = (pow(rim, _AlphaPower))*_AllPower; } ENDCG } Fallback "VertexLit" }
HittedMatEffect.cs
// HittedMatEffect.cs using UnityEngine; using System.Collections; public class HittedMatEffect : MonoBehaviour { bool mbActive = false; bool mbInit = false; Material mMat = null; public float mLife; private static int s_InnerColor = -1; private static int s_AllPower = -1; private static int s_AlphaPower = -1; void Awake() { s_InnerColor = Shader.PropertyToID("_InnerColor"); s_AllPower = Shader.PropertyToID("_AllPower"); s_AlphaPower = Shader.PropertyToID("_AlphaPower"); } // Use this for initialization /// <summary> /// 設置材質顏色 /// </summary> /// <param name="color"></param> public void SetColor(Color color) { mMat.SetColor(s_InnerColor, color); } public void SetLifeTime(float time) { mLife = time; } public void Active() { if (!mbInit) AddEffect(); mMat.SetFloat(s_AllPower, 0.9f); mbActive = true; mLife = 0.2f; } void Update() { if (!mbActive) return; mLife -= Time.deltaTime; if (mLife < 0) { mbActive = false; mMat.SetFloat(s_AllPower, 0); } float v = Mathf.Sin((1 - mLife) * 8 * Mathf.PI) + 2; mMat.SetFloat(s_AlphaPower, v); } void AddEffect() { Object mat = Resources.Load("Material/HittedMatEffect"); mMat = GameObject.Instantiate(mat) as Material; foreach (var curMeshRender in transform.GetComponentsInChildren<Renderer>()) { Material[] newMaterialArray = new Material[curMeshRender.materials.Length + 1]; for (int i = 0; i < curMeshRender.materials.Length; i++) { if (curMeshRender.materials[i].name.Contains("HittedMatEffect")) { return; } else { newMaterialArray[i] = curMeshRender.materials[i]; } } if (null != mMat) newMaterialArray[curMeshRender.materials.Length] = mMat; curMeshRender.materials = newMaterialArray; } mbInit = true; } void RemoveEffect() { foreach (var curMeshRender in transform.GetComponentsInChildren<Renderer>()) { int newMaterialArrayCount = 0; for (int i = 0; i < curMeshRender.materials.Length; i++) { if (curMeshRender.materials[i].name.Contains("HittedMatEffect")) { newMaterialArrayCount++; } } if (newMaterialArrayCount > 0) { Material[] newMaterialArray = new Material[newMaterialArrayCount]; int curMaterialIndex = 0; for (int i = 0; i < curMeshRender.materials.Length; i++) { if (curMaterialIndex >= newMaterialArrayCount) { break; } if (!curMeshRender.materials[i].name.Contains("HittedMatEffect")) { newMaterialArray[curMaterialIndex] = curMeshRender.materials[i]; curMaterialIndex++; } } curMeshRender.materials = newMaterialArray; } } } }
Runner.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Runner : MonoBehaviour { // Use this for initialization void Start() { } // Update is called once per frame void Update() { if (Input.GetMouseButtonDown(0)) { HittedMatEffect sc = gameObject.GetComponent<HittedMatEffect>(); if (null == sc) sc = gameObject.AddComponent<HittedMatEffect>(); sc.Active(); sc.SetColor(Color.red); } } }
看完上述內容,是不是對Unity實現角色受擊身體邊緣發光特效的方法有進一步的了解,如果還想學習更多內容,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。