Ejemplo n.º 1
0
        void Update()
        {
            if (Main.settings.cloudShadowsEnabled)
            {
                SunLight.cookie = WeatherSource.SunShadowRenderImage;
            }
            else
            {
                SunLight.cookie = null;
            }

#if !CYBEX_TIME
            // fauxnik time algo
            ProceduralSkyTimeSource.Instance.CalculateTimeProgress(Time.deltaTime);
            var currentTime = TimeSourceAdapter.GetCurrentTime();
#if DEBUG
            DevGUI devGui = GetComponent <DevGUI>();
            if (devGui != null && devGui.dateTimeOverride)
            {
                currentTime = devGui.CurrentTime;
            }
#endif

            DateToSkyMapper.ApplyDate(currentTime);

            // daily & yearly rotation of skybox night
            SkyboxNight.localRotation = DateToSkyMapper.SkyboxNightRotation;

            // daily & seasonal rotation of the sun
            SunPathCenter.parent.localRotation = DateToSkyMapper.SunPivotRotation;
            SunPathCenter.localPosition        = DateToSkyMapper.SunOffsetFromPath;

            // daily & seasonal rotation of the moon
            MoonPathCenter.parent.localRotation = DateToSkyMapper.MoonPivotRotation;
            MoonPathCenter.localPosition        = DateToSkyMapper.MoonOffsetFromPath;
#else
            // cybex time algo
            CybexTime.CalculateTimeProgress(Main.settings.latitude, 0);

            // rotations
            SkyboxNight.localRotation           = Quaternion.Euler(CybexTime.SkyboxNightRotation);
            SunPathCenter.parent.localRotation  = Quaternion.Euler(CybexTime.SunPivotRotation);
            MoonPathCenter.parent.localRotation = Quaternion.Euler(CybexTime.MoonRotation);

#if DEBUG
            // TODO: update this
            DevGUI devGui = GetComponent <DevGUI>();
            if (devGui != null && devGui.posOverride)
            {
                devGui.CalculateRotationOverride();
                SkyboxNight.localRotation    = devGui.skyRot;
                SunPathCenter.localRotation  = devGui.sunRot;
                MoonPathCenter.localRotation = devGui.moonRot;
            }
#endif
#endif

            // movement
            worldPos           = PlayerManager.PlayerTransform.position - WorldMover.currentMove;
            transform.position = new Vector3(worldPos.x * .001f, 0, worldPos.z * .001f);

            Vector3 highLatitudeCorrection = SunPathCenter.parent.TransformVector(SunPathCenter.localPosition) - SunPathCenter.parent.position / DateToSkyMapper.maxProjectedSunOffset;
            Vector3 sunPos            = SunLight.transform.position - SunPathCenter.position + highLatitudeCorrection;
            float   sunOverHorizonFac = Mathf.Clamp01(sunPos.y);
            SunLight.intensity = sunOverHorizonFac * 1.5f;
            SunLight.color     = Color.Lerp(new Color(1f, 0.5f, 0), Color.white, sunOverHorizonFac);

            StarMaterial.SetFloat("_Visibility", (-sunOverHorizonFac + 1) * .01f);

            MoonMaterial.SetFloat("_MoonDayNight", Mathf.Lerp(2.19f, 1.5f, sunOverHorizonFac));
            // gives aproximate moon phase
            MoonMaterial.SetFloat("_MoonPhase", Vector3.SignedAngle(SunPathCenter.right, MoonPathCenter.right, SunPathCenter.forward) / 180);
            MoonMaterial.SetFloat("_Exposure", Mathf.Lerp(2f, 4f, sunOverHorizonFac));

            SkyMaterial.SetFloat("_Exposure", Mathf.Lerp(.01f, 1f, sunOverHorizonFac));
            SkyMaterial.SetFloat("_AtmosphereThickness", Mathf.Lerp(0.1f, 1f, Mathf.Clamp01(sunOverHorizonFac * 10)));

            CloudMaterial.SetFloat("_NScale", WeatherSource.CloudNoiseScaleBlend);
            CloudMaterial.SetFloat("_ClearSky", WeatherSource.CloudClearSkyBlend);
            float facC = Mathf.Lerp(.002f, 1f, sunOverHorizonFac);
            CloudMaterial.SetFloat("_CloudBright", WeatherSource.CloudBrightnessBlend * facC);
            float facG = Mathf.Lerp(.25f, 0.0f, sunOverHorizonFac);
            CloudMaterial.SetFloat("_CloudGradient", WeatherSource.CloudGradientBlend + facG);
            CloudMaterial.SetFloat("_CloudSpeed", WeatherSource.CloudSpeedBlend);
            CloudMaterial.SetFloat("_CloudChange", WeatherSource.CloudChangeBlend);
#if DEBUG
            if (devGui != null && devGui.cloudOverride)
            {
                CloudMaterial.SetFloat("_NScale", devGui.loadedWeatherState.cloudNoiseScale);
                CloudMaterial.SetFloat("_ClearSky", devGui.loadedWeatherState.cloudClearSky);
                CloudMaterial.SetFloat("_CloudBright", devGui.loadedWeatherState.cloudBrightness * facC);
                CloudMaterial.SetFloat("_CloudGradient", devGui.loadedWeatherState.cloudGradient + facG);
                CloudMaterial.SetFloat("_CloudSpeed", devGui.loadedWeatherState.cloudSpeed);
                CloudMaterial.SetFloat("_CloudChange", devGui.loadedWeatherState.cloudChange);
            }
#endif

            RenderSettings.fogColor        = Color.Lerp(nightFog, defaultFog, sunOverHorizonFac);
            RenderSettings.ambientSkyColor = Color.Lerp(ambientNight, ambientDay, sunOverHorizonFac);

#if DEBUG
            if (devGui != null && devGui.rainOverride)
            {
                RenderSettings.fogDensity = Mathf.Lerp(defaultFogDensity, defaultFogDensity * 3, devGui.loadedWeatherState.rainParticleStrength);
                RainController.SetRainStrength(devGui.loadedWeatherState.rainParticleStrength);
            }
            else
            {
                RenderSettings.fogDensity = Mathf.Lerp(defaultFogDensity, defaultFogDensity * 3, WeatherSource.RainStrengthBlend);
                RainController.SetRainStrength(WeatherSource.RainStrengthBlend);
            }
#else
            RenderSettings.fogDensity = Mathf.Lerp(defaultFogDensity, defaultFogDensity * 3, WeatherSource.RainStrengthBlend);
            RainController.SetRainStrength(WeatherSource.RainStrengthBlend);
#endif

            RainController.SetRainColor(new Color(RenderSettings.fogColor.r + 0.5f, RenderSettings.fogColor.g + 0.5f, RenderSettings.fogColor.b + 0.5f, 1));
        }
Ejemplo n.º 2
0
        public void Init()
        {
#if DEBUG
            Debug.Log(">>> >>> >>> Cybex_ProceduralSkyMod : Initializer Starting Setup...");
            Debug.Log(">>> >>> >>> Loading Asset Bundle...");
#endif
            // Load the asset bundle
            AssetBundle assets = AssetBundle.LoadFromFile(Main.ModPath + "Resources/proceduralskymod");

            _skyMaterial   = assets.LoadAsset <Material>("Assets/Materials/Sky.mat");
            _rainAudioClip = assets.LoadAsset <AudioClip>("Assets/Audio/rain-03.wav");
            _cloudPrefab   = assets.LoadAsset <GameObject>("Assets/Prefabs/CloudPlane.prefab");
            _starMaterial  = assets.LoadAsset <Material>("Assets/Materials/StarBox.mat");
            _moonPrefab    = assets.LoadAsset <GameObject>("Assets/Prefabs/Moon.prefab");
            _rainPrefab    = assets.LoadAsset <GameObject>("Assets/Prefabs/RainDrop.prefab");

            assets.Unload(false);

#if DEBUG
            Debug.Log(">>> >>> >>> Loading Saved State...");
#endif
            SkySaveManager.Load();
            ProceduralSkyTimeSource.LoadSavedTime();

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Skybox Material...");
#endif
            // Set skybox material
            Material skyMaterial = _skyMaterial;

            skyMaterial.SetColor("_SkyTint", new Color(0.3f, 0.3f, 0.8f, 1f));
            skyMaterial.SetColor("_GroundColor", new Color(0.369f, 0.349f, 0.341f, 1f));

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Procedural Sky Master...");
#endif
            // Setup dynamic sky
            GameObject psMaster = new GameObject()
            {
                name = "ProceduralSkyMod"
            };
            psMaster.transform.Reset();
            SkyManager skyManager = psMaster.AddComponent <SkyManager>();

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Directional Light...");
#endif
            // Find directional light and setup
            GameObject[] roots = SceneManager.GetActiveScene().GetRootGameObjects();
            for (int i = 0; i < roots.Length; i++)
            {
                if (roots[i].name == "Directional Light")
                {
                    roots[i].SetActive(false);
                }
            }

            dirLight = new GameObject()
            {
                name = "Sun"
            }.AddComponent <Light>();

            dirLight.type           = LightType.Directional;
            dirLight.shadows        = LightShadows.Soft;
            dirLight.shadowStrength = 0.9f;
            dirLight.gameObject.AddComponent <LookAtConstraintOnPreCull>().target = psMaster.transform;
            dirLight.cookieSize = 2000;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Cameras...");
#endif
            // main cam
            mainCam              = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>();
            mainCam.clearFlags   = CameraClearFlags.Depth;
            mainCam.cullingMask  = -1;
            mainCam.cullingMask &= ~(1 << 31);
            //mainCam.depth = -1; // original setting

            // sky cam
            Camera skyCam = new GameObject()
            {
                name = "SkyCam"
            }.AddComponent <Camera>();
            GameObject skyCamGimbal = new GameObject {
                name = "SkyCamGimbal"
            };
            skyCamGimbal.transform.SetParent(psMaster.transform, false);
            skyCam.transform.SetParent(skyCamGimbal.transform, false);
            skyCam.clearFlags    = CameraClearFlags.Depth;
            skyCam.cullingMask   = 0;
            skyCam.cullingMask  |= 1 << 31;
            skyCam.depth         = -2;
            skyCam.fieldOfView   = mainCam.fieldOfView;
            skyCam.nearClipPlane = mainCam.nearClipPlane;
            skyCam.farClipPlane  = 100;
            // this localScale negates VR stereo separation
            skyCamGimbal.transform.localScale = Vector3.zero;
            skyCamGimbal.AddComponent <PositionConstraintOnPreCull>().source = psMaster.transform;

            // clear cam
            Camera clearCam = new GameObject()
            {
                name = "ClearCam"
            }.AddComponent <Camera>();
            clearCam.clearFlags  = CameraClearFlags.Skybox;
            clearCam.cullingMask = 0;
            clearCam.depth       = -3;
            clearCam.fieldOfView = mainCam.fieldOfView;

            SkyCamConstraint constraint = skyCam.gameObject.AddComponent <SkyCamConstraint>();
            constraint.main  = mainCam;
            constraint.sky   = skyCam;
            constraint.clear = clearCam;

            // cloud render texture cam
            Camera cloudRendTexCam = new GameObject()
            {
                name = "CloudRendTexCam"
            }.AddComponent <Camera>();
            cloudRendTexCam.transform.SetParent(psMaster.transform);
            cloudRendTexCam.transform.ResetLocal();
            cloudRendTexCam.transform.localPosition = new Vector3(0, 3, 0);
            cloudRendTexCam.transform.localRotation = Quaternion.Euler(new Vector3(90, 0, 0));
            cloudRendTexCam.clearFlags             = CameraClearFlags.Color;
            cloudRendTexCam.backgroundColor        = Color.clear;
            cloudRendTexCam.cullingMask            = 0;
            cloudRendTexCam.cullingMask           |= 1 << 31;
            cloudRendTexCam.orthographic           = true;
            cloudRendTexCam.orthographicSize       = 3;
            cloudRendTexCam.nearClipPlane          = 0;
            cloudRendTexCam.farClipPlane           = 3;
            cloudRendTexCam.renderingPath          = RenderingPath.Forward;
            cloudRendTexCam.targetTexture          = WeatherSource.CloudRenderTex;
            cloudRendTexCam.useOcclusionCulling    = false;
            cloudRendTexCam.allowHDR               = false;
            cloudRendTexCam.allowMSAA              = false;
            cloudRendTexCam.allowDynamicResolution = false;
            cloudRendTexCam.forceIntoRenderTexture = true;
            WeatherSource.CloudRenderTexCam        = cloudRendTexCam;
            cloudRendTexCam.enabled = false;             // disable the camera, renders will be triggered by script

            // sun shadow render texture cam
            Camera sunShadowRendTexCam = new GameObject()
            {
                name = "SunShadowRendTextCam"
            }.AddComponent <Camera>();
            sunShadowRendTexCam.transform.SetParent(dirLight.transform);
            sunShadowRendTexCam.transform.ResetLocal();

            sunShadowRendTexCam.clearFlags      = CameraClearFlags.Color;
            sunShadowRendTexCam.backgroundColor = Color.clear;
            sunShadowRendTexCam.cullingMask     = 0;
            sunShadowRendTexCam.cullingMask    |= 1 << 31;

            //sunShadowRendTexCam.fieldOfView = dirLight.spotAngle;
            sunShadowRendTexCam.orthographic     = true;
            sunShadowRendTexCam.orthographicSize = 2;
            sunShadowRendTexCam.nearClipPlane    = 0;
            sunShadowRendTexCam.farClipPlane     = 100;

            sunShadowRendTexCam.renderingPath          = RenderingPath.Forward;
            sunShadowRendTexCam.targetTexture          = WeatherSource.SunShadowRenderTex;
            sunShadowRendTexCam.useOcclusionCulling    = false;
            sunShadowRendTexCam.allowHDR               = false;
            sunShadowRendTexCam.allowMSAA              = false;
            sunShadowRendTexCam.allowDynamicResolution = false;
            sunShadowRendTexCam.forceIntoRenderTexture = true;
            WeatherSource.SunShadowRenderTexCam        = sunShadowRendTexCam;
            sunShadowRendTexCam.enabled = false;             // disable the camera, renders will be triggered by script

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Audio Sources...");
#endif
            GameObject psAudio = new GameObject()
            {
                name = "ProceduralSkyAudio"
            };
            psAudio.transform.SetParent(mainCam.transform);
            RainController.RainAudio = psAudio.AddComponent <AudioSource>();

            RainController.RainAudio.clip                  = _rainAudioClip;
            RainController.RainAudio.mute                  = false;
            RainController.RainAudio.bypassEffects         = false;
            RainController.RainAudio.bypassListenerEffects = false;
            RainController.RainAudio.bypassReverbZones     = false;
            RainController.RainAudio.playOnAwake           = true;
            RainController.RainAudio.loop                  = true;
            RainController.RainAudio.priority              = 128;
            RainController.RainAudio.volume                = 0; // always ramp up from 0 when the game loads

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Cloud Plane...");
#endif
            GameObject cloudPlane = new GameObject();

            MeshFilter filter = cloudPlane.AddComponent <MeshFilter>();
            filter.sharedMesh = _cloudPrefab.GetComponent <MeshFilter>().sharedMesh;
            MeshRenderer renderer = cloudPlane.AddComponent <MeshRenderer>();
            Material     cloudMat = renderer.sharedMaterial = _cloudPrefab.GetComponent <MeshRenderer>().sharedMaterial;

            cloudPlane.transform.SetParent(psMaster.transform);
            cloudPlane.transform.ResetLocal();
            cloudPlane.layer = 31;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Skybox Night...");
#endif
            GameObject skyboxNight = new GameObject()
            {
                name = "SkyboxNight"
            };
            skyboxNight.transform.SetParent(psMaster.transform);
            skyboxNight.transform.ResetLocal();

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Sun Position...");
#endif
            GameObject sunPivot = new GameObject()
            {
                name = "SunPivot"
            };
            GameObject sunSlider = new GameObject()
            {
                name = "SunSlider"
            };                                                                          // sunSlider mimics moonBillboards in-built mesh offset
            sunPivot.transform.SetParent(psMaster.transform, false);
            sunSlider.transform.SetParent(sunPivot.transform, false);
            dirLight.transform.SetParent(sunSlider.transform, false);
            dirLight.transform.position     += Vector3.up * sunDistanceToCamera;
            dirLight.transform.localRotation = Quaternion.Euler(new Vector3(90, 0, 0));

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Starbox...");
#endif
            GameObject starBox = GameObject.CreatePrimitive(PrimitiveType.Cube);
            starBox.GetComponent <MeshRenderer>().sharedMaterial = _starMaterial;
            starBox.transform.SetParent(skyboxNight.transform);
            starBox.transform.ResetLocal();
            starBox.transform.localRotation = Quaternion.Euler(new Vector3(0, 68.5f, 28.9f));
            starBox.transform.localScale    = Vector3.one * 20;
            starBox.layer = 31;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Moon Billboard...");
#endif
            GameObject moonBillboard = new GameObject()
            {
                name = "MoonBillboard"
            };
            GameObject moonPivot = new GameObject()
            {
                name = "MoonPivot"
            };

            filter                  = moonBillboard.AddComponent <MeshFilter>();
            filter.sharedMesh       = _moonPrefab.GetComponent <MeshFilter>().sharedMesh;
            renderer                = moonBillboard.AddComponent <MeshRenderer>();
            renderer.sharedMaterial = _moonPrefab.GetComponent <MeshRenderer>().sharedMaterial;

            moonPivot.transform.SetParent(psMaster.transform, false);
            moonBillboard.transform.SetParent(moonPivot.transform, false);
            moonBillboard.transform.localScale = Vector3.one * moonDistanceToCamera / 2;             // moonBillboard mesh has in-built offset of 2
            moonBillboard.layer = 31;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Rain Particle System...");
#endif
            GameObject psRainParticleSys = new GameObject()
            {
                name = "ProceduralSkyRainParticleSystem"
            };

            PositionConstraintOnUpdate psRainParticleSysconstraint = psRainParticleSys.AddComponent <PositionConstraintOnUpdate>();
            psRainParticleSysconstraint.source = mainCam.transform;

            GameObject rainObj = GameObject.Instantiate(_rainPrefab);
            rainObj.transform.SetParent(psRainParticleSys.transform);
            rainObj.transform.ResetLocal();
            rainObj.transform.Translate(Vector3.up * 16);

            RainController.SetRainParticleSystemArray(psRainParticleSys.GetComponentsInChildren <ParticleSystem>(true));
            WeatherSource.CloudRenderEvent += RainController.SetShapeTextures;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Weather Source...");
#endif
            WeatherSource.CurrentWeatherState = WeatherState.LoadFromXML(WeatherSource.XMLWeatherStatePath + SkySaveManager.State.currentWeatherState);
            if (!string.IsNullOrEmpty(SkySaveManager.State.nextWeatherState))
            {
                WeatherSource.NextWeatherState = WeatherState.LoadFromXML(WeatherSource.XMLWeatherStatePath + SkySaveManager.State.nextWeatherState);
            }
            WeatherSource.WeatherStateBlending = SkySaveManager.State.weatherStateBlending;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Sky Manager Properties...");
#endif
            // assign skyboxNight after sun is positioned to get correct sun rotation
            skyManager.SkyboxNight   = skyboxNight.transform;
            skyManager.SunPathCenter = sunSlider.transform;
            skyManager.SunLight      = dirLight;

            skyManager.CloudPlane    = cloudPlane.transform;
            skyManager.CloudMaterial = cloudMat;

            skyManager.StarMaterial = starBox.GetComponent <MeshRenderer>().sharedMaterial;

            skyManager.SkyCam      = skyCam.transform;
            skyManager.SkyMaterial = skyMaterial;

            skyManager.ClearCam = clearCam.transform;

            skyManager.MoonPathCenter = moonBillboard.transform;
            skyManager.MoonMaterial   = moonBillboard.GetComponent <MeshRenderer>().sharedMaterial;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Render Settings...");
#endif
            // Set render settings
            RenderSettings.sun         = dirLight;
            RenderSettings.skybox      = skyMaterial;
            RenderSettings.ambientMode = UnityEngine.Rendering.AmbientMode.Flat;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Sky Save...");
#endif
            DV.AppUtil.GamePaused += SkySaveManager.Save;

#if DEBUG
            Debug.Log(">>> >>> >>> Setting Up Reflection Probe Updater...");
#endif
            ReflectionProbeUpdater.probe = FindObjectOfType <DynamicReflectionProbe>().GetComponent <ReflectionProbe>();

#if DEBUG
            psMaster.AddComponent <DevGUI>();
            Debug.Log(">>> >>> >>> Cybex_ProceduralSkyMod : Initializer Finished Setup...");
#endif
            GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
            go.transform.ResetLocal();
            go.transform.position   += Vector3.up * 130;
            go.transform.localScale *= 10;
        }