User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

Decal Render Order

When working with mesh decals render order can become important as it comes to user controlled decorations as seen in popular racing games. 

The code example below shows a possible implementation of a simple component that controls the render order using material queues.

Code

using ch.sycoforge.Decal;
using UnityEngine;

[ExecuteInEditMode]
[RequireComponent(typeof(EasyDecal))]
public class RenderOrder : MonoBehaviour
{
    //------------------------
    // Exposed Fields
    //------------------------

    [Range(0, 100)]
    public int ZIndex;

    //------------------------
    // Fields
    //------------------------
    private EasyDecal decal;
    private float lastZIndex = int.MinValue;

    private int defaultQueue;

    //------------------------
    // unity Methods
    //------------------------
    private void Start()
    {
        decal = GetComponent<EasyDecal>();

        defaultQueue = decal.DecalMaterial.renderQueue;
    }

    private void Update()
    {
        // Did the value change?
        if (lastZIndex != ZIndex)
        {
            decal.DecalMaterial.renderQueue = defaultQueue + ZIndex;

            lastZIndex = ZIndex;
        }
    }
}

The component above can just be added to the same game object as the EasyDecal script.