Exemple #1
0
        public void Update()
        {
            if (Time.timeSinceLevelLoad < 1f || CameraManager.Instance == null)
            {
                return;
            }
    #if DEBUGOPTIONS
            if (GameSettings.MODIFIER_KEY.GetKey() && Input.GetKeyDown(KeyCode.A))
            {
                DebugDontRotateIVACamera = !DebugDontRotateIVACamera;
                JSIAdvPodsUtil.SetCameraCullingMaskForIVA(Maincamera.name, DebugDontRotateIVACamera);
            }

            if (GameSettings.MODIFIER_KEY.GetKey() && Input.GetKeyDown(KeyCode.Q))
            {
                DebugShowLaser = !DebugShowLaser;
            }
    #endif
            //If Stock Overlay Cam is On or we are NOT in Flight camera mode (IE. Map or IVA mode), turn OFF our camera.
            if (JSIAdvPodsUtil.StockOverlayCamIsOn ||
                (HighLogic.LoadedSceneIsFlight && CameraManager.Instance.currentCameraMode != CameraManager.CameraMode.Flight))
            {
                TurnoffIVACamera();
                return;
            }

            //So we are in flight cam mode, the stock camera overlay is not on.
            //Check our IVACamera exists, if it doesn't, create one.
            if (IVAcamera == null)
            {
                CreateIVACamera();
            }
            TurnonIVACamera();
        }
        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?");
            }
        }