コード例 #1
0
        public void SetupLightningBolt(LightningBoltDependencies dependencies)
        {
            if (dependencies == null || dependencies.Parameters.Count == 0)
            {
                Debug.LogError("Lightning bolt dependencies must not be null");
                return;
            }
            else if (this.dependencies != null)
            {
                Debug.LogError("This lightning bolt is already in use!");
                return;
            }

            this.dependencies  = dependencies;
            CameraMode         = dependencies.CameraMode;
            timeSinceLevelLoad = LightningBoltScript.TimeSinceStart;
            CheckForGlow(dependencies.Parameters);
            MinimumDelay = float.MaxValue;

#if !UNITY_WEBGL
            if (dependencies.ThreadState != null)
            {
                startTimeOffset = DateTime.UtcNow;
                dependencies.ThreadState.AddActionForBackgroundThread(ProcessAllLightningParameters);
            }
            else
#endif

            {
                ProcessAllLightningParameters();
            }
        }
コード例 #2
0
 private void ReturnLightningDependenciesToCache(LightningBoltDependencies d)
 {
     d.Parameters                  = null;
     d.OriginParticleSystem        = null;
     d.DestParticleSystem          = null;
     d.LightningMaterialMesh       = null;
     d.LightningMaterialMeshNoGlow = null;
     dependenciesCache.Add(d);
 }
コード例 #3
0
 /// <summary>
 /// Create multiple lightning bolts, attempting to batch them into as few draw calls as possible
 /// </summary>
 /// <param name="parameters">Lightning bolt creation parameters</param>
 public void CreateLightningBolts(ICollection <LightningBoltParameters> parameters)
 {
     if (parameters != null && parameters.Count != 0)
     {
         UpdateTexture();
         LightningBolt             bolt         = GetOrCreateLightningBolt();
         LightningBoltDependencies dependencies = CreateLightningBoltDependencies(parameters);
         bolt.SetupLightningBolt(dependencies);
     }
 }
コード例 #4
0
 /// <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);
     }
 }
コード例 #5
0
        private LightningBoltDependencies CreateLightningBoltDependencies(ICollection <LightningBoltParameters> parameters)
        {
            LightningBoltDependencies d;

            if (dependenciesCache.Count == 0)
            {
                d = new LightningBoltDependencies();
                d.AddActiveBolt  = AddActiveBolt;
                d.LightAdded     = OnLightAdded;
                d.LightRemoved   = OnLightRemoved;
                d.ReturnToCache  = ReturnLightningDependenciesToCache;
                d.StartCoroutine = StartCoroutineWrapper;
                d.Parent         = gameObject;
            }
            else
            {
                int i = dependenciesCache.Count - 1;
                d = dependenciesCache[i];
                dependenciesCache.RemoveAt(i);
            }

            d.CameraPos                   = Camera.transform.position;
            d.CameraIsOrthographic        = Camera.orthographic;
            d.CameraMode                  = calculatedCameraMode;
            d.LevelOfDetailDistance       = LevelOfDetailDistance;
            d.DestParticleSystem          = LightningDestinationParticleSystem;
            d.LightningMaterialMesh       = lightningMaterialMeshInternal;
            d.LightningMaterialMeshNoGlow = lightningMaterialMeshNoGlowInternal;
            d.OriginParticleSystem        = LightningOriginParticleSystem;
            d.SortLayerName               = SortLayerName;
            d.SortOrderInLayer            = SortOrderInLayer;
            d.UseWorldSpace               = UseWorldSpace;

#if !UNITY_WEBGL
            d.ThreadState = threadState;

            // clone parameters list if threading, otherwise just set it
            if (threadState != null)
            {
                d.Parameters = new List <LightningBoltParameters>(parameters);
            }
            else
#endif

            {
                d.Parameters = parameters;
            }

            d.LightningBoltStarted = LightningStartedCallback;
            d.LightningBoltEnded   = LightningEndedCallback;

            return(d);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        /// <summary>
        /// Create multiple lightning bolts, attempting to batch them into as few draw calls as possible
        /// </summary>
        /// <param name="parameters">Lightning bolt creation parameters</param>
        public void CreateLightningBolts(ICollection <LightningBoltParameters> parameters)
        {
#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 (parameters != null && parameters.Count != 0 && Camera != null)
            {
                UpdateTexture();
                LightningBolt             bolt         = GetOrCreateLightningBolt();
                LightningBoltDependencies dependencies = CreateLightningBoltDependencies(parameters);
                bolt.SetupLightningBolt(dependencies);
            }
        }
コード例 #8
0
        /// <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);
            }
        }
コード例 #9
0
        private void ProcessAllLightningParameters()
        {
            int           maxLightsForEachParameters = MaximumLightsPerBatch / dependencies.Parameters.Count;
            RangeOfFloats delay        = new RangeOfFloats();
            List <int>    groupIndexes = new List <int>(dependencies.Parameters.Count + 1);
            int           i            = 0;

#if ENABLE_PROFILING
            System.Diagnostics.Stopwatch w = System.Diagnostics.Stopwatch.StartNew();
#endif

            foreach (LightningBoltParameters parameters in dependencies.Parameters)
            {
                delay.Minimum        = parameters.DelayRange.Minimum + parameters.Delay;
                delay.Maximum        = parameters.DelayRange.Maximum + parameters.Delay;
                parameters.maxLights = maxLightsForEachParameters;
                groupIndexes.Add(segmentGroups.Count);
                ProcessParameters(parameters, delay, dependencies);
            }
            groupIndexes.Add(segmentGroups.Count);

#if ENABLE_PROFILING
            w.Stop();
            UnityEngine.Debug.LogFormat("GENERATE: {0}", w.Elapsed.TotalMilliseconds);
            w.Reset();
            w.Start();
#endif

            LightningBoltDependencies dependenciesRef = dependencies;
            foreach (LightningBoltParameters parameters in dependenciesRef.Parameters)
            {
                Transform transform = RenderLightningBolt(parameters.quality, parameters.Generations, groupIndexes[i], groupIndexes[++i], parameters);

#if !UNITY_WEBGL
                if (dependenciesRef.ThreadState != null)
                {
                    dependenciesRef.ThreadState.AddActionForMainThread(() =>
                    {
                        dependenciesRef.StartCoroutine(NotifyBolt(dependenciesRef, parameters, transform, parameters.Start, parameters.End));
                    });
                }
                else
#endif

                {
                    dependenciesRef.StartCoroutine(NotifyBolt(dependenciesRef, parameters, transform, parameters.Start, parameters.End));
                }
            }

#if ENABLE_PROFILING
            w.Stop();
            UnityEngine.Debug.LogFormat("RENDER: {0}", w.Elapsed.TotalMilliseconds);
#endif

#if !UNITY_WEBGL
            if (dependencies.ThreadState != null)
            {
                dependencies.ThreadState.AddActionForMainThread(EnableCurrentLineRendererFromThread);
            }
            else
#endif

            {
                EnableCurrentLineRenderer();
                dependencies.AddActiveBolt(this);
            }
        }
コード例 #10
0
        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;
            }
        }