using UnityEngine;

// Share a material while choosing projection space and density for each model.
[ExecuteAlways]
public class TriplanarSettings : MonoBehaviour
{
    public bool worldSpace;

    void OnEnable() => Apply();
    void OnValidate() => Apply();

    public void Apply()
    {
        var properties = new MaterialPropertyBlock();
        properties.SetFloat("_WorldSpace", worldSpace ? 1f : 0f);

        foreach (var renderer in GetComponentsInChildren<Renderer>())
            renderer.SetPropertyBlock(properties);
    }

    void OnDisable()
    {
        foreach (var renderer in GetComponentsInChildren<Renderer>())
            renderer.SetPropertyBlock(null);
    }
}
