Skip to content

Commit 75bcb75

Browse files
committed
use local bounds for drag cubes
1 parent 242e713 commit 75bcb75

5 files changed

Lines changed: 19 additions & 9 deletions

File tree

FerramAerospaceResearch/FARPartGeometry/GeometryMesh.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public class GeometryMesh
5757
public readonly Part part;
5858
private readonly GeometryPartModule module;
5959
public readonly bool isSkinned;
60+
public readonly Bounds meshLocalBounds;
6061

6162
public readonly int invertXYZ;
6263

@@ -65,6 +66,7 @@ public class GeometryMesh
6566

6667
public Matrix4x4 thisToVesselMatrix;
6768
public Matrix4x4 meshLocalToWorld;
69+
public Matrix4x4 meshLocalToPart;
6870
public Bounds bounds;
6971
public bool valid;
7072

@@ -75,6 +77,8 @@ public GeometryMesh(
7577
GeometryPartModule module
7678
)
7779
{
80+
this.module = module;
81+
part = module.part;
7882
meshLocalVerts = meshData.vertices;
7983
triangles = meshData.triangles;
8084
isSkinned = meshData.isSkinned;
@@ -110,6 +114,7 @@ GeometryPartModule module
110114

111115
gameObjectActiveInHierarchy = meshTransform.gameObject.activeInHierarchy;
112116

117+
meshLocalBounds = meshBounds;
113118
bounds = TransformBounds(meshBounds, thisToVesselMatrix);
114119

115120
float tmpTestBounds = bounds.center.x +
@@ -128,9 +133,6 @@ GeometryPartModule module
128133
valid = true;
129134
}
130135

131-
this.module = module;
132-
part = module.part;
133-
134136
if (!module.part.isMirrored)
135137
invertXYZ = 1;
136138
else
@@ -154,6 +156,7 @@ private void UpdateLocalToWorldMatrix()
154156
meshLocalToWorld = !isSkinned
155157
? meshTransform.localToWorldMatrix
156158
: Matrix4x4.TRS(meshTransform.position, meshTransform.rotation, Vector3.one);
159+
meshLocalToPart = part.transform.worldToLocalMatrix * meshLocalToWorld;
157160
}
158161

159162
public void TransformBasis(Matrix4x4 newThisToVesselMatrix)
@@ -208,7 +211,7 @@ public void MultithreadTransformBasis(object newThisToVesselMatrixObj)
208211
}
209212
}
210213

211-
private static Bounds TransformBounds(Bounds oldBounds, Matrix4x4 matrix)
214+
public static Bounds TransformBounds(Bounds oldBounds, Matrix4x4 matrix)
212215
{
213216
Vector3 center = oldBounds.center;
214217
Vector3 extents = oldBounds.extents;

FerramAerospaceResearch/FARPartGeometry/GeometryPartModule.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public class GeometryPartModule : PartModule, IRescalable<GeometryPartModule>
6969
public Rigidbody partRigidBody;
7070

7171
public Bounds overallMeshBounds;
72+
public Bounds localMeshBounds;
7273

7374
public List<GeometryMesh> meshDataList;
7475
private List<IGeometryUpdater> geometryUpdaters;
@@ -279,14 +280,14 @@ private void FixedUpdate()
279280
{
280281
if (!_ready && _meshesToUpdate == 0)
281282
{
282-
overallMeshBounds = SetBoundsFromMeshes();
283+
(localMeshBounds, overallMeshBounds) = SetBoundsFromMeshes();
283284

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

292293
part.DragCubes.ForceUpdate(true, true);
@@ -367,7 +368,7 @@ private bool IgnoredPredicate(Transform t)
367368
return true;
368369
}
369370

370-
private Bounds SetBoundsFromMeshes()
371+
private (Bounds, Bounds) SetBoundsFromMeshes()
371372
{
372373
if (meshDataList.Count == 0)
373374
{
@@ -377,16 +378,22 @@ private Bounds SetBoundsFromMeshes()
377378
}
378379

379380
Vector3 upper = Vector3.one * float.NegativeInfinity, lower = Vector3.one * float.PositiveInfinity;
381+
Vector3 upperLocal = Vector3.one * float.NegativeInfinity, lowerLocal = Vector3.one * float.PositiveInfinity;
380382
foreach (GeometryMesh geoMesh in meshDataList)
381383
{
382384
if (!geoMesh.valid)
383385
continue;
384386

385387
upper = Vector3.Max(upper, geoMesh.bounds.max);
386388
lower = Vector3.Min(lower, geoMesh.bounds.min);
389+
390+
Bounds meshBounds = GeometryMesh.TransformBounds(geoMesh.meshLocalBounds, geoMesh.meshLocalToPart);
391+
upperLocal = Vector3.Max(upperLocal, meshBounds.max);
392+
lowerLocal = Vector3.Max(lowerLocal, meshBounds.min);
387393
}
388394

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

391398
float tmpTestBounds = overallBounds.center.x +
392399
overallBounds.center.y +
@@ -404,7 +411,7 @@ private Bounds SetBoundsFromMeshes()
404411
Valid = true;
405412
}
406413

407-
return overallBounds;
414+
return (localBounds, overallBounds);
408415
}
409416

410417
private void GetAnimations()
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)