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);
        }
예제 #2
0
        List <TreeSegment> BuildSegments(TreeData data, float radius, Vector3 normal, Vector3 binormal)
        {
            var segments = new List <TreeSegment>();

            var curve = ScriptableObject.CreateInstance <CatmullRomCurve>();

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

            curve.Points[0] = from;
            curve.Points[1] = Vector3.Lerp(from, to, 0.25f) + bend;
            curve.Points[2] = Vector3.Lerp(from, to, 0.75f) + bend;
            curve.Points[3] = to;

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

            // var frames = curve.ComputeFrenetFrames(data.HeightSegments, false);
            for (int i = 0, n = frames.Count; i < n; i++)
            {
                var u        = 1f * i / (n - 1);
                var position = curve.GetPointAt(u);
                var segment  = new TreeSegment(frames[i], position);
                segments.Add(segment);
            }
            return(segments);
        }