internal void Apply(CelestialBody celestialBody, Transform scaledCelestialTransform, CloudsMaterial cloudsMaterial, float radius, Tools.Layer layer = Tools.Layer.Scaled) { CloudsManager.Log("Applying 2D clouds..."); Remove(); this.celestialBody = celestialBody; this.scaledCelestialTransform = scaledCelestialTransform; HalfSphere hp = new HalfSphere(radius, ref CloudMaterial, CloudShader); CloudMesh = hp.GameObject; CloudMaterial.name = "Clouds2D"; this.radius = radius; macroCloudMaterial.Radius = radius; this.cloudsMat = cloudsMaterial; this.scaledLayer = layer; CloudMaterial.SetMatrix(ShaderProperties._ShadowBodies_PROPERTY, Matrix4x4.zero); if (shadowMaterial != null) { ShadowProjectorGO = new GameObject(); ShadowProjector = ShadowProjectorGO.AddComponent <Projector>(); ShadowProjector.nearClipPlane = 10; ShadowProjector.fieldOfView = 60; ShadowProjector.aspectRatio = 1; ShadowProjector.orthographic = true; ShadowProjector.transform.parent = celestialBody.transform; ShadowProjector.material = new Material(CloudShadowShader); shadowMaterial.ApplyMaterialProperties(ShadowProjector.material); } Scaled = true; }
private void ApplyToMainMenu() { if (HighLogic.LoadedScene == GameScenes.MAINMENU) { GameObject go = Tools.GetMainMenuObject(body); if (go != null && go.transform != mainMenuBodyTransform) { mainMenuCamera = GameObject.FindObjectsOfType <Camera>().First(c => (c.cullingMask & (1 << go.layer)) > 0 && c.isActiveAndEnabled); if (layer2D != null) { if (mainMenuLayer != null) { mainMenuLayer.Remove(); } mainMenuBodyTransform = go.transform; mainMenuLayer = layer2D.CloneForMainMenu(go); CloudsManager.Log(this.name + " Applying to main menu!"); } } else if (go == null) { CloudsManager.Log("Cannot find " + body + " to apply to main Menu!"); } else if (mainMenuBodyTransform == go.transform) { CloudsManager.Log("Already Applied to main Menu!"); } } }
public void Remove() { if (CloudMesh != null) { CloudsManager.Log("Removing 2D clouds..."); CloudMesh.transform.parent = null; GameObject.DestroyImmediate(CloudMesh); CloudMesh = null; } if (ShadowProjectorGO != null) { ShadowProjectorGO.transform.parent = null; ShadowProjector.transform.parent = null; GameObject.DestroyImmediate(ShadowProjector); GameObject.DestroyImmediate(ShadowProjectorGO); ShadowProjector = null; ShadowProjectorGO = null; } if (screenSpaceShadowGO != null) { screenSpaceShadowGO.transform.parent = null; GameObject.DestroyImmediate(screenSpaceShadowGO); screenSpaceShadowGO = null; } }
internal void Apply(CelestialBody celestialBody, Transform scaledCelestialTransform, CloudsMaterial cloudsMaterial, string name, float radius, float arc, Tools.Layer layer = Tools.Layer.Scaled) { CloudsManager.Log("Applying 2D clouds..."); Remove(); this.celestialBody = celestialBody; this.scaledCelestialTransform = scaledCelestialTransform; if (arc == 360) { HalfSphere hp = new HalfSphere(radius, ref CloudMaterial, CloudShader); CloudMesh = hp.GameObject; } else { UVSphere hp = new UVSphere(radius, arc, ref CloudMaterial, CloudShader); CloudMesh = hp.GameObject; } CloudMesh.name = name; CloudMaterial.name = "Clouds2D"; this.radius = radius; this.arc = arc; macroCloudMaterial.Radius = radius; this.cloudsMat = cloudsMaterial; this.scaledLayer = layer; CloudMaterial.SetMatrix(ShaderProperties._ShadowBodies_PROPERTY, Matrix4x4.zero); if (shadowMaterial != null) { ShadowProjectorGO = new GameObject("EVE ShadowProjector"); ShadowProjector = ShadowProjectorGO.AddComponent <Projector>(); ShadowProjector.nearClipPlane = 10; ShadowProjector.fieldOfView = 60; ShadowProjector.aspectRatio = 1; ShadowProjector.orthographic = true; ShadowProjector.transform.parent = celestialBody.transform; ShadowProjector.material = new Material(CloudShadowShader); shadowMaterial.ApplyMaterialProperties(ShadowProjector.material); // Workaround Unity bug (Case 841236) ShadowProjector.enabled = false; ShadowProjector.enabled = true; // Here create the screenSpaceShadowMaterialStuff screenSpaceShadowGO = new GameObject("EVE ScreenSpaceShadow"); screenSpaceShadowGO.transform.parent = celestialBody.transform; screenSpaceShadow = screenSpaceShadowGO.AddComponent <ScreenSpaceShadow>(); //can this be a single class that will handle the mesh, and meshrenderer and everything? screenSpaceShadow.material = new Material(ScreenSpaceCloudShadowShader); shadowMaterial.ApplyMaterialProperties(screenSpaceShadow.material); screenSpaceShadow.Init(); screenSpaceShadowGO.SetActive(false); screenSpaceShadow.SetActive(false); } Scaled = true; }
protected override void Clean() { CloudsManager.Log("Cleaning Clouds!"); foreach (CloudsObject obj in ObjectList) { obj.Remove(); GameObject go = obj.gameObject; go.transform.parent = null; GameObject.DestroyImmediate(obj); GameObject.DestroyImmediate(go); } ObjectList.Clear(); }
public override void OnSphereActive() { CloudsManager.Log("CloudsPQS: (" + this.name + ") OnSphereActive"); if (layer2D != null) { layer2D.Scaled = false; } if (!volumeApplied) { if (layerVolume != null) { layerVolume.Apply(cloudsMaterial, (float)celestialBody.Radius + altitude, celestialBody.transform); } volumeApplied = true; } }
public override void OnSphereInactive() { CloudsManager.Log("CloudsPQS: (" + this.name + ") OnSphereInactive"); if (layer2D != null) { layer2D.Scaled = true; } if (!MapView.MapIsEnabled) { if (layerVolume != null) { layerVolume.Remove(); } volumeApplied = false; } }
internal void Apply(String body, CloudsMaterial cloudsMaterial, Clouds2D layer2D, CloudsVolume layerVolume, float altitude, Vector3d speed, Vector3d detailSpeed, Vector3 offset, Matrix4x4 rotationAxis, bool killBodyRotation) { this.body = body; this.cloudsMaterial = cloudsMaterial; this.layer2D = layer2D; this.layerVolume = layerVolume; this.altitude = altitude; this.offset = -offset; this.rotationAxis = rotationAxis; this.killBodyRotation = killBodyRotation; celestialBody = Tools.GetCelestialBody(body); scaledCelestialTransform = Tools.GetScaledTransform(body); PQS pqs = null; if (celestialBody != null && celestialBody.pqsController != null) { pqs = celestialBody.pqsController; } else { CloudsManager.Log("No PQS! Instanciating one."); pqs = PQSManagerClass.GetPQS(body); } CloudsManager.Log("PQS Applied"); if (pqs != null) { this.sphere = pqs; this.transform.parent = pqs.transform; this.requirements = PQS.ModiferRequirements.Default; this.modEnabled = true; this.order += 10; this.transform.localPosition = Vector3.zero; this.transform.localRotation = Quaternion.identity; this.transform.localScale = Vector3.one; this.radius = (altitude + celestialBody.Radius); double circumference = 2f * Mathf.PI * radius; mainPeriod = -(speed) / circumference; detailPeriod = -(detailSpeed) / circumference; if (layer2D != null) { this.layer2D.Apply(celestialBody, scaledCelestialTransform, cloudsMaterial, this.name, (float)radius); } if (!pqs.isActive || HighLogic.LoadedScene == GameScenes.TRACKSTATION) { this.OnSphereInactive(); } else { this.OnSphereActive(); } this.OnSetup(); pqs.EnableSphere(); } else { CloudsManager.Log("PQS is null somehow!?"); } GameEvents.OnMapExited.Add(ExitMapView); GameEvents.onGameSceneLoadRequested.Add(SceneLoaded); if (HighLogic.LoadedScene == GameScenes.MAINMENU) { ApplyToMainMenu(); } }
protected void Update() { bool visible = HighLogic.LoadedScene == GameScenes.TRACKSTATION || HighLogic.LoadedScene == GameScenes.FLIGHT || HighLogic.LoadedScene == GameScenes.SPACECENTER || HighLogic.LoadedScene == GameScenes.MAINMENU; if (visible) { double ut; if (HighLogic.LoadedScene == GameScenes.MAINMENU) { ut = Time.time; } else { ut = Planetarium.GetUniversalTime(); } Vector3d detailRotation = (ut * detailPeriod); detailRotation -= new Vector3d((int)detailRotation.x, (int)detailRotation.y, (int)detailRotation.z); detailRotation *= 360; detailRotation += offset; Vector3d mainRotation = (ut * mainPeriod); mainRotation -= new Vector3d((int)mainRotation.x, (int)mainRotation.y, (int)mainRotation.z); mainRotation *= 360f; mainRotation += offset; QuaternionD mainRotationQ = Quaternion.identity; if (killBodyRotation) { mainRotationQ = QuaternionD.AngleAxis(celestialBody.rotationAngle, Vector3.up); } mainRotationQ *= QuaternionD.AngleAxis(mainRotation.x, (Vector3)rotationAxis.GetRow(0)) * QuaternionD.AngleAxis(mainRotation.y, (Vector3)rotationAxis.GetRow(1)) * QuaternionD.AngleAxis(mainRotation.z, (Vector3)rotationAxis.GetRow(2)); Matrix4x4 mainRotationMatrix = Matrix4x4.TRS(Vector3.zero, mainRotationQ, Vector3.one).inverse; QuaternionD detailRotationQ = QuaternionD.AngleAxis(detailRotation.x, Vector3.right) * QuaternionD.AngleAxis(detailRotation.y, Vector3.up) * QuaternionD.AngleAxis(detailRotation.z, Vector3.forward); Matrix4x4 detailRotationMatrix = Matrix4x4.TRS(Vector3.zero, detailRotationQ, Vector3.one).inverse; if (this.sphere != null) { Matrix4x4 world2SphereMatrix = this.sphere.transform.worldToLocalMatrix; if (layer2D != null) { if (HighLogic.LoadedScene == GameScenes.SPACECENTER || (HighLogic.LoadedScene == GameScenes.FLIGHT && sphere.isActive && !MapView.MapIsEnabled)) { layer2D.UpdateRotation(Quaternion.FromToRotation(Vector3.up, this.sphere.relativeTargetPosition), world2SphereMatrix, mainRotationMatrix, detailRotationMatrix); } else if (HighLogic.LoadedScene == GameScenes.MAINMENU && mainMenuLayer != null) { //mainMenuCamera.transform.position -= 5 * mainMenuCamera.transform.forward; Transform transform = mainMenuCamera.transform; Vector3 pos = mainMenuBodyTransform.InverseTransformPoint(transform.position); mainMenuLayer.UpdateRotation(Quaternion.FromToRotation(Vector3.up, pos), mainMenuBodyTransform.worldToLocalMatrix, mainRotationMatrix, detailRotationMatrix); } else if (MapView.MapIsEnabled || HighLogic.LoadedScene == GameScenes.TRACKSTATION || (HighLogic.LoadedScene == GameScenes.FLIGHT && !sphere.isActive)) { Transform transform = ScaledCamera.Instance.galaxyCamera.transform; Vector3 pos = scaledCelestialTransform.InverseTransformPoint(transform.position); layer2D.UpdateRotation(Quaternion.FromToRotation(Vector3.up, pos), scaledCelestialTransform.worldToLocalMatrix, mainRotationMatrix, detailRotationMatrix); } } if (layerVolume != null && sphere.isActive) { if (FlightCamera.fetch != null) { var inRange = layer2D == null ? true : Mathf.Abs(FlightCamera.fetch.cameraAlt - layer2D.Altitude()) < layerVolume.VisibleRange(); if (inRange != layerVolume.enabled) { CloudsManager.Log((inRange ? "Enable" : "Disable") + " clouds when camera: " + FlightCamera.fetch.cameraAlt + " layer: " + (layer2D == null ? "none" : layer2D.Altitude().ToString())); } if (inRange) { layerVolume.enabled = true; layerVolume.UpdatePos(FlightCamera.fetch.mainCamera.transform.position, world2SphereMatrix, mainRotationQ, detailRotationQ, mainRotationMatrix, detailRotationMatrix); } else { layerVolume.enabled = false; } } else { layerVolume.UpdatePos(this.sphere.target.position, world2SphereMatrix, mainRotationQ, detailRotationQ, mainRotationMatrix, detailRotationMatrix); layerVolume.enabled = true; } } } } }