Exemple #1
0
        //public static Settings settings;
        //private string globalConfigFilename;
        //private ConfigNode globalNode = new ConfigNode();

        //Awake Event - when the DLL is loaded
        public void Awake()
        {
            if (Instance != null)
            {
                return;
            }
            Instance = this;
            DontDestroyOnLoad(this);
            //settings = new Settings();
            //globalConfigFilename = Path.Combine(_AssemblyFolder, "Config.cfg").Replace("\\", "/");
            //JSIAdvPodsUtil.Log("globalConfigFilename = " + globalConfigFilename);
            //if (!File.Exists(globalConfigFilename))
            //{
            //    settings.Save(globalNode);
            //    globalNode.Save(globalConfigFilename);
            //}
            //globalNode = ConfigNode.Load(globalConfigFilename);
            //settings.Load(globalNode);
            //JSIAdvPodsUtil.debugLoggingEnabled = settings.DebugLogging;
            //JSIAdvPodsUtil.debugLoggingEnabled = HighLogic.CurrentGame.Parameters.CustomParams<JSIATP_SettingsParms>().DebugLogging;
            //LoadedInactive = HighLogic.CurrentGame.Parameters.CustomParams<JSIATP_SettingsParms>().LoadedInactive;
            GameEvents.OnGameSettingsApplied.Add(onGameSettingsApplied);
            GameEvents.onGameStatePostLoad.Add(onGameStatePostLoad);
            JSIAdvPodsUtil.Log("JSIAdvTransparentPods LoadGlobals Awake Complete");
        }
 //Awake Event - when the DLL is loaded
 public void Awake()
 {
     if (Instance != null)
     {
         return;
     }
     Instance = this;
     DontDestroyOnLoad(this);
     settings             = new Settings();
     globalConfigFilename = Path.Combine(_AssemblyFolder, "Config.cfg").Replace("\\", "/");
     JSIAdvPodsUtil.Log("globalConfigFilename = " + globalConfigFilename);
     if (!File.Exists(globalConfigFilename))
     {
         settings.Save(globalNode);
         globalNode.Save(globalConfigFilename);
     }
     globalNode = ConfigNode.Load(globalConfigFilename);
     settings.Load(globalNode);
     JSIAdvPodsUtil.debugLoggingEnabled = settings.DebugLogging;
     JSIAdvPodsUtil.Log("JSIAdvTransparentPods LoadGlobals Awake Complete");
 }
        public void Update()
        {
            if (Time.timeSinceLevelLoad < 1f)
            {
                return;
            }

            // In the editor, none of this logic should matter, even though the IVA probably exists already.
            if (HighLogic.LoadedSceneIsEditor && !disableLoadingInEditor)
            {
                if (transparentPodSetting == "ON" || (transparentPodSetting == "AUTO" && mouseOver))
                {
                    // Make the internal model visible...
                    if (part.internalModel != null)
                    {
                        part.internalModel.SetVisible(true);
                    }
                    // And for a good measure we make sure the shader change has been applied.
                    SetShaders(true);
                    // Now we attach the restored IVA directly into the pod at zero local coordinates and rotate it,
                    // so that it shows up on the main outer view camera in the correct location.
                    VoodooRotate();
                    setVisible = true;
                    //if (JSIZfighterStock != null)
                    //    JSIZfighterStock.Update(stockOverlayTransform);
                    //if (JSIZfightertransparent != null)
                    //    JSIZfightertransparent.Update(transparentPodTransform);
                }

                // If we are in editor mode we need to turn off the internal if the internal is in OFF or AUTO mode and not moused over.
                //Otherwise we make it visible.
                if (transparentPodSetting == "OFF" || (transparentPodSetting == "AUTO" && !mouseOver))
                {
                    // Make the internal model Invisible...
                    if (part.internalModel != null)
                    {
                        part.internalModel.SetVisible(false);
                    }
                    SetShaders(false);
                    setVisible = false;
                }

                JSIAdvPodsUtil.Log_Debug("Part {0} : Layer {1}", part.name, part.gameObject.layer);

                //Turn the DepthMasks off in the Editor or we get Z-Fighting.
                SetDepthMask();
            }
            if (HighLogic.LoadedSceneIsFlight)
            {
                //Now FlightScene Processing

                //IVA OBstruction process of the transparentPodSetting field
                //If previously IVA was obstructed and now it is not reset the transparentPodSetting back to it's previous value.
                if (previsIVAobstructed && !isIVAobstructed)
                {
                    transparentPodSetting                    = prevtransparentPodSetting;
                    displaytransparentPodSetting             = prevdisplaytransparentPodSetting;
                    Events["eventToggleTransparency"].active = true;
                }
                previsIVAobstructed = isIVAobstructed;
                //isIVAobstructed = false;

                // If the root part changed, or the IVA is mysteriously missing, we reset it and take note of where it ended up.
                if (vessel.rootPart != knownRootPart || lastActiveVessel != FlightGlobals.ActiveVessel || part.internalModel == null)
                {
                    ResetIVA();
                }

                // So we do have an internal model, right?
                if (part.internalModel != null)
                {
                    // If transparentPodSetting = OFF or AUTO and not the focused active part we treat the part like a non-transparent part.
                    // and we turn off the shaders (if set) and the internal to the filter list and exit OnUpdate.
                    if (transparentPodSetting == "OFF" || (transparentPodSetting == "AUTO" && !mouseOver) && !isIVAobstructed)
                    {
                        SetShaders(false);
                        if (!JSIAdvTransparentPods.PartstoFilterfromIVADict.Contains(part))
                        {
                            JSIAdvTransparentPods.PartstoFilterfromIVADict.Add(part);
                        }
                        setVisible = false;
                        return;
                    }

                    //If we are in flight and the user has the Stock Overlay on and this part is not part of the active vessel we turn off the internal.
                    // also if the user has set the LoadedInactive to False - we don't show TransparentPods that aren't on the active vessel.
                    // We turn it off rather than registering it for the PreCull list because if Stock Overlay is on the JSI camera is not active.
                    if (!vessel.isActiveVessel &&
                        (JSIAdvPodsUtil.StockOverlayCamIsOn || !LoadGlobals.Instance.LoadedInactive))
                    {
                        part.internalModel.SetVisible(false);
                        setVisible = false;
                        SetShaders(false);
                        JSIAdvPodsUtil.Log_Debug(
                            "Internal turned off as vessel is Not Active Vessel and stock overlay is on or LoadedInactive is False: ({0}) {1}",
                            part.craftID, vessel.vesselName);
                        return;
                    }

                    if (!vessel.isActiveVessel)
                    {
                        //For some reason (probably performance) Squad do not actively update the position and rotation of InternalModels that are not part of the active vessel.
                        //Calculate the Vessel position and rotation and then apply that to the InternalModel position and rotation with the MagicalVoodooRotation.
                        Vector3 VesselPosition = part.vessel.transform.position +
                                                 part.vessel.transform.rotation * part.orgPos;
                        part.internalModel.transform.position = InternalSpace.WorldToInternal(VesselPosition);
                        Quaternion VesselRotation = part.vessel.transform.rotation * part.orgRot;
                        part.internalModel.transform.rotation = InternalSpace.WorldToInternal(VesselRotation) *
                                                                MagicalVoodooRotation;

                        // If the current part is not part of the active vessel, we calculate the distance from the part to the flight camera.
                        // If this distance is > distanceToCameraThreshold metres we turn off transparency for the part.
                        // Uses Maths calcs intead of built in Unity functions as this is up to 5 times faster.
                        Vector3   heading;
                        Transform thisPart     = part.transform;
                        Transform flightCamera = FlightCamera.fetch.transform;
                        heading.x = thisPart.position.x - flightCamera.position.x;
                        heading.y = thisPart.position.y - flightCamera.position.y;
                        heading.z = thisPart.position.z - flightCamera.position.z;
                        var distanceSquared = heading.x * heading.x + heading.y * heading.y + heading.z * heading.z;
                        distanceToCamera = Mathf.Sqrt(distanceSquared);

                        if (distanceToCamera > distanceToCameraThreshold)
                        {
                            SetShaders(false);
                            //part.internalModel.SetVisible(false);
                            if (!JSIAdvTransparentPods.PartstoFilterfromIVADict.Contains(part))
                            {
                                JSIAdvTransparentPods.PartstoFilterfromIVADict.Add(part);
                            }
                            setVisible = false;
                            return;
                        }
                    }

                    //If inactive vessel IVAs are turned on via the settings then we:
                    //Check for obstructions between this IVA and the Camera that may be on lower layers and turn off the IVA if there is one.
                    //Not a perfect solution..... and bad performance-wise.
                    if (LoadGlobals.Instance.LoadedInactive)
                    {
                        if (JSIAdvTransparentPods.Instance != null &&
                            CameraManager.Instance.currentCameraMode == CameraManager.CameraMode.Flight)
                        {
                            if (JSIAdvTransparentPods.Instance.MaincameraTransform != null)
                            {
                                isIVAobstructed = IsIVAObstructed(part.transform, JSIAdvTransparentPods.Instance.MaincameraTransform);
                                if (isIVAobstructed)
                                {
                                    if (!JSIAdvTransparentPods.PartstoFilterfromIVADict.Contains(part))
                                    {
                                        JSIAdvTransparentPods.PartstoFilterfromIVADict.Add(part);
                                    }
                                    SetShaders(false);
                                    setVisible = false;
                                    //Set the prevtransparentPodSetting to the current transparentPodSetting and then set transparenPodSetting to "OFF"
                                    if (!previsIVAobstructed)
                                    {
                                        Events["eventToggleTransparency"].active = false;
                                        prevtransparentPodSetting        = transparentPodSetting;
                                        prevdisplaytransparentPodSetting = displaytransparentPodSetting;
                                        transparentPodSetting            = "Obstructed";
                                        displaytransparentPodSetting     = transparentPodSetting;
                                    }
                                    return;
                                }
                            }
                        }
                    }

                    // Make the internal model visible...
                    // And for a good measure we make sure the shader change has been applied.
                    SetShaders(true);
                    if (JSIAdvTransparentPods.PartstoFilterfromIVADict.Contains(part))
                    {
                        JSIAdvTransparentPods.PartstoFilterfromIVADict.Remove(part);
                    }
                    setVisible = true;
                    part.internalModel.SetVisible(true);
                }
                else
                {
                    JSIAdvPodsUtil.Log("Where is my Internal model for : {0}", part.craftID);
                }
            }
        }
        public override void OnStart(StartState state)
        {
            JSIAdvPodsUtil.Log_Debug("OnStart {0} {1} in state {2}", part.craftID, part.name, state);
            if (state == StartState.Editor && disableLoadingInEditor)
            {
                // Early out for people who want to disable transparency in
                // the editor due to low-spec computers.
                return;
            }

            DepthMaskShader = Shader.Find(DepthMaskShaderName);

            if (distanceToCameraThreshold > 200)
            {
                distanceToCameraThreshold = 200;
            }

            if (!string.IsNullOrEmpty(opaqueShaderName))
            {
                opaqueShader = Shader.Find(opaqueShaderName);
                if (transparentShader == null)
                {
                    JSIAdvPodsUtil.Log("opaqueShader {0} not found.", opaqueShaderName);
                }
                else
                {
                    hasOpaqueShader = true;
                }
            }

            // In Editor, the camera we want to change is called "Main Camera". In flight, the camera to change is
            // "Camera 00", i.e. close range camera.
            //JSIAdvPodsUtil.DumpCameras();
            if (state == StartState.Editor)
            {
                // I'm not sure if this change is actually needed, even. Main Camera's culling mask seems to already include IVA objects,
                // they just don't normally spawn them.
                JSIAdvPodsUtil.SetCameraCullingMaskForIVA("Main Camera", true);
                //JSIAdvPodsUtil.SetCameraCullingMaskForIVA("Main Camera", false);
            }

            // If the internal model has not yet been created, try creating it and log the exception if we fail.
            if (part.internalModel == null)
            {
                try
                {
                    part.CreateInternalModel();
                    if (part.internalModel != null && HighLogic.LoadedSceneIsFlight)
                    {
                        part.internalModel.Initialize(part);
                        part.internalModel.SpawnCrew();
                    }
                }
                catch (Exception e)
                {
                    JSIAdvPodsUtil.Log("failed to create internal model in Onstart");
                    Debug.LogException(e, this);
                }
            }

            if (part.internalModel == null && part.partInfo != null)
            {
                // KSP 1.0.x introduced a new feature where it doesn't appear
                // to fully load parts if they're not the root.  In particular,
                // that CreateInternalModel() call above here returns null for
                // non-root parts until one exits the VAB and returns.
                // If the internalModel doesn't exist yet, I find the config
                // for this part, and create the Model ourselves.

                JSIAdvPodsUtil.Log_Debug("Let's see if anyone included parts so I can assemble the interior");

                if (part.partInfo != null && part.partInfo.internalConfig != null && part.partInfo.internalConfig.HasData)
                {
                    part.AddInternalPart(part.partInfo.internalConfig);
                }
                if (part.internalModel != null && HighLogic.LoadedSceneIsFlight)
                {
                    part.internalModel.Initialize(part);
                    part.internalModel.SpawnCrew();
                }
            }

            // Apply shaders to transforms on startup.
            if (!string.IsNullOrEmpty(transparentTransforms))
            {
                try
                {
                    transparentShader = Shader.Find(transparentShaderName);
                }
                catch (Exception ex)
                {
                    JSIAdvPodsUtil.Log("Get transparentShader {0} failed. Error: {1}", transparentShaderName, ex);
                }
                if (transparentShader == null)
                {
                    JSIAdvPodsUtil.Log("transpartShader {0} not found.", transparentShaderName);
                }
                foreach (string transformName in transparentTransforms.Split('|'))
                {
                    try
                    {
                        Transform tr = part.FindModelTransform(transformName.Trim());
                        if (tr != null)
                        {
                            //We both change the shader and backup the original shader so we can undo it later.
                            Shader backupShader = tr.GetComponent <Renderer>().material.shader;
                            if (backupShader != null)
                            {
                                backupShader = transparentShader;
                                shadersBackup.Add(new KeyValuePair <Transform, Shader>(tr, backupShader));
                            }
                        }
                        if (part.internalModel != null)
                        {
                            Transform itr = part.internalModel.FindModelTransform(transformName.Trim());
                            if (itr != null)
                            {
                                // We both change the shader and backup the original shader so we can undo it later.
                                Shader backupShader = itr.GetComponent <Renderer>().material.shader;
                                if (backupShader != null)
                                {
                                    backupShader = transparentShader;
                                    shadersBackup.Add(new KeyValuePair <Transform, Shader>(itr, backupShader));
                                }
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e, this);
                    }
                }
            }

            //If debugging is ON dump the InternalModel transforms.
            if (JSIAdvPodsUtil.debugLoggingEnabled && part.internalModel != null)
            {
                StringBuilder sb = new StringBuilder();
                JSIAdvPodsUtil.DumpGameObjectChilds(part.internalModel.gameObject.transform.parent.gameObject, part.name + " Internal ", sb);
                print("[JSIATP] " + sb);
            }

            //Check and process transparentPodDepthMaskShaderTransform field.
            if (!string.IsNullOrEmpty(transparentPodDepthMaskShaderTransform) && part.internalModel != null)
            {
                transparentPodTransform = part.internalModel.gameObject.GetComponentsInChildren <Transform>().FirstOrDefault(t => t.name == transparentPodDepthMaskShaderTransform);
                if (transparentPodTransform == null)
                {
                    transparentPodDepthMaskShaderTransform = "";
                    JSIAdvPodsUtil.Log("Unable to find transparentPodDepthMaskShaderTransform {0} in InternalModel", transparentPodDepthMaskShaderTransform);
                }
            }

            //Check and process stockOverlayDepthMaskShaderTransform field.
            if (!string.IsNullOrEmpty(stockOverlayDepthMaskShaderTransform) && part.internalModel != null)
            {
                stockOverlayTransform = part.internalModel.gameObject.GetComponentsInChildren <Transform>().FirstOrDefault(t => t.name == stockOverlayDepthMaskShaderTransform);
                if (stockOverlayTransform == null)
                {
                    transparentPodDepthMaskShaderTransform = "";
                    JSIAdvPodsUtil.Log("Unable to find stockOverlayDepthMaskShaderTransform {0} in InternalModel", stockOverlayDepthMaskShaderTransform);
                }
            }

            // If we ended up with an existing internal model,
            if (part.internalModel != null)
            {
                // Rotate it now, so that it is shown correctly in the editor. - OLD Method.
                if (state == StartState.Editor)
                {
                    // Just rotating the internal is sufficient in this case.
                    part.internalModel.transform.localRotation = MagicalVoodooRotation;
                    //Find all Renderer's with DepthMask shader assigned to them and make them inactive as they cause Z-Fighting in the Editor and are
                    //not needed in the editor - OLD Method.
                    SetDepthMask();
                    //Turn on Zfighters for the depthmask overlays if they are present.

                    /*if (transparentPodTransform != null && JSIZfightertransparent == null)
                     * {
                     *  JSIZfightertransparent = new JSIZFighter();
                     *  JSIZfightertransparent.Start(transparentPodTransform);
                     * }
                     * if (stockOverlayTransform != null && JSIZfighterStock == null)
                     * {
                     *  JSIZfighterStock = new JSIZFighter();
                     *  JSIZfighterStock.Start(stockOverlayTransform);
                     * }*/
                }
                else
                {
                    // Else this is our first startup in flight scene, we reset the IVA.
                    ResetIVA();
                }
            }
            else
            {
                // Some error-proofing. I won't bother doing this every frame, because one error message
                // should suffice, this module is not supposed to be attached to parts that have no internals in the first place.
                JSIAdvPodsUtil.Log("Wait, where's my internal model?");
            }
        }
        public override void OnStart(StartState state)
        {
            JSIAdvPodsUtil.Log_Debug("OnStart {0} {1} in state {2}", part.craftID, part.name, state);
            if (state == StartState.Editor && disableLoadingInEditor)
            {
                // Early out for people who want to disable transparency in
                // the editor due to low-spec computers.
                return;
            }

            DepthMaskShader = Shader.Find(DepthMaskShaderName);

            if (distanceToCameraThreshold > 200)
            {
                distanceToCameraThreshold = 200;
            }

            if (!string.IsNullOrEmpty(opaqueShaderName))
            {
                opaqueShader = Shader.Find(opaqueShaderName);
                if (transparentShader == null)
                {
                    JSIAdvPodsUtil.Log("opaqueShader {0} not found.", opaqueShaderName);
                }
                else
                {
                    hasOpaqueShader = true;
                }
            }

            // In Editor, the camera we want to change is called "Main Camera". In flight, the camera to change is
            // "Camera 00", i.e. close range camera.

            if (state == StartState.Editor)
            {
                // I'm not sure if this change is actually needed, even. Main Camera's culling mask seems to already include IVA objects,
                // they just don't normally spawn them.
                JSIAdvPodsUtil.SetCameraCullingMaskForIVA("Main Camera", true);
            }

            // If the internal model has not yet been created, try creating it and log the exception if we fail.
            if (part.internalModel == null)
            {
                try
                {
                    part.CreateInternalModel();
                }
                catch (Exception e)
                {
                    JSIAdvPodsUtil.Log("failed to create internal model in Onstart");
                    Debug.LogException(e, this);
                }
            }

            if (part.internalModel == null && part.partInfo != null)
            {
                // KSP 1.0.x introduced a new feature where it doesn't appear
                // to fully load parts if they're not the root.  In particular,
                // that CreateInternalModel() call above here returns null for
                // non-root parts until one exits the VAB and returns.
                // If the internalModel doesn't exist yet, I find the config
                // for this part, extract the INTERNAL node, and try to create
                // the model myself. Awfully roundabout.

                JSIAdvPodsUtil.Log_Debug("Let's see if anyone included parts so I can assemble the interior");
                ConfigNode ipNameNode = (from cfg in GameDatabase.Instance.GetConfigs("PART")
                                         where cfg.url == part.partInfo.partUrl
                                         select cfg.config.GetNode("INTERNAL")).FirstOrDefault();

                if (ipNameNode != null)
                {
                    part.internalModel = part.AddInternalPart(ipNameNode);
                }
            }

            // Apply shaders to transforms on startup.
            if (!string.IsNullOrEmpty(transparentTransforms))
            {
                try
                {
                    transparentShader = Shader.Find(transparentShaderName);
                }
                catch (Exception ex)
                {
                    JSIAdvPodsUtil.Log("Get transparentShader {0} failed. Error: {1}", transparentShaderName, ex);
                }
                if (transparentShader == null)
                {
                    JSIAdvPodsUtil.Log("transparentShader {0} not found.", transparentShaderName);
                }
                foreach (string transformName in transparentTransforms.Split('|'))
                {
                    try
                    {
                        Transform tr = part.FindModelTransform(transformName.Trim());
                        if (tr != null)
                        {
                            //We both change the shader and backup the original shader so we can undo it later.
                            Shader backupShader = tr.GetComponent <Renderer>().material.shader;
                            tr.GetComponent <Renderer>().material.shader = transparentShader;
                            shadersBackup.Add(tr, backupShader);
                        }
                        if (part.internalModel != null)
                        {
                            Transform itr = part.internalModel.FindModelTransform(transformName.Trim());
                            if (itr != null)
                            {
                                // We both change the shader and backup the original shader so we can undo it later.
                                Shader backupShader = itr.GetComponent <Renderer>().material.shader;
                                itr.GetComponent <Renderer>().material.shader = transparentShader;
                                shadersBackup.Add(itr, backupShader);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.LogException(e, this);
                    }
                }
            }


            // If we ended up with an existing internal model,
            if (part.internalModel != null)
            {
                // Rotate it now, so that it is shown correctly in the editor. - OLD Method.
                if (state == StartState.Editor)
                {
                    // Just rotating the internal is sufficient in this case.
                    part.internalModel.transform.localRotation = MagicalVoodooRotation;
                    //Find all Renderer's with DepthMask shader assigned to them and make them inactive as they cause Z-Fighting in the Editor and are
                    //not needed in the editor - OLD Method.
                    SetDepthMask();
                }
                else
                {
                    // Else this is our first startup in flight scene, we reset the IVA.
                    ResetIVA();
                }
            }
            else
            {
                // Some error-proofing. I won't bother doing this every frame, because one error message
                // should suffice, this module is not supposed to be attached to parts that have no internals in the first place.
                JSIAdvPodsUtil.Log("Wait, where's my internal model?");
            }
        }