The Damage FX Object consists of three essentials pieces:
Damage FX Shader
Let’s look at the basic application of the shader with the base layer which is rendering the initial or untouched mesh, and also the damage layer added by the Damage FX function. The universal example would be FORGE3D/DamageFX/PBR Specular Gloss shader in our case.
The damage is added dynamically upon providing an object space point and non empty value to the shader data array. The shader function will iterate through the available impact points building the required masks given the following values:
These five values are individual per impact point and you can experiment with them in the real-time by launching the example scenes.
Note the default configuration is set to 200 impact points per Damage FX Object. Adding more points over the limit will overwrite existing ones from the beginning of the shader array.
Damage FX Script Component
Assets/FORGE3D/DamageFX/Scripts/DamageFX.cs
In order to control the Damage FX Shader, an instance of the script should be present on each Damage FX Object. The script component handles the data processing for impact spots and fades the heat values over time.
Use DamageFX.Hit() method of the script to add new impacts to the surface of the mesh as shown below:
Here is the simple code snippet that will add the damage point to the Damage FX Object under the mouse cursor on left mouse click:
public float HitRadius = 0.1f;
public float Dirt = 1f;
public float Burn = 1f;
public float Heat = 1f;
public float Clip = 0.7f;
private RaycastHit _hitInfo;
private void Update()
{
if (!Input.GetMouseButtonDown(0)) return;
var screenRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!Physics.Raycast(screenRay, out _hitInfo, Mathf.Infinity)) return;
var dfx = _hitInfo.collider.GetComponent<DamageFX>();
if (dfx != null) dfx.Hit(dfx.transform.InverseTransformPoint(_hitInfo.point), HitRadius, Dirt, Burn, Heat, Clip);
} |
public float HitRadius = 0.1f;
public float Dirt = 1f;
public float Burn = 1f;
public float Heat = 1f;
public float Clip = 0.7f;
private RaycastHit _hitInfo;
private void Update()
{
if (!Input.GetMouseButtonDown(0)) return;
var screenRay = Camera.main.ScreenPointToRay(Input.mousePosition);
if (!Physics.Raycast(screenRay, out _hitInfo, Mathf.Infinity)) return;
var dfx = _hitInfo.collider.GetComponent<DamageFX>();
if (dfx != null) dfx.Hit(dfx.transform.InverseTransformPoint(_hitInfo.point), HitRadius, Dirt, Burn, Heat, Clip);
}
Colliders
The Damage FX works best with precise colliders that match its mesh geometry, e.g. mesh colliders.
If the distance between the closest mesh point and the collider hitpoint is bigger than the hit radius, then the effect won’t be visible at all. It’s important to provide a properly designed colliders to your Damage FX Objects.
The key is to get the impact from the hitpoint of the raycast against the collider to the original vertex position of the mesh. Sometimes this is not possible, for example, because you want to use the rigid body on the same object which will cause Unity to throw an error. Here’s a workaround to it. Remember, that you can use a convex collider or any number of simplified ones instead, either detach the mesh collider in the Awake(), and move it along each frame. You can also try using any third party tools/assets to help with generating a series of convex colliders covering the mesh.