-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAvatarStateSystem.cs
More file actions
84 lines (69 loc) · 2.36 KB
/
AvatarStateSystem.cs
File metadata and controls
84 lines (69 loc) · 2.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using UnityEngine;
using static UnityEngine.Mathf;
namespace CustomAvatars
{
public class AvatarStateSystem
{
public event Action<string, bool> OnBoolChanged;
public event Action<string, float> OnFloatChanged;
private readonly Dictionary<string, bool> bools = new();
private readonly Dictionary<string, float> floats = new();
public void SetBool(string name, bool value)
{
if (bools.TryGetValue(name, out var old) && old == value)
return;
bools[name] = value;
OnBoolChanged?.Invoke(name, value);
}
public bool GetBool(string name)
=> bools.TryGetValue(name, out var v) && v;
public void SetFloat(string name, float value)
{
if (floats.TryGetValue(name, out var old) && Approximately(old, value))
return;
floats[name] = value;
OnFloatChanged?.Invoke(name, value);
}
public float GetFloat(string name)
=> floats.TryGetValue(name, out var v) ? v : 0f;
}
[Serializable]
public class ShaderState
{
public Renderer renderer;
public string property = "_EmissionStrength";
// Applies to all materials with the valid property if -1
public int materialIndex = -1;
public void ApplyFloat(float value)
{
if (!renderer) return;
var mats = renderer.materials;
if (materialIndex >= 0 && materialIndex < mats.Length)
{
if (mats[materialIndex] != null && mats[materialIndex].HasFloat(property))
mats[materialIndex].SetFloat(property, value);
}
else
{
foreach (var m in mats)
{
if (m.HasProperty(property))
m.SetFloat(property, value);
}
}
}
public void ApplyBool(bool value) => ApplyFloat(value ? 1f : 0f);
}
[Serializable]
public class BlendshapeState
{
public SkinnedMeshRenderer smr;
public int index = -1;
public void Apply(float weight)
{
if (smr && index >= 0)
smr.SetBlendShapeWeight(index, Clamp(weight, 0f, 100f));
}
public void Apply(bool on) => Apply(on ? 100f : 0f);
}
}