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;
        }
        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();
        }
Exemple #3
0
 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);
     }
 }
        /// <summary>
        /// Create new lightning bolt parameters and populate
        /// </summary>
        /// <returns>LightningBoltParameters</returns>
        protected LightningBoltParameters CreateParameters()
        {
            LightningBoltParameters p = OnCreateParameters();

            p.quality = QualitySetting;
            PopulateParameters(p);

            return(p);
        }
Exemple #5
0
        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);
        }
 /// <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 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 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);
            }
        }
        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;
        }
Exemple #10
0
        private void CallLightning(Vector3?start, Vector3?end)
        {
            int count = CountRange.Random(random);

            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();
        }
Exemple #11
0
 public bool ShouldCreateFork(LightningBoltParameters p, int generation, int totalGenerations)
 {
     return(generation > p.generationWhereForksStop && generation >= totalGenerations - p.forkednessCalculated && (float)p.Random.NextDouble() < p.Forkedness);
 }
Exemple #12
0
 protected virtual void OnGenerateLightningBolt(LightningBolt bolt, Vector3 start, Vector3 end, LightningBoltParameters p)
 {
     GenerateLightningBoltStandard(bolt, start, end, p.Generations, p.Generations, 0.0f, p);
 }
Exemple #13
0
 protected override void OnGenerateLightningBolt(LightningBolt bolt, Vector3 start, Vector3 end, LightningBoltParameters p)
 {
     GenerateLightningBoltPath(bolt, start, end, p);
 }
Exemple #14
0
        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        = Mathf.Sqrt(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;
                }
            }
        }
Exemple #15
0
 public void GenerateLightningBolt(LightningBolt bolt, LightningBoltParameters p, out Vector3 start, out Vector3 end)
 {
     start = p.ApplyVariance(p.Start, p.StartVariance);
     end   = p.ApplyVariance(p.End, p.EndVariance);
     OnGenerateLightningBolt(bolt, start, end, p);
 }
Exemple #16
0
        public void GenerateLightningBolt(LightningBolt bolt, LightningBoltParameters p)
        {
            Vector3 start, end;

            GenerateLightningBolt(bolt, p, out start, out end);
        }
 /// <summary>
 /// Populate lightning bolt parameters
 /// </summary>
 /// <param name="parameters">Parameters</param>
 protected virtual void PopulateParameters(LightningBoltParameters parameters)
 {
 }
 protected override void OnGenerateLightningBolt(Vector3 start, Vector3 end, LightningBoltParameters p)
 {
     GenerateLightningBoltPath(start, end, p as LightningBoltPathParameters);
 }
Exemple #19
0
 /// <summary>
 /// Derived classes can override and can call this base class method last to add the lightning bolt parameters to the list of batched lightning bolts
 /// </summary>
 /// <param name="p">Lightning bolt creation parameters</param>
 public override void CreateLightningBolt(LightningBoltParameters p)
 {
     batchParameters.Add(p);
     // do not call the base method, we batch up and use CreateLightningBolts
 }
Exemple #20
0
        public void GenerateLightningBoltStandard(LightningBolt bolt, Vector3 start, Vector3 end, int generation, int totalGenerations, float offsetAmount, LightningBoltParameters p)
        {
            if (generation < 1)
            {
                return;
            }

            LightningBoltSegmentGroup group = bolt.AddGroup();

            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(bolt, 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(bolt, p, generation, totalGenerations, start, midPoint);
                }

                // halve the distance the lightning can deviate for each generation down
                offsetAmount *= 0.5f;
            }
        }
 /// <summary>
 /// Get new lightning bolt parameters
 /// </summary>
 /// <returns>LightningBoltParameters</returns>
 protected virtual LightningBoltParameters OnCreateParameters()
 {
     return(LightningBoltParameters.GetOrCreateParameters());
 }