private static float TraverseMaxLength(TreeBranch branch) { float max = 0f; branch.Children.ForEach(c => { max = Mathf.Max(max, TraverseMaxLength(c)); }); return(branch.Length + max); }
private static void Traverse(TreeBranch from, Action <TreeBranch> action) { if (from.Children.Count > 0) { from.Children.ForEach(child => { Traverse(child, action); }); } action(from); }
protected TreeBranch(List <TreeBranch> branches, int generation, int generations, Vector3 from, Vector3 tangent, Vector3 normal, Vector3 binormal, float length, float radius, float offset, TreeData data) { m_Generation = generation; m_FromRadius = radius; m_ToRadius = (generation == 0) ? 0f : radius * data.radiusAttenuation; m_From = from; var scale = Mathf.Lerp(1f, data.growthAngleScale, 1f - 1f * generation / generations); var rotation = Quaternion.AngleAxis(scale * data.GetRandomGrowthAngle(), normal) * Quaternion.AngleAxis(scale * data.GetRandomGrowthAngle(), binormal); m_To = from + rotation * tangent * length; m_Length = length; m_Offset = offset; m_Segments = BuildSegments(data, m_FromRadius, m_ToRadius, normal, binormal); branches.Add(this); m_Children = new List <TreeBranch>(); if (generation > 0) { int count = data.GetRandomBranches(); for (int i = 0; i < count; i++) { float ratio; if (count == 1) { // for zero division ratio = 1f; } else { ratio = Mathf.Lerp(0.5f, 1f, (1f * i) / (count - 1)); } var index = Mathf.FloorToInt(ratio * (m_Segments.Count - 1)); var segment = m_Segments[index]; Vector3 nt, nn, nb; if (ratio >= 1f) { // sequence branch nt = segment.Frame.Tangent; nn = segment.Frame.Normal; nb = segment.Frame.Binormal; } else { var rot = Quaternion.AngleAxis(i * 90f, tangent); nt = rot * tangent; nn = rot * normal; nb = rot * binormal; } var child = new TreeBranch( branches, this.m_Generation - 1, generations, segment.Position, nt, nn, nb, length * Mathf.Lerp(1f, data.lengthAttenuation, ratio), radius * Mathf.Lerp(1f, data.radiusAttenuation, ratio), offset + length, data ); m_Children.Add(child); } } }
public static Mesh Build(TreeData data, int generations, float length, float radius) { data.Setup(); var root = new TreeBranch( generations, length, radius, data ); var vertices = new List <Vector3>(); var normals = new List <Vector3>(); var tangents = new List <Vector4>(); var uvs = new List <Vector2>(); var triangles = new List <int>(); float maxLength = TraverseMaxLength(root); Traverse(root, (branch) => { var offset = vertices.Count; var vOffset = branch.Offset / maxLength; var vLength = branch.Length / maxLength; for (int i = 0, n = branch.Segments.Count; i < n; i++) { var t = 1f * i / (n - 1); var v = vOffset + vLength * t; var segment = branch.Segments[i]; var N = segment.Frame.Normal; var B = segment.Frame.Binormal; for (int j = 0; j <= data.radialSegments; j++) { // 0.0 ~ 2π var u = 1f * j / data.radialSegments; float rad = u * PI2; float cos = Mathf.Cos(rad), sin = Mathf.Sin(rad); var normal = (cos * N + sin * B).normalized; vertices.Add(segment.Position + segment.Radius * normal); normals.Add(normal); var tangent = segment.Frame.Tangent; tangents.Add(new Vector4(tangent.x, tangent.y, tangent.z, 0f)); uvs.Add(new Vector2(u, v)); } } for (int j = 1; j <= data.heightSegments; j++) { for (int i = 1; i <= data.radialSegments; i++) { int a = (data.radialSegments + 1) * (j - 1) + (i - 1); int b = (data.radialSegments + 1) * j + (i - 1); int c = (data.radialSegments + 1) * j + i; int d = (data.radialSegments + 1) * (j - 1) + i; a += offset; b += offset; c += offset; d += offset; triangles.Add(a); triangles.Add(d); triangles.Add(b); triangles.Add(b); triangles.Add(d); triangles.Add(c); } } }); var mesh = new Mesh { vertices = vertices.ToArray(), normals = normals.ToArray(), tangents = tangents.ToArray(), uv = uvs.ToArray(), triangles = triangles.ToArray() }; return(mesh); }