Exemplo n.º 1
0
        void LateUpdate()
        {
            //cache time variables for performance;
            _time      = Time.time;
            _deltaTime = Time.deltaTime;

            //use Tenkoku Random
            if (tenkokuModule != null)
            {
                if (srSeed != tenkokuModule.randSeed)
                {
                    srSeed        = tenkokuModule.randSeed;
                    lightningRand = new Tenkoku.Core.Random(srSeed);
                }
            }

            if (lightningRand == null)
            {
                lightningRand = new Tenkoku.Core.Random(srSeed);
            }

            //handle system values
            freq = tenkokuModule.weather_lightning;
            lightningDirection   = tenkokuModule.weather_lightningDir;
            lightningRandomRange = tenkokuModule.weather_lightningRange;
            lightningFrequency   = Mathf.Lerp(25.0f, 1.0f, freq);
            lightningIntensity   = Mathf.Clamp(Mathf.Lerp(1.0f, 10.0f, freq), 0.0f, 5.0f);

            //reset based on frequency changes
            if (freq != savefrequency && freq > 0.0f)
            {
                savefrequency         = freq;
                useLightningFrequency = lightningFrequency;
            }

            //clamp
            lightningIntensity = Mathf.Clamp(lightningIntensity, 0.0f, 10.0f);



            //enable lightening
            volEnable = 0.0f;
            if (tenkokuModule.enableSoundFX)
            {
                volEnable = 1.0f;
            }
            if (freq <= 0.0f)
            {
                startLightning = false;
            }

            //set overall Thunder Volume
            for (vX = 0; vX < thunderAudio.Length; vX++)
            {
                thunderAudio[vX].volume = thunderVolume[vX] * tenkokuModule.volumeThunder * volEnable;
            }

            //Lightning Timing
            if (!startLightning)
            {
                lightningUseInt     = 0.0f;
                lightningTime       = 0.0f;
                useLightningSysTime = 0.0f;

                lightningFreqTime = lightningFreqTime + (_deltaTime / useLightningFrequency);
                if (lightningFreqTime >= 1.0f)
                {
                    startLightning = true;
                }
            }
            else
            {
                //init settings
                if (useLightningSysTime == 0.0f)
                {
                    lightningDistance = lightningRand.Next(-0.3f, 0.6f);
                    lightningDistance = Mathf.Clamp(lightningDistance, 0.0f, 1.0f);
                    lightningSpeed    = lightningRand.Next(5.0f, 12.0f);
                    lightningSysTime  = lightningRand.Next(0.0f, 1.4f);
                    lightningSysTime  = Mathf.Clamp(lightningSysTime, 0.2f, 1.0f);
                    lightningUseLight = false;
                    if (lightningRand.Next(0.0f, 1.5f + freq) >= 1.0f)
                    {
                        lightningUseLight = true;
                    }
                    useLightningRandPos = lightningRand.Next(0.0f - lightningRandomRange, lightningRandomRange);

                    boltTime              = 1.0f;
                    lightningFreqTime     = 0.0f;
                    useLightningFrequency = lightningRand.Next(lightningFrequency - (lightningFrequency * 0.75f), lightningFrequency + (lightningFrequency * 0.25f));
                }

                //timers
                useLightningSysTime = useLightningSysTime + _deltaTime;
                lightningTime       = lightningTime + (_deltaTime * lightningSpeed);
                if (lightningTime <= 1.0f)
                {
                    lightningUseInt = Mathf.SmoothStep(lightningUseInt, lightningIntensity, lightningTime);
                }
                if (lightningTime > 1.0f)
                {
                    lightningUseInt = Mathf.SmoothStep(lightningUseInt, 0.2f, lightningTime - 1.0f);
                }


                //reset
                if (useLightningSysTime >= lightningSysTime && lightningTime > 2.0f)
                {
                    startLightning = false;
                    startThunder   = false;
                }


                if (lightningTime > 2.0f)
                {
                    lightningTime  = 0.0f;
                    lightningSpeed = lightningRand.Next(5.0f, 12.0f);
                    boltTime       = boltTime + _deltaTime * lightningRand.Next(2.0f, 10.0f);
                    boltPoints     = Mathf.FloorToInt(lightningRand.Next(40.0f, 80.0f));
                }

                //handle thunder
                if (!startThunder)
                {
                    startThunder = true;
                    ThunderHandler();
                }
            }



            //position
            lightningUseVec = Quaternion.Euler(Mathf.Lerp(-10.0f, -40.0f, lightningDistance), lightningDirection + useLightningRandPos, 0.0f) * Vector3.forward;


            //set light
            if (lightningLight != null)
            {
                if (lightningUseLight)
                {
                    if (!tenkokuModule.allowMultiLights)
                    {
                        lightObjectWorld.enabled = false;
                        lightObjectNight.enabled = false;
                    }
                    lightningUseInt            = lightningUseInt * Mathf.Lerp(0.0f, 2.0f, tenkokuModule.lightningColor.a);
                    lightningLight.enabled     = true;
                    lightningLightPosition.x   = Mathf.Clamp(0f - Mathf.Lerp(0f, -60f, lightningDistance), 20f, 90f);
                    lightningLightPosition.y   = (lightningDirection + useLightningRandPos) - 180f;
                    lightningLightPosition.z   = 0.0f;
                    lightningTrans.eulerAngles = lightningLightPosition;
                    lightningLight.intensity   = lightningUseInt * lightningRand.Next(0.4f, 1.0f);
                    lightningLight.color       = tenkokuModule.lightningColor;
                }
                else
                {
                    lightningLight.enabled   = false;
                    lightningLight.intensity = 0f;
                }

                if (lightningLight.intensity <= 0.05f && !tenkokuModule.allowMultiLights)
                {
                    lightObjectWorld.enabled = true;
                    lightObjectNight.enabled = true;
                    lightningLight.enabled   = false;
                }
            }

            //send to shader
            Shader.SetGlobalVector("Tenkoku_Vec_LightningFwd", lightningUseVec);
            Shader.SetGlobalFloat("Tenkoku_LightningIntensity", 0.2f * (lightningUseInt + ((1f * (1f - lightningDistance)) * lightningUseInt)));
            Shader.SetGlobalFloat("Tenkoku_LightningLightIntensity", 0.2f * lightningLight.intensity);
            Shader.SetGlobalColor("Tenkoku_LightningColor", tenkokuModule.lightningColor);


            //-------------------------
            //###  Bolt Rendering  ###
            //-------------------------
            if (Camera.main != null)
            {
                lightningLineTrans.position = Camera.main.transform.position;
                //lightningLine.transform.Translate(lightningUseVec * 3000.0f, Space.Self);
                lightningLineTrans.Translate(lightningUseVec * lightningRand.Next(2000f, 3000f), Space.Self);
                boltPosStart   = lightningLineTrans.position;
                boltPosStart.y = Mathf.Clamp(boltPosStart.y, 500.0f, 2000f);

                if (lightningRand != null)
                {
                    if (boltTime > lightningRand.Next(0.1f, 1f) || lightningSysTime == 0f)
                    {
                        boltTime = 0f;

                        if (lightningLine != null)
                        {
                            //init positional array
                            boltPoints = Mathf.FloorToInt(Mathf.Clamp(boltPoints, 40, 80));
                            lPositions = new Vector3[boltPoints];
                            usePos     = boltPosStart;

                            //set start position
                            lPositions[0] = boltPosStart;

                            //set positions
                            xR   = 0.0f;
                            sPos = usePos;
                                        #if UNITY_5_5_OR_NEWER
                            lightningLine.positionCount = lPositions.Length;
                                        #else
                            lightningLine.SetVertexCount(lPositions.Length);
                                        #endif
                            for (px = 0; px < lPositions.Length; px++)
                            {
                                //set base vertical position
                                sPos.y = usePos.y;
                                sPos.y = sPos.y - ((boltLength / (boltPoints - 1)) * px);
                                sPos.x = sPos.x - lightningRand.Next(((-100f / (boltPoints - 1f)) * px), ((100f / (boltPoints - 1f)) * px));

                                if (px > 0 && px < boltPoints - 1)
                                {
                                    //set width variance
                                    xR     = ((px * 1f) / (boltLength * 1f)) * _time;
                                    sPos.x = sPos.x - Mathf.PerlinNoise(xR, xR) * lightningRand.Next(-1f, 1f) * boltWidth;
                                    sPos.z = sPos.z - Mathf.PerlinNoise(xR, xR) * lightningRand.Next(-1f, 1f) * boltWidth;

                                    //reset variance toward end
                                    sPos.x = Mathf.Lerp(sPos.x, usePos.x, ((px * 1f) / (boltPoints * 1f)) * 0.1f);
                                    sPos.z = Mathf.Lerp(sPos.z, usePos.z, ((px * 1f) / (boltPoints * 1f)) * 0.5f);

                                    //set height variance
                                    sPos.y = sPos.y - Mathf.PerlinNoise(xR, xR) * lightningRand.Next(-1f, 1f) * 0.5f;
                                }

                                lPositions[px] = sPos;
                                lightningLine.SetPosition(px, lPositions[px]);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Add Tenkoku to the scene
        /// </summary>
        public static void GX_AddTenkoku()
        {
            //Add the tenkoku prefab to the scene

            //See if we can locate it
            GameObject tenkokuPrefab = Gaia.Utils.GetAssetPrefab("Tenkoku DynamicSky");

            if (tenkokuPrefab == null)
            {
                Debug.LogWarning("Unable to locate Tenkoku - Aborting!");
                return;
            }

            //See if we can locate it
            if (GameObject.Find("Tenkoku DynamicSky") != null)
            {
                Debug.LogWarning("Tenkoku Dynamic Sky already in scene - Aborting!");
                return;
            }

            //See if we can create it
            GameObject tenkokuObj = Instantiate(tenkokuPrefab);

            if (tenkokuObj == null)
            {
                Debug.LogWarning("Unable to creat Tenkoku object - Aborting!");
                return;
            }
            else
            {
                tenkokuObj.name = "Tenkoku DynamicSky";
            }

            //See if we can configure it - via reflection as JS and C# dont play nice
            //var tenkokuModule = tenkokuObj.GetComponent<Tenkoku.Core.TenkokuModule>() as Tenkoku.Core.TenkokuModule;
            var tenkokuModule = (Tenkoku.Core.TenkokuModule)FindObjectOfType(typeof(Tenkoku.Core.TenkokuModule));

            var tenkokuRand = new Tenkoku.Core.Random(123);

            if (tenkokuModule != null)
            {
                //Set the reflection probe settings
                tenkokuModule.enableProbe        = true;
                tenkokuModule.reflectionProbeFPS = 0.0f;

                //Set scene lighting settings
                //FieldInfo enableProbe = tenkokuModule.GetType().GetField("enableProbe", BindingFlags.Public | BindingFlags.Instance);
                //if (enableProbe != null) enableProbe.SetValue(tenkokuModule, true);
                //RenderSettings.ambientMode = AmbientMode.Flat;
                //RenderSettings.ambientMode = Rendering.AmbientMode.Flat;

                //Add some random clouds
                tenkokuModule.weather_cloudAltoStratusAmt = tenkokuRand.Next(0.0f, 0.25f);
                tenkokuModule.weather_cloudCirrusAmt      = tenkokuRand.Next(0.0f, 0.5f);
                tenkokuModule.weather_cloudCumulusAmt     = tenkokuRand.Next(0.0f, 0.6f);


                //Set the camera
                Camera camera = Camera.main;
                if (camera == null)
                {
                    camera = FindObjectOfType <Camera>();
                }
                if (camera != null)
                {
                    tenkokuModule.mainCamera = camera.transform;

                    //add fog effect to camera
                    camera.gameObject.AddComponent <TenkokuSkyFog>();

                    //set fog limits based on camera clip distance
                    tenkokuModule.fogDist = camera.farClipPlane * 0.99f;
                }
            }

            //Disable the existing directional light if it exists
            GameObject lightObj = GameObject.Find("Directional Light");

            if (lightObj != null)
            {
                lightObj.SetActive(false);
            }
        }