示例#1
0
        public static Mesh Build(TreeData data, int generations, float length, float radius)
        {
            data.Setup();

            TreeBranch root = new TreeBranch(
                generations,
                length,
                radius,
                data
                );

            List <Vector3> vertices  = new List <Vector3>();
            List <Vector3> normals   = new List <Vector3>();
            List <Vector4> tangents  = new List <Vector4>();
            List <Vector2> uvs       = new List <Vector2>();
            List <int>     triangles = new List <int>();

            // 木の全長を取得
            // 枝の長さを全長で割ることで、uv座標の高さ(uv.y)が
            // 根元から枝先に至るまで[0.0 ~ 1.0]で変化するように設定する
            float maxLength = TraverseMaxLength(root);

            // 再帰的に全ての枝を辿り、一つ一つの枝に対応するMeshを生成する
            Traverse(root, (TreeBranch branch) => {
                int offset = vertices.Count;

                float vOffset = branch.Offset / maxLength;
                float vLength = branch.Length / maxLength;

                // 一本の枝から頂点データを生成する
                for (int i = 0, n = branch.Segments.Count; i < n; i++)
                {
                    float t = 1f * i / (n - 1);
                    float v = vOffset + vLength * t;

                    TreeSegment segment = branch.Segments[i];
                    Vector3 N           = segment.Frame.Normal;
                    Vector3 B           = segment.Frame.Binormal;
                    for (int j = 0; j <= data.radialSegments; j++)
                    {
                        // 0.0 ~ 2π
                        float u   = 1f * j / data.radialSegments;
                        float rad = u * PI2;

                        float cos      = Mathf.Cos(rad), sin = Mathf.Sin(rad);
                        Vector3 normal = (cos * N + sin * B).normalized;
                        vertices.Add(segment.Position + segment.Radius * normal);
                        normals.Add(normal);

                        Vector3 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);
                    }
                }
            });

            Mesh mesh = new Mesh();

            mesh.vertices  = vertices.ToArray();
            mesh.normals   = normals.ToArray();
            mesh.tangents  = tangents.ToArray();
            mesh.uv        = uvs.ToArray();
            mesh.triangles = triangles.ToArray();
            mesh.RecalculateBounds();

            return(mesh);
        }
示例#2
0
        protected TreeBranch(int generation, int generations, Vector3 from, Vector3 tangent, Vector3 normal, Vector3 binormal, float length, float radius, float offset, TreeData data)
        {
            this.generation = generation;

            this.fromRadius = radius;

            // 枝先である場合は先端の太さが0になる
            this.toRadius = (generation == 0) ? 0f : radius *data.radiusAttenuation;

            this.from = from;

            // 枝先ほど分岐する角度が大きくなる
            float scale = Mathf.Lerp(1f, data.growthAngleScale, 1f - 1f * generation / generations);

            // normal方向の回転
            Quaternion qn = Quaternion.AngleAxis(scale * data.GetRandomGrowthAngle(), normal);

            // binormal方向の回転
            Quaternion qb = Quaternion.AngleAxis(scale * data.GetRandomGrowthAngle(), binormal);

            // 枝先が向いているtangent方向にqn * qbの回転をかけつつ、枝先の位置を決める
            this.to = from + (qn * qb) * tangent * length;

            this.length = length;
            this.offset = offset;

            // モデル生成に必要な節を構築
            segments = BuildSegments(data, fromRadius, toRadius, normal, binormal);

            children = new List <TreeBranch>();
            if (generation > 0)
            {
                // 分岐する数を取得
                int count = data.GetRandomBranches();
                for (int i = 0; i < count; i++)
                {
                    float ratio; // [0.0 ~ 1.0]
                    if (count == 1)
                    {
                        // 分岐数が1である場合(0除算を回避)
                        ratio = 1f;
                    }
                    else
                    {
                        ratio = Mathf.Lerp(0.5f, 1f, (1f * i) / (count - 1));
                    }

                    // 分岐元の節を取得
                    int         index   = Mathf.FloorToInt(ratio * (segments.Count - 1));
                    TreeSegment segment = segments[index];

                    // 分岐元の節が持つベクトルをTreeBranchに渡すことで滑らかな分岐を得る
                    Vector3 nt = segment.Frame.Tangent;
                    Vector3 nn = segment.Frame.Normal;
                    Vector3 nb = segment.Frame.Binormal;

                    TreeBranch child = new TreeBranch(
                        this.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
                        );

                    children.Add(child);
                }
            }
        }