public void Start()
        {
            if (HighLogic.LoadedSceneIsEditor)
            {
                return;
            }

            if (string.IsNullOrEmpty(cameraTransform))
            {
                // Nothing to do if there're no camera transforms.
                return;
            }

            string[] cameraTransformList = cameraTransform.Split('|');

            // I'm sure this and the loop can be done a little differently to
            // make it clearer, but this works.
            string[] fovLimitsList   = (string.IsNullOrEmpty(fovLimits)) ? null : fovLimits.Split('|');
            string[] yawLimitsList   = (string.IsNullOrEmpty(yawLimits)) ? null : yawLimits.Split('|');
            string[] pitchLimitsList = (string.IsNullOrEmpty(pitchLimits)) ? null : pitchLimits.Split('|');
            string[] zoomRateList    = (string.IsNullOrEmpty(zoomRate)) ? null : zoomRate.Split('|');
            string[] yawRateList     = (string.IsNullOrEmpty(yawRate)) ? null : yawRate.Split('|');
            string[] pitchRateList   = (string.IsNullOrEmpty(pitchRate)) ? null : pitchRate.Split('|');

            // cameraTransformList controls the number of cameras instantiated.
            // Every other value has a default, so if it's not specified, we
            // will use that default.
            for (int i = 0; i < cameraTransformList.Length; ++i)
            {
                Vector2 thisFovLimit   = (fovLimitsList != null && i < fovLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(fovLimitsList[i]) : defaultFovLimits;
                Vector2 thisYawLimit   = (yawLimitsList != null && i < yawLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(yawLimitsList[i]) : defaultYawLimits;
                Vector2 thisPitchLimit = (pitchLimitsList != null && i < pitchLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(pitchLimitsList[i]) : defaultPitchLimits;
                float   thisZoomRate   = (zoomRateList != null && i < zoomRateList.Length) ? JUtil.GetFloat(zoomRateList[i]) ?? 0.0f : 0.0f;
                float   thisYawRate    = (yawRateList != null && i < yawRateList.Length) ? JUtil.GetFloat(yawRateList[i]) ?? 0.0f : 0.0f;
                float   thisPitchRate  = (pitchRateList != null && i < pitchRateList.Length) ? JUtil.GetFloat(pitchRateList[i]) ?? 0.0f : 0.0f;

                var thatCamera = new SteerableCameraParameters(cameraTransformList[i],
                                                               thisFovLimit, thisYawLimit, thisPitchLimit,
                                                               thisZoomRate, thisYawRate, thisPitchRate);
                cameras.Add(thatCamera);
            }

            gizmoTexture = JUtil.GetGizmoTexture();

            iconMaterial = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));

            // MOARdV: The maneuver gizmo texture is white. Unity's DrawTexture
            // expects a (0.5, 0.5, 0.5, 0.5) texture to be neutral for coloring
            // purposes.  Multiplying the desired alpha by 1/2 gets around the
            // gizmo texture's color, and gets correct alpha effects.
            Color32 iconColor = ConfigNode.ParseColor32(targetIconColor);

            iconColor.a       /= 2;
            iconMaterial.color = iconColor;

            homeCrosshairMaterial       = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));
            homeCrosshairMaterial.color = ConfigNode.ParseColor32(homeCrosshairColor);
        }
Beispiel #2
0
        private Vector2 GetNormalizedScreenPosition(SteerableCameraParameters activeCamera, Vector3 directionVector, float cameraAspect)
        {
            // Transform direction using the active camera's rotation.
            var targetTransformed = cameraObject.CameraRotation(activeCamera.currentYaw, -activeCamera.currentPitch).Inverse() * directionVector;

            // (x, y) provided the lateral displacement.  (z) provides the "in front of / behind"
            var targetDisp = new Vector2(targetTransformed.x, -targetTransformed.y);

            // I want to scale the displacement such that 1.0
            // represents the edge of the viewport. And my math is too
            // rusty to remember the right way to get that scalar.
            // Both of these are off by just a bit at wider zooms
            // (tan scales a little too much, sin a little too
            // little).  It may simply be an artifact of the camera
            // perspective divide.
            var fovScale = new Vector2(cameraAspect * Mathf.Tan(Mathf.Deg2Rad * activeCamera.currentFoV * 0.5f), Mathf.Tan(Mathf.Deg2Rad * activeCamera.currentFoV * 0.5f));

            //Vector2 fovScale = new Vector2(cameraAspect * Mathf.Sin(Mathf.Deg2Rad * currentFoV * 0.5f), Mathf.Sin(Mathf.Deg2Rad * currentFoV * 0.5f));

            // MOARdV: Are there no overloaded operators for vector math?
            // Normalize to a [-1,+1] range on both axes
            targetDisp.x = targetDisp.x / fovScale.x;
            targetDisp.y = targetDisp.y / fovScale.y;

            // If the target is behind the camera, or outside the
            // bounds of the viewport, the icon needs to be clamped
            // to the edge.
            if (targetTransformed.z < 0.0f || Math.Max(Math.Abs(targetDisp.x), Math.Abs(targetDisp.y)) > 1.0f)
            {
                targetDisp = ClampToEdge(targetDisp);
            }

            targetDisp.x = targetDisp.x * 0.5f + 0.5f;
            targetDisp.y = targetDisp.y * 0.5f + 0.5f;

            return(targetDisp);
        }
		public void Start()
		{
			if (HighLogic.LoadedSceneIsEditor)
				return;

			if (string.IsNullOrEmpty(cameraTransform)) {
				// Nothing to do if there're no camera transforms.
				return;
			}

			string[] cameraTransformList = cameraTransform.Split('|');

			// I'm sure this and the loop can be done a little differently to
			// make it clearer, but this works.
			string[] fovLimitsList = (string.IsNullOrEmpty(fovLimits)) ? null : fovLimits.Split('|');
			string[] yawLimitsList = (string.IsNullOrEmpty(yawLimits)) ? null : yawLimits.Split('|');
			string[] pitchLimitsList = (string.IsNullOrEmpty(pitchLimits)) ? null : pitchLimits.Split('|');
			string[] zoomRateList = (string.IsNullOrEmpty(zoomRate)) ? null : zoomRate.Split('|');
			string[] yawRateList = (string.IsNullOrEmpty(yawRate)) ? null : yawRate.Split('|');
			string[] pitchRateList = (string.IsNullOrEmpty(pitchRate)) ? null : pitchRate.Split('|');

			// cameraTransformList controls the number of cameras instantiated.
			// Every other value has a default, so if it's not specified, we
			// will use that default.
			for (int i = 0; i < cameraTransformList.Length; ++i)
			{
				Vector2 thisFovLimit = (fovLimitsList != null && i < fovLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(fovLimitsList[i]) : defaultFovLimits;
				Vector2 thisYawLimit = (yawLimitsList != null && i < yawLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(yawLimitsList[i]) : defaultYawLimits;
				Vector2 thisPitchLimit = (pitchLimitsList != null && i < pitchLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(pitchLimitsList[i]) : defaultPitchLimits;
				float thisZoomRate = (zoomRateList != null && i < zoomRateList.Length) ? JUtil.GetFloat(zoomRateList[i]) ?? 0.0f : 0.0f;
				float thisYawRate = (yawRateList != null && i < yawRateList.Length) ? JUtil.GetFloat(yawRateList[i]) ?? 0.0f : 0.0f;
				float thisPitchRate = (pitchRateList != null && i < pitchRateList.Length) ? JUtil.GetFloat(pitchRateList[i]) ?? 0.0f : 0.0f;

				var thatCamera = new SteerableCameraParameters(cameraTransformList[i],
					thisFovLimit, thisYawLimit, thisPitchLimit,
					thisZoomRate, thisYawRate, thisPitchRate);
				cameras.Add(thatCamera);
			}

			gizmoTexture = JUtil.GetGizmoTexture();

			iconMaterial = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));

			// MOARdV: The maneuver gizmo texture is white. Unity's DrawTexture
			// expects a (0.5, 0.5, 0.5, 0.5) texture to be neutral for coloring
			// purposes.  Multiplying the desired alpha by 1/2 gets around the
			// gizmo texture's color, and gets correct alpha effects.
			Color32 iconColor = ConfigNode.ParseColor32(targetIconColor);
			iconColor.a /= 2;
			iconMaterial.color = iconColor;

			homeCrosshairMaterial = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));
			homeCrosshairMaterial.color = ConfigNode.ParseColor32(homeCrosshairColor);
		}
		private Vector2 GetNormalizedScreenPosition(SteerableCameraParameters activeCamera, Vector3 directionVector, float cameraAspect)
		{
			// Transform direction using the active camera's rotation.
			var targetTransformed = cameraObject.CameraRotation(activeCamera.currentYaw, -activeCamera.currentPitch).Inverse() * directionVector;

			// (x, y) provided the lateral displacement.  (z) provides the "in front of / behind"
			var targetDisp = new Vector2(targetTransformed.x, -targetTransformed.y);

			// I want to scale the displacement such that 1.0
			// represents the edge of the viewport. And my math is too
			// rusty to remember the right way to get that scalar.
			// Both of these are off by just a bit at wider zooms
			// (tan scales a little too much, sin a little too
			// little).  It may simply be an artifact of the camera
			// perspective divide.
			var fovScale = new Vector2(cameraAspect * Mathf.Tan(Mathf.Deg2Rad * activeCamera.currentFoV * 0.5f), Mathf.Tan(Mathf.Deg2Rad * activeCamera.currentFoV * 0.5f));
			//Vector2 fovScale = new Vector2(cameraAspect * Mathf.Sin(Mathf.Deg2Rad * currentFoV * 0.5f), Mathf.Sin(Mathf.Deg2Rad * currentFoV * 0.5f));

			// MOARdV: Are there no overloaded operators for vector math?
			// Normalize to a [-1,+1] range on both axes
			targetDisp.x = targetDisp.x / fovScale.x;
			targetDisp.y = targetDisp.y / fovScale.y;

			// If the target is behind the camera, or outside the
			// bounds of the viewport, the icon needs to be clamped
			// to the edge.
			if (targetTransformed.z < 0.0f || Math.Max(Math.Abs(targetDisp.x), Math.Abs(targetDisp.y)) > 1.0f) {
				targetDisp = ClampToEdge(targetDisp);
			}

			targetDisp.x = targetDisp.x * 0.5f + 0.5f;
			targetDisp.y = targetDisp.y * 0.5f + 0.5f;

			return targetDisp;
		}
Beispiel #5
0
        public void Start()
        {
            if (HighLogic.LoadedSceneIsEditor)
            {
                return;
            }

            rpmComp = RasterPropMonitorComputer.Instantiate(internalProp, true);

            if (string.IsNullOrEmpty(cameraTransform))
            {
                // Nothing to do if there're no camera transforms.
                return;
            }

            string[] cameraTransformList = cameraTransform.Split('|');

            // I'm sure this and the loop can be done a little differently to
            // make it clearer, but this works.
            string[] fovLimitsList   = (string.IsNullOrEmpty(fovLimits)) ? null : fovLimits.Split('|');
            string[] yawLimitsList   = (string.IsNullOrEmpty(yawLimits)) ? null : yawLimits.Split('|');
            string[] pitchLimitsList = (string.IsNullOrEmpty(pitchLimits)) ? null : pitchLimits.Split('|');
            string[] zoomRateList    = (string.IsNullOrEmpty(zoomRate)) ? null : zoomRate.Split('|');
            string[] yawRateList     = (string.IsNullOrEmpty(yawRate)) ? null : yawRate.Split('|');
            string[] pitchRateList   = (string.IsNullOrEmpty(pitchRate)) ? null : pitchRate.Split('|');

            // cameraTransformList controls the number of cameras instantiated.
            // Every other value has a default, so if it's not specified, we
            // will use that default.
            for (int i = 0; i < cameraTransformList.Length; ++i)
            {
                Vector2 thisFovLimit   = (fovLimitsList != null && i < fovLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(fovLimitsList[i]) : defaultFovLimits;
                Vector2 thisYawLimit   = (yawLimitsList != null && i < yawLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(yawLimitsList[i]) : defaultYawLimits;
                Vector2 thisPitchLimit = (pitchLimitsList != null && i < pitchLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(pitchLimitsList[i]) : defaultPitchLimits;
                float   thisZoomRate   = (zoomRateList != null && i < zoomRateList.Length) ? JUtil.GetFloat(zoomRateList[i]) ?? 0.0f : 0.0f;
                float   thisYawRate    = (yawRateList != null && i < yawRateList.Length) ? JUtil.GetFloat(yawRateList[i]) ?? 0.0f : 0.0f;
                float   thisPitchRate  = (pitchRateList != null && i < pitchRateList.Length) ? JUtil.GetFloat(pitchRateList[i]) ?? 0.0f : 0.0f;

                var thatCamera = new SteerableCameraParameters(cameraTransformList[i],
                                                               thisFovLimit, thisYawLimit, thisPitchLimit,
                                                               thisZoomRate, thisYawRate, thisPitchRate, i + 1);
                cameras.Add(thatCamera);
            }

            gizmoTexture = JUtil.GetGizmoTexture();

            iconMaterial = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));

            // MOARdV: The maneuver gizmo texture is white. Unity's DrawTexture
            // expects a (0.5, 0.5, 0.5, 0.5) texture to be neutral for coloring
            // purposes.  Multiplying the desired alpha by 1/2 gets around the
            // gizmo texture's color, and gets correct alpha effects.
            Color32 iconColor = ConfigNode.ParseColor32(targetIconColor);

            iconColor.a       /= 2;
            iconMaterial.color = iconColor;

            homeCrosshairMaterial       = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));
            homeCrosshairMaterial.color = ConfigNode.ParseColor32(homeCrosshairColor);

            if (!string.IsNullOrEmpty(cameraInfoVarName))
            {
                //rpmComp = RasterPropMonitorComputer.Instantiate(internalProp);
                //if (rpmComp.HasPropVar(cameraInfoVarName + "_ID", internalProp.propID))
                //{
                //    currentCamera = rpmComp.GetPropVar(cameraInfoVarName + "_ID", internalProp.propID) - 1;
                //}
                //else
                //{
                //    rpmComp.SetPropVar(cameraInfoVarName + "_ID", internalProp.propID, currentCamera + 1);
                //}
            }

            if (!string.IsNullOrEmpty(cameraEffectShader))
            {
                cameraEffectMaterial = new Material(JUtil.LoadInternalShader(cameraEffectShader));

                if (!string.IsNullOrEmpty(cameraEffectVariables))
                {
                    try
                    {
                        string[] vars = cameraEffectVariables.Split('|');
                        for (int i = 0; i < vars.Length; ++i)
                        {
                            string[] components = vars[i].Split(',');
                            if (components.Length == 2)
                            {
                                ShaderEffectVariable sev = new ShaderEffectVariable();
                                sev.variable = Shader.PropertyToID(components[0].Trim());
                                sev.value    = rpmComp.InstantiateVariableOrNumber(components[1]);
                                ceVariables.Add(sev);
                            }
                        }
                    }
                    catch { }
                }

                if (!string.IsNullOrEmpty(cameraEffectTextures))
                {
                    try
                    {
                        string[] vars = cameraEffectTextures.Split('|');
                        for (int i = 0; i < vars.Length; ++i)
                        {
                            string[] components = vars[i].Split(',');
                            if (components.Length == 2)
                            {
                                Texture tex = GameDatabase.Instance.GetTexture(components[1], false);
                                cameraEffectMaterial.SetTexture(components[0], tex);
                            }
                        }
                    }
                    catch { }
                }
            }

            if (!string.IsNullOrEmpty(cameraPixelSize))
            {
                string[] vars = cameraPixelSize.Split(',');
                if (vars.Length == 2)
                {
                    if (!int.TryParse(vars[0], out rentexWidth) || !int.TryParse(vars[1], out rentexHeight) || rentexHeight < 0 || rentexWidth < 0)
                    {
                        JUtil.LogMessage(this, "Bad image dimensions? {0} and {1}", vars[0], vars[1]);
                        rentexHeight = rentexWidth = 0;
                    }
                    else
                    {
                        JUtil.LogMessage(this, "Setting rentex to {0} x {1}", rentexWidth, rentexHeight);
                    }
                }
            }
        }
        public void Start()
        {
            if (HighLogic.LoadedSceneIsEditor)
                return;

            rpmComp = RasterPropMonitorComputer.Instantiate(internalProp, true);

            if (string.IsNullOrEmpty(cameraTransform))
            {
                // Nothing to do if there're no camera transforms.
                return;
            }

            string[] cameraTransformList = cameraTransform.Split('|');

            // I'm sure this and the loop can be done a little differently to
            // make it clearer, but this works.
            string[] fovLimitsList = (string.IsNullOrEmpty(fovLimits)) ? null : fovLimits.Split('|');
            string[] yawLimitsList = (string.IsNullOrEmpty(yawLimits)) ? null : yawLimits.Split('|');
            string[] pitchLimitsList = (string.IsNullOrEmpty(pitchLimits)) ? null : pitchLimits.Split('|');
            string[] zoomRateList = (string.IsNullOrEmpty(zoomRate)) ? null : zoomRate.Split('|');
            string[] yawRateList = (string.IsNullOrEmpty(yawRate)) ? null : yawRate.Split('|');
            string[] pitchRateList = (string.IsNullOrEmpty(pitchRate)) ? null : pitchRate.Split('|');

            // cameraTransformList controls the number of cameras instantiated.
            // Every other value has a default, so if it's not specified, we
            // will use that default.
            for (int i = 0; i < cameraTransformList.Length; ++i)
            {
                Vector2 thisFovLimit = (fovLimitsList != null && i < fovLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(fovLimitsList[i]) : defaultFovLimits;
                Vector2 thisYawLimit = (yawLimitsList != null && i < yawLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(yawLimitsList[i]) : defaultYawLimits;
                Vector2 thisPitchLimit = (pitchLimitsList != null && i < pitchLimitsList.Length) ? (Vector2)ConfigNode.ParseVector2(pitchLimitsList[i]) : defaultPitchLimits;
                float thisZoomRate = (zoomRateList != null && i < zoomRateList.Length) ? JUtil.GetFloat(zoomRateList[i]) ?? 0.0f : 0.0f;
                float thisYawRate = (yawRateList != null && i < yawRateList.Length) ? JUtil.GetFloat(yawRateList[i]) ?? 0.0f : 0.0f;
                float thisPitchRate = (pitchRateList != null && i < pitchRateList.Length) ? JUtil.GetFloat(pitchRateList[i]) ?? 0.0f : 0.0f;

                var thatCamera = new SteerableCameraParameters(cameraTransformList[i],
                    thisFovLimit, thisYawLimit, thisPitchLimit,
                    thisZoomRate, thisYawRate, thisPitchRate, i + 1);
                cameras.Add(thatCamera);
            }

            gizmoTexture = JUtil.GetGizmoTexture();

            iconMaterial = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));

            // MOARdV: The maneuver gizmo texture is white. Unity's DrawTexture
            // expects a (0.5, 0.5, 0.5, 0.5) texture to be neutral for coloring
            // purposes.  Multiplying the desired alpha by 1/2 gets around the
            // gizmo texture's color, and gets correct alpha effects.
            Color32 iconColor = ConfigNode.ParseColor32(targetIconColor);
            iconColor.a /= 2;
            iconMaterial.color = iconColor;

            homeCrosshairMaterial = new Material(Shader.Find("KSP/Alpha/Unlit Transparent"));
            homeCrosshairMaterial.color = ConfigNode.ParseColor32(homeCrosshairColor);

            if (!string.IsNullOrEmpty(cameraInfoVarName))
            {
                //rpmComp = RasterPropMonitorComputer.Instantiate(internalProp);
                //if (rpmComp.HasPropVar(cameraInfoVarName + "_ID", internalProp.propID))
                //{
                //    currentCamera = rpmComp.GetPropVar(cameraInfoVarName + "_ID", internalProp.propID) - 1;
                //}
                //else
                //{
                //    rpmComp.SetPropVar(cameraInfoVarName + "_ID", internalProp.propID, currentCamera + 1);
                //}
            }

            if (!string.IsNullOrEmpty(cameraEffectShader))
            {
                cameraEffectMaterial = new Material(JUtil.LoadInternalShader(cameraEffectShader));

                if (!string.IsNullOrEmpty(cameraEffectVariables))
                {
                    try
                    {
                        string[] vars = cameraEffectVariables.Split('|');
                        for (int i = 0; i < vars.Length; ++i)
                        {
                            string[] components = vars[i].Split(',');
                            if (components.Length == 2)
                            {
                                ShaderEffectVariable sev = new ShaderEffectVariable();
                                sev.variable = Shader.PropertyToID(components[0].Trim());
                                sev.value = rpmComp.InstantiateVariableOrNumber(components[1]);
                                ceVariables.Add(sev);
                            }
                        }
                    }
                    catch { }
                }

                if (!string.IsNullOrEmpty(cameraEffectTextures))
                {
                    try
                    {
                        string[] vars = cameraEffectTextures.Split('|');
                        for (int i = 0; i < vars.Length; ++i)
                        {
                            string[] components = vars[i].Split(',');
                            if (components.Length == 2)
                            {
                                Texture tex = GameDatabase.Instance.GetTexture(components[1], false);
                                cameraEffectMaterial.SetTexture(components[0], tex);
                            }
                        }
                    }
                    catch { }
                }
            }

            if (!string.IsNullOrEmpty(cameraPixelSize))
            {
                string[] vars = cameraPixelSize.Split(',');
                if (vars.Length == 2)
                {
                    if (!int.TryParse(vars[0], out rentexWidth) || !int.TryParse(vars[1], out rentexHeight) || rentexHeight < 0 || rentexWidth < 0)
                    {
                        JUtil.LogMessage(this, "Bad image dimensions? {0} and {1}", vars[0], vars[1]);
                        rentexHeight = rentexWidth = 0;
                    }
                    else
                    {
                        JUtil.LogMessage(this, "Setting rentex to {0} x {1}", rentexWidth, rentexHeight);
                    }
                }
            }
        }