internal void Update(RPMVesselComputer comp) { float value; if (enablingVariable != null) { if (!enablingVariable.IsInRange(comp)) { return; } } if (variable.Get(out value, comp)) { if (useLog10) { value = JUtil.PseudoLog10(value); } float xOffset = JUtil.DualLerp(textureLimit, scale, value); MeshFilter meshFilter = barObject.GetComponent <MeshFilter>(); meshFilter.mesh.uv = new[] { new Vector2(xOffset - textureSize, 0.0f), new Vector2(xOffset + textureSize, 0.0f), new Vector2(xOffset - textureSize, 1.0f), new Vector2(xOffset + textureSize, 1.0f) }; JUtil.ShowHide(true, barObject); } }
internal void Update() { if (enablingVariable != null) { if (!enablingVariable.IsInRange()) { return; } } float value = variable.AsFloat(); if (useLog10) { value = JUtil.PseudoLog10(value); } float yOffset = JUtil.DualLerp(textureLimit, scale, value); MeshFilter meshFilter = barObject.GetComponent <MeshFilter>(); meshFilter.mesh.uv = new[] { new Vector2(0.0f, yOffset - textureSize), new Vector2(1.0f, yOffset - textureSize), new Vector2(0.0f, yOffset + textureSize), new Vector2(1.0f, yOffset + textureSize) }; JUtil.ShowHide(true, barObject); }
internal HorizontalBar(ConfigNode node, float screenWidth, float screenHeight, int drawingLayer, Shader displayShader, GameObject cameraBody) { JUtil.LogMessage(this, "Configuring for {0}", node.GetValue("name")); if (!node.HasValue("variableName")) { throw new Exception("HorizontalBar " + node.GetValue("name") + " missing variableName"); } variable = VariableOrNumber.Instantiate(node.GetValue("variableName")); if (!node.HasValue("texture")) { throw new Exception("HorizontalBar " + node.GetValue("name") + " missing texture"); } Texture2D tex = GameDatabase.Instance.GetTexture(node.GetValue("texture"), false); if (tex == null) { throw new Exception("HorizontalBar " + node.GetValue("name") + " texture " + node.GetValue("texture") + " can't be loaded."); } tex.wrapMode = TextureWrapMode.Clamp; if (node.HasValue("useLog10") && bool.TryParse(node.GetValue("useLog10"), out useLog10) == false) { // I think this is redundant useLog10 = false; } if (!node.HasValue("scale")) { throw new Exception("HorizontalBar " + node.GetValue("name") + " missing scale"); } scale = ConfigNode.ParseVector2(node.GetValue("scale")); if (useLog10) { scale.x = JUtil.PseudoLog10(scale.x); scale.y = JUtil.PseudoLog10(scale.y); } if (!node.HasValue("textureSize")) { throw new Exception("HorizontalBar " + node.GetValue("name") + " missing textureSize"); } if (!float.TryParse(node.GetValue("textureSize"), out textureSize)) { throw new Exception("HorizontalBar " + node.GetValue("name") + " failed parsing textureSize"); } textureSize = 0.5f * textureSize / (float)tex.width; if (!node.HasValue("textureLimit")) { throw new Exception("HorizontalBar " + node.GetValue("name") + " missing textureLimit"); } textureLimit = ConfigNode.ParseVector2(node.GetValue("textureLimit")); textureLimit.x = 1.0f - textureLimit.x / (float)tex.width; textureLimit.y = 1.0f - textureLimit.y / (float)tex.width; if (!node.HasValue("position")) { throw new Exception("HorizontalBar " + node.GetValue("name") + " missing position"); } Vector4 position = ConfigNode.ParseVector4(node.GetValue("position")); if (node.HasValue("enablingVariable") && node.HasValue("enablingVariableRange")) { string[] range = node.GetValue("enablingVariableRange").Split(','); if (range.Length != 2) { throw new Exception("HorizontalBar " + node.GetValue("name") + " has an invalid enablingVariableRange"); } enablingVariable = new VariableOrNumberRange(node.GetValue("enablingVariable").Trim(), range[0].Trim(), range[1].Trim()); } barObject = JUtil.CreateSimplePlane("HorizontalBar" + node.GetValue("name"), new Vector2(0.5f * position.z, 0.5f * position.w), new Rect(0.0f, 0.0f, 1.0f, 1.0f), drawingLayer); Material barMaterial = new Material(displayShader); barMaterial.color = Color.white; barMaterial.mainTexture = tex; // Position in camera space has (0, 0) in the center, so we need to // translate everything appropriately. Y is odd since the coordinates // supplied are Left-Handed (0Y on top, growing down), not RH. barObject.transform.position = new Vector3(position.x + 0.5f * (position.z - screenWidth), 0.5f * (screenHeight - position.w) - position.y, 1.4f); barObject.GetComponent <Renderer>().material = barMaterial; barObject.transform.parent = cameraBody.transform; JUtil.ShowHide(true, barObject); }
public bool RenderHUD(RenderTexture screen, float cameraAspect) { if (screen == null || !startupComplete || HighLogic.LoadedSceneIsEditor) { return(false); } // Clear the background, if configured. GL.Clear(true, true, backgroundColorValue); // Configure the camera, if configured. // MOARdV: Might be worthwhile to refactor the flying camera so // it is created in Start (like new FlyingCamera(part, cameraTransform)), // and pass the screen, FoV, and aspect ratio (or just screen and // FoV) as Render parameters, so there's no need to test if the // camera's been created every render call. if (cameraObject == null && !string.IsNullOrEmpty(cameraTransform)) { cameraObject = new FlyingCamera(part, screen, cameraAspect); cameraObject.PointCamera(cameraTransform, hudFov); } // Draw the camera's view, if configured. if (cameraObject != null) { cameraObject.Render(); } // Configure the matrix so that the origin is the center of the screen. GL.PushMatrix(); // Draw the HUD ladder // MOARdV note, 2014/03/19: swapping the y values, to invert the // coordinates so the prograde icon is right-side up. GL.LoadPixelMatrix(-horizonSize.x * 0.5f, horizonSize.x * 0.5f, horizonSize.y * 0.5f, -horizonSize.y * 0.5f); GL.Viewport(new Rect((screen.width - horizonSize.x) * 0.5f, (screen.height - horizonSize.y) * 0.5f, horizonSize.x, horizonSize.y)); Vector3 coM = vessel.findWorldCenterOfMass(); Vector3 up = (coM - vessel.mainBody.position).normalized; Vector3 forward = vessel.GetTransform().up; Vector3 right = vessel.GetTransform().right; Vector3 top = Vector3.Cross(right, forward); Vector3 north = Vector3.Exclude(up, (vessel.mainBody.position + (Vector3d)vessel.mainBody.transform.up * vessel.mainBody.Radius) - coM).normalized; Vector3d velocityVesselSurface = vessel.orbit.GetVel() - vessel.mainBody.getRFrmVel(coM); Vector3 velocityVesselSurfaceUnit = velocityVesselSurface.normalized; if (ladderMaterial) { // Figure out the texture coordinate scaling for the ladder. float ladderTextureOffset = horizonTextureSize.y / ladderMaterial.mainTexture.height; float cosUp = Vector3.Dot(forward, up); float cosRoll = Vector3.Dot(top, up); float sinRoll = Vector3.Dot(right, up); var normalizedRoll = new Vector2(cosRoll, sinRoll); normalizedRoll.Normalize(); if (normalizedRoll.magnitude < 0.99f) { // If we're hitting +/- 90 nearly perfectly, the sin and cos will // be too far out of whack to normalize. Arbitrarily pick // a roll of 0.0. normalizedRoll.x = 1.0f; normalizedRoll.y = 0.0f; } cosRoll = normalizedRoll.x; sinRoll = normalizedRoll.y; // Mihara: I'm pretty sure this was negative of what it should actually be, at least according to my mockup. float pitch = -(Mathf.Asin(cosUp) * Mathf.Rad2Deg); float ladderMidpointCoord; if (use360horizon) { // Straight up is texture coord 0.75; // Straight down is TC 0.25; ladderMidpointCoord = JUtil.DualLerp(0.25f, 0.75f, -90f, 90f, pitch); } else { // Straight up is texture coord 1.0; // Straight down is TC 0.0; ladderMidpointCoord = JUtil.DualLerp(0.0f, 1.0f, -90f, 90f, pitch); } ladderMaterial.SetPass(0); GL.Begin(GL.QUADS); // transform -x -y GL.TexCoord2(0.5f + horizonTextureSize.x, ladderMidpointCoord - ladderTextureOffset); GL.Vertex3(cosRoll * horizonSize.x + sinRoll * horizonSize.y, -sinRoll * horizonSize.x + cosRoll * horizonSize.y, 0.0f); // transform +x -y GL.TexCoord2(0.5f - horizonTextureSize.x, ladderMidpointCoord - ladderTextureOffset); GL.Vertex3(-cosRoll * horizonSize.x + sinRoll * horizonSize.y, sinRoll * horizonSize.x + cosRoll * horizonSize.y, 0.0f); // transform +x +y GL.TexCoord2(0.5f - horizonTextureSize.x, ladderMidpointCoord + ladderTextureOffset); GL.Vertex3(-cosRoll * horizonSize.x - sinRoll * horizonSize.y, sinRoll * horizonSize.x - cosRoll * horizonSize.y, 0.0f); // transform -x +y GL.TexCoord2(0.5f + horizonTextureSize.x, ladderMidpointCoord + ladderTextureOffset); GL.Vertex3(cosRoll * horizonSize.x - sinRoll * horizonSize.y, -sinRoll * horizonSize.x - cosRoll * horizonSize.y, 0.0f); GL.End(); float AoA = velocityVesselSurfaceUnit.AngleInPlane(right, forward); float AoATC; if (use360horizon) { // Straight up is texture coord 0.75; // Straight down is TC 0.25; AoATC = JUtil.DualLerp(0.25f, 0.75f, -90f, 90f, pitch + AoA); } else { // Straight up is texture coord 1.0; // Straight down is TC 0.0; AoATC = JUtil.DualLerp(0.0f, 1.0f, -90f, 90f, pitch + AoA); } float Ypos = JUtil.DualLerp( -horizonSize.y, horizonSize.y, ladderMidpointCoord - ladderTextureOffset, ladderMidpointCoord + ladderTextureOffset, AoATC); // Placing the icon on the (0, Ypos) location, so simplify the transform. DrawIcon(-sinRoll * Ypos, -cosRoll * Ypos, GizmoIcons.GetIconLocation(GizmoIcons.IconType.PROGRADE), progradeColorValue); } // Draw the rest of the HUD stuff (0,0) is the top left corner of the screen. GL.LoadPixelMatrix(0, screen.width, screen.height, 0); GL.Viewport(new Rect(0, 0, screen.width, screen.height)); if (headingMaterial != null) { Quaternion rotationSurface = Quaternion.LookRotation(north, up); Quaternion rotationVesselSurface = Quaternion.Inverse(Quaternion.Euler(90, 0, 0) * Quaternion.Inverse(vessel.GetTransform().rotation) * rotationSurface); float headingTexture = JUtil.DualLerp(0f, 1f, 0f, 360f, rotationVesselSurface.eulerAngles.y); float headingTextureOffset = (headingBarWidth / headingMaterial.mainTexture.width) / 2; headingMaterial.SetPass(0); GL.Begin(GL.QUADS); GL.TexCoord2(headingTexture - headingTextureOffset, 1.0f); GL.Vertex3(headingBarPosition.x, headingBarPosition.y, 0.0f); GL.TexCoord2(headingTexture + headingTextureOffset, 1.0f); GL.Vertex3(headingBarPosition.x + headingBarPosition.z, headingBarPosition.y, 0.0f); GL.TexCoord2(headingTexture + headingTextureOffset, 0.0f); GL.Vertex3(headingBarPosition.x + headingBarPosition.z, headingBarPosition.y + headingBarPosition.w, 0.0f); GL.TexCoord2(headingTexture - headingTextureOffset, 0.0f); GL.Vertex3(headingBarPosition.x, headingBarPosition.y + headingBarPosition.w, 0.0f); GL.End(); if (showHeadingBarPrograde) { float slipAngle = velocityVesselSurfaceUnit.AngleInPlane(up, forward); float slipTC = JUtil.DualLerp(0f, 1f, 0f, 360f, rotationVesselSurface.eulerAngles.y + slipAngle); float slipIconX = JUtil.DualLerp(headingBarPosition.x, headingBarPosition.x + headingBarPosition.z, headingTexture - headingTextureOffset, headingTexture + headingTextureOffset, slipTC); DrawIcon(slipIconX, headingBarPosition.y + headingBarPosition.w * 0.5f, GizmoIcons.GetIconLocation(GizmoIcons.IconType.PROGRADE), progradeColorValue); } } if (vertBar1Material != null) { float value = comp.ProcessVariable(vertBar1Variable).MassageToFloat(); if (float.IsNaN(value)) { value = 0.0f; } if (vertBar1UseLog10) { value = JUtil.PseudoLog10(value); } float vertBar1TexCoord = JUtil.DualLerp(vertBar1TextureLimit.x, vertBar1TextureLimit.y, vertBar1Limit.x, vertBar1Limit.y, value); vertBar1Material.SetPass(0); GL.Begin(GL.QUADS); GL.TexCoord2(0.0f, vertBar1TexCoord + vertBar1TextureSize); GL.Vertex3(vertBar1Position.x, vertBar1Position.y, 0.0f); GL.TexCoord2(1.0f, vertBar1TexCoord + vertBar1TextureSize); GL.Vertex3(vertBar1Position.x + vertBar1Position.z, vertBar1Position.y, 0.0f); GL.TexCoord2(1.0f, vertBar1TexCoord - vertBar1TextureSize); GL.Vertex3(vertBar1Position.x + vertBar1Position.z, vertBar1Position.y + vertBar1Position.w, 0.0f); GL.TexCoord2(0.0f, vertBar1TexCoord - vertBar1TextureSize); GL.Vertex3(vertBar1Position.x, vertBar1Position.y + vertBar1Position.w, 0.0f); GL.End(); } if (vertBar2Material != null) { float value = comp.ProcessVariable(vertBar2Variable).MassageToFloat(); if (float.IsNaN(value)) { value = 0.0f; } if (vertBar2UseLog10) { value = JUtil.PseudoLog10(value); } float vertBar2TexCoord = JUtil.DualLerp(vertBar2TextureLimit.x, vertBar2TextureLimit.y, vertBar2Limit.x, vertBar2Limit.y, value); vertBar2Material.SetPass(0); GL.Begin(GL.QUADS); GL.TexCoord2(0.0f, vertBar2TexCoord + vertBar2TextureSize); GL.Vertex3(vertBar2Position.x, vertBar2Position.y, 0.0f); GL.TexCoord2(1.0f, vertBar2TexCoord + vertBar2TextureSize); GL.Vertex3(vertBar2Position.x + vertBar2Position.z, vertBar2Position.y, 0.0f); GL.TexCoord2(1.0f, vertBar2TexCoord - vertBar2TextureSize); GL.Vertex3(vertBar2Position.x + vertBar2Position.z, vertBar2Position.y + vertBar2Position.w, 0.0f); GL.TexCoord2(0.0f, vertBar2TexCoord - vertBar2TextureSize); GL.Vertex3(vertBar2Position.x, vertBar2Position.y + vertBar2Position.w, 0.0f); GL.End(); } if (overlayMaterial != null) { overlayMaterial.SetPass(0); GL.Begin(GL.QUADS); GL.TexCoord2(0.0f, 1.0f); GL.Vertex3(0.0f, 0.0f, 0.0f); GL.TexCoord2(1.0f, 1.0f); GL.Vertex3(screen.width, 0.0f, 0.0f); GL.TexCoord2(1.0f, 0.0f); GL.Vertex3(screen.width, screen.height, 0.0f); GL.TexCoord2(0.0f, 0.0f); GL.Vertex3(0.0f, screen.height, 0.0f); GL.End(); } GL.PopMatrix(); return(true); }
public void Start() { if (HighLogic.LoadedSceneIsEditor) { return; } try { backgroundColorValue = ConfigNode.ParseColor32(backgroundColor); Shader unlit = Shader.Find("Hidden/Internal-GUITexture"); ladderMaterial = new Material(unlit); ladderMaterial.color = new Color(0.5f, 0.5f, 0.5f, 1.0f); if (!String.IsNullOrEmpty(horizonTexture)) { ladderMaterial.mainTexture = GameDatabase.Instance.GetTexture(horizonTexture.EnforceSlashes(), false); if (ladderMaterial.mainTexture != null) { horizonTextureSize.x = horizonTextureSize.x / ladderMaterial.mainTexture.width; ladderMaterial.mainTexture.wrapMode = TextureWrapMode.Clamp; } } if (!String.IsNullOrEmpty(headingBar)) { headingMaterial = new Material(unlit); headingMaterial.color = new Color(0.5f, 0.5f, 0.5f, 1.0f); headingMaterial.mainTexture = GameDatabase.Instance.GetTexture(headingBar.EnforceSlashes(), false); } if (!String.IsNullOrEmpty(staticOverlay)) { overlayMaterial = new Material(unlit); overlayMaterial.color = new Color(0.5f, 0.5f, 0.5f, 1.0f); overlayMaterial.mainTexture = GameDatabase.Instance.GetTexture(staticOverlay.EnforceSlashes(), false); } if (!String.IsNullOrEmpty(vertBar1Texture) && !String.IsNullOrEmpty(vertBar1Variable)) { vertBar1Material = new Material(unlit); vertBar1Material.color = new Color(0.5f, 0.5f, 0.5f, 1.0f); vertBar1Material.mainTexture = GameDatabase.Instance.GetTexture(vertBar1Texture.EnforceSlashes(), false); if (vertBar1Material.mainTexture != null) { float height = (float)vertBar1Material.mainTexture.height; vertBar1TextureLimit.x = 1.0f - (vertBar1TextureLimit.x / height); vertBar1TextureLimit.y = 1.0f - (vertBar1TextureLimit.y / height); vertBar1TextureSize = 0.5f * (vertBar1TextureSize / height); vertBar1Material.mainTexture.wrapMode = TextureWrapMode.Clamp; } } if (!String.IsNullOrEmpty(vertBar2Texture) && !String.IsNullOrEmpty(vertBar2Variable)) { vertBar2Material = new Material(unlit); vertBar2Material.color = new Color(0.5f, 0.5f, 0.5f, 1.0f); vertBar2Material.mainTexture = GameDatabase.Instance.GetTexture(vertBar2Texture.EnforceSlashes(), false); if (vertBar2Material.mainTexture != null) { float height = (float)vertBar2Material.mainTexture.height; vertBar2TextureLimit.x = 1.0f - (vertBar2TextureLimit.x / height); vertBar2TextureLimit.y = 1.0f - (vertBar2TextureLimit.y / height); vertBar2TextureSize = 0.5f * (vertBar2TextureSize / height); vertBar2Material.mainTexture.wrapMode = TextureWrapMode.Clamp; } } if (vertBar1UseLog10) { vertBar1Limit.x = JUtil.PseudoLog10(vertBar1Limit.x); vertBar1Limit.y = JUtil.PseudoLog10(vertBar1Limit.y); } if (vertBar2UseLog10) { vertBar2Limit.x = JUtil.PseudoLog10(vertBar2Limit.x); vertBar2Limit.y = JUtil.PseudoLog10(vertBar2Limit.y); } if (!string.IsNullOrEmpty(progradeColor)) { progradeColorValue = ConfigNode.ParseColor32(progradeColor); } comp = RasterPropMonitorComputer.Instantiate(internalProp); iconMaterial = new Material(unlit); iconMaterial.color = new Color(0.5f, 0.5f, 0.5f, 1.0f); gizmoTexture = JUtil.GetGizmoTexture(); startupComplete = true; } catch { JUtil.AnnoyUser(this); throw; } }
public void Start() { if (HighLogic.LoadedSceneIsEditor) { return; } try { backgroundColorValue = ConfigNode.ParseColor32(backgroundColor); foreach (ConfigNode propNode in GameDatabase.Instance.GetConfigNodes("PROP")) { ConfigNode[] moduleNodes = propNode.GetNodes("MODULE"); foreach (ConfigNode moduleNode in moduleNodes) { ConfigNode[] pageNodes = moduleNode.GetNodes("PAGE"); foreach (ConfigNode pagenode in pageNodes) { ConfigNode[] bghandlerNodes = pagenode.GetNodes("BACKGROUNDHANDLER"); foreach (ConfigNode bghandlerNode in bghandlerNodes) { ConfigNode[] gaugeNodes = bghandlerNode.GetNodes("GAUGE"); foreach (ConfigNode node in gaugeNodes) { InfoGauge gauge = new InfoGauge(); JUtil.LogErrorMessage(this, "one item created"); gauge.Texture = node.GetValue("Texture"); JUtil.LogErrorMessage(this, "texture obtained"); gauge.Position = ConfigNode.ParseVector4(node.GetValue("Position")); JUtil.LogErrorMessage(this, "position obtained"); gauge.Limit = ConfigNode.ParseVector2(node.GetValue("Limit")); JUtil.LogErrorMessage(this, "limit obtained"); gauge.TextureLimit = ConfigNode.ParseVector4(node.GetValue("TextureLimit")); JUtil.LogErrorMessage(this, "texlim obtained"); gauge.TextureSize = float.Parse(node.GetValue("TextureSize")); JUtil.LogErrorMessage(this, "texsiz obtained"); gauge.VerticalMovement = bool.Parse(node.GetValue("VerticalMovement")); JUtil.LogErrorMessage(this, "vertmov obtained"); gauge.UseLog10 = bool.Parse(node.GetValue("UseLog10")); JUtil.LogErrorMessage(this, "uselog obtained"); gauge.Use360Horizon = bool.Parse(node.GetValue("Use360Horizon")); JUtil.LogErrorMessage(this, "360 obtained"); gauge.RotateWithVessel = bool.Parse(node.GetValue("RotateWithVessel")); JUtil.LogErrorMessage(this, "rotate obtained"); gauge.Variable = node.GetValue("Variable"); JUtil.LogErrorMessage(this, "variable obtained"); gaugeList.Add(gauge); JUtil.LogErrorMessage(this, "one item added"); } } } } } Shader unlit = Shader.Find("Hidden/Internal-GUITexture"); foreach (InfoGauge gauge in gaugeList) { if (!String.IsNullOrEmpty(gauge.Texture)) { gauge.Material = new Material(unlit); gauge.Material.color = new Color(0.0f, 0.0f, 0.0f, 0.0f); gauge.Material.mainTexture = GameDatabase.Instance.GetTexture(gauge.Texture.EnforceSlashes(), false); } if (gauge.Material.mainTexture != null) { float height = (float)gauge.Material.mainTexture.height; float width = (float)gauge.Material.mainTexture.width; gauge.TextureLimit.x = 1.0f - (gauge.TextureLimit.x / width); gauge.TextureLimit.y = 1.0f - (gauge.TextureLimit.y / width); gauge.TextureLimit.z = 1.0f - (gauge.TextureLimit.z / height); gauge.TextureLimit.w = 1.0f - (gauge.TextureLimit.w / height); gauge.TextureSize = 0.5f * (gauge.TextureSize / height);//实际调用的时候用的是宽和高的一半。 if (gauge.Use360Horizon) { gauge.Material.mainTexture.wrapMode = TextureWrapMode.Repeat; } else { gauge.Material.mainTexture.wrapMode = TextureWrapMode.Clamp; } } if (gauge.UseLog10) { gauge.Limit.x = JUtil.PseudoLog10(gauge.Limit.x); gauge.Limit.y = JUtil.PseudoLog10(gauge.Limit.y); } } JUtil.LogMessage(this, "material read"); comp = RasterPropMonitorComputer.Instantiate(internalProp); startupComplete = true; } catch { JUtil.AnnoyUser(this); throw; } }