/// <summary> /// Populate a list of spline points from source points /// </summary> /// <param name="splinePoints">List to fill with spline points</param> /// <param name="sourcePoints">Source points</param> /// <param name="generations">Generations</param> /// <param name="distancePerSegmentHit">Distance per segment hint - if non-zero, attempts to maintain this distance between spline points.</param> /// <param name="camera">Optional camera</param> public static void PopulateSpline(List <Vector3> splinePoints, List <Vector3> sourcePoints, int generations, float distancePerSegmentHit, Camera camera) { splinePoints.Clear(); PathGenerator.Is2D = (camera != null && camera.orthographic); if (distancePerSegmentHit > 0.0f) { PathGenerator.CreateSplineWithSegmentDistance(splinePoints, sourcePoints, distancePerSegmentHit / generations, false); } else { PathGenerator.CreateSpline(splinePoints, sourcePoints, sourcePoints.Count * generations * generations, false); } }
public void GenerateLightningBoltPath(LightningBolt bolt, Vector3 start, Vector3 end, LightningBoltParameters p) { if (p.Points.Count < 2) { Debug.LogError("Lightning path should have at least two points"); return; } int generation = p.Generations; int totalGenerations = generation; float offsetAmount, d; int smoothingFactor = p.SmoothingFactor - 1; Vector3 distance, randomVector; LightningBoltSegmentGroup group = bolt.AddGroup(); group.LineWidth = p.TrunkWidth; group.Generation = generation--; group.EndWidthMultiplier = p.EndWidthMultiplier; group.Color = Color.white; p.Start = p.Points[0] + start; p.End = p.Points[p.Points.Count - 1] + end; end = p.Start; for (int i = 1; i < p.Points.Count; i++) { start = end; end = p.Points[i]; distance = (end - start); d = PathGenerator.SquareRoot(distance.sqrMagnitude); if (p.ChaosFactor > 0.0f) { if (bolt.CameraMode == CameraMode.Perspective) { end += (d * p.ChaosFactor * RandomDirection3D(p.Random)); } else if (bolt.CameraMode == CameraMode.OrthographicXY) { end += (d * p.ChaosFactor * RandomDirection2D(p.Random)); } else { end += (d * p.ChaosFactor * RandomDirection2DXZ(p.Random)); } distance = (end - start); } group.Segments.Add(new LightningBoltSegment { Start = start, End = end }); offsetAmount = d * p.ChaosFactor; RandomVector(bolt, ref start, ref end, offsetAmount, p.Random, out randomVector); if (ShouldCreateFork(p, generation, totalGenerations)) { Vector3 branchVector = distance * p.ForkMultiplier() * smoothingFactor * 0.5f; Vector3 forkEnd = end + branchVector + randomVector; GenerateLightningBoltStandard(bolt, start, forkEnd, generation, totalGenerations, 0.0f, p); } if (--smoothingFactor == 0) { smoothingFactor = p.SmoothingFactor - 1; } } }