Advanced research on painting a level with decals in Unity 3D.

After another day of research, I am quite satisfied with the result:

Paint decal

Finally, I used:

  • Decals
  • Batching (thanks to built-in Unity optimization)
  • Recycling

What I didn’t achieve:

  • Permanent paint, even with a giant pool the time will come where some paintings will be removed
  • Fluids


I used the script I published before and added some new feature.

Recycling pool

Decals are stored in an array. If we reach the limit, we delete the oldest decal to recreate a new one.

If the pool is large enough, you’ll have some time before the player see some splash are missing.

Maybe this can be optimized at splash time: if the raycast already return a splash, maybe we don’t need to create a new one.

Dynamic Batching

I discovered that Unity will automatically optimize draw calls when it can. See the documentation about Draw Call Batching.

However, it is not that simple in our case: we want multicolor paintings. But if you assign a new color to a shared material, either all decals using it will change color or you will create a new material and lose the shared connection.

So I made a simple list-based system where we create a material only when we want to use a decal/color we never used before and we register it in the list.

Unity will batch all calls of the same decals. If you open the Stats window, you can see this optimization.

Batching draw calls

You can also mark your decals as “Static” if you don’t plan to move them.

If you have a lot of colors or decals, you may have to find another solution to preload your materials.

The script

Here is the script. Provided “as it’, “no warranty”, “works for me”.

See on Gist

// Author: Damien Mayance (
// 2013 - Pixelnest Studio (
// This script paints decals on surfaces it hits from a point.
// See for further explanations.
// Usage:
// - Attach it to an unique object that won't be deleted of your choice. There should be only one instance of this script.
// - Then fill the "PaintDecalPrefabs" list with your decals prefabs.
// - Finally, just call DecalPainter.Instance.Paint from another script to paint!
// It included a debug mode where you can click in the scene to paint. Everything between UNITY_EDITOR is just debug stuff that can be removed.
using UnityEngine;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

/// <summary>
/// Generate paint decals
/// </summary>
public class DecalPainter : MonoBehaviour
    public static DecalPainter Instance;

    /// <summary>
    /// Paint decals to reproduce on textures
    /// </summary>
    public List<Transform> PaintDecalPrefabs;

    /// <summary>
    /// Parent to affect for scene management
    /// </summary>
    public Transform DecalsParent;

    /// <summary>
    /// Minimal scale of a prefab
    /// </summary>
    public float MinScale = 0.75f;

    /// <summary>
    /// Maximal scale of a prefab
    /// </summary>
    public float MaxScale = 3f;

    /// <summary>
    /// Range of the splash raycast
    /// </summary>
    public float SplashRange = 1.5f;

    /// <summary>
    /// Number of decals
    /// </summary>
    public int PoolSize = 300;

    private Transform[] paintDecals;
    private int currentPoolIndex;
    private List<Material> materials;

    private bool mDrawDebug;
    private Vector3 mHitPoint;
    private List<Ray> mRaysDebug = new List<Ray>();

    void Awake()
        materials = new List<Material>();

        if (Instance != null) Debug.LogError("More than one Painter has been instanciated in this scene!");
        Instance = this;

        if (PaintDecalPrefabs.Count == 0) Debug.LogError("Missing Paint decals prefabs!");

        paintDecals = new Transform[PoolSize];
        currentPoolIndex = 0;

    void Update()
        // Check for a click
        if (Input.GetMouseButtonDown(0))
            // Raycast
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, Mathf.Infinity))
                // Paint!
                Color color =;
                Paint(hit.point + hit.normal * 1f, color, 10); // Step back a little

    public void Paint(Vector3 location, Color color, int drops, float scaleBonus = 1f)

        mHitPoint = location;
        mDrawDebug = true;

        RaycastHit hit;

        // Generate multiple decals in once
        int n = 0;
        while(n < drops)
            var dir = transform.TransformDirection(Random.onUnitSphere * SplashRange);

            // Avoid raycast backward as we're in a 2D space
            if (dir.z < 0) dir.z = Random.Range(0f, 1f);

            // Raycast around the position to splash everwhere we can
            if (Physics.Raycast(location, dir, out hit, SplashRange))
                PaintDecal(hit, color, scaleBonus);

                mRaysDebug.Add(new Ray(location, dir));


    private void PaintDecal(RaycastHit hit, Color color, float scaleBonus)
        // Create a splash if we found a surface
        int randomIndex = Random.Range(0, PaintDecalPrefabs.Count);
        Transform paintDecal = PaintDecalPrefabs[randomIndex];

        var paintSplatter = GameObject.Instantiate(paintDecal,
            // Rotation from the original sprite to the normal
            // Prefab are currently oriented to z+ so we use the opposite
                                                   Quaternion.FromToRotation(Vector3.back, hit.normal)
                                                   ) as Transform;

        // Find an existing material to enable batching
        var sharedMat = materials.Where(m =>
                                            && m.color.Equals(color)

        // New material
        if (sharedMat == null)
            Material mat = paintSplatter.renderer.material;
            mat.color = color;

        // Old one
            paintSplatter.renderer.sharedMaterial = sharedMat;

        // Random scale
        var scaler = Random.Range(MinScale, MaxScale) * scaleBonus;

        paintSplatter.localScale = new Vector3(
            paintSplatter.localScale.x * scaler,
            paintSplatter.localScale.y * scaler,

        // Random rotation effect
        var rater = Random.Range(0, 359);
        paintSplatter.transform.RotateAround(hit.point, hit.normal, rater);

        paintSplatter.parent = DecalsParent;

        // Pool
        if (paintDecals[currentPoolIndex] != null)
            paintDecals[currentPoolIndex] = null;

        paintDecals[currentPoolIndex] = paintSplatter;

        if (currentPoolIndex >= PoolSize) currentPoolIndex = 0;

    void OnDrawGizmos()
        if (mDrawDebug)
            Gizmos.DrawSphere(mHitPoint, 0.2f);
            foreach (var r in mRaysDebug)

Feel free to contact me for any question.

Interesting resources

If you are interested on the subject: