Skip to content

Commit

Permalink
Merge pull request #53 from dkavolis/unity
Browse files Browse the repository at this point in the history
Unity improvements
  • Loading branch information
dkavolis authored Apr 29, 2019
2 parents f4bd3e3 + 26248a1 commit 5e90954
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public enum OverlayType
int _numGridLines;

Material _rendererMaterial;
private static readonly int colorId = Shader.PropertyToID("_Color");

public static EditorAreaRulingOverlay CreateNewAreaRulingOverlay(Color axisColor, Color crossSectionColor, Color derivColor, double yScaleMaxDistance, double yAxisGridScale)
{
Expand Down Expand Up @@ -167,7 +168,7 @@ LineRenderer CreateNewRenderer(Color color, float width, Material material)
if (material.HasProperty("_Color"))
{
// FARLogger.Info("Setting _Color on " + material + "to " + color);
rendererMaterial.SetColor("_Color", color);
rendererMaterial.SetColor(colorId, color);
}
else
FARLogger.Warning("Material " + material + " has no _Color property");
Expand Down Expand Up @@ -343,10 +344,12 @@ void UpdateRenderer(LineRenderer renderer, Matrix4x4 transformMatrix, double[] x

void UpdateRenderer(LineRenderer renderer, Matrix4x4 transformMatrix, double[] xCoords, double[] yCoords, double yScalingFactor)
{
renderer.transform.parent = EditorLogic.RootPart.partTransform;
renderer.transform.localPosition = Vector3.zero;
renderer.transform.localRotation = Quaternion.identity;
renderer.transform.SetAsFirstSibling();
// getting transform is internal call, cache
Transform transform = renderer.transform;
transform.parent = EditorLogic.RootPart.partTransform;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
transform.SetAsFirstSibling();

renderer.positionCount = xCoords.Length;

Expand Down
6 changes: 4 additions & 2 deletions FerramAerospaceResearch/FARGUI/FAREditorGUI/EditorGUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ private void UpdateGeometryEvent(ConstructionEventType type, Part pEvent)

private void UpdateGeometryModule(ConstructionEventType type, Part p)
{
GeometryPartModule g = p?.GetComponent<GeometryPartModule>();
if (p is null) return;
GeometryPartModule g = p.GetComponent<GeometryPartModule>();
if (g != null && g.Ready)
{
if (type == ConstructionEventType.Unknown)
Expand All @@ -296,7 +297,8 @@ private void UpdateGeometryModule(ConstructionEventType type, Part p)

private void UpdateGeometryModule(Part p)
{
GeometryPartModule g = p?.GetComponent<GeometryPartModule>();
if (p is null) return;
GeometryPartModule g = p.GetComponent<GeometryPartModule>();
if (g != null && g.Ready)
{
g.EditorUpdate();
Expand Down
11 changes: 7 additions & 4 deletions FerramAerospaceResearch/FARGUI/FARFlightGUI/PhysicsCalcs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,17 @@ private void CalculateVesselOrientation(Vector3d velVectorNorm)
{
Transform refTransform = _vessel.ReferenceTransform;

Vector3 tmpVec = refTransform.up * Vector3.Dot(refTransform.up, velVectorNorm) + refTransform.forward * Vector3.Dot(refTransform.forward, velVectorNorm); //velocity vector projected onto a plane that divides the airplane into left and right halves
vesselInfo.aoA = Vector3.Dot(tmpVec.normalized, refTransform.forward);
Vector3 up = refTransform.up;
Vector3 forward = refTransform.forward;
Vector3 right = refTransform.right;
Vector3 tmpVec = up * Vector3.Dot(up, velVectorNorm) + forward * Vector3.Dot(forward, velVectorNorm); //velocity vector projected onto a plane that divides the airplane into left and right halves
vesselInfo.aoA = Vector3.Dot(tmpVec.normalized, forward);
vesselInfo.aoA = FARMathUtil.rad2deg * Math.Asin(vesselInfo.aoA);
if (double.IsNaN(vesselInfo.aoA))
vesselInfo.aoA = 0;

tmpVec = refTransform.up * Vector3.Dot(refTransform.up, velVectorNorm) + refTransform.right * Vector3.Dot(refTransform.right, velVectorNorm); //velocity vector projected onto the vehicle-horizontal plane
vesselInfo.sideslipAngle = Vector3.Dot(tmpVec.normalized, refTransform.right);
tmpVec = up * Vector3.Dot(up, velVectorNorm) + right * Vector3.Dot(right, velVectorNorm); //velocity vector projected onto the vehicle-horizontal plane
vesselInfo.sideslipAngle = Vector3.Dot(tmpVec.normalized, right);
vesselInfo.sideslipAngle = FARMathUtil.rad2deg * Math.Asin(vesselInfo.sideslipAngle);
if (double.IsNaN(vesselInfo.sideslipAngle))
vesselInfo.sideslipAngle = 0;
Expand Down
5 changes: 3 additions & 2 deletions FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -835,8 +835,9 @@ private List<MeshData> CreateMeshListFromTransforms(ref List<Transform> meshTran
return meshList;
}

Bounds rendererBounds = this.part.GetPartOverallMeshBoundsInBasis(part.partTransform.worldToLocalMatrix);
Bounds colliderBounds = this.part.GetPartColliderBoundsInBasis(part.partTransform.worldToLocalMatrix);
var worldToLocalMatrix = part.partTransform.worldToLocalMatrix;
var rendererBounds = part.GetPartOverallMeshBoundsInBasis(worldToLocalMatrix);
var colliderBounds = part.GetPartColliderBoundsInBasis(worldToLocalMatrix);

bool cantUseColliders = true;
bool isFairing = part.Modules.Contains<ModuleProceduralFairing>() || part.Modules.Contains("ProceduralFairingSide");
Expand Down
86 changes: 46 additions & 40 deletions FerramAerospaceResearch/LEGACYferram4/FARControllableSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,19 +376,32 @@ public void CalculateSurfaceFunctions()
if (HighLogic.LoadedSceneIsEditor && (!FlightGlobals.ready || (object)vessel == null || (object)part.partTransform == null))
return;

// use caching to improve performance since all vector properties call into native code
Vector3 partForward = part.partTransform.forward;
Vector3 partPosition = part.partTransform.position;
Vector3 forward, right, up;
if (HighLogic.LoadedSceneIsFlight)
{
forward = vessel.ReferenceTransform.forward;
right = vessel.ReferenceTransform.right;
up = vessel.ReferenceTransform.up;
}
else
{
forward = EditorLogic.RootPart.partTransform.forward;
right = EditorLogic.RootPart.partTransform.right;
up = EditorLogic.RootPart.partTransform.up;
}

if (part.symMethod == SymmetryMethod.Mirror || part.symmetryCounterparts.Count < 1)
{
if (HighLogic.LoadedSceneIsFlight)
flapLocation = Math.Sign(Vector3.Dot(vessel.ReferenceTransform.forward, part.partTransform.forward)); //figure out which way is up
else
flapLocation = Math.Sign(Vector3.Dot(EditorLogic.RootPart.partTransform.forward, part.partTransform.forward)); //figure out which way is up
flapLocation = Math.Sign(Vector3.Dot(forward, partForward));

spoilerLocation = -flapLocation;
}
else if (part.parent != null)
{
flapLocation = Math.Sign(Vector3.Dot(part.partTransform.position - part.parent.partTransform.position, part.partTransform.forward));
flapLocation = Math.Sign(Vector3.Dot(partPosition - part.parent.partTransform.position, partForward));
spoilerLocation = flapLocation;
}
else
Expand All @@ -412,30 +425,15 @@ public void CalculateSurfaceFunctions()
if (HighLogic.LoadedSceneIsEditor && (isFlap || isSpoiler))
SetControlStateEditor(CoM, part.partTransform.up, 0, 0, 0, 0, false);

float roll2 = 0;
if (HighLogic.LoadedSceneIsEditor)
{
Vector3 CoMoffset = (part.partTransform.position - CoM).normalized;
PitchLocation = Vector3.Dot(part.partTransform.forward, EditorLogic.RootPart.partTransform.forward) * Math.Sign(Vector3.Dot(CoMoffset, EditorLogic.RootPart.partTransform.up));
YawLocation = -Vector3.Dot(part.partTransform.forward, EditorLogic.RootPart.partTransform.right) * Math.Sign(Vector3.Dot(CoMoffset, EditorLogic.RootPart.partTransform.up));
RollLocation = Vector3.Dot(part.partTransform.forward, EditorLogic.RootPart.partTransform.forward) * Math.Sign(Vector3.Dot(CoMoffset, -EditorLogic.RootPart.partTransform.right));
roll2 = Vector3.Dot(part.partTransform.forward, EditorLogic.RootPart.partTransform.right) * Math.Sign(Vector3.Dot(CoMoffset, EditorLogic.RootPart.partTransform.forward));
BrakeRudderLocation = Vector3.Dot(part.partTransform.forward, EditorLogic.RootPart.partTransform.forward);
BrakeRudderSide = Math.Sign(Vector3.Dot(CoMoffset, EditorLogic.RootPart.partTransform.right));
AoAsign = Math.Sign(Vector3.Dot(part.partTransform.up, EditorLogic.RootPart.partTransform.up));
}
else
{
//Figures out where the ctrl surface is; this must be done after physics starts to get vessel COM
Vector3 CoMoffset = (part.partTransform.position - CoM).normalized;
PitchLocation = Vector3.Dot(part.partTransform.forward, vessel.ReferenceTransform.forward) * Math.Sign(Vector3.Dot(CoMoffset, vessel.ReferenceTransform.up));
YawLocation = -Vector3.Dot(part.partTransform.forward, vessel.ReferenceTransform.right) * Math.Sign(Vector3.Dot(CoMoffset, vessel.ReferenceTransform.up));
RollLocation = Vector3.Dot(part.partTransform.forward, vessel.ReferenceTransform.forward) * Math.Sign(Vector3.Dot(CoMoffset, -vessel.ReferenceTransform.right));
roll2 = Vector3.Dot(part.partTransform.forward, vessel.ReferenceTransform.right) * Math.Sign(Vector3.Dot(CoMoffset, vessel.ReferenceTransform.forward));
BrakeRudderLocation = Vector3.Dot(part.partTransform.forward, vessel.ReferenceTransform.forward);
BrakeRudderSide = Mathf.Sign(Vector3.Dot(CoMoffset, vessel.ReferenceTransform.right));
AoAsign = Math.Sign(Vector3.Dot(part.partTransform.up, vessel.ReferenceTransform.up));
}
Vector3 CoMoffset = (partPosition - CoM).normalized;
Vector3 partUp = part.partTransform.up;
PitchLocation = Vector3.Dot(partForward, forward) * Math.Sign(Vector3.Dot(CoMoffset, up));
YawLocation = -Vector3.Dot(partForward, right) * Math.Sign(Vector3.Dot(CoMoffset, up));
RollLocation = Vector3.Dot(partForward, forward) * Math.Sign(Vector3.Dot(CoMoffset, -right));
float roll2 = Vector3.Dot(partForward, right) * Math.Sign(Vector3.Dot(CoMoffset, forward));
BrakeRudderLocation = Vector3.Dot(partForward, forward);
BrakeRudderSide = Math.Sign(Vector3.Dot(CoMoffset, right));
AoAsign = Math.Sign(Vector3.Dot(partUp, up));
//PitchLocation *= PitchLocation * Mathf.Sign(PitchLocation);
//YawLocation *= YawLocation * Mathf.Sign(YawLocation);
//RollLocation = RollLocation * RollLocation * Mathf.Sign(RollLocation) + roll2 * roll2 * Mathf.Sign(roll2);
Expand Down Expand Up @@ -616,13 +614,21 @@ public void SetControlStateEditor(Vector3 CoM, Vector3 velocityVec, float pitch,
Transform partTransform = part.partTransform;
Transform rootTransform = EditorLogic.RootPart.partTransform;

Vector3 CoMoffset = (partTransform.position - CoM);
PitchLocation = Vector3.Dot(partTransform.forward, rootTransform.forward) * Math.Sign(Vector3.Dot(CoMoffset, rootTransform.up));
YawLocation = -Vector3.Dot(partTransform.forward, rootTransform.right) * Math.Sign(Vector3.Dot(CoMoffset, rootTransform.up));
RollLocation = Vector3.Dot(partTransform.forward, rootTransform.forward) * Math.Sign(Vector3.Dot(CoMoffset, -rootTransform.right));
BrakeRudderLocation = Vector3.Dot(partTransform.forward, rootTransform.forward);
BrakeRudderSide = Mathf.Sign(Vector3.Dot(CoMoffset, rootTransform.right));
AoAsign = Math.Sign(Vector3.Dot(partTransform.up, rootTransform.up));
// cache transform vectors
Vector3 partPosition = partTransform.position;
Vector3 CoMoffset = partPosition - CoM;

Vector3 partForward = partTransform.forward;
Vector3 forward = rootTransform.forward;
Vector3 up = rootTransform.up;
Vector3 right = rootTransform.right;

PitchLocation = Vector3.Dot(partForward, forward) * Math.Sign(Vector3.Dot(CoMoffset, up));
YawLocation = -Vector3.Dot(partForward, right) * Math.Sign(Vector3.Dot(CoMoffset, up));
RollLocation = Vector3.Dot(partForward, forward) * Math.Sign(Vector3.Dot(CoMoffset, -right));
BrakeRudderLocation = Vector3.Dot(partForward, forward);
BrakeRudderSide = Mathf.Sign(Vector3.Dot(CoMoffset, right));
AoAsign = Math.Sign(Vector3.Dot(partTransform.up, up));
AoAdesiredControl = 0;
if (pitchaxis != 0.0)
{
Expand All @@ -643,7 +649,7 @@ public void SetControlStateEditor(Vector3 CoM, Vector3 velocityVec, float pitch,
AoAdesiredControl *= maxdeflect;
if (pitchaxisDueToAoA != 0.0)
{
Vector3 tmpVec = rootTransform.up * Vector3.Dot(rootTransform.up, velocityVec) + rootTransform.forward * Vector3.Dot(rootTransform.forward, velocityVec); //velocity vector projected onto a plane that divides the airplane into left and right halves
Vector3 tmpVec = up * Vector3.Dot(up, velocityVec) + forward * Vector3.Dot(forward, velocityVec); //velocity vector projected onto a plane that divides the airplane into left and right halves
double AoA = base.CalculateAoA(tmpVec.normalized); //using base.CalculateAoA gets the deflection using WingAeroModel's code, which does not account for deflection; this gives us the AoA that the surface _would_ be at if it hadn't deflected at all.
AoA = FARMathUtil.rad2deg * AoA;
if (double.IsNaN(AoA))
Expand All @@ -659,15 +665,15 @@ public void SetControlStateEditor(Vector3 CoM, Vector3 velocityVec, float pitch,
if (part.symMethod == SymmetryMethod.Mirror || part.symmetryCounterparts.Count < 1)
{
if (HighLogic.LoadedSceneIsFlight)
flapLocation = Math.Sign(Vector3.Dot(vessel.ReferenceTransform.forward, part.partTransform.forward)); //figure out which way is up
flapLocation = Math.Sign(Vector3.Dot(vessel.ReferenceTransform.forward, partForward)); //figure out which way is up
else
flapLocation = Math.Sign(Vector3.Dot(EditorLogic.RootPart.partTransform.forward, part.partTransform.forward)); //figure out which way is up
flapLocation = Math.Sign(Vector3.Dot(EditorLogic.RootPart.partTransform.forward, partForward)); //figure out which way is up

spoilerLocation = -flapLocation;
}
else if (part.parent != null)
{
flapLocation = Math.Sign(Vector3.Dot(part.partTransform.position - part.parent.partTransform.position, part.partTransform.forward));
flapLocation = Math.Sign(Vector3.Dot(partPosition - part.parent.partTransform.position, partForward));
spoilerLocation = flapLocation;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -656,8 +656,9 @@ public virtual void FixedUpdate()
//This accounts for the effect of flap effects only being handled by the rearward surface
scaledForce *= S / (S + wingInteraction.EffectiveUpstreamArea);

double forwardScaledForceMag = Vector3d.Dot(scaledForce, part_transform.forward);
Vector3d forwardScaledForce = forwardScaledForceMag * (Vector3d)part_transform.forward;
Vector3 forward = part_transform.forward;
double forwardScaledForceMag = Vector3d.Dot(scaledForce, forward);
Vector3d forwardScaledForce = forwardScaledForceMag * (Vector3d)forward;

if (Math.Abs(forwardScaledForceMag) > YmaxForce * failureForceScaling * (1 + part.submergedPortion * 1000) || (scaledForce - forwardScaledForce).magnitude > XZmaxForce * failureForceScaling * (1 + part.submergedPortion * 1000))
if (part.parent && !vessel.packed)
Expand Down
Loading

0 comments on commit 5e90954

Please sign in to comment.