[Unity3D] Ablation effects

1 Introduction

        In the ablation effect of the selected object , the ablation effect is realized based on Shader. This article will implement the ablation effect based on Shader Graph. The principle of the two is the same, but the expression method is different. In addition, in the ablation effect of the selected object , the fragment is discarded through discard. This article uses the alpha test to discard the fragment Yuan.

        For Shader Graph environment construction and simple application, please refer to → Introduction to Shader Graph .

        For the complete resources of this article, see → Unity3D ablation effects .

2 Realization of special effects of ablation

        Create Unlit Shader Graph, rename it to DissolveEffect, edit as follows. 

        Among them, the Alpha test needs to be enabled in Graph Settings, as follows.

        In order to realize the disappearing effect after clicking, you need to write the script as follows.

        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);
    }
}

        The running effect is as follows.

Guess you like

Origin blog.csdn.net/m0_37602827/article/details/132126353