Textures.DissolveShader - robblofield/TomeboundDocs GitHub Wiki
Author: Eric Fernandes
Last Updated: 07/12/2023
A unity shader graph that creates a dissolving effect, along with several scripts being related/attached to the graph that help apply the effect on the object with the given shader.
- Dissolve Shader Graph
- Dissolve Script
- Dissolve Trigger Script
- Trigger Movement Script
- Trigger Dis Script
- Use Case
- Breakdown of Shader/Texture/Material
- Shader Graph
- Nodes
- Dissolve Script
- Using Statements
- Class Declaration
- Variables
- Material NewRockDissolve
- Float dissolveAmount
- Float dissolveSpeed
- Bool isDissolving
- Bool isTriggered
- Start()
- Update()
- StartDissolve()
- StopDissolve()
- Shader Graph
Both the graph and the Dissolve Scripts would be applied to the game object or instance that is intended to be dissolved during gameplay, while the trigger movement and dis script will be applied to a single trigger that would move in a straight, linear path to the target, and during that movement, it will act as a trigger for the dissolve scripts, which would activate the dissolve shader.
[Image]
[Description]
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DissolveScript : MonoBehaviour
private Material NewRockDissolve;
private float dissolveAmount;
private float dissolveSpeed;
private bool isDissolving;
[SerializeField] public bool isTriggered;
Material NewRockDissolve: Allows the script to link with the chosen material that will be dissolved
Float dissolveAmount: The value will determine how dissolved the object is, and it is used in the script as a slider, rapidly changing from one value smoothly to another value.
Float dissolveSpeed: The value that will determine how quickly the dissolve will take place on the object with the material.
Bool isDissolving: The statement to check if the material is already dissolving on the object, which will be used to determine the object's collisions & activeness [True to False]
Bool isTriggered: Similar functionality to isDissolving.
public void Start()
{
dissolveAmount = 0f;
isTriggered= false;
}
The start function makes sure that the dissolveAmount value for the material is set to 0, so it does not start with half of the object already dissolved/missing.
public void Update()
{
if (isDissolving && isTriggered)
{
Debug.Log("I am now Dissolving");
dissolveAmount = Mathf.Clamp01(dissolveAmount + dissolveSpeed * Time.deltaTime);
gameObject.GetComponent<Renderer>().sharedMaterial.SetFloat("_DissolveAmount", dissolveAmount);
}
else
{
dissolveAmount = Mathf.Clamp01(dissolveAmount - dissolveSpeed * Time.deltaTime);
gameObject.GetComponent<Renderer>().sharedMaterial.SetFloat("_DissolveAmount", dissolveAmount);
}
}
The update function is checking if the object has been both triggered and is starting to dissolve through the two boolean statements, and if one or both conditions are met, then the object will start to dissolve at the given speed along with delta.Time. To ensure that only the object that was triggered dissolves, it has to be made clear in the code that it is to only grab the game object's renderer component and edit the float value of the material which is shared.
public void StartDissolve(float dissolveSpeed)
{
isDissolving = true;
isTriggered = true;
this.dissolveSpeed = dissolveSpeed;
}
This function/method is what changes the two boolean states to be true in order to start the dissolve of the object, along with setting the dissolve speed to a set value which can be edited in the variables at the top of the script.
public void StopDissolve(float dissolveSpeed)
{
isDissolving = false;
isTriggered = false;
this.dissolveSpeed = dissolveSpeed;
}
This function performs the opposite of the function above, and will prevent the shader from overlapping and repeating a dissolve on an already disabled & dissolved object.
In future, the shader's dissolve speed will be made quicker to match the requirements and demands set by the lead of the project. Along with more refinement of the code to make it a clearer viewing and also to prevent any sort of possible issues when interacting with other scripts.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DissolveScript : MonoBehaviour
{
private Material NewRockDissolve;
private float dissolveAmount;
private float dissolveSpeed;
private bool isDissolving;
[SerializeField] public bool isTriggered;
public void Start()
{
dissolveAmount = 0f;
isTriggered= false;
}
public void Update()
{
if (isDissolving && isTriggered)
{
Debug.Log("I am now Dissolving");
dissolveAmount = Mathf.Clamp01(dissolveAmount + dissolveSpeed * Time.deltaTime);
gameObject.GetComponent<Renderer>().sharedMaterial.SetFloat("_DissolveAmount", dissolveAmount);
}
else
{
dissolveAmount = Mathf.Clamp01(dissolveAmount - dissolveSpeed * Time.deltaTime);
gameObject.GetComponent<Renderer>().sharedMaterial.SetFloat("_DissolveAmount", dissolveAmount);
}
}
public void StartDissolve(float dissolveSpeed)
{
isDissolving = true;
isTriggered = true;
this.dissolveSpeed = dissolveSpeed;
}
public void StopDissolve(float dissolveSpeed)
{
isDissolving = false;
isTriggered = false;
this.dissolveSpeed = dissolveSpeed;
}
}