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(); } } } } }
internal override void Update () { //toggle whether GUI is visible or not if ((Input.GetKey (KeyCode.LeftAlt) || Input.GetKey (KeyCode.RightAlt)) && (Input.GetKeyDown (KeyCode.F11) || (Input.GetKeyDown (KeyCode.F10)))) Visible = !Visible; if (isActive && ScaledSpace.Instance) { if (!found) { // CelestialBodies = (CelestialBody[])CelestialBody.FindObjectsOfType (typeof(CelestialBody)); foreach (scattererCelestialBody sctBody in scattererCelestialBodies) { var _sct = false; var _idx = 0; var celBody = CelestialBodies.Single (_cb => _cb.bodyName == sctBody.celestialBodyName); if (celBody == null) { celBody = CelestialBodies.Single (_cb => _cb.bodyName == sctBody.transformName); } Debug.Log ("[Scatterer] Celestial Body: " + celBody); if (celBody != null) { _sct = true; _idx = scattererCelestialBodies.IndexOf (sctBody); Debug.Log ("[Scatterer] Found: " + sctBody.celestialBodyName + " / " + celBody.GetName ()); } ; sctBody.celestialBody = celBody; celestialBodiesWithDistance.Add (new celestialBodySortableByDistance () { CelestialBody = sctBody.celestialBody, Distance = 0, usesScatterer = _sct, scattererIndex = _idx }); var sctBodyTransform = ScaledSpace.Instance.transform.FindChild (sctBody.transformName); if (!sctBodyTransform) { sctBodyTransform = ScaledSpace.Instance.transform.FindChild (sctBody.celestialBodyName); } if (sctBodyTransform) { sctBody.transform = sctBodyTransform; sctBody.hasTransform = true; } sctBody.active = false; } sunCelestialBody = CelestialBodies.Single (_cb => _cb.GetName () == "Sun"); //we need to load all of the celestial bodies in celestialBodiesWithDistance regardless //of whether they are loaded ins catterer or not //this is because we need to sort everything by distance to fix their draw orders //and avoid kerbin's atmo being visible from behind the mun for example for (int k = 0; k < CelestialBodies.Length; k++) { bool inScatterer = false; for (int i=0; i<scattererCelestialBodies.Count; i++) { if (CelestialBodies [k].GetName () == scattererCelestialBodies [i].celestialBodyName) { inScatterer = true; i += 100; } } if (!inScatterer) { celestialBodiesWithDistance.Add (new celestialBodySortableByDistance () {CelestialBody = CelestialBodies[k], Distance = 0, usesScatterer=false, scattererIndex=0 }); } } 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]; Debug.Log("orig farCamera.nearClipPlane"+farCamera.nearClipPlane.ToString()); farCamera.nearClipPlane=1000f; } if (cams [i].name == "Camera 00") { nearCamera = cams [i]; // Debug.Log("orig nearCamera.nearClipPlane"+nearCamera.nearClipPlane.ToString()); // Debug.Log("orig nearCamera.farClipPlane"+nearCamera.farClipPlane.ToString()); nearCamera.nearClipPlane=0.15f; nearCamera.farClipPlane=1002f; } } found = true; } if (ScaledSpace.Instance && farCamera) { // if (farCamera) { if (!depthBufferSet) { if (!render24bitDepthBuffer || d3d9) { farCamera.depthTextureMode = DepthTextureMode.Depth; Debug.Log ("[Scatterer] Running in Direct3D 9, using default depth buffer"); } else { customDepthBuffer = (CustomDepthBufferCam)farCamera.gameObject.AddComponent (typeof(CustomDepthBufferCam)); customDepthBuffer.inCamera = farCamera; customDepthBuffer.incore = this; customDepthBufferTexture = new RenderTexture (Screen.width, Screen.height, 24, RenderTextureFormat.Depth); // customDepthBufferTexture = new RenderTexture (Screen.width, Screen.height, 0, RenderTextureFormat.RFloat); customDepthBufferTexture.Create (); customDepthBuffer._depthTex = customDepthBufferTexture; Debug.Log ("[Scatterer] Running custom depth buffer"); if (forceDisableDefaultDepthBuffer) { Debug.Log ("[Scatterer] Forcing default depth buffer off"); } } depthBufferSet = true; } if (render24bitDepthBuffer && !d3d9 && !customDepthBufferTexture.IsCreated ()) { customDepthBufferTexture.Create (); } if (forceDisableDefaultDepthBuffer) { //want this to be forced off every frame, in case some other mod is reenabling it farCamera.depthTextureMode = DepthTextureMode.None; } pqsEnabled = false; foreach (scattererCelestialBody _cur in scattererCelestialBodies) { float dist; if (_cur.hasTransform) { if (FlightGlobals.ActiveVessel) { dist = Vector3.Distance ( FlightGlobals.ActiveVessel.transform.position, ScaledSpace.ScaledToLocalSpace (_cur.transform.position)); } else { dist = Vector3.Distance ( farCamera.transform.position, ScaledSpace.ScaledToLocalSpace (_cur.transform.position)); } if (_cur.active) { if (dist > _cur.unloadDistance && !MapView.MapIsEnabled) { _cur.m_manager.OnDestroy (); UnityEngine.Object.Destroy (_cur.m_manager); _cur.m_manager = null; //ReactivateAtmosphere(cur.transformName,cur.originalPlanetMaterialBackup); _cur.active = false; Resources.UnloadUnusedAssets (); System.GC.Collect (); Debug.Log ("[Scatterer] Effects unloaded for " + _cur.celestialBodyName); } else { _cur.m_manager.Update (); pqsEnabled = pqsEnabled || !_cur.m_manager.m_skyNode.inScaledSpace; } } else { if (dist < _cur.loadDistance && !MapView.MapIsEnabled && _cur.transform && _cur.celestialBody) { _cur.m_manager = new Manager (); _cur.m_manager.setParentCelestialBody (_cur.celestialBody); _cur.m_manager.setParentPlanetTransform (_cur.transform); _cur.m_manager.setSunCelestialBody (sunCelestialBody); _cur.m_manager.SetCore (this); _cur.m_manager.hasOcean = _cur.hasOcean; _cur.m_manager.Awake (); _cur.active = true; selectedConfigPoint = 0; displayOceanSettings = false; selectedPlanet = scattererCelestialBodies.IndexOf (_cur); getSettingsFromSkynode (); if (_cur.hasOcean) { getSettingsFromOceanNode (); } Debug.Log ("[Scatterer] Effects loaded for " + _cur.celestialBodyName); } } } } fixDrawOrders (); } } }