private void CallLightning(Vector3?start, Vector3?end) { System.Random r = (RandomOverride ?? random); int count = CountRange.Random(r); for (int i = 0; i < count; i++) { LightningBoltParameters p = CreateParameters(); if (CountProbabilityModifier >= 0.9999f || i == 0 || (float)p.Random.NextDouble() <= CountProbabilityModifier) { p.CustomTransform = (CustomTransformHandler == null ? (System.Action <LightningCustomTransformStateInfo>)null : CustomTransform); CreateLightningBolt(p); if (start != null) { p.Start = start.Value; } if (end != null) { p.End = end.Value; } } else { LightningBoltParameters.ReturnParametersToCache(p); } } CreateLightningBoltsNow(); }
public void GenerateLightningBolt(LightningBolt bolt, LightningBoltParameters p) { Vector3 start = p.ApplyVariance(p.Start, p.StartVariance); Vector3 end = p.ApplyVariance(p.End, p.EndVariance); OnGenerateLightningBolt(bolt, start, end, p); }
private void CallLightning() { if (SpaceBarLabel != null) { SpaceBarLabel.CrossFadeColor(new Color(0.0f, 0.0f, 0.0f, 0.0f), 1.0f, true, true); SpaceBarLabel = null; } UnityEngine.Profiling.Profiler.BeginSample("CreateLightningBolt"); System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); lastStart = StartImage.transform.position + (Camera.main.transform.forward * DistanceSlider.value); lastEnd = EndImage.transform.position + (Camera.main.transform.forward * DistanceSlider.value); lastStart = Camera.main.ScreenToWorldPoint(lastStart); lastEnd = Camera.main.ScreenToWorldPoint(lastEnd); int count = (int)BoltCountSlider.value; float duration = DurationSlider.value; float delay = 0.0f; float chaosFactor = ChaosSlider.value; float trunkWidth = TrunkWidthSlider.value; float forkedness = ForkednessSlider.value; if (!int.TryParse(SeedInputField.text, out lastSeed)) { lastSeed = UnityEngine.Random.Range(int.MinValue, int.MaxValue); } System.Random r = new System.Random(lastSeed); float singleDuration = Mathf.Max(1.0f / 30.0f, (duration / (float)count)); float fadePercent = FadePercentSlider.value; float growthMultiplier = GrowthMultiplierSlider.value; while (count-- > 0) { LightningBoltParameters parameters = new LightningBoltParameters { Start = lastStart, End = lastEnd, Generations = (int)GenerationsSlider.value, LifeTime = (count == 1 ? singleDuration : (singleDuration * (((float)r.NextDouble() * 0.4f) + 0.8f))), Delay = delay, ChaosFactor = chaosFactor, TrunkWidth = trunkWidth, GlowIntensity = GlowIntensitySlider.value, GlowWidthMultiplier = GlowWidthSlider.value, Forkedness = forkedness, Random = r, FadePercent = fadePercent, GrowthMultiplier = growthMultiplier }; LightningBoltScript.CreateLightningBolt(parameters); delay += (singleDuration * (((float)r.NextDouble() * 0.8f) + 0.4f)); } timer.Stop(); UnityEngine.Profiling.Profiler.EndSample(); UpdateStatusLabel(timer.Elapsed); }
protected override void PopulateParameters(LightningBoltParameters p) { base.PopulateParameters(p); p.RandomOverride = RandomOverride; float duration = DurationRange.Random(p.Random); float trunkWidth = TrunkWidthRange.Random(p.Random); p.Generations = Generations; p.LifeTime = duration; p.ChaosFactor = ChaosFactor; p.ChaosFactorForks = ChaosFactorForks; p.TrunkWidth = trunkWidth; p.Intensity = Intensity; p.GlowIntensity = GlowIntensity; p.GlowWidthMultiplier = GlowWidthMultiplier; p.Forkedness = Forkedness; p.ForkLengthMultiplier = ForkLengthMultiplier; p.ForkLengthVariance = ForkLengthVariance; p.FadePercent = FadePercent; p.FadeInMultiplier = FadeInMultiplier; p.FadeOutMultiplier = FadeOutMultiplier; p.FadeFullyLitMultiplier = FadeFullyLitMultiplier; p.GrowthMultiplier = GrowthMultiplier; p.EndWidthMultiplier = EndWidthMultiplier; p.ForkEndWidthMultiplier = ForkEndWidthMultiplier; p.DelayRange = DelayRange; p.LightParameters = LightParameters; }
public override void CreateLightningBolt(LightningBoltParameters parameters) { if (meshHelper == null) { return; } Generations = parameters.Generations = Mathf.Clamp(Generations, 1, LightningSplineScript.MaxSplineGenerations); LightningBoltPathParameters pathParameters = parameters as LightningBoltPathParameters; sourcePoints.Clear(); PopulateSourcePoints(sourcePoints); if (sourcePoints.Count > 1) { if (Spline && sourcePoints.Count > 3) { pathParameters.Points = new List <Vector3>(sourcePoints.Count * Generations); LightningSplineScript.PopulateSpline(pathParameters.Points, sourcePoints, Generations, DistancePerSegmentHint, Camera); pathParameters.SmoothingFactor = (pathParameters.Points.Count - 1) / sourcePoints.Count; } else { pathParameters.Points = new List <Vector3>(sourcePoints); pathParameters.SmoothingFactor = 1; } base.CreateLightningBolt(pathParameters); } }
/// <summary> /// Create lightning bolt path parameters /// </summary> /// <returns>Lightning bolt path parameters</returns> protected override LightningBoltParameters OnCreateParameters() { LightningBoltParameters p = base.OnCreateParameters(); p.Generator = LightningGeneratorPath.GeneratorInstance; return(p); }
protected LightningBoltParameters CreateParameters() { LightningBoltParameters p = OnCreateParameters(); p.quality = QualitySetting; return(p); }
/// <summary> /// Return parameters to cache. NOT thread safe. /// </summary> /// <param name="p">Parameters</param> public static void ReturnParametersToCache(LightningBoltParameters p) { if (!cache.Contains(p)) { // reset variables that are state-machine dependant p.Reset(); cache.Add(p); } }
public void CreateFork(LightningBoltParameters p, int generation, int totalGenerations, Vector3 start, Vector3 midPoint) { if (ShouldCreateFork(p, generation, totalGenerations)) { Vector3 branchVector = (midPoint - start) * p.ForkMultiplier(); Vector3 splitEnd = midPoint + branchVector; GenerateLightningBoltStandard(midPoint, splitEnd, generation, totalGenerations, 0.0f, p); } }
public override void CreateLightningBolt(LightningBoltParameters parameters) { parameters.Start = (Source == null ? parameters.Start : Source.transform.position); parameters.End = (Destination == null ? parameters.End : Destination.transform.position); parameters.StartVariance = StartVariance; parameters.EndVariance = EndVariance; base.CreateLightningBolt(parameters); }
public void CreateFork(LightningBolt bolt, LightningBoltParameters p, int generation, int totalGenerations, Vector3 start, Vector3 midPoint) { if (ShouldCreateFork(p, generation, totalGenerations)) { Vector3 branchVector = (midPoint - start) * p.ForkMultiplier(); Vector3 splitEnd = midPoint + branchVector; GenerateLightningBoltStandard(bolt, midPoint, splitEnd, generation, totalGenerations, 0.0f, p); } }
public override void CreateLightningBolt(LightningBoltParameters parameters) { Vector3 start = UnityEngine.Random.insideUnitSphere * InnerRadius; Vector3 end = UnityEngine.Random.onUnitSphere * Radius; parameters.Start = start; parameters.End = end; base.CreateLightningBolt(parameters); }
public void GenerateLightningBolt(LightningBoltParameters p, LightningBolt bolt) { CurrentBolt = bolt; Vector3 start = p.ApplyVariance(p.Start, p.StartVariance); Vector3 end = p.ApplyVariance(p.End, p.EndVariance); OnGenerateLightningBolt(start, end, p); CurrentBolt = null; }
/// <summary> /// Create a lightning bolt /// </summary> /// <param name="p">Lightning bolt creation parameters</param> public virtual void CreateLightningBolt(LightningBoltParameters p) { if (p != null) { UpdateTexture(); oneParameterArray[0] = p; LightningBolt bolt = GetOrCreateLightningBolt(); LightningBoltDependencies dependencies = CreateLightningBoltDependencies(oneParameterArray); bolt.SetupLightningBolt(dependencies); } }
public override void CreateLightningBolt(LightningBoltParameters parameters) { if (Source == null || Destination == null) { return; } parameters.Start = Source.transform.position; parameters.End = Destination.transform.position; base.CreateLightningBolt(parameters); }
public override void CreateLightningBolt(LightningBoltParameters parameters) { Vector2 circle1 = UnityEngine.Random.insideUnitCircle * InnerRadius; Vector3 start = transform.rotation * new Vector3(circle1.x, circle1.y, 0.0f); Vector2 circle2 = UnityEngine.Random.insideUnitCircle * OuterRadius; Vector3 end = (transform.rotation * new Vector3(circle2.x, circle2.y, 0.0f)) + (transform.forward * Length); parameters.Start = start; parameters.End = end; base.CreateLightningBolt(parameters); }
public override void CreateLightningBolt(LightningBoltParameters parameters) { Vector3? currentPoint = null; List <GameObject> lightningPath = (LightningPath == null ? null : LightningPath.List); if (lightningPath == null || lightningPath.Count < 2) { return; } else if (nextIndex >= lightningPath.Count) { if (!Repeat) { return; } else if (lightningPath[lightningPath.Count - 1] == lightningPath[0]) { nextIndex = 1; } else { nextIndex = 0; lastPoint = null; } } try { if (lastPoint == null) { lastPoint = lightningPath[nextIndex++].transform.position; } currentPoint = lightningPath[nextIndex].transform.position; if (lastPoint != null && currentPoint != null) { parameters.Start = lastPoint.Value; parameters.End = currentPoint.Value; base.CreateLightningBolt(parameters); if ((nextInterval -= Speed) <= 0.0f) { float speedValue = UnityEngine.Random.Range(SpeedIntervalRange.Minimum, SpeedIntervalRange.Maximum); nextInterval = speedValue + nextInterval; lastPoint = currentPoint; nextIndex++; } } } catch (System.NullReferenceException) { // null reference exception can happen here, in which case we bail as the list is broken until the null object gets taken out } }
public override void CreateLightningBolt(LightningBoltParameters parameters) { // get two random points in the bounds parameters.Start = RandomPointInBounds(); parameters.End = RandomPointInBounds(); if (Light != null) { Light.enabled = true; } base.CreateLightningBolt(parameters); }
public override void CreateLightningBolt(LightningBoltParameters parameters) { if (LightningPath == null) { return; } sourcePoints.Clear(); try { foreach (GameObject obj in LightningPath.List) { if (obj != null) { sourcePoints.Add(obj.transform.position); } } } catch (System.NullReferenceException) { return; } if (sourcePoints.Count < PathGenerator.MinPointsForSpline) { Debug.LogError("To create spline lightning, you need a lightning path with at least " + PathGenerator.MinPointsForSpline + " points."); } else { Generations = parameters.Generations = Mathf.Clamp(Generations, 1, MaxSplineGenerations); parameters.Points.Clear(); if (previousGenerations != Generations || previousDistancePerSegment != DistancePerSegmentHint || SourceChanged()) { previousGenerations = Generations; previousDistancePerSegment = DistancePerSegmentHint; PopulateSpline(parameters.Points, sourcePoints, Generations, DistancePerSegmentHint, Camera); prevSourcePoints.Clear(); prevSourcePoints.AddRange(sourcePoints); savedSplinePoints.Clear(); savedSplinePoints.AddRange(parameters.Points); } else { parameters.Points.AddRange(savedSplinePoints); } parameters.SmoothingFactor = (parameters.Points.Count - 1) / sourcePoints.Count; base.CreateLightningBolt(parameters); } }
public override void CreateLightningBolt(LightningBoltParameters parameters) { Vector3? vector = null; List <GameObject> currentPathObjects = GetCurrentPathObjects(); if (currentPathObjects.Count < 2) { return; } if (nextIndex >= currentPathObjects.Count) { if (!Repeat) { return; } if (currentPathObjects[currentPathObjects.Count - 1] == currentPathObjects[0]) { nextIndex = 1; } else { nextIndex = 0; lastPoint = null; } } try { if (!lastPoint.HasValue) { lastPoint = currentPathObjects[nextIndex++].transform.position; } vector = currentPathObjects[nextIndex].transform.position; if (lastPoint.HasValue && vector.HasValue) { parameters.Start = lastPoint.Value; parameters.End = vector.Value; base.CreateLightningBolt(parameters); if ((nextInterval -= Speed) <= 0f) { float num = UnityEngine.Random.Range(SpeedIntervalRange.Minimum, SpeedIntervalRange.Maximum); nextInterval = num + nextInterval; lastPoint = vector; nextIndex++; } } } catch (NullReferenceException) { } }
private static IEnumerator NotifyBolt(LightningBoltDependencies dependencies, LightningBoltParameters p, Transform transform, Vector3 start, Vector3 end) { float delay = p.delaySeconds; float lifeTime = p.LifeTime; yield return(new WaitForSecondsLightning(delay)); if (dependencies.LightningBoltStarted != null) { dependencies.LightningBoltStarted(p, start, end); } LightningCustomTransformStateInfo state = (p.CustomTransform == null ? null : LightningCustomTransformStateInfo.GetOrCreateStateInfo()); if (state != null) { state.Parameters = p; state.BoltStartPosition = start; state.BoltEndPosition = end; state.State = LightningCustomTransformState.Started; state.Transform = transform; p.CustomTransform(state); state.State = LightningCustomTransformState.Executing; } if (p.CustomTransform == null) { yield return(new WaitForSecondsLightning(lifeTime)); } else { while (lifeTime > 0.0f) { p.CustomTransform(state); lifeTime -= LightningBoltScript.DeltaTime; yield return(null); } } if (p.CustomTransform != null) { state.State = LightningCustomTransformState.Ended; p.CustomTransform(state); LightningCustomTransformStateInfo.ReturnStateInfoToCache(state); } if (dependencies.LightningBoltEnded != null) { dependencies.LightningBoltEnded(p, start, end); } LightningBoltParameters.ReturnParametersToCache(p); }
public override void CreateLightningBolt(LightningBoltParameters parameters) { if (LightningPath == null || LightningPath.List == null) { return; } sourcePoints.Clear(); try { foreach (GameObject obj in LightningPath.List) { if (obj != null) { sourcePoints.Add(obj.transform.position); } } } catch (System.NullReferenceException) { return; } if (sourcePoints.Count < PathGenerator.MinPointsForSpline) { Debug.LogError("To create spline lightning, you need a lightning path with at least " + PathGenerator.MinPointsForSpline + " points."); } Generations = parameters.Generations = Mathf.Clamp(Generations, 1, MaxSplineGenerations); LightningBoltPathParameters sp = parameters as LightningBoltPathParameters; if (previousGenerations != Generations || previousDistancePerSegment != DistancePerSegmentHint || SourceChanged()) { previousGenerations = Generations; previousDistancePerSegment = DistancePerSegmentHint; sp.Points = new List<Vector3>(sourcePoints.Count * Generations); PopulateSpline(sp.Points, sourcePoints, Generations, DistancePerSegmentHint, Camera); prevSourcePoints.Clear(); prevSourcePoints.AddRange(sourcePoints); savedSplinePoints = sp.Points; } else { sp.Points = savedSplinePoints; } sp.SmoothingFactor = (sp.Points.Count - 1) / sourcePoints.Count; base.CreateLightningBolt(parameters); }
private void CallLightning() { // Important, make sure this script is assigned properly, or you will get null ref exceptions. DigitalRuby.ThunderAndLightning.LightningBoltScript script = gameObject.GetComponent <DigitalRuby.ThunderAndLightning.LightningBoltScript>(); int count = 5; float duration = 0.3939312f; float delay = 0.0f; int seed = -210785313; System.Random r = new System.Random(seed); Vector3 start = new Vector3(0f, 241.0437f, 500f); Vector3 end = new Vector3(0f, -239.0398f, 500f); int generations = 5; float chaosFactor = 0.3054504f; float trunkWidth = 10.39684f; float glowIntensity = 0.201021f; float glowWidthMultiplier = 4f; float forkedness = 0.5f; float singleDuration = Mathf.Max(1.0f / 30.0f, (duration / (float)count)); float fadePercent = 0.15f; float growthMultiplier = 0f; System.Collections.Generic.List <DigitalRuby.ThunderAndLightning.LightningBoltParameters> paramList = new System.Collections.Generic.List <DigitalRuby.ThunderAndLightning.LightningBoltParameters>(); while (count-- > 0) { DigitalRuby.ThunderAndLightning.LightningBoltParameters parameters = new DigitalRuby.ThunderAndLightning.LightningBoltParameters { Start = start, End = end, Generations = generations, LifeTime = (count == 1 ? singleDuration : (singleDuration * (((float)r.NextDouble() * 0.4f) + 0.8f))), Delay = delay, ChaosFactor = chaosFactor, TrunkWidth = trunkWidth, GlowIntensity = glowIntensity, GlowWidthMultiplier = glowWidthMultiplier, Forkedness = forkedness, Random = r, FadePercent = fadePercent, // set to 0 to disable fade in / out GrowthMultiplier = growthMultiplier }; paramList.Add(parameters); delay += (singleDuration * (((float)r.NextDouble() * 0.8f) + 0.4f)); } script.CreateLightningBolts(paramList); }
private void CallLightning(Vector3?start, Vector3?end) { int count = random.Next(CountRange.Minimum, CountRange.Maximum + 1); while (count-- > 0) { float duration = ((float)random.NextDouble() * (DurationRange.Maximum - DurationRange.Minimum)) + DurationRange.Maximum; float trunkWidth = ((float)random.NextDouble() * (TrunkWidthRange.Maximum - TrunkWidthRange.Minimum)) + TrunkWidthRange.Maximum; LightningBoltParameters p = CreateParameters(); p.Generations = Generations; p.LifeTime = duration; p.ChaosFactor = ChaosFactor; p.TrunkWidth = trunkWidth; p.GlowIntensity = GlowIntensity; p.GlowWidthMultiplier = GlowWidthMultiplier; p.Forkedness = Forkedness; p.ForkLengthMultiplier = ForkLengthMultiplier; p.ForkLengthVariance = ForkLengthVariance; p.FadePercent = FadePercent; p.GrowthMultiplier = GrowthMultiplier; p.EndWidthMultiplier = EndWidthMultiplier; p.ForkEndWidthMultiplier = ForkEndWidthMultiplier; p.Random = random; p.Delay = UnityEngine.Random.Range(DelayRange.Minimum, DelayRange.Maximum); p.LightParameters = LightParameters; CreateLightningBolt(p); if (start != null) { p.Start = start.Value; } if (end != null) { p.End = end.Value; } } int tmp = LightningBolt.MaximumLightsPerBatch; LightningBolt.MaximumLightsPerBatch = MaximumLightsPerBatch; CreateLightningBolts(batchParameters); LightningBolt.MaximumLightsPerBatch = tmp; batchParameters.Clear(); }
/// <summary> /// Create a lightning bolt /// </summary> /// <param name="p">Lightning bolt creation parameters</param> public virtual void CreateLightningBolt(LightningBoltParameters p) { #if UNITY_EDITOR if (Camera == null) { UnityEngine.Debug.LogError("Camera not assigned to lightning script. Either set the camera or tag your camera as main camera."); } #endif if (p != null && Camera != null) { UpdateTexture(); oneParameterArray[0] = p; LightningBolt bolt = GetOrCreateLightningBolt(); LightningBoltDependencies dependencies = CreateLightningBoltDependencies(oneParameterArray); bolt.SetupLightningBolt(dependencies); } }
/// <summary> /// Get or create lightning bolt parameters. If cache has parameters, one is taken, otherwise a new object is created. NOT thread safe. /// </summary> /// <returns>Lightning bolt parameters</returns> public static LightningBoltParameters GetOrCreateParameters() { LightningBoltParameters p; if (cache.Count == 0) { unchecked { p = new LightningBoltParameters(); } } else { int i = cache.Count - 1; p = cache[i]; cache.RemoveAt(i); } return(p); }
public void GenerateSpark() { // Important, make sure this script is assigned properly, or you will get null ref exceptions. DigitalRuby.ThunderAndLightning.LightningBoltScript script = gameObject.GetComponent <DigitalRuby.ThunderAndLightning.LightningBoltScript>(); int count = 1; float duration = 0.25f; float delay = 0.0f; System.Random r = new System.Random(); Vector3 start = this.sparkingStartPoint.transform.position; Vector3 end = this.sparkingEndPoint.transform.position; int generations = 6; float chaosFactor = 0.2f; float trunkWidth = 0.05f; float glowIntensity = 0.1793653f; float glowWidthMultiplier = 4f; float forkedness = 0.5f; float singleDuration = Mathf.Max(1.0f / 30.0f, (duration / (float)count)); float fadePercent = 0.15f; float growthMultiplier = 0f; while (count-- > 0) { DigitalRuby.ThunderAndLightning.LightningBoltParameters parameters = new DigitalRuby.ThunderAndLightning.LightningBoltParameters { Start = start, End = end, Generations = generations, LifeTime = (count == 1 ? singleDuration : (singleDuration * (((float)r.NextDouble() * 0.4f) + 0.8f))), Delay = delay, ChaosFactor = chaosFactor, TrunkWidth = trunkWidth, GlowIntensity = glowIntensity, GlowWidthMultiplier = glowWidthMultiplier, Forkedness = forkedness, Random = r, FadePercent = fadePercent, // set to 0 to disable fade in / out GrowthMultiplier = growthMultiplier }; script.CreateLightningBolt(parameters); delay += (singleDuration * (((float)r.NextDouble() * 0.8f) + 0.4f)); } }
private void CallLightning(Vector3?start, Vector3?end) { int count = random.Next(CountRange.Minimum, CountRange.Maximum + 1); for (int i = 0; i < count; i++) { LightningBoltParameters p = CreateParameters(); if (CountProbabilityModifier == 1.0f || i == 0 || (float)p.Random.NextDouble() <= CountProbabilityModifier) { CreateLightningBolt(p); if (start != null) { p.Start = start.Value; } if (end != null) { p.End = end.Value; } } } CreateLightningBoltsNow(); }
protected void PopulateParameters(LightningBoltParameters p) { float duration = ((float)random.NextDouble() * (DurationRange.Maximum - DurationRange.Minimum)) + DurationRange.Maximum; float trunkWidth = ((float)random.NextDouble() * (TrunkWidthRange.Maximum - TrunkWidthRange.Minimum)) + TrunkWidthRange.Maximum; p.Generations = Generations; p.LifeTime = duration; p.ChaosFactor = ChaosFactor; p.TrunkWidth = trunkWidth; p.GlowIntensity = GlowIntensity; p.GlowWidthMultiplier = GlowWidthMultiplier; p.Forkedness = Forkedness; p.ForkLengthMultiplier = ForkLengthMultiplier; p.ForkLengthVariance = ForkLengthVariance; p.FadePercent = FadePercent; p.GrowthMultiplier = GrowthMultiplier; p.EndWidthMultiplier = EndWidthMultiplier; p.ForkEndWidthMultiplier = ForkEndWidthMultiplier; p.Random = random; p.DelayRange = DelayRange; p.LightParameters = LightParameters; }
public override void CreateLightningBolt(LightningBoltParameters parameters) { minimumLengthSquared = MinimumLength * MinimumLength; for (int i = 0; i < 16; i++) { // get two random points in the bounds parameters.Start = RandomPointInBounds(); parameters.End = RandomPointInBounds(); if ((parameters.End - parameters.Start).sqrMagnitude >= minimumLengthSquared) { break; } } if (Light != null) { Light.enabled = true; } base.CreateLightningBolt(parameters); }
private void ProcessParameters(LightningBoltParameters p, RangeOfFloats delay, LightningBoltDependencies depends) { Vector3 start, end; MinimumDelay = Mathf.Min(delay.Minimum, MinimumDelay); p.delaySeconds = delay.Random(p.Random); // apply LOD if specified if (depends.LevelOfDetailDistance > Mathf.Epsilon) { float d; if (p.Points.Count > 1) { d = Vector3.Distance(depends.CameraPos, p.Points[0]); d = Mathf.Min(Vector3.Distance(depends.CameraPos, p.Points[p.Points.Count - 1])); } else { d = Vector3.Distance(depends.CameraPos, p.Start); d = Mathf.Min(Vector3.Distance(depends.CameraPos, p.End)); } int modifier = Mathf.Min(8, (int)(d / depends.LevelOfDetailDistance)); p.Generations = Mathf.Max(1, p.Generations - modifier); p.GenerationWhereForksStopSubtractor = Mathf.Clamp(p.GenerationWhereForksStopSubtractor - modifier, 0, 8); } p.generationWhereForksStop = p.Generations - p.GenerationWhereForksStopSubtractor; lifeTime = Mathf.Max(p.LifeTime + p.delaySeconds, lifeTime); maxLifeTime = Mathf.Max(lifeTime, maxLifeTime); p.forkednessCalculated = (int)Mathf.Ceil(p.Forkedness * (float)p.Generations); if (p.Generations > 0) { p.Generator = p.Generator ?? LightningGenerator.GeneratorInstance; p.Generator.GenerateLightningBolt(this, p, out start, out end); p.Start = start; p.End = end; } }
/// <summary> /// Triggers lightning that follows a set of points, rather than the standard lightning bolt that goes between two points. /// </summary> /// <param name="points">Points to follow</param> /// <param name="spline">Whether to spline the lightning through the points or not</param> public void Trigger(List <Vector3> points, bool spline) { if (points.Count < 2) { return; } Generations = Mathf.Clamp(Generations, 1, MaxSplineGenerations); LightningBoltParameters parameters = CreateParameters(); parameters.Points.Clear(); if (spline && points.Count > 3) { LightningSplineScript.PopulateSpline(parameters.Points, points, Generations, DistancePerSegmentHint, Camera); parameters.SmoothingFactor = (parameters.Points.Count - 1) / points.Count; } else { parameters.Points.AddRange(points); parameters.SmoothingFactor = 1; } base.CreateLightningBolt(parameters); CreateLightningBoltsNow(); }
public void CallLightning() { int count = random.Next(CountRange.Minimum, CountRange.Maximum + 1); while (count-- > 0) { float duration = ((float)random.NextDouble() * (DurationRange.Maximum - DurationRange.Minimum)) + DurationRange.Maximum; float trunkWidth = ((float)random.NextDouble() * (TrunkWidthRange.Maximum - TrunkWidthRange.Minimum)) + TrunkWidthRange.Maximum; LightningBoltParameters parameters = new LightningBoltParameters { Generations = Generations, LifeTime = duration, ChaosFactor = ChaosFactor, TrunkWidth = trunkWidth, GlowIntensity = GlowIntensity, GlowWidthMultiplier = GlowWidthMultiplier, Forkedness = Forkedness, FadePercent = FadePercent, GrowthMultiplier = GrowthMultiplier, EndWidthMultiplier = EndWidthMultiplier, Random = random, Delay = UnityEngine.Random.Range(DelayRange.Minimum, DelayRange.Maximum), LightParameters = LightParameters }; CreateLightningBolt(parameters); } int tmp = LightningBolt.MaximumLightsPerBatch; LightningBolt.MaximumLightsPerBatch = MaximumLightsPerBatch; CreateLightningBolts(batchParameters); LightningBolt.MaximumLightsPerBatch = tmp; batchParameters.Clear(); }
public override void CreateLightningBolt(LightningBoltParameters parameters) { Vector3? currentPoint = null; List<GameObject> lightningPath = (LightningPath == null ? null : LightningPath.List); if (lightningPath == null || lightningPath.Count < 2) { return; } else if (nextIndex >= lightningPath.Count) { if (!Repeat) { return; } else if (lightningPath[lightningPath.Count - 1] == lightningPath[0]) { nextIndex = 1; } else { nextIndex = 0; lastPoint = null; } } try { if (lastPoint == null) { lastPoint = lightningPath[nextIndex++].transform.position; } currentPoint = lightningPath[nextIndex].transform.position; if (lastPoint != null && currentPoint != null) { parameters.Start = lastPoint.Value; parameters.End = currentPoint.Value; base.CreateLightningBolt(parameters); if ((nextInterval -= Speed) <= 0.0f) { float speedValue = UnityEngine.Random.Range(SpeedIntervalRange.Minimum, SpeedIntervalRange.Maximum); nextInterval = speedValue + nextInterval; lastPoint = currentPoint; nextIndex++; } } } catch (System.NullReferenceException) { // null reference exception can happen here, in which case we bail as the list is broken until the null object gets taken out } }
public override void GenerateLightningBolt(LightningBoltParameters p) { GenerateLightningBoltPath(p as LightningBoltPathParameters); }
public virtual void GenerateLightningBolt(LightningBoltParameters p) { GenerateLightningBoltStandard(p.Start, p.End, p.Generations, p.Generations, 0.0f, p); }
public bool ShouldCreateFork(LightningBoltParameters p, int generation, int totalGenerations) { return (generation > p.generationWhereForksStop && generation >= totalGenerations - p.forkednessCalculated && (float)p.Random.NextDouble() < p.Forkedness); }
public void GenerateLightningBoltStandard(Vector3 start, Vector3 end, int generation, int totalGenerations, float offsetAmount, LightningBoltParameters p) { if (generation < 1) { return; } LightningBoltSegmentGroup group = CreateGroup(); group.Segments.Add(new LightningBoltSegment { Start = start, End = end }); // every generation, get the percentage we have gone down and square it, this makes lines thinner float widthMultiplier = (float)generation / (float)totalGenerations; widthMultiplier *= widthMultiplier; Vector3 randomVector; group.LineWidth = p.TrunkWidth * widthMultiplier; group.Generation = generation; group.Color = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)(255.0f * widthMultiplier)); group.EndWidthMultiplier = p.EndWidthMultiplier * p.ForkEndWidthMultiplier; if (offsetAmount <= 0.0f) { offsetAmount = (end - start).magnitude * p.ChaosFactor; } while (generation-- > 0) { int previousStartIndex = group.StartIndex; group.StartIndex = group.Segments.Count; for (int i = previousStartIndex; i < group.StartIndex; i++) { start = group.Segments[i].Start; end = group.Segments[i].End; // determine a new direction for the split Vector3 midPoint = (start + end) * 0.5f; // adjust the mid point to be the new location RandomVector(ref start, ref end, offsetAmount, p.Random, out randomVector); midPoint += randomVector; // add two new segments group.Segments.Add(new LightningBoltSegment { Start = start, End = midPoint }); group.Segments.Add(new LightningBoltSegment { Start = midPoint, End = end }); CreateFork(p, generation, totalGenerations, start, midPoint); } // halve the distance the lightning can deviate for each generation down offsetAmount *= 0.5f; } }
private void Strike(bool intense, int generations, float duration, float intensity, float chaosFactor, float glowIntensity, float glowWidth, float forkedness, int count, Camera camera, Camera visibleInCamera) { if (count < 1) { return; } // find a point around the camera that is not too close System.Random r = new System.Random(); const float minDistance = 500.0f; float minValue = (intense ? -1000.0f : -5000.0f); float maxValue = (intense ? 1000 : 5000.0f); float closestValue = (intense ? 500.0f : 2500.0f); float x = (UnityEngine.Random.Range(0, 2) == 0 ? UnityEngine.Random.Range(minValue, -closestValue) : UnityEngine.Random.Range(closestValue, maxValue)); float y = 620.0f; float z = (UnityEngine.Random.Range(0, 2) == 0 ? UnityEngine.Random.Range(minValue, -closestValue) : UnityEngine.Random.Range(closestValue, maxValue)); float delay = 0.0f; Vector3 start = script.Camera.transform.position; start.x += x; start.y = y; start.z += z; if (visibleInCamera != null) { // try and make sure the strike is visible in the camera Quaternion q = visibleInCamera.transform.rotation; visibleInCamera.transform.rotation = Quaternion.Euler(0.0f, q.eulerAngles.y, 0.0f); float screenX = UnityEngine.Random.Range(visibleInCamera.pixelWidth * 0.1f, visibleInCamera.pixelWidth * 0.9f); float ScreenZ = UnityEngine.Random.Range(visibleInCamera.nearClipPlane + closestValue + closestValue, maxValue); Vector3 point = visibleInCamera.ScreenToWorldPoint(new Vector3(screenX, 0.0f, ScreenZ)); start = point; start.y = y; visibleInCamera.transform.rotation = q; } while (count-- > 0) { // for each strike, calculate the end position and perform the strike Vector3 end = start; x = UnityEngine.Random.Range(-100, 100.0f); // 1 in 4 chance not to strike the ground y = (UnityEngine.Random.Range(0, 4) == 0 ? UnityEngine.Random.Range(-1, 600.0f) : -1.0f); z += UnityEngine.Random.Range(-100.0f, 100.0f); end.x += x; end.y = y; end.z += z; // make sure the bolt points away from the camera end.x += (closestValue * camera.transform.forward.x); end.z += (closestValue * camera.transform.forward.z); while ((start - end).magnitude < minDistance) { end.x += (closestValue * camera.transform.forward.x); end.z += (closestValue * camera.transform.forward.z); } if (script.LightningBoltScript != null) { if (UnityEngine.Random.value < script.CloudLightningChance) { // cloud only lightning generations = 0; } LightningBoltParameters parameters = new LightningBoltParameters { Start = start, End = end, Generations = generations, LifeTime = duration, Delay = delay, ChaosFactor = chaosFactor, TrunkWidth = 8.0f, EndWidthMultiplier = 0.25f, GlowIntensity = glowIntensity, GlowWidthMultiplier = glowWidth, Forkedness = forkedness, Random = r, LightParameters = new LightningLightParameters { LightIntensity = intensity, LightRange = 5000.0f, LightShadowPercent = 1.0f, } }; script.LightningBoltScript.CreateLightningBolt(parameters); delay += ((duration / count) * UnityEngine.Random.Range(0.2f, 0.5f)); } } }
protected void PopulateParameters(LightningBoltParameters p) { float duration = ((float)random.NextDouble() * (DurationRange.Maximum - DurationRange.Minimum)) + DurationRange.Maximum; float trunkWidth = ((float)random.NextDouble() * (TrunkWidthRange.Maximum - TrunkWidthRange.Minimum)) + TrunkWidthRange.Maximum; p.Generations = Generations; p.LifeTime = duration; p.ChaosFactor = ChaosFactor; p.TrunkWidth = trunkWidth; p.GlowIntensity = GlowIntensity; p.GlowWidthMultiplier = GlowWidthMultiplier; p.Forkedness = Forkedness; p.ForkLengthMultiplier = ForkLengthMultiplier; p.ForkLengthVariance = ForkLengthVariance; p.FadePercent = FadePercent; p.GrowthMultiplier = GrowthMultiplier; p.EndWidthMultiplier = EndWidthMultiplier; p.ForkEndWidthMultiplier = ForkEndWidthMultiplier; p.Random = random; p.Delay = UnityEngine.Random.Range(DelayRange.Minimum, DelayRange.Maximum); p.LightParameters = LightParameters; }
private void CallLightning() { if (SpaceBarLabel != null) { SpaceBarLabel.CrossFadeColor(new Color(0.0f, 0.0f, 0.0f, 0.0f), 1.0f, true, true); SpaceBarLabel = null; } UnityEngine.Profiler.BeginSample("CreateLightningBolt"); System.Diagnostics.Stopwatch timer = System.Diagnostics.Stopwatch.StartNew(); lastStart = StartImage.transform.position + (Camera.main.transform.forward * DistanceSlider.value); lastEnd = EndImage.transform.position + (Camera.main.transform.forward * DistanceSlider.value); lastStart = Camera.main.ScreenToWorldPoint(lastStart); lastEnd = Camera.main.ScreenToWorldPoint(lastEnd); int count = (int)BoltCountSlider.value; float duration = DurationSlider.value; float delay = 0.0f; float chaosFactor = ChaosSlider.value; float trunkWidth = TrunkWidthSlider.value; float forkedness = ForkednessSlider.value; if (!int.TryParse(SeedInputField.text, out lastSeed)) { lastSeed = UnityEngine.Random.Range(int.MinValue, int.MaxValue); } System.Random r = new System.Random(lastSeed); float singleDuration = Mathf.Max(1.0f / 30.0f, (duration / (float)count)); float fadePercent = FadePercentSlider.value; float growthMultiplier = GrowthMultiplierSlider.value; while (count-- > 0) { LightningBoltParameters parameters = new LightningBoltParameters { Start = lastStart, End = lastEnd, Generations = (int)GenerationsSlider.value, LifeTime = (count == 1 ? singleDuration : (singleDuration * (((float)r.NextDouble() * 0.4f) + 0.8f))), Delay = delay, ChaosFactor = chaosFactor, TrunkWidth = trunkWidth, GlowIntensity = GlowIntensitySlider.value, GlowWidthMultiplier = GlowWidthSlider.value, Forkedness = forkedness, Random = r, FadePercent = fadePercent, GrowthMultiplier = growthMultiplier }; LightningBoltScript.CreateLightningBolt(parameters); delay += (singleDuration * (((float)r.NextDouble() * 0.8f) + 0.4f)); } timer.Stop(); UnityEngine.Profiler.EndSample(); UpdateStatusLabel(timer.Elapsed); }
public override void CreateLightningBolt(LightningBoltParameters parameters) { if (meshHelper == null) { return; } Generations = parameters.Generations = Mathf.Clamp(Generations, 1, LightningSplineScript.MaxSplineGenerations); LightningBoltPathParameters pathParameters = parameters as LightningBoltPathParameters; sourcePoints.Clear(); PopulateSourcePoints(sourcePoints); if (sourcePoints.Count > 1) { if (Spline && sourcePoints.Count > 3) { pathParameters.Points = new List<Vector3>(sourcePoints.Count * Generations); LightningSplineScript.PopulateSpline(pathParameters.Points, sourcePoints, Generations, DistancePerSegmentHint, Camera); pathParameters.SmoothingFactor = (pathParameters.Points.Count - 1) / sourcePoints.Count; } else { pathParameters.Points = new List<Vector3>(sourcePoints); pathParameters.SmoothingFactor = 1; } base.CreateLightningBolt(pathParameters); } }
/// <summary> /// Create a lightning bolt /// </summary> /// <param name="p">Lightning bolt creation parameters</param> public virtual void CreateLightningBolt(LightningBoltParameters p) { if (p != null) { UpdateTexture(); oneParameterArray[0] = p; LightningBolt bolt = LightningBolt.GetOrCreateLightningBolt(); bolt.Initialize(Camera, UseWorldSpace, lightningBoltRenderer, gameObject, this, LightningOriginParticleSystem, LightningDestinationParticleSystem, oneParameterArray); bolts.Add(bolt); } }
private void CreateLightOnly(LightningBoltParameters p, LightningLightParameters lp, Vector3 pos) { lightOnlyParameters = lp; lightOnlyObject = CreateLight(lp); hasLight = true; }
private void RenderLightningBolt(LightningBoltQualitySetting quality, int generations, Vector3 start, Vector3 end, int groupIndex, ParticleSystem originParticleSystem, ParticleSystem destParticleSystem, LightningBoltParameters parameters, LightningLightParameters lp, int maxLights) { if (parameters.Generations < 1) { CreateLightOnly(parameters, lp, start); return; } else if (segmentGroups.Count == 0 || groupIndex >= segmentGroups.Count) { return; } float delayBase = parameters.LifeTime / (float)segmentGroups.Count; float minDelayValue = delayBase * 0.9f; float maxDelayValue = delayBase * 1.1f; float delayDiff = maxDelayValue - minDelayValue; parameters.FadePercent = Mathf.Clamp(parameters.FadePercent, 0.0f, 0.5f); if (originParticleSystem != null) { // we have a strike, create a particle where the lightning is coming from Script.StartCoroutine(GenerateParticle(originParticleSystem, start, parameters.Delay)); } if (destParticleSystem != null) { Script.StartCoroutine(GenerateParticle(destParticleSystem, end, parameters.Delay * 1.1f)); } if (HasGlow) { lightningBoltRenderer.GlowIntensityMultiplier = parameters.GlowIntensity; lightningBoltRenderer.GlowWidthMultiplier = parameters.GlowWidthMultiplier; } float currentDelayAmount = 0.0f; for (int i = groupIndex; i < segmentGroups.Count; i++) { LightningBoltSegmentGroup group = segmentGroups[i]; group.Delay = currentDelayAmount + parameters.Delay; group.LifeTime = parameters.LifeTime - currentDelayAmount; group.PeakStart = group.LifeTime * parameters.FadePercent; group.PeakEnd = group.LifeTime - group.PeakStart; group.LightParameters = lp; lightningBoltRenderer.AddGroup(this, group, parameters.GrowthMultiplier); currentDelayAmount += ((float)parameters.Random.NextDouble() * minDelayValue) + delayDiff; // create lights only on the main trunk if (lp != null && group.Generation == generations) { CreateLightsForGroup(group, lp, quality, maxLights, groupIndex); } } }
private void ProcessParameters(LightningBoltParameters p, ParticleSystem sourceParticleSystem, ParticleSystem destinationParticleSystem, int maxLights) { p.Generator.CurrentBolt = this; MinimumDelay = Mathf.Min(p.Delay, MinimumDelay); p.generationWhereForksStop = p.Generations - p.GenerationWhereForksStopSubtractor; p.GlowIntensity = Mathf.Clamp(p.GlowIntensity, 0.0f, 1.0f); p.Random = (p.Random ?? new System.Random(System.Environment.TickCount)); lifeTime = Mathf.Max(p.LifeTime + p.Delay, lifeTime); LightningLightParameters lp = p.LightParameters; if (lp != null) { if ((hasLight |= lp.HasLight)) { lp.LightPercent = Mathf.Clamp(lp.LightPercent, Mathf.Epsilon, 1.0f); lp.LightShadowPercent = Mathf.Clamp(lp.LightShadowPercent, 0.0f, 1.0f); } else { lp = null; } } p.forkednessCalculated = (int)Mathf.Ceil(p.Forkedness * (float)p.Generations); int groupIndex = segmentGroups.Count; if (p.Generations > 0) { p.Generator.GenerateLightningBolt(p); } RenderLightningBolt(p.quality, p.Generations, p.Start, p.End, groupIndex, sourceParticleSystem, destinationParticleSystem, p, lp, maxLights); }
public override void CreateLightningBolt(LightningBoltParameters p) { batchParameters.Add(p); // do not call the base method, we batch up and use CreateLightningBolts }