| | | 1 | | //======================================================================= |
| | | 2 | | // LSMeshCollider.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2026–present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using FixedMathSharp; |
| | | 9 | | using FixedMathSharp.Geometry; |
| | | 10 | | using Gravitas.Queries; |
| | | 11 | | using SwiftCollections; |
| | | 12 | | using SwiftCollections.Query; |
| | | 13 | | using System; |
| | | 14 | | using System.Runtime.CompilerServices; |
| | | 15 | | |
| | | 16 | | namespace Gravitas.Colliders; |
| | | 17 | | |
| | | 18 | | /// <summary>Represents a runtime 3D collider backed by deterministic triangle-mesh geometry.</summary> |
| | | 19 | | public sealed class LSMeshCollider : LSCollider |
| | | 20 | | { |
| | 324 | 21 | | private readonly SwiftList<int> _triangleQueryBuffer = new(); |
| | | 22 | | |
| | | 23 | | /// <inheritdoc/> |
| | 1225 | 24 | | public override ColliderType Shape => ColliderType.Mesh; |
| | | 25 | | |
| | | 26 | | /// <inheritdoc/> |
| | 270 | 27 | | public override int Priority => ColliderSettings.GetPriority(Shape); |
| | | 28 | | |
| | | 29 | | /// <inheritdoc/> |
| | 35 | 30 | | public override Fixed64 Area => Mesh.TotalArea; |
| | | 31 | | |
| | | 32 | | /// <summary>Gets the deterministic runtime mesh geometry.</summary> |
| | | 33 | | public PhysicsMesh Mesh { get; private set; } |
| | | 34 | | |
| | | 35 | | /// <summary>Gets the mesh collision mode.</summary> |
| | 1751 | 36 | | public MeshColliderMode Mode => Mesh.Mode; |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Gets whether the mesh's exact-position-welded triangle topology is one |
| | | 40 | | /// connected, consistently wound, closed two-manifold surface. |
| | | 41 | | /// </summary> |
| | 1 | 42 | | public bool IsClosedSurface => Mesh.IsClosedSurface; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Determines how inertia is derived when this mesh collider is attached to a body with angular dynamics. |
| | | 46 | | /// </summary> |
| | | 47 | | public MeshInertiaPolicy InertiaPolicy { get; } |
| | | 48 | | |
| | | 49 | | /// <summary>Creates a convex mesh collider from authored vertices and triangle indices.</summary> |
| | | 50 | | public LSMeshCollider(Vector3d[] vertices, int[] triangles) |
| | 3 | 51 | | : this(vertices, triangles, MeshColliderMode.Convex, MeshInertiaPolicy.RequireClosedVolume) |
| | | 52 | | { |
| | 3 | 53 | | } |
| | | 54 | | |
| | | 55 | | /// <summary>Creates a mesh collider with an explicit collision mode.</summary> |
| | | 56 | | public LSMeshCollider(Vector3d[] vertices, int[] triangles, MeshColliderMode mode) |
| | 2 | 57 | | : this(vertices, triangles, mode, MeshInertiaPolicy.RequireClosedVolume) |
| | | 58 | | { |
| | 2 | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary>Creates a mesh collider with explicit collision and inertia policies.</summary> |
| | 324 | 62 | | public LSMeshCollider( |
| | 324 | 63 | | Vector3d[] vertices, |
| | 324 | 64 | | int[] triangles, |
| | 324 | 65 | | MeshColliderMode mode, |
| | 324 | 66 | | MeshInertiaPolicy inertiaPolicy) |
| | | 67 | | { |
| | 324 | 68 | | ValidateInertiaPolicy(inertiaPolicy); |
| | 324 | 69 | | InertiaPolicy = inertiaPolicy; |
| | 324 | 70 | | Mesh = new PhysicsMesh(vertices, triangles, Vector3d.Zero, FixedQuaternion.Identity, mode); |
| | 324 | 71 | | _offset = Mesh.LocalBounds.Center; |
| | 324 | 72 | | Mesh.UpdatePosition(_offset, FixedQuaternion.Identity); |
| | 324 | 73 | | _size = Mesh.LocalBounds.Proportions; |
| | 324 | 74 | | _radius = _size.Magnitude * Fixed64.Half; |
| | 324 | 75 | | _bounds = Mesh.Bounds; |
| | 324 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary>Creates a runtime convex mesh from an authored shape definition.</summary> |
| | | 79 | | public LSMeshCollider(ColliderShapeDefinition definition) |
| | 31 | 80 | | : this( |
| | 31 | 81 | | GetVertices(definition), |
| | 31 | 82 | | GetTriangles(definition), |
| | 31 | 83 | | MeshColliderMode.Convex, |
| | 31 | 84 | | GetInertiaPolicy(definition)) |
| | | 85 | | { |
| | 31 | 86 | | Material = definition.Material; |
| | 31 | 87 | | } |
| | | 88 | | |
| | | 89 | | /// <inheritdoc/> |
| | | 90 | | public override Fixed64 ScaledRadius |
| | | 91 | | { |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 93 | | get |
| | | 94 | | { |
| | 17 | 95 | | return Mesh.ScaledLocalRadius; |
| | | 96 | | } |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private protected override void PrepareShape(in ColliderShapeSnapshot snapshot) |
| | | 100 | | { |
| | 292 | 101 | | Mesh.PrepareTransformation( |
| | 292 | 102 | | snapshot.Center, |
| | 292 | 103 | | snapshot.Rotation, |
| | 292 | 104 | | snapshot.OwnerScale, |
| | 292 | 105 | | snapshot.PartScale, |
| | 292 | 106 | | InertiaPolicy); |
| | 286 | 107 | | if (CompoundOwner == null |
| | 286 | 108 | | && !CalculatePreparedLocalMassPoint().TryGetPoint(out _)) |
| | | 109 | | { |
| | 1 | 110 | | throw new InvalidOperationException( |
| | 1 | 111 | | "Prepared collider mass-property point is outside the Fixed64 coordinate domain."); |
| | | 112 | | } |
| | 285 | 113 | | SetPreparedBounds(Mesh.PreparedBounds); |
| | 285 | 114 | | } |
| | | 115 | | |
| | | 116 | | private protected override void PublishShape() => |
| | 284 | 117 | | Mesh.PublishPreparedTransformation(); |
| | | 118 | | |
| | | 119 | | internal override ExactMassWeight CalculateMassPropertyWeight() |
| | | 120 | | { |
| | 45 | 121 | | if (InertiaPolicy == MeshInertiaPolicy.SurfaceApproximation) |
| | 20 | 122 | | return Mesh.SurfaceMassWeight; |
| | | 123 | | |
| | 25 | 124 | | if (Mesh.TryGetClosedVolumeMassProperties(out MeshMassProperties properties, out _)) |
| | 20 | 125 | | return ExactMassWeight.FromMeasure(properties.Volume); |
| | | 126 | | |
| | 5 | 127 | | return ExactMassWeight.Zero; |
| | | 128 | | } |
| | | 129 | | |
| | | 130 | | internal override ExactMassWeight CalculatePreparedMassPropertyWeight() |
| | | 131 | | { |
| | 72 | 132 | | if (InertiaPolicy == MeshInertiaPolicy.SurfaceApproximation) |
| | 22 | 133 | | return Mesh.PreparedSurfaceMassWeight; |
| | | 134 | | |
| | 50 | 135 | | if (Mesh.TryGetPreparedClosedVolumeMassProperties( |
| | 50 | 136 | | out MeshMassProperties properties, |
| | 50 | 137 | | out _)) |
| | | 138 | | { |
| | 8 | 139 | | return ExactMassWeight.FromMeasure(properties.Volume); |
| | | 140 | | } |
| | | 141 | | |
| | 42 | 142 | | return ExactMassWeight.Zero; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | internal override bool SupportsMassProperties => |
| | 23 | 146 | | InertiaPolicy == MeshInertiaPolicy.SurfaceApproximation |
| | 23 | 147 | | || Mesh.TryGetClosedVolumeMassProperties(out _, out _); |
| | | 148 | | |
| | | 149 | | internal override ExactMassPoint3D CalculateLocalMassPoint() |
| | | 150 | | { |
| | | 151 | | Vector3d meshCenterOfMass; |
| | 373 | 152 | | if (InertiaPolicy == MeshInertiaPolicy.SurfaceApproximation) |
| | | 153 | | { |
| | 262 | 154 | | meshCenterOfMass = |
| | 262 | 155 | | Mesh.SurfaceMassProperties.CenterOfMass; |
| | | 156 | | } |
| | 111 | 157 | | else if (Mesh.TryGetClosedVolumeMassProperties( |
| | 111 | 158 | | out MeshMassProperties properties, |
| | 111 | 159 | | out _)) |
| | | 160 | | { |
| | 83 | 161 | | meshCenterOfMass = properties.CenterOfMass; |
| | | 162 | | } |
| | | 163 | | else |
| | | 164 | | { |
| | 28 | 165 | | meshCenterOfMass = Vector3d.Zero; |
| | | 166 | | } |
| | | 167 | | |
| | 373 | 168 | | return TransformRelativeMassPropertyPointExact( |
| | 373 | 169 | | meshCenterOfMass); |
| | | 170 | | } |
| | | 171 | | |
| | | 172 | | internal override ExactMassPoint3D CalculatePreparedLocalMassPoint() |
| | | 173 | | { |
| | | 174 | | Vector3d meshCenterOfMass; |
| | 495 | 175 | | if (InertiaPolicy == MeshInertiaPolicy.SurfaceApproximation) |
| | | 176 | | { |
| | 337 | 177 | | meshCenterOfMass = |
| | 337 | 178 | | Mesh.PreparedSurfaceMassProperties.CenterOfMass; |
| | | 179 | | } |
| | 158 | 180 | | else if (Mesh.TryGetPreparedClosedVolumeMassProperties( |
| | 158 | 181 | | out MeshMassProperties properties, |
| | 158 | 182 | | out _)) |
| | | 183 | | { |
| | 22 | 184 | | meshCenterOfMass = properties.CenterOfMass; |
| | | 185 | | } |
| | | 186 | | else |
| | | 187 | | { |
| | 136 | 188 | | meshCenterOfMass = Vector3d.Zero; |
| | | 189 | | } |
| | | 190 | | |
| | 495 | 191 | | return TransformPreparedRelativeMassPropertyPointExact( |
| | 495 | 192 | | meshCenterOfMass); |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | internal override Fixed3x3 CalculateCenterOfMassInertiaTensor( |
| | | 196 | | Fixed64 mass) |
| | | 197 | | { |
| | | 198 | | Vector3d center; |
| | 153 | 199 | | if (InertiaPolicy == MeshInertiaPolicy.SurfaceApproximation) |
| | | 200 | | { |
| | 117 | 201 | | center = Mesh.SurfaceMassProperties.CenterOfMass; |
| | | 202 | | } |
| | 36 | 203 | | else if (Mesh.TryGetClosedVolumeMassProperties( |
| | 36 | 204 | | out MeshMassProperties properties, |
| | 36 | 205 | | out _)) |
| | | 206 | | { |
| | 35 | 207 | | center = properties.CenterOfMass; |
| | | 208 | | } |
| | | 209 | | else |
| | | 210 | | { |
| | 1 | 211 | | center = Vector3d.Zero; |
| | | 212 | | } |
| | | 213 | | |
| | 153 | 214 | | return Mesh.CalculateInertiaTensor( |
| | 153 | 215 | | mass, |
| | 153 | 216 | | InertiaPolicy, |
| | 153 | 217 | | center); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <inheritdoc/> |
| | | 221 | | public override Fixed64 GetFrontalArea(Vector3d direction) => |
| | 6 | 222 | | Mesh.GetFrontalArea(direction); |
| | | 223 | | |
| | | 224 | | /// <summary>Writes triangle indices near a world-space point into a caller-owned buffer.</summary> |
| | | 225 | | public void GetNearbyTriangles(Vector3d queryPoint, SwiftList<int> result) |
| | | 226 | | { |
| | 1 | 227 | | FixedBoundVolume queryBounds = CreateQueryBounds(queryPoint, GetMeshQueryHalfExtent()); |
| | 1 | 228 | | GetTrianglesInBounds(queryBounds, result); |
| | 1 | 229 | | } |
| | | 230 | | |
| | | 231 | | /// <summary>Writes triangle indices overlapping world-space bounds into a caller-owned buffer.</summary> |
| | | 232 | | public void GetTrianglesInBounds(FixedBoundVolume queryBounds, SwiftList<int> result) |
| | | 233 | | { |
| | 2337 | 234 | | Mesh.GetTrianglesInWorldBounds(queryBounds, result); |
| | 2337 | 235 | | } |
| | | 236 | | |
| | | 237 | | /// <inheritdoc/> |
| | | 238 | | public override Vector3d ClosestPointOnSurface(Vector3d queryPoint) |
| | | 239 | | { |
| | 19 | 240 | | FixedPointAnchor closest = |
| | 19 | 241 | | GetClosestSurfaceAnchor(queryPoint, out _); |
| | 19 | 242 | | if (closest.TryGetPoint(out Vector3d point)) |
| | 18 | 243 | | return point; |
| | | 244 | | |
| | 1 | 245 | | throw new InvalidOperationException( |
| | 1 | 246 | | "The closest mesh point is outside the representable coordinate range."); |
| | | 247 | | } |
| | | 248 | | |
| | | 249 | | internal override FixedPointAnchor GetClosestSurfaceAnchor( |
| | | 250 | | Vector3d queryPoint, |
| | | 251 | | out Vector3d normal) |
| | | 252 | | { |
| | 164 | 253 | | FindClosestPointAnchor( |
| | 164 | 254 | | queryPoint, |
| | 164 | 255 | | _triangleQueryBuffer, |
| | 164 | 256 | | out FixedPointAnchor closest, |
| | 164 | 257 | | out normal); |
| | 164 | 258 | | return closest; |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | internal void FindClosestPointAnchor( |
| | | 262 | | Vector3d queryPoint, |
| | | 263 | | out FixedPointAnchor closest, |
| | | 264 | | out Vector3d normal) => |
| | 51 | 265 | | FindClosestPointAnchor( |
| | 51 | 266 | | queryPoint, |
| | 51 | 267 | | _triangleQueryBuffer, |
| | 51 | 268 | | out closest, |
| | 51 | 269 | | out normal); |
| | | 270 | | |
| | | 271 | | internal void FindClosestPointAnchor( |
| | | 272 | | Vector3d queryPoint, |
| | | 273 | | SwiftList<int> triangleBuffer, |
| | | 274 | | out FixedPointAnchor closest, |
| | | 275 | | out Vector3d normal) |
| | | 276 | | { |
| | 217 | 277 | | if (!Mesh.TryConvertWorldToScaledLocal( |
| | 217 | 278 | | queryPoint, |
| | 217 | 279 | | out Vector3d localQueryPoint)) |
| | | 280 | | { |
| | 4 | 281 | | FindClosestPointWithoutLocalQuery( |
| | 4 | 282 | | queryPoint, |
| | 4 | 283 | | out closest, |
| | 4 | 284 | | out normal); |
| | 4 | 285 | | return; |
| | | 286 | | } |
| | | 287 | | |
| | 213 | 288 | | Vector3d localClosest = localQueryPoint; |
| | 213 | 289 | | normal = Vector3d.Zero; |
| | 213 | 290 | | int closestTriangleIndex = -1; |
| | 213 | 291 | | _ = KeepClosestPointOnTriangle( |
| | 213 | 292 | | 0, |
| | 213 | 293 | | localQueryPoint, |
| | 213 | 294 | | ref closestTriangleIndex, |
| | 213 | 295 | | ref localClosest, |
| | 213 | 296 | | ref normal); |
| | 213 | 297 | | if (localClosest == localQueryPoint) |
| | | 298 | | { |
| | 52 | 299 | | closest = Mesh.CreatePointAnchor(localClosest); |
| | 52 | 300 | | normal = Mesh.Rotation.Rotate(normal).Normalized; |
| | 52 | 301 | | return; |
| | | 302 | | } |
| | | 303 | | |
| | 161 | 304 | | if (!TryCreateClosestPointSearchBounds( |
| | 161 | 305 | | localQueryPoint, |
| | 161 | 306 | | localClosest, |
| | 161 | 307 | | out FixedBoundVolume queryBounds)) |
| | | 308 | | { |
| | 2 | 309 | | FindClosestPointAcrossAllTriangles( |
| | 2 | 310 | | localQueryPoint, |
| | 2 | 311 | | 1, |
| | 2 | 312 | | ref closestTriangleIndex, |
| | 2 | 313 | | ref localClosest, |
| | 2 | 314 | | ref normal); |
| | | 315 | | } |
| | | 316 | | else |
| | | 317 | | { |
| | 159 | 318 | | Mesh.GetTrianglesInLocalBounds(queryBounds, triangleBuffer); |
| | 2764 | 319 | | for (int i = 0; i < triangleBuffer.Count; i++) |
| | | 320 | | { |
| | 1223 | 321 | | _ = KeepClosestPointOnTriangle( |
| | 1223 | 322 | | triangleBuffer[i], |
| | 1223 | 323 | | localQueryPoint, |
| | 1223 | 324 | | ref closestTriangleIndex, |
| | 1223 | 325 | | ref localClosest, |
| | 1223 | 326 | | ref normal); |
| | | 327 | | } |
| | | 328 | | } |
| | | 329 | | |
| | 161 | 330 | | closest = Mesh.CreatePointAnchor(localClosest); |
| | 161 | 331 | | normal = Mesh.Rotation.Rotate(normal).Normalized; |
| | 161 | 332 | | } |
| | | 333 | | |
| | | 334 | | private void FindClosestPointWithoutLocalQuery( |
| | | 335 | | Vector3d queryPoint, |
| | | 336 | | out FixedPointAnchor closest, |
| | | 337 | | out Vector3d normal) |
| | | 338 | | { |
| | 4 | 339 | | var queryAnchor = new FixedPointAnchor( |
| | 4 | 340 | | queryPoint, |
| | 4 | 341 | | FixedQuaternion.Identity, |
| | 4 | 342 | | Vector3d.Zero); |
| | 4 | 343 | | closest = GetClosestPointAnchorOnTriangle( |
| | 4 | 344 | | 0, |
| | 4 | 345 | | queryAnchor, |
| | 4 | 346 | | out normal); |
| | 4 | 347 | | for (int triangleIndex = 1; |
| | 6 | 348 | | triangleIndex < Mesh.TriangleCount; |
| | 2 | 349 | | triangleIndex++) |
| | | 350 | | { |
| | 2 | 351 | | FixedPointAnchor candidate = GetClosestPointAnchorOnTriangle( |
| | 2 | 352 | | triangleIndex, |
| | 2 | 353 | | queryAnchor, |
| | 2 | 354 | | out Vector3d candidateNormal); |
| | 2 | 355 | | int comparison = |
| | 2 | 356 | | queryAnchor.CompareSquaredDistance(candidate, closest); |
| | 2 | 357 | | if (comparison >= 0) |
| | | 358 | | { |
| | | 359 | | continue; |
| | | 360 | | } |
| | | 361 | | |
| | 1 | 362 | | closest = candidate; |
| | 1 | 363 | | normal = candidateNormal; |
| | | 364 | | } |
| | | 365 | | |
| | 4 | 366 | | } |
| | | 367 | | |
| | | 368 | | private FixedPointAnchor GetClosestPointAnchorOnTriangle( |
| | | 369 | | int triangleIndex, |
| | | 370 | | in FixedPointAnchor query, |
| | | 371 | | out Vector3d normal) |
| | | 372 | | { |
| | 6 | 373 | | Mesh.GetLocalTriangleVertices( |
| | 6 | 374 | | triangleIndex, |
| | 6 | 375 | | out Vector3d first, |
| | 6 | 376 | | out Vector3d second, |
| | 6 | 377 | | out Vector3d third); |
| | 6 | 378 | | var triangle = new FixedTriangle(first, second, third); |
| | 6 | 379 | | FixedPointAnchor closest = triangle.GetClosestPointAnchor( |
| | 6 | 380 | | Mesh.Origin, |
| | 6 | 381 | | Mesh.Rotation, |
| | 6 | 382 | | query); |
| | 6 | 383 | | normal = OrientNormalTowardPoint( |
| | 6 | 384 | | Mesh.GetFaceNormalWorld(triangleIndex), |
| | 6 | 385 | | query, |
| | 6 | 386 | | closest); |
| | 6 | 387 | | return closest; |
| | | 388 | | } |
| | | 389 | | |
| | | 390 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 391 | | private Fixed64 GetMeshQueryHalfExtent() |
| | | 392 | | { |
| | 1 | 393 | | Vector3d extents = |
| | 1 | 394 | | ColliderCanonicalBounds.GetMaximumAbsoluteExtents( |
| | 1 | 395 | | this, |
| | 1 | 396 | | Mesh.Origin); |
| | 1 | 397 | | return FixedMath.Max( |
| | 1 | 398 | | FixedMath.Max(extents.X, extents.Y), |
| | 1 | 399 | | extents.Z); |
| | | 400 | | } |
| | | 401 | | |
| | | 402 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 403 | | private static FixedBoundVolume CreateQueryBounds(Vector3d center, Fixed64 halfExtent) |
| | | 404 | | { |
| | 1 | 405 | | Vector3d extents = Vector3d.One * halfExtent; |
| | 1 | 406 | | return new FixedBoundVolume(center - extents, center + extents); |
| | | 407 | | } |
| | | 408 | | |
| | | 409 | | private static void ValidateInertiaPolicy(MeshInertiaPolicy inertiaPolicy) |
| | | 410 | | { |
| | 324 | 411 | | SwiftThrowHelper.ThrowIfArgument( |
| | 324 | 412 | | inertiaPolicy != MeshInertiaPolicy.RequireClosedVolume && |
| | 324 | 413 | | inertiaPolicy != MeshInertiaPolicy.SurfaceApproximation, |
| | 324 | 414 | | nameof(inertiaPolicy), |
| | 324 | 415 | | "Unsupported mesh inertia policy."); |
| | 324 | 416 | | } |
| | | 417 | | |
| | | 418 | | private static Vector3d[] GetVertices(ColliderShapeDefinition definition) |
| | | 419 | | { |
| | 31 | 420 | | definition.EnsureKind(ColliderShapeDefinitionKind.ConvexMesh); |
| | 31 | 421 | | return definition.GetMeshVerticesForRuntime(); |
| | | 422 | | } |
| | | 423 | | |
| | | 424 | | private static int[] GetTriangles(ColliderShapeDefinition definition) |
| | | 425 | | { |
| | 31 | 426 | | definition.EnsureKind(ColliderShapeDefinitionKind.ConvexMesh); |
| | 31 | 427 | | return definition.GetMeshTrianglesForRuntime(); |
| | | 428 | | } |
| | | 429 | | |
| | | 430 | | private static MeshInertiaPolicy GetInertiaPolicy(ColliderShapeDefinition definition) |
| | | 431 | | { |
| | 31 | 432 | | definition.EnsureKind(ColliderShapeDefinitionKind.ConvexMesh); |
| | 31 | 433 | | return definition.MeshInertiaPolicy; |
| | | 434 | | } |
| | | 435 | | |
| | | 436 | | private static bool TryCreateClosestPointSearchBounds( |
| | | 437 | | Vector3d point, |
| | | 438 | | Vector3d closest, |
| | | 439 | | out FixedBoundVolume bounds) |
| | | 440 | | { |
| | 161 | 441 | | if (!Vector3d.TrySubtract(point, closest, out Vector3d delta) |
| | 161 | 442 | | || !delta.TryGetMagnitudeCeiling(out Fixed64 halfExtent) |
| | 161 | 443 | | || !Fixed64.TryAdd( |
| | 161 | 444 | | halfExtent, |
| | 161 | 445 | | Fixed64.Epsilon, |
| | 161 | 446 | | out halfExtent)) |
| | | 447 | | { |
| | 2 | 448 | | bounds = default; |
| | 2 | 449 | | return false; |
| | | 450 | | } |
| | | 451 | | |
| | 159 | 452 | | Vector3d extents = Vector3d.One * halfExtent; |
| | 159 | 453 | | Vector3d min = point - extents; |
| | 159 | 454 | | Vector3d max = point + extents; |
| | 159 | 455 | | bounds = new FixedBoundVolume(min, max); |
| | 159 | 456 | | return true; |
| | | 457 | | } |
| | | 458 | | |
| | | 459 | | private void FindClosestPointAcrossAllTriangles( |
| | | 460 | | Vector3d point, |
| | | 461 | | int startTriangleIndex, |
| | | 462 | | ref int closestTriangleIndex, |
| | | 463 | | ref Vector3d closest, |
| | | 464 | | ref Vector3d normal) |
| | | 465 | | { |
| | 6 | 466 | | for (int i = startTriangleIndex; i < Mesh.TriangleCount; i++) |
| | | 467 | | { |
| | 1 | 468 | | _ = KeepClosestPointOnTriangle( |
| | 1 | 469 | | i, |
| | 1 | 470 | | point, |
| | 1 | 471 | | ref closestTriangleIndex, |
| | 1 | 472 | | ref closest, |
| | 1 | 473 | | ref normal); |
| | | 474 | | } |
| | 2 | 475 | | } |
| | | 476 | | |
| | | 477 | | private bool KeepClosestPointOnTriangle( |
| | | 478 | | int triangleIndex, |
| | | 479 | | Vector3d point, |
| | | 480 | | ref int closestTriangleIndex, |
| | | 481 | | ref Vector3d closest, |
| | | 482 | | ref Vector3d normal) |
| | | 483 | | { |
| | 1437 | 484 | | Mesh.GetLocalTriangleVertices( |
| | 1437 | 485 | | triangleIndex, |
| | 1437 | 486 | | out Vector3d first, |
| | 1437 | 487 | | out Vector3d second, |
| | 1437 | 488 | | out Vector3d third); |
| | 1437 | 489 | | var triangle = new FixedTriangle(first, second, third); |
| | 1437 | 490 | | Vector3d faceNormal = triangle.Normal; |
| | 1437 | 491 | | Vector3d pointOnTriangle = triangle.ClosestPoint(point); |
| | 1437 | 492 | | if (closestTriangleIndex >= 0) |
| | | 493 | | { |
| | 1224 | 494 | | int distanceComparison = Vector3d.CompareDistanceSquared( |
| | 1224 | 495 | | point, |
| | 1224 | 496 | | pointOnTriangle, |
| | 1224 | 497 | | point, |
| | 1224 | 498 | | closest); |
| | 1224 | 499 | | if (distanceComparison > 0 |
| | 1224 | 500 | | || (distanceComparison == 0 && triangleIndex >= closestTriangleIndex)) |
| | | 501 | | { |
| | 941 | 502 | | return false; |
| | | 503 | | } |
| | | 504 | | } |
| | | 505 | | |
| | 496 | 506 | | closestTriangleIndex = triangleIndex; |
| | 496 | 507 | | closest = pointOnTriangle; |
| | 496 | 508 | | normal = OrientNormalTowardPoint(faceNormal, point - pointOnTriangle); |
| | 496 | 509 | | return true; |
| | | 510 | | } |
| | | 511 | | |
| | | 512 | | /// <inheritdoc/> |
| | | 513 | | public override Vector3d GetNormalAtPoint(Vector3d point) |
| | | 514 | | { |
| | 143 | 515 | | _ = GetClosestSurfaceAnchor( |
| | 143 | 516 | | point, |
| | 143 | 517 | | out Vector3d normal); |
| | 143 | 518 | | return normal; |
| | | 519 | | } |
| | | 520 | | |
| | | 521 | | internal override bool TryGetPlanarSurfaceNormal(Vector3d point, out Vector3d normal) |
| | | 522 | | { |
| | 2 | 523 | | FindClosestPointAnchor( |
| | 2 | 524 | | point, |
| | 2 | 525 | | _triangleQueryBuffer, |
| | 2 | 526 | | out _, |
| | 2 | 527 | | out normal); |
| | 2 | 528 | | return true; |
| | | 529 | | } |
| | | 530 | | |
| | | 531 | | /// <inheritdoc/> |
| | | 532 | | public override bool ColliderOverlapsRay(RaycastSegmentWorker worker, ref SwiftList<Vector3d> outputIntersectionPoin |
| | | 533 | | { |
| | 397 | 534 | | return worker.CheckMeshOverlaps(this, ref outputIntersectionPoints); |
| | | 535 | | } |
| | | 536 | | |
| | | 537 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 538 | | private static Vector3d OrientNormalTowardPoint(Vector3d normal, Vector3d targetDirection) |
| | | 539 | | { |
| | 496 | 540 | | if (targetDirection.MagnitudeSquared <= Fixed64.Epsilon) |
| | 211 | 541 | | return normal; |
| | | 542 | | |
| | 285 | 543 | | return Vector3d.Dot(normal, targetDirection) < Fixed64.Zero ? -normal : normal; |
| | | 544 | | } |
| | | 545 | | |
| | | 546 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 547 | | private static Vector3d OrientNormalTowardPoint( |
| | | 548 | | Vector3d normal, |
| | | 549 | | in FixedPointAnchor target, |
| | | 550 | | in FixedPointAnchor surfacePoint) |
| | | 551 | | { |
| | 6 | 552 | | if (target.ProjectNonNegativeOffsetFrom(surfacePoint, normal) |
| | 6 | 553 | | > Fixed64.Zero) |
| | | 554 | | { |
| | 2 | 555 | | return normal; |
| | | 556 | | } |
| | | 557 | | |
| | 4 | 558 | | return surfacePoint.ProjectNonNegativeOffsetFrom(target, normal) |
| | 4 | 559 | | > Fixed64.Zero |
| | 4 | 560 | | ? -normal |
| | 4 | 561 | | : normal; |
| | | 562 | | } |
| | | 563 | | } |