45 lines
1.3 KiB
C#
45 lines
1.3 KiB
C#
using UnityEngine;
|
|
|
|
[RequireComponent(typeof(Renderer))]
|
|
public class CarPartVisuals : MonoBehaviour
|
|
{
|
|
private Renderer partRenderer;
|
|
private MaterialPropertyBlock propertyBlock;
|
|
|
|
private static readonly int RustAmountProp = Shader.PropertyToID("_RustAmount");
|
|
private static readonly int BaseColorProp = Shader.PropertyToID("_BaseColor");
|
|
|
|
[Range(0f, 1f)] [SerializeField] private float currentRust = 0f;
|
|
[SerializeField] private Color currentColor = Color.white;
|
|
|
|
public float CurrentRust => currentRust;
|
|
public Color CurrentColor => currentColor;
|
|
|
|
private void Awake()
|
|
{
|
|
partRenderer = GetComponent<Renderer>();
|
|
propertyBlock = new MaterialPropertyBlock();
|
|
}
|
|
|
|
public void SetRust(float amount)
|
|
{
|
|
currentRust = Mathf.Clamp01(amount);
|
|
ApplyProperties();
|
|
}
|
|
|
|
public void SetPaintColor(Color newColor)
|
|
{
|
|
currentColor = newColor;
|
|
ApplyProperties();
|
|
}
|
|
|
|
private void ApplyProperties()
|
|
{
|
|
if (partRenderer == null) return;
|
|
|
|
partRenderer.GetPropertyBlock(propertyBlock);
|
|
propertyBlock.SetFloat(RustAmountProp, currentRust);
|
|
propertyBlock.SetColor(BaseColorProp, currentColor);
|
|
partRenderer.SetPropertyBlock(propertyBlock);
|
|
}
|
|
} |