Exemple #1
0
        protected override Mesh Build()
        {
            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>();

            // 曲線からFrenet frameを取得
            var frames = curve.ComputeFrenetFrames(tubularSegments, closed);

            // Tubularの頂点データを生成
            for (int i = 0; i < tubularSegments; i++)
            {
                GenerateSegment(curve, frames, vertices, normals, tangents, i);
            }
            // 閉じた筒型を生成する場合は曲線の始点に最後の頂点を配置し、閉じない場合は曲線の終点に配置する
            GenerateSegment(curve, frames, vertices, normals, tangents, (!closed) ? tubularSegments : 0);

            // 曲線の始点から終点に向かってuv座標を設定していく
            for (int i = 0; i <= tubularSegments; i++)
            {
                for (int j = 0; j <= radialSegments; j++)
                {
                    float u = 1f * j / radialSegments;
                    float v = 1f * i / tubularSegments;
                    uvs.Add(new Vector2(u, v));
                }
            }

            // 側面を構築
            for (int j = 1; j <= tubularSegments; j++)
            {
                for (int i = 1; i <= radialSegments; i++)
                {
                    int a = (radialSegments + 1) * (j - 1) + (i - 1);
                    int b = (radialSegments + 1) * j + (i - 1);
                    int c = (radialSegments + 1) * j + i;
                    int d = (radialSegments + 1) * (j - 1) + i;

                    triangles.Add(a); triangles.Add(d); triangles.Add(b);
                    triangles.Add(b); triangles.Add(d); triangles.Add(c);
                }
            }

            var mesh = new Mesh();

            mesh.vertices  = vertices.ToArray();
            mesh.normals   = normals.ToArray();
            mesh.tangents  = tangents.ToArray();
            mesh.uv        = uvs.ToArray();
            mesh.triangles = triangles.ToArray();
            return(mesh);
        }
        List <TreeSegment> BuildSegments(TreeData data, float fromRadius, float toRadius, Vector3 normal, Vector3 binormal)
        {
            var segments = new List <TreeSegment>();

            var points = new List <Vector3>();

            var length = (to - from).magnitude;
            var bend   = length * (normal * data.GetRandomBendDegree() + binormal * data.GetRandomBendDegree());

            points.Add(from);
            points.Add(Vector3.Lerp(from, to, 0.25f) + bend);
            points.Add(Vector3.Lerp(from, to, 0.75f) + bend);
            points.Add(to);

            var curve = new CatmullRomCurve(points);

            var frames = curve.ComputeFrenetFrames(data.heightSegments, normal, binormal, false);

            for (int i = 0, n = frames.Count; i < n; i++)
            {
                var u      = 1f * i / (n - 1);
                var radius = Mathf.Lerp(fromRadius, toRadius, u);

                var position = curve.GetPointAt(u);
                var segment  = new TreeSegment(frames[i], position, radius);
                segments.Add(segment);
            }
            return(segments);
        }