-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProceduralAsset.cs
More file actions
146 lines (117 loc) · 3.47 KB
/
ProceduralAsset.cs
File metadata and controls
146 lines (117 loc) · 3.47 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using UnityEngine;
using System.Collections.Generic;
#if UNITY_EDITOR
using Forge.EditorUtils;
#endif
namespace Forge {
[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
[System.Serializable]
public class ProceduralAsset : MonoBehaviour {
public delegate void OnDrawGizmosHandler(GameObject go);
public event OnDrawGizmosHandler OnDrawGizmos;
public Template Template = null;
[HideInInspector] [SerializeField] private List<ParameterValue> ParameterValues = new List<ParameterValue>();
[HideInInspector] public Mesh Mesh = null;
#if UNITY_EDITOR
private MeshDisplay _meshDisplay = null;
public MeshDisplay MeshDisplay {
get {
if (_meshDisplay == null) {
_meshDisplay = (MeshDisplay) ScriptableObject.CreateInstance(typeof(MeshDisplay));
_meshDisplay.hideFlags = HideFlags.HideAndDontSave;
}
return _meshDisplay;
}
}
[HideInInspector] [System.NonSerialized] public Mesh GhostMesh = null;
#endif
private System.Diagnostics.Stopwatch Stopwatch = null;
[HideInInspector] public double LastBuildTime = 0;
[HideInInspector] public Geometry Geometry = Geometry.Empty;
[HideInInspector] [System.NonSerialized] public bool IsBuilt = false;
// Retrieves the value of a parameter by its GUID
public object GetParameter(string GUID) {
for (int i = 0; i < ParameterValues.Count; i++) {
if (ParameterValues[i].GUID == GUID) {
return ParameterValues[i].Value;
}
}
return null;
}
// Sets a parameter by its label
public void SetParameter(string label, object value) {
foreach (Parameter par in Template.Parameters) {
if (par.Label == label) {
SetParameterByGUID(par.GUID, value);
return;
}
}
Debug.LogWarningFormat("{0} does not have a parameter with the label \"{1}\"", gameObject.name, label);
}
// Sets a parameter by its GUID
public void SetParameterByGUID(string GUID, object value) {
ParameterValue newParam = new ParameterValue(GUID, value);
for (int i = 0; i < ParameterValues.Count; i++) {
if (ParameterValues[i].GUID == GUID) {
ParameterValues[i] = newParam;
return;
}
}
ParameterValues.Add(newParam);
}
public virtual Geometry Build() {
return Geometry.Empty;
}
public void Generate() {
if (Stopwatch == null) {
Stopwatch = new System.Diagnostics.Stopwatch();
}
#if UNITY_EDITOR
// Reset the ghost mesh
GhostMesh = null;
#endif
// Statistics
#if UNITY_EDITOR
Stopwatch.Start();
#endif
// Build it
if (Template != null) {
Geometry = Template.Build(this);
} else {
Geometry = Build();
}
// Statistics
#if UNITY_EDITOR
LastBuildTime = Stopwatch.Elapsed.TotalMilliseconds;
Stopwatch.Reset();
#endif
// Mesh
Mesh = (Mesh == null) ? new Mesh() : Mesh;
Mesh.Clear();
Mesh.vertices = Geometry.Vertices;
Mesh.normals = Geometry.Normals;
Mesh.tangents = Geometry.Tangents;
Mesh.triangles = Geometry.Triangles;
Mesh.uv = Geometry.UV;
GetComponent<MeshFilter>().sharedMesh = Mesh;
IsBuilt = true;
}
#if UNITY_EDITOR
public void Ghost(Geometry geo) {
GhostMesh = new Mesh();
GhostMesh.Clear();
GhostMesh.vertices = geo.Vertices;
GhostMesh.normals = geo.Normals;
GhostMesh.tangents = geo.Tangents;
GhostMesh.triangles = geo.Triangles;
GhostMesh.uv = geo.UV;
}
void OnDrawGizmosSelected() {
if (!IsBuilt) Generate();
MeshDisplay.DrawHandles(this, transform);
if (OnDrawGizmos != null) OnDrawGizmos(gameObject);
}
#endif
}
}