Unity - trailing effects

Smearing is a cool special effect. The principle of smearing comes from human visual residue: observing fast-moving bright objects, you will see the moving track of the object . By adjusting the shutter time, the camera can also take photos with trailing effect, such as in the night scene of the city, the taillights of cars trail out red lines.

In older Unity versions, the trailing effect needs to be implemented with plugins. Now Unity has a built-in Trail Renderer (trailer renderer) component, which can easily create a trailing effect.

The use of the trailing renderer component is relatively simple, and the steps are as follows.

  1. Create a new sphere and add a Trail Renderer component to it
  2. You don't need to run the game, just change the position of the sphere in the scene, and you will see the trailing effect. But because no trailing material is specified, the display is magenta (magenta means that the material is wrong or missing).
  3. Find the Materials option in the Trail Renderer component, expand this option, and specify the number of materials and material resources. If the particle effect material has been imported, you can find multiple trail-specific materials by searching "Trail", just choose any one
  4. Select the material, and then drag the object, you will see the trailing effect

 The picture shows the effect of Sparks

 

 In order to achieve a better effect of trailing, it is necessary to carefully adjust parameters such as length (dwell time), width, material, and color gradient of the trailing trail. The following will explain the properties of the Trail Renderer component one by one

Attributes meaning illustrate
Cast Shadows Whether to cast shadows Trailing itself can also cast shadows like entities, but whether it can actually cast shadows is related to the material, and some materials themselves cannot cast shadows (generally do not need shadows)
Receive Shadows Do you accept shadows Trailing will show shadows cast by other objects (generally no shadows are needed)
Materials material Trailing material. one of the most important attributes
Time time The duration of the smear. Determines the overall length of the trail
Min Vertex Distance Minimum Vertex Distance The smear itself, like the model, has a finite number of fixed points. This option specifies the distance between two trailing vertices. The smaller the distance, the more vertices and the smoother the trailing, but the performance consumption is also greater
Auto Destruct auto destroy Destroy the trail when the game object is idle, which can save resources
Width width This property is a curve that can control the thickness change of the trail from the head to the tail
Color color Use a color gradient to control the change of trailing color
Corner Vertices corner vertex Increase this value to make the small corners of the trail more rounded
End Cap Vertices end cap apex Increase this value to make trailing endpoints more rounded
Alignment align The trail itself is a "billboard" that looks the same from every angle. You can choose to have it face the camera or the z-axis
Texture Mode texture mode Controls how the texture map is applied to the trail. Including Stretch (stretch), Wrap (repeat texture), etc. The texture method is related to the material, some materials are suitable for repetition, some things and stretching

There are also some properties related to lighting and advanced rendering, and their use is related to lighting and rendering, which will not be described in detail here.

After the trailing is set, it will appear automatically when the object is moving, no additional settings are required. The script is mainly to turn on or off the trail, and change the time and material of the trail, examples are as follows.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestTrail : MonoBehaviour
{
    TrailRenderer trail;
    public Material mat1;
    public Material mat2;
    void Start()
    {
        trail = GetComponent<TrailRenderer>();
        trail.sharedMaterial = mat1;
    }


    void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            if(trail.sharedMaterial==mat1)
            {
                trail.sharedMaterial = mat2;
                trail.startColor = Color.red;
                trail.endColor = Color.red;
            }
            else
            {
                trail.sharedMaterial = mat1;
                trail.startColor = Color.blue;
                trail.endColor = Color.blue;
            }

        }
    }
}

The effect is as follows

trailing transformation effect

Guess you like

Origin blog.csdn.net/m0_63024355/article/details/132483984