153 lines
4.3 KiB
C#
153 lines
4.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class AgingController : MonoBehaviour
|
|
{
|
|
[Header("Ìàòåð³àëè øê³ðè")]
|
|
public Material faceMaterial;
|
|
public Material neckMaterial;
|
|
public Material bodyMaterial;
|
|
|
|
[Header("Çà÷³ñêè")]
|
|
public GameObject youngHair;
|
|
public GameObject oldHaircut;
|
|
|
|
[Header("Áîðîäà")]
|
|
public GameObject beard;
|
|
|
|
[Header("Blend Shapes")]
|
|
public SkinnedMeshRenderer bodyMesh;
|
|
public int bellyBlendIndex = 4;
|
|
public int overweightBlendIndex = 1;
|
|
|
|
[Header("×àñ")]
|
|
public float agingDuration = 20f;
|
|
|
|
[HideInInspector] public float currentAge = 25f;
|
|
[HideInInspector] public bool isFinished = false;
|
|
|
|
private Material[] oldHairMats;
|
|
private Material[] youngHairMats;
|
|
private Material[] beardMats;
|
|
private float timer = 0f;
|
|
private Color currentHairColor = new Color(0.80f, 0.80f, 0.80f);
|
|
private Color _lastAppliedColor = Color.clear;
|
|
private int _currentPlanetIndex = 2;
|
|
|
|
private HairColorSettings hairColorSettings;
|
|
|
|
void Awake()
|
|
{
|
|
hairColorSettings = FindObjectOfType<HairColorSettings>();
|
|
youngHairMats = GetMaterials(youngHair);
|
|
oldHairMats = GetMaterials(oldHaircut);
|
|
beardMats = GetMaterials(beard);
|
|
RestartAging();
|
|
}
|
|
|
|
void Update()
|
|
{
|
|
if (isFinished) return;
|
|
|
|
if (hairColorSettings != null)
|
|
{
|
|
Color newColor = hairColorSettings.GetHairColor(_currentPlanetIndex);
|
|
if (newColor != _lastAppliedColor)
|
|
{
|
|
_lastAppliedColor = newColor;
|
|
currentHairColor = newColor;
|
|
ApplyColorToMats(oldHairMats);
|
|
ApplyColorToMats(beardMats);
|
|
}
|
|
}
|
|
|
|
timer += Time.deltaTime;
|
|
float t = Mathf.Clamp01(timer / agingDuration);
|
|
currentAge = Mathf.Lerp(25f, 70f, t);
|
|
|
|
faceMaterial.SetFloat("_AgingProgress", t);
|
|
bodyMesh.SetBlendShapeWeight(bellyBlendIndex, t * 100f);
|
|
bodyMesh.SetBlendShapeWeight(overweightBlendIndex, t * 10f);
|
|
|
|
SetAlpha(youngHairMats, 1f - t);
|
|
|
|
if (t >= 0.4f)
|
|
{
|
|
float hairT = Mathf.Clamp01((t - 0.4f) / 0.6f);
|
|
SetAlphaWithColor(oldHairMats, hairT);
|
|
SetAlphaWithColor(beardMats, hairT);
|
|
}
|
|
|
|
if (t >= 1f) isFinished = true;
|
|
}
|
|
|
|
public void RestartAging()
|
|
{
|
|
faceMaterial.SetFloat("_AgingProgress", 0f);
|
|
SetAlpha(oldHairMats, 0f);
|
|
SetAlpha(beardMats, 0f);
|
|
SetAlpha(youngHairMats, 1f);
|
|
bodyMesh.SetBlendShapeWeight(bellyBlendIndex, 0f);
|
|
bodyMesh.SetBlendShapeWeight(overweightBlendIndex, 0f);
|
|
timer = 0f;
|
|
currentAge = 25f;
|
|
isFinished = false;
|
|
_lastAppliedColor = Color.clear;
|
|
}
|
|
|
|
public void ApplyHairColor(int planetIndex)
|
|
{
|
|
if (hairColorSettings == null) return;
|
|
_currentPlanetIndex = planetIndex;
|
|
currentHairColor = hairColorSettings.GetHairColor(planetIndex);
|
|
_lastAppliedColor = currentHairColor;
|
|
ApplyColorToMats(oldHairMats);
|
|
ApplyColorToMats(beardMats);
|
|
}
|
|
|
|
void ApplyColorToMats(Material[] mats)
|
|
{
|
|
foreach (var m in mats)
|
|
{
|
|
Color c = m.GetColor("_BaseColor");
|
|
c.r = currentHairColor.r;
|
|
c.g = currentHairColor.g;
|
|
c.b = currentHairColor.b;
|
|
m.SetColor("_BaseColor", c);
|
|
}
|
|
}
|
|
|
|
Material[] GetMaterials(GameObject obj)
|
|
{
|
|
var renderers = obj.GetComponentsInChildren<Renderer>();
|
|
var list = new List<Material>();
|
|
foreach (var r in renderers)
|
|
foreach (var m in r.materials)
|
|
list.Add(m);
|
|
return list.ToArray();
|
|
}
|
|
|
|
void SetAlpha(Material[] mats, float alpha)
|
|
{
|
|
foreach (var m in mats)
|
|
{
|
|
Color c = m.GetColor("_BaseColor");
|
|
c.a = alpha;
|
|
m.SetColor("_BaseColor", c);
|
|
}
|
|
}
|
|
|
|
void SetAlphaWithColor(Material[] mats, float alpha)
|
|
{
|
|
foreach (var m in mats)
|
|
{
|
|
Color c = m.GetColor("_BaseColor");
|
|
c.r = currentHairColor.r;
|
|
c.g = currentHairColor.g;
|
|
c.b = currentHairColor.b;
|
|
c.a = alpha;
|
|
m.SetColor("_BaseColor", c);
|
|
}
|
|
}
|
|
} |