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);
        }
Exemple #2
0
        void DrawCurve()
        {
            const float size = 0.025f;

            Gizmos.matrix = transform.localToWorldMatrix;
            var frames = curve.ComputeFrenetFrames(tubularSegments, closed);

            for (int i = 0, n = tubularSegments; i < n; i++)
            {
                var u0 = 1f * i / tubularSegments;
                var p0 = curve.GetPointAt(u0);

                // draw line
                if (i < n - 1)
                {
                    var u1 = 1f * (i + 1) / tubularSegments;
                    var p1 = curve.GetPointAt(u1);
                    Gizmos.color = Color.white;
                    Gizmos.DrawLine(p0, p1);
                }

                Gizmos.color = Color.green;
                Gizmos.DrawSphere(p0, size);

                var frame = frames[i];
                var N     = frame.Normal;
                var B     = frame.Binormal;

                Gizmos.color = Color.yellow;
                var radius = size * 4f;
                for (int j = 0; j <= radialSegments; j++)
                {
                    // 0.0 ~ 2π
                    float rad0 = 1f * j / radialSegments * PI2;
                    float rad1 = 1f * (j + 1) / radialSegments * PI2;

                    float cos0 = Mathf.Cos(rad0), sin0 = Mathf.Sin(rad0);
                    float cos1 = Mathf.Cos(rad1), sin1 = Mathf.Sin(rad1);

                    var normal0 = (cos0 * N + sin0 * B).normalized;
                    var normal1 = (cos1 * N + sin1 * B).normalized;
                    var v0      = (p0 + radius * normal0);
                    var v1      = (p0 + radius * normal1);
                    Gizmos.DrawLine(v0, v1);
                }
            }
        }