forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeAOImporter.cs
More file actions
217 lines (189 loc) · 8.26 KB
/
TreeAOImporter.cs
File metadata and controls
217 lines (189 loc) · 8.26 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEditor;
namespace UnityEditor
{
internal class TreeAO
{
const int kWorkLayer = 29;
static bool kDebug = false;
const float occlusion = .5f;
static Vector3[] directions;
private static int PermuteCuboid(Vector3[] dirs, int offset, float x, float y, float z)
{
dirs[offset + 0] = new Vector3(+x, +y, +z);
dirs[offset + 1] = new Vector3(+x, +y, -z);
dirs[offset + 2] = new Vector3(+x, -y, +z);
dirs[offset + 3] = new Vector3(+x, -y, -z);
dirs[offset + 4] = new Vector3(-x, +y, +z);
dirs[offset + 5] = new Vector3(-x, +y, -z);
dirs[offset + 6] = new Vector3(-x, -y, +z);
dirs[offset + 7] = new Vector3(-x, -y, -z);
return offset + 8;
}
public static void InitializeDirections()
{
// Vertices of truncated icosahedron (60 vertices).
// http://en.wikipedia.org/wiki/Truncated_icosahedron
//
// Using random set of vertices creates slightly biased
// set of points that are not centered around zero.
float f = (1.0f + Mathf.Sqrt(5.0f)) / 2.0f;
directions = new Vector3[60];
directions[0] = new Vector3(0, +1, +3 * f);
directions[1] = new Vector3(0, +1, -3 * f);
directions[2] = new Vector3(0, -1, +3 * f);
directions[3] = new Vector3(0, -1, -3 * f);
directions[4] = new Vector3(+1, +3 * f, 0);
directions[5] = new Vector3(+1, -3 * f, 0);
directions[6] = new Vector3(-1, +3 * f, 0);
directions[7] = new Vector3(-1, -3 * f, 0);
directions[8] = new Vector3(+3 * f, 0, +1);
directions[9] = new Vector3(+3 * f, 0, -1);
directions[10] = new Vector3(-3 * f, 0, +1);
directions[11] = new Vector3(-3 * f, 0, -1);
int offset = 12;
offset = PermuteCuboid(directions, offset, 2, 1 + 2 * f, f);
offset = PermuteCuboid(directions, offset, 1 + 2 * f, f, 2);
offset = PermuteCuboid(directions, offset, f, 2, 1 + 2 * f);
offset = PermuteCuboid(directions, offset, 1, 2 + f, 2 * f);
offset = PermuteCuboid(directions, offset, 2 + f, 2 * f, 1);
offset = PermuteCuboid(directions, offset, 2 * f, 1, 2 + f);
for (int i = 0; i < directions.Length; i++)
directions[i] = directions[i].normalized;
}
public static void CalcSoftOcclusion(Mesh mesh)
{
// Create the helper object
GameObject go = new GameObject("Test");
go.layer = kWorkLayer; // Grab a system layer for this
MeshFilter mf = (MeshFilter)go.AddComponent<MeshFilter>();
mf.mesh = mesh;
go.AddComponent<MeshCollider>();
// Intialize
if (directions == null)
InitializeDirections();
// Calc weights for all directions
Vector4[] weights = new Vector4[directions.Length];
for (int i = 0; i < directions.Length; i++)
{
weights[i] = new Vector4(GetWeight(1, directions[i]), GetWeight(2, directions[i]), GetWeight(3, directions[i]), GetWeight(0, directions[i]));
}
Vector3[] verts = mesh.vertices;
Vector4[] sh = new Vector4[verts.Length];
float totalW = 0;
for (int i = 0; i < verts.Length; i += 1)
{
Vector4 result = Vector4.zero;
Vector3 v = go.transform.TransformPoint(verts[i]);
for (int j = 0; j < directions.Length; j++)
{
float occ = CountIntersections(v, go.transform.TransformDirection(directions[j]), 3);
occ = Mathf.Pow(occlusion, occ);
result += weights[j] * occ;
}
result /= directions.Length;
totalW += result.w;
sh[i] = result;
}
totalW /= verts.Length;
for (int i = 0; i < verts.Length; i++)
sh[i].w -= totalW;
mesh.tangents = sh;
Object.DestroyImmediate(go);
}
static int CountIntersections(Vector3 v, Vector3 dist, float length)
{
v += dist * .01f;
if (!kDebug)
{
return Physics.RaycastAll(v, dist, length, 1 << kWorkLayer).Length +
Physics.RaycastAll(v + dist * length, -dist, length, 1 << kWorkLayer).Length;
}
RaycastHit[] hits = Physics.RaycastAll(v, dist, length, 1 << kWorkLayer);
int hitLength = hits.Length;
float maxDist = 0;
if (hitLength > 0)
maxDist = hits[hits.Length - 1].distance;
hits = Physics.RaycastAll(v + dist * length, -dist, length, 1 << kWorkLayer);
if (hits.Length > 0)
{
float len = length - hits[0].distance;
if (len > maxDist)
{
maxDist = len;
}
}
return hitLength + hits.Length;
}
static float GetWeight(int coeff, Vector3 dir)
{
switch (coeff)
{
case 0: // Just the constant
return .5f; // Average of all dimensions.
case 1:
return .5f * dir.x;
case 2:
return .5f * dir.y;
case 3:
return .5f * dir.z;
}
Debug.Log("Only defined up to 3");
return 0;
}
}
internal class TreeAOImporter : AssetPostprocessor
{
void OnPostprocessModel(GameObject root)
{
// Check if path contains "AO Tree"
string lowerPath = assetPath.ToLower();
if (lowerPath.IndexOf("ambient-occlusion") != -1)
{
Component[] filters = root.GetComponentsInChildren(typeof(MeshFilter));
foreach (MeshFilter filter in filters)
{
if (filter.sharedMesh != null)
{
Mesh mesh = filter.sharedMesh;
// Calculate AO
TreeAO.CalcSoftOcclusion(mesh);
// Calculate vertex colors for tree waving
Bounds bounds = mesh.bounds;
Color[] colors = mesh.colors;
Vector3[] vertices = mesh.vertices;
Vector4[] tangents = mesh.tangents;
if (colors.Length == 0)
{
colors = new Color[mesh.vertexCount];
for (int i = 0; i < colors.Length; i++)
colors[i] = Color.white;
}
float maxAO = 0.0F;
for (int i = 0; i < tangents.Length; i++)
maxAO = Mathf.Max(tangents[i].w, maxAO);
float largest = 0.0F;
for (int i = 0; i < colors.Length; i++)
{
Vector2 offset = new Vector2(vertices[i].x, vertices[i].z);
float branch = offset.magnitude;
largest = Mathf.Max(branch, largest);
}
for (int i = 0; i < colors.Length; i++)
{
Vector2 offset = new Vector2(vertices[i].x, vertices[i].z);
float branch = offset.magnitude / largest;
float height = (vertices[i].y - bounds.min.y) / bounds.size.y;
// colors[i].a = tangents[i].w * maxAO + height;
colors[i].a = (height * branch) * 0.6F + height * 0.5F;
}
mesh.colors = colors;
}
}
}
}
}
} //namespace