Skip to content

Commit

Permalink
use local bounds for drag cubes
Browse files Browse the repository at this point in the history
  • Loading branch information
dkavolis committed Jun 18, 2021
1 parent 242e713 commit 5f0e911
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
13 changes: 9 additions & 4 deletions FerramAerospaceResearch/FARPartGeometry/GeometryMesh.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ public class GeometryMesh
private readonly Vector3[] meshLocalVerts;
public readonly int[] triangles;
public readonly Transform meshTransform;
public readonly Transform partTransform;
public readonly Part part;
private readonly GeometryPartModule module;
public readonly bool isSkinned;
public readonly Bounds meshLocalBounds;

public readonly int invertXYZ;

Expand All @@ -65,6 +67,7 @@ public class GeometryMesh

public Matrix4x4 thisToVesselMatrix;
public Matrix4x4 meshLocalToWorld;
public Matrix4x4 meshLocalToPart;
public Bounds bounds;
public bool valid;

Expand All @@ -75,13 +78,16 @@ public GeometryMesh(
GeometryPartModule module
)
{
this.module = module;
part = module.part;
meshLocalVerts = meshData.vertices;
triangles = meshData.triangles;
isSkinned = meshData.isSkinned;
Bounds meshBounds = meshData.bounds;

vertices = new Vector3[meshLocalVerts.Length];
this.meshTransform = meshTransform;
partTransform = part.transform;
UpdateLocalToWorldMatrix();
thisToVesselMatrix = worldToVesselMatrix * meshLocalToWorld;

Expand Down Expand Up @@ -110,6 +116,7 @@ GeometryPartModule module

gameObjectActiveInHierarchy = meshTransform.gameObject.activeInHierarchy;

meshLocalBounds = meshBounds;
bounds = TransformBounds(meshBounds, thisToVesselMatrix);

float tmpTestBounds = bounds.center.x +
Expand All @@ -128,9 +135,6 @@ GeometryPartModule module
valid = true;
}

this.module = module;
part = module.part;

if (!module.part.isMirrored)
invertXYZ = 1;
else
Expand All @@ -154,6 +158,7 @@ private void UpdateLocalToWorldMatrix()
meshLocalToWorld = !isSkinned
? meshTransform.localToWorldMatrix
: Matrix4x4.TRS(meshTransform.position, meshTransform.rotation, Vector3.one);
meshLocalToPart = partTransform.worldToLocalMatrix * meshLocalToWorld;
}

public void TransformBasis(Matrix4x4 newThisToVesselMatrix)
Expand Down Expand Up @@ -208,7 +213,7 @@ public void MultithreadTransformBasis(object newThisToVesselMatrixObj)
}
}

private static Bounds TransformBounds(Bounds oldBounds, Matrix4x4 matrix)
public static Bounds TransformBounds(Bounds oldBounds, Matrix4x4 matrix)
{
Vector3 center = oldBounds.center;
Vector3 extents = oldBounds.extents;
Expand Down
17 changes: 12 additions & 5 deletions FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public class GeometryPartModule : PartModule, IRescalable<GeometryPartModule>
public Rigidbody partRigidBody;

public Bounds overallMeshBounds;
public Bounds localMeshBounds;

public List<GeometryMesh> meshDataList;
private List<IGeometryUpdater> geometryUpdaters;
Expand Down Expand Up @@ -279,14 +280,14 @@ private void FixedUpdate()
{
if (!_ready && _meshesToUpdate == 0)
{
overallMeshBounds = SetBoundsFromMeshes();
(localMeshBounds, overallMeshBounds) = SetBoundsFromMeshes();

// @DRVeyl: Force all cubes to have the same bounds. Do this any time you recalculate a mesh
// (ie when handling animations since this breaks the cubes for anything other than the *current* cube.)
foreach (DragCube cube in part.DragCubes.Cubes)
{
cube.Size = overallMeshBounds.size;
cube.Center = overallMeshBounds.center;
cube.Size = localMeshBounds.size;
cube.Center = localMeshBounds.center;
}

part.DragCubes.ForceUpdate(true, true);
Expand Down Expand Up @@ -367,7 +368,7 @@ private bool IgnoredPredicate(Transform t)
return true;
}

private Bounds SetBoundsFromMeshes()
private (Bounds, Bounds) SetBoundsFromMeshes()
{
if (meshDataList.Count == 0)
{
Expand All @@ -377,16 +378,22 @@ private Bounds SetBoundsFromMeshes()
}

Vector3 upper = Vector3.one * float.NegativeInfinity, lower = Vector3.one * float.PositiveInfinity;
Vector3 upperLocal = Vector3.one * float.NegativeInfinity, lowerLocal = Vector3.one * float.PositiveInfinity;
foreach (GeometryMesh geoMesh in meshDataList)
{
if (!geoMesh.valid)
continue;

upper = Vector3.Max(upper, geoMesh.bounds.max);
lower = Vector3.Min(lower, geoMesh.bounds.min);

Bounds meshBounds = GeometryMesh.TransformBounds(geoMesh.meshLocalBounds, geoMesh.meshLocalToPart);
upperLocal = Vector3.Max(upperLocal, meshBounds.max);
lowerLocal = Vector3.Min(lowerLocal, meshBounds.min);
}

var overallBounds = new Bounds((upper + lower) * 0.5f, upper - lower);
var localBounds = new Bounds((upperLocal + lowerLocal) * 0.5f, upperLocal - lowerLocal);

float tmpTestBounds = overallBounds.center.x +
overallBounds.center.y +
Expand All @@ -404,7 +411,7 @@ private Bounds SetBoundsFromMeshes()
Valid = true;
}

return overallBounds;
return (localBounds, overallBounds);
}

private void GetAnimations()
Expand Down
Binary file not shown.
Binary file not shown.
Binary file modified GameData/FerramAerospaceResearch/Plugins/ferramGraph.dll
Binary file not shown.

0 comments on commit 5f0e911

Please sign in to comment.