【Unity3D】消融特效

2023-10-15 15:01:04

1 前言

選中物體消融特效中基於 Shader 實現了消融特效,本文將基於 Shader Graph 實現消融特效,兩者原理一樣,只是表達方式不同,另外,選中物體消融特效中通過 discard 丟棄片元,本文通過 alpha 測試丟棄片元。

​ Shader Graph環境搭建、簡單應用詳見→Shader Graph簡介

​ 本文完整資源見→Unity3D消融特效

2 消融特效實現

​ 建立 Unlit Shader Graph,重新命名為 DissolveEffect,編輯如下。

​ 其中,Alpha 測試需要在 Graph Settings 中開啟,如下。

​ 為實現點選消失效果,需要編寫指令碼如下。

​ DieController.cs

using UnityEngine;
 
public class DieController : MonoBehaviour {
    private RaycastHit hit; // 碰撞資訊
 
    private void Start() {
        hit = new RaycastHit();
    }

    private void Update() {
        if (Input.GetMouseButtonUp(0)) {
            GameObject hitObj = GetHitObj();
            if (hitObj != null) {
                GameObject rootObj = GetRootObj(hitObj);
                rootObj.AddComponent<DissolveEffect>();
            }
        }
    }

    private GameObject GetHitObj() { // 獲取螢幕射線碰撞的物體
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit)) {
            return hit.collider.gameObject;
        }
        return null;
    }

    private GameObject GetRootObj(GameObject obj) { // 獲取根物件
        while (obj.transform.parent != null && obj.layer == obj.transform.parent.gameObject.layer) {
            obj = obj.transform.parent.gameObject;
        }
        return obj;
    }
}

​ DissolveEffect.cs

using UnityEngine;
 
[DisallowMultipleComponent] // 不允許在同一物件上掛載多個該元件
public class DissolveEffect : MonoBehaviour {
    private Renderer[] renderers; // 渲染器
    private Material dissolveMat; // 消融材質
    private float burnSpeed = 0.25f; // 燃燒速度
    private float burnAmount = 0; // 燃燒量, 值越大模型鏤空的越多
 
    private void Awake() {
        dissolveMat = Resources.Load<Material>("DissolveMat");
        renderers = GetComponentsInChildren<Renderer>();
    }

    private void OnEnable() {
        foreach (Renderer renderer in renderers) {
            Material[] materials = renderer.sharedMaterials;
            Material[] dissolveMaterials = new Material[materials.Length];
            for (int i = 0; i < materials.Length; i++) {
                Material newMaterial = new Material(dissolveMat);
                SetTexture(materials[i], newMaterial);
                SetColor(materials[i], newMaterial);
                newMaterial.SetFloat("_BurnAmount", 0);
                dissolveMaterials[i] = newMaterial;
            }
            renderer.sharedMaterials = dissolveMaterials;
        }
    }

    private void Update() {
        burnAmount += Time.deltaTime * burnSpeed;
        foreach (Renderer renderer in renderers) {
            Material[] materials = renderer.sharedMaterials;
            foreach (Material material in materials) {
                material.SetFloat("_BurnAmount", burnAmount);
            }
        }
        if (burnAmount >= 1f) {
            Destroy(gameObject);
        }
    }

    private void SetTexture(Material oldMaterial, Material newMaterial) { // 設定材質
        if (oldMaterial.HasTexture("_MainTex")) {
            Texture texture = oldMaterial.GetTexture("_MainTex");
            newMaterial.SetTexture("_MainTex", texture);
        }
    }

    private void SetColor(Material oldMaterial, Material newMaterial) { // 設定顏色
        Color color = Color.white;
        if (oldMaterial.HasColor("_Color")) {
            color = oldMaterial.GetColor("_Color");
        }
        newMaterial.SetColor("_Color", color);
    }
}

​ 執行效果如下。

​ 宣告:本文轉自【Unity3D】消融特效