Exemplo n.º 1
0
        void Update()
        {
            //toggle whether GUI is visible or not
            if ((Input.GetKey(guiModifierKey1) || Input.GetKey(guiModifierKey2)) && (Input.GetKeyDown(guiKey1) || (Input.GetKeyDown(guiKey2))))
            {
                visible = !visible;
            }

            if (isActive && ScaledSpace.Instance)
            {
                if (!found)
                {
                    //set shadows
                    setShadows();

                    //find scatterer celestial bodies
                    findScattererCelestialBodies();

                    //find sun
                    sunCelestialBody = CelestialBodies.SingleOrDefault(_cb => _cb.GetName() == mainSunCelestialBodyName);

                    //find main cameras
                    Camera[] cams = Camera.allCameras;
                    for (int i = 0; i < cams.Length; i++)
                    {
                        if (cams [i].name == "Camera ScaledSpace")
                        {
                            scaledSpaceCamera = cams [i];
                        }

                        if (cams [i].name == "Camera 01")
                        {
                            farCamera = cams [i];
                        }

                        if (cams [i].name == "Camera 00")
                        {
                            nearCamera = cams [i];
                            nearCamera.nearClipPlane = nearClipPlane;
                        }
                    }



                    //find sunlight and set shadow bias
                    lights = (Light[])Light.FindObjectsOfType(typeof(Light));

                    foreach (Light _light in lights)
                    {
                        if (_light.gameObject.name == "Scaledspace SunLight")
                        {
                            scaledspaceSunLight = _light.gameObject;
                            Debug.Log("Found scaled sunlight");

                            _light.shadowNormalBias = shadowNormalBias;
                            _light.shadowBias       = shadowBias;
                        }

                        if (_light.gameObject.name == "SunLight")
                        {
                            sunLight = _light.gameObject;
                            Debug.Log("Found Sunlight");
                        }
                    }

                    //load planetshine "cookie" cubemap
                    if (usePlanetShine)
                    {
                        planetShineCookieCubeMap = new Cubemap(512, TextureFormat.ARGB32, true);

                        Texture2D[] cubeMapFaces = new Texture2D[6];
                        for (int i = 0; i < 6; i++)
                        {
                            cubeMapFaces[i] = new Texture2D(512, 512);
                        }

                        cubeMapFaces[0].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", path + "/planetShineCubemap", "_NegativeX.png")));
                        cubeMapFaces[1].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", path + "/planetShineCubemap", "_PositiveX.png")));
                        cubeMapFaces[2].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", path + "/planetShineCubemap", "_NegativeY.png")));
                        cubeMapFaces[3].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", path + "/planetShineCubemap", "_PositiveY.png")));
                        cubeMapFaces[4].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", path + "/planetShineCubemap", "_NegativeZ.png")));
                        cubeMapFaces[5].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", path + "/planetShineCubemap", "_PositiveZ.png")));

                        planetShineCookieCubeMap.SetPixels(cubeMapFaces[0].GetPixels(), CubemapFace.NegativeX);
                        planetShineCookieCubeMap.SetPixels(cubeMapFaces[1].GetPixels(), CubemapFace.PositiveX);
                        planetShineCookieCubeMap.SetPixels(cubeMapFaces[2].GetPixels(), CubemapFace.NegativeY);
                        planetShineCookieCubeMap.SetPixels(cubeMapFaces[3].GetPixels(), CubemapFace.PositiveY);
                        planetShineCookieCubeMap.SetPixels(cubeMapFaces[4].GetPixels(), CubemapFace.NegativeZ);
                        planetShineCookieCubeMap.SetPixels(cubeMapFaces[5].GetPixels(), CubemapFace.PositiveZ);
                        planetShineCookieCubeMap.Apply();
                    }

                    //find and fix renderQueues of kopernicus rings
                    foreach (CelestialBody _cb in CelestialBodies)
                    {
                        GameObject ringObject;
                        ringObject = GameObject.Find(_cb.name + "Ring");
                        if (ringObject)
                        {
                            ringObject.GetComponent <MeshRenderer> ().material.renderQueue = 3005;
                            Debug.Log("[Scatterer] Found rings for " + _cb.name);
                        }
                    }

//					//find and fix renderqueue of sun corona
//					Transform scaledSunTransform=GetScaledTransform(mainSunCelestialBodyName);
//					foreach (Transform child in scaledSunTransform)
//					{
//						MeshRenderer temp = child.gameObject.GetComponent<MeshRenderer>();
//						temp.material.renderQueue = 3000;
//					}

                    //set up planetshine lights
                    if (usePlanetShine)
                    {
                        foreach (PlanetShineLightSource _aSource in celestialLightSourcesData)
                        {
                            var celBody = CelestialBodies.SingleOrDefault(_cb => _cb.bodyName == _aSource.bodyName);
                            if (celBody)
                            {
                                PlanetShineLight aPsLight = new PlanetShineLight();
                                aPsLight.isSun            = _aSource.isSun;
                                aPsLight.source           = celBody;
                                aPsLight.sunCelestialBody = sunCelestialBody;

                                GameObject ScaledPlanetShineLight = (UnityEngine.GameObject)Instantiate(scaledspaceSunLight);
                                GameObject LocalPlanetShineLight  = (UnityEngine.GameObject)Instantiate(scaledspaceSunLight);

                                ScaledPlanetShineLight.GetComponent <Light>().type = LightType.Point;
                                if (!_aSource.isSun)
                                {
                                    ScaledPlanetShineLight.GetComponent <Light>().cookie = planetShineCookieCubeMap;
                                }

                                //ScaledPlanetShineLight.GetComponent<Light>().range=1E9f;
                                ScaledPlanetShineLight.GetComponent <Light>().range = _aSource.scaledRange;
                                ScaledPlanetShineLight.GetComponent <Light>().color = new Color(_aSource.color.x, _aSource.color.y, _aSource.color.z);
                                ScaledPlanetShineLight.name = celBody.name + "PlanetShineLight(ScaledSpace)";


                                LocalPlanetShineLight.GetComponent <Light>().type = LightType.Point;
                                if (!_aSource.isSun)
                                {
                                    LocalPlanetShineLight.GetComponent <Light>().cookie = planetShineCookieCubeMap;
                                }
                                //LocalPlanetShineLight.GetComponent<Light>().range=1E9f;
                                LocalPlanetShineLight.GetComponent <Light>().range                  = _aSource.scaledRange * 6000;
                                LocalPlanetShineLight.GetComponent <Light>().color                  = new Color(_aSource.color.x, _aSource.color.y, _aSource.color.z);
                                LocalPlanetShineLight.GetComponent <Light>().cullingMask            = 557591;
                                LocalPlanetShineLight.GetComponent <Light>().shadows                = LightShadows.Soft;
                                LocalPlanetShineLight.GetComponent <Light>().shadowCustomResolution = 2048;
                                LocalPlanetShineLight.name = celBody.name + "PlanetShineLight(LocalSpace)";

                                aPsLight.scaledLight = ScaledPlanetShineLight;
                                aPsLight.localLight  = LocalPlanetShineLight;

                                celestialLightSources.Add(aPsLight);
                                Debug.Log("[Scatterer] Added celestialLightSource " + aPsLight.source.name);
                            }
                        }
                    }


                    //find EVE clouds
                    if (integrateWithEVEClouds)
                    {
                        mapEVEClouds();
                    }

                    found = true;
                }


                if (ScaledSpace.Instance && scaledSpaceCamera)
                {
                    if (HighLogic.LoadedScene == GameScenes.SPACECENTER)
                    {
                        //magically fix stupid issues when reverting to space center from map view
                        MapView.MapIsEnabled = false;
                    }

                    if (callCollector)
                    {
                        GC.Collect();
                        callCollector = false;
                    }


                    if (!depthBufferSet)
                    {
                        if (HighLogic.LoadedScene != GameScenes.TRACKSTATION)
                        {
                            customDepthBuffer          = (CustomDepthBufferCam)farCamera.gameObject.AddComponent(typeof(CustomDepthBufferCam));
                            customDepthBuffer.inCamera = farCamera;
                            customDepthBuffer.start();

                            customDepthBufferTexture            = new RenderTexture(Screen.width, Screen.height, 16, RenderTextureFormat.RFloat);
                            customDepthBufferTexture.useMipMap  = false;
                            customDepthBufferTexture.filterMode = FilterMode.Point;                             // if this isn't in point filtering artifacts appear
                            customDepthBufferTexture.Create();

                            if (useGodrays)
                            {
                                godrayDepthTexture                = new RenderTexture(Screen.width, Screen.height, 16, RenderTextureFormat.RFloat);
                                godrayDepthTexture.filterMode     = FilterMode.Point;
                                godrayDepthTexture.useMipMap      = false;
                                customDepthBuffer._godrayDepthTex = godrayDepthTexture;
                                godrayDepthTexture.Create();
                            }

                            customDepthBuffer._depthTex = customDepthBufferTexture;

                            //refraction stuff
                            if (useOceanShaders && oceanRefraction)
                            {
                                refractionCam          = (RefractionCamera)farCamera.gameObject.AddComponent(typeof(RefractionCamera));
                                refractionCam.inCamera = farCamera;
                                refractionCam.start();

                                refractionTexture            = new RenderTexture(Screen.width, Screen.height, 16, RenderTextureFormat.ARGB32);
                                refractionTexture.useMipMap  = false;
                                refractionTexture.filterMode = FilterMode.Bilinear;
                                refractionTexture.Create();

                                refractionCam._refractionTex = refractionTexture;
                            }
                        }
                        depthBufferSet = true;
                    }


                    //custom lens flares
                    if ((fullLensFlareReplacement) && !customSunFlareAdded)
                    {
                        //disable stock sun flares
                        global::SunFlare[] stockFlares = (global::SunFlare[])global::SunFlare.FindObjectsOfType(typeof(global::SunFlare));
                        foreach (global::SunFlare _flare in stockFlares)
                        {
                            if (sunflaresList.Contains(_flare.sun.name))
                            {
                                Debug.Log("[Scatterer] Disabling stock sunflare for " + _flare.sun.name);
                                _flare.sunFlare.enabled = false;
                            }
                        }

                        foreach (string sunflareBody in sunflaresList)
                        {
                            SunFlare customSunFlare = (SunFlare)scaledSpaceCamera.gameObject.AddComponent(typeof(SunFlare));

                            try
                            {
                                customSunFlare.source     = CelestialBodies.SingleOrDefault(_cb => _cb.GetName() == sunflareBody);
                                customSunFlare.sourceName = sunflareBody;
                                customSunFlare.start();
                                customSunFlares.Add(customSunFlare);
                            }
                            catch (Exception stupid)
                            {
                                Debug.Log("[Scatterer] Custom sunflare cannot be added to " + sunflareBody + " " + stupid.ToString());

                                Component.Destroy(customSunFlare);
                                UnityEngine.Object.Destroy(customSunFlare);

                                if (customSunFlares.Contains(customSunFlare))
                                {
                                    customSunFlares.Remove(customSunFlare);
                                }

                                continue;
                            }
                        }

                        customSunFlareAdded = true;
                    }

                    if (disableAmbientLight && !ambientLightScript)
                    {
                        ambientLightScript = (DisableAmbientLight)scaledSpaceCamera.gameObject.AddComponent(typeof(DisableAmbientLight));
                    }

                    if (!(HighLogic.LoadedScene == GameScenes.TRACKSTATION))
                    {
                        if (!customDepthBufferTexture.IsCreated())
                        {
                            customDepthBufferTexture.Create();
                        }

                        if (useOceanShaders && oceanRefraction)
                        {
                            if (!refractionTexture.IsCreated())
                            {
                                refractionTexture.Create();
                            }
                        }
                    }

                    pqsEnabled = false;
                    underwater = false;

                    foreach (ScattererCelestialBody _cur in scattererCelestialBodies)
                    {
                        float dist, shipDist = 0f;
                        if (_cur.hasTransform)
                        {
                            dist = Vector3.Distance(ScaledSpace.ScaledToLocalSpace(scaledSpaceCamera.transform.position),
                                                    ScaledSpace.ScaledToLocalSpace(_cur.transform.position));

                            //don't unload planet the player ship is close to if panning away in map view
                            if (FlightGlobals.ActiveVessel)
                            {
                                shipDist = Vector3.Distance(FlightGlobals.ActiveVessel.transform.position,
                                                            ScaledSpace.ScaledToLocalSpace(_cur.transform.position));
                            }

                            if (_cur.active)
                            {
                                if (dist > _cur.unloadDistance && (shipDist > _cur.unloadDistance || shipDist == 0f))
                                {
                                    _cur.m_manager.OnDestroy();
                                    UnityEngine.Object.Destroy(_cur.m_manager);
                                    _cur.m_manager = null;
                                    _cur.active    = false;
                                    callCollector  = true;

                                    Debug.Log("[Scatterer] Effects unloaded for " + _cur.celestialBodyName);
                                }
                                else
                                {
                                    _cur.m_manager.Update();
                                    {
                                        if (!_cur.m_manager.m_skyNode.inScaledSpace)
                                        {
                                            pqsEnabled = true;
                                        }

                                        if (_cur.m_manager.hasOcean && useOceanShaders && pqsEnabled)
                                        {
                                            underwater = _cur.m_manager.GetOceanNode().isUnderwater;
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if (dist < _cur.loadDistance && _cur.transform && _cur.celestialBody)
                                {
                                    _cur.m_manager = new Manager();
                                    _cur.m_manager.setParentCelestialBody(_cur.celestialBody);
                                    _cur.m_manager.setParentPlanetTransform(_cur.transform);

                                    CelestialBody currentSunCelestialBody = CelestialBodies.SingleOrDefault(_cb => _cb.GetName() == _cur.mainSunCelestialBody);
                                    _cur.m_manager.setSunCelestialBody(currentSunCelestialBody);

                                    //Find eclipse casters
                                    List <CelestialBody> eclipseCasters = new List <CelestialBody> {
                                    };

                                    if (useEclipses)
                                    {
                                        for (int k = 0; k < _cur.eclipseCasters.Count; k++)
                                        {
                                            var cc = CelestialBodies.SingleOrDefault(_cb => _cb.GetName() == _cur.eclipseCasters[k]);
                                            if (cc == null)
                                            {
                                                Debug.Log("[Scatterer] Eclipse caster " + _cur.eclipseCasters[k] + " not found for " + _cur.celestialBodyName);
                                            }
                                            else
                                            {
                                                eclipseCasters.Add(cc);
                                                Debug.Log("[Scatterer] Added eclipse caster " + _cur.eclipseCasters[k] + " for " + _cur.celestialBodyName);
                                            }
                                        }
                                        _cur.m_manager.eclipseCasters = eclipseCasters;
                                    }

                                    List <AtmoPlanetShineSource> planetshineSources = new List <AtmoPlanetShineSource> {
                                    };

                                    if (usePlanetShine)
                                    {
                                        for (int k = 0; k < _cur.planetshineSources.Count; k++)
                                        {
                                            var cc = CelestialBodies.SingleOrDefault(_cb => _cb.GetName() == _cur.planetshineSources[k].bodyName);
                                            if (cc == null)
                                            {
                                                Debug.Log("[Scatterer] planetshine source " + _cur.planetshineSources[k].bodyName + " not found for " + _cur.celestialBodyName);
                                            }
                                            else
                                            {
                                                AtmoPlanetShineSource src = _cur.planetshineSources[k];
                                                src.body = cc;
                                                _cur.planetshineSources[k].body = cc;
                                                planetshineSources.Add(src);
                                                Debug.Log("[Scatterer] Added planetshine source" + _cur.planetshineSources[k].bodyName + " for " + _cur.celestialBodyName);
                                            }
                                        }
                                        _cur.m_manager.planetshineSources = planetshineSources;
                                    }


                                    _cur.m_manager.hasOcean             = _cur.hasOcean;
                                    _cur.m_manager.usesCloudIntegration = _cur.usesCloudIntegration;
                                    _cur.m_manager.Awake();
                                    _cur.active = true;


                                    GUItool.selectedConfigPoint  = 0;
                                    GUItool.displayOceanSettings = false;
                                    GUItool.selectedPlanet       = scattererCelestialBodies.IndexOf(_cur);
                                    GUItool.getSettingsFromSkynode();
                                    if (_cur.hasOcean && useOceanShaders)
                                    {
                                        GUItool.getSettingsFromOceanNode();
                                    }

                                    callCollector = true;
                                    Debug.Log("[Scatterer] Effects loaded for " + _cur.celestialBodyName);
                                }
                            }
                        }
                    }

                    //fixDrawOrders ();

                    //if in mapView check that depth texture is clear for the sunflare shader
                    if (customDepthBuffer)
                    {
                        if (!customDepthBuffer.depthTextureCleared && (MapView.MapIsEnabled || !pqsEnabled))
                        {
                            customDepthBuffer.clearDepthTexture();
                        }
                    }

                    //update sun flare
                    if (fullLensFlareReplacement)
                    {
                        foreach (SunFlare customSunFlare in customSunFlares)
                        {
                            customSunFlare.updateNode();
                        }
                    }

                    //update planetshine lights
                    if (usePlanetShine)
                    {
                        foreach (PlanetShineLight _aLight in celestialLightSources)
                        {
//							Debug.Log("updating "+_aLight.source.name);
                            _aLight.updateLight();
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void Init()
        {
            //load planetshine "cookie" cubemap
            planetShineCookieCubeMap = new Cubemap(512, TextureFormat.ARGB32, true);
            Texture2D[] cubeMapFaces = new Texture2D[6];
            for (int i = 0; i < 6; i++)
            {
                cubeMapFaces [i] = new Texture2D(512, 512);
            }
            cubeMapFaces [0].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", Utils.PluginPath + "/planetShineCubemap", "_NegativeX.png")));
            cubeMapFaces [1].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", Utils.PluginPath + "/planetShineCubemap", "_PositiveX.png")));
            cubeMapFaces [2].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", Utils.PluginPath + "/planetShineCubemap", "_NegativeY.png")));
            cubeMapFaces [3].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", Utils.PluginPath + "/planetShineCubemap", "_PositiveY.png")));
            cubeMapFaces [4].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", Utils.PluginPath + "/planetShineCubemap", "_NegativeZ.png")));
            cubeMapFaces [5].LoadImage(System.IO.File.ReadAllBytes(String.Format("{0}/{1}", Utils.PluginPath + "/planetShineCubemap", "_PositiveZ.png")));
            planetShineCookieCubeMap.SetPixels(cubeMapFaces [0].GetPixels(), CubemapFace.NegativeX);
            planetShineCookieCubeMap.SetPixels(cubeMapFaces [1].GetPixels(), CubemapFace.PositiveX);
            planetShineCookieCubeMap.SetPixels(cubeMapFaces [2].GetPixels(), CubemapFace.NegativeY);
            planetShineCookieCubeMap.SetPixels(cubeMapFaces [3].GetPixels(), CubemapFace.PositiveY);
            planetShineCookieCubeMap.SetPixels(cubeMapFaces [4].GetPixels(), CubemapFace.NegativeZ);
            planetShineCookieCubeMap.SetPixels(cubeMapFaces [5].GetPixels(), CubemapFace.PositiveZ);
            planetShineCookieCubeMap.Apply();


            foreach (PlanetShineLightSource _aSource in Scatterer.Instance.planetsConfigsReader.celestialLightSourcesData)
            {
                var celBody = Scatterer.Instance.scattererCelestialBodiesManager.CelestialBodies.SingleOrDefault(_cb => _cb.bodyName == _aSource.bodyName);
                if (celBody)
                {
                    PlanetShineLight aPsLight = new PlanetShineLight();
                    aPsLight.isSun  = _aSource.isSun;
                    aPsLight.source = celBody;
                    if (!_aSource.isSun)
                    {
                        aPsLight.sunCelestialBody = Scatterer.Instance.scattererCelestialBodiesManager.CelestialBodies.SingleOrDefault(_cb => _cb.GetName() == _aSource.mainSunCelestialBody);
                    }
                    GameObject ScaledPlanetShineLight = (UnityEngine.GameObject)Instantiate(Scatterer.Instance.scaledSpaceSunLight.gameObject);
                    GameObject LocalPlanetShineLight  = (UnityEngine.GameObject)Instantiate(Scatterer.Instance.scaledSpaceSunLight.gameObject);
                    ScaledPlanetShineLight.GetComponent <Light> ().type = LightType.Point;
                    if (!_aSource.isSun)
                    {
                        ScaledPlanetShineLight.GetComponent <Light> ().cookie = planetShineCookieCubeMap;
                    }
                    //ScaledPlanetShineLight.GetComponent<Light>().range=1E9f;
                    ScaledPlanetShineLight.GetComponent <Light> ().range = _aSource.scaledRange;
                    ScaledPlanetShineLight.GetComponent <Light> ().color = new Color(_aSource.color.x, _aSource.color.y, _aSource.color.z);
                    ScaledPlanetShineLight.name = celBody.name + "PlanetShineLight(ScaledSpace)";
                    LocalPlanetShineLight.GetComponent <Light> ().type = LightType.Point;
                    if (!_aSource.isSun)
                    {
                        LocalPlanetShineLight.GetComponent <Light> ().cookie = planetShineCookieCubeMap;
                    }
                    //LocalPlanetShineLight.GetComponent<Light>().range=1E9f;
                    LocalPlanetShineLight.GetComponent <Light> ().range                  = _aSource.scaledRange * 6000;
                    LocalPlanetShineLight.GetComponent <Light> ().color                  = new Color(_aSource.color.x, _aSource.color.y, _aSource.color.z);
                    LocalPlanetShineLight.GetComponent <Light> ().cullingMask            = 557591;
                    LocalPlanetShineLight.GetComponent <Light> ().shadows                = LightShadows.Soft;
                    LocalPlanetShineLight.GetComponent <Light> ().shadowCustomResolution = 2048;
                    LocalPlanetShineLight.name = celBody.name + "PlanetShineLight(LocalSpace)";
                    aPsLight.scaledLight       = ScaledPlanetShineLight;
                    aPsLight.localLight        = LocalPlanetShineLight;
                    celestialLightSources.Add(aPsLight);
                    Utils.LogDebug("Added celestialLightSource " + aPsLight.source.name);
                }
            }
        }