Created
October 28, 2018 14:59
-
-
Save olegknyazev/f46c38be91b2929249b457c789dfa6f3 to your computer and use it in GitHub Desktop.
About Formatting: Writing dense code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public Vector3 WallNormal(Vector3 position, bool smoothNormalAtCorners = false) { | |
var clampedPosition = Clamp(position); | |
var positions = | |
_alignmentArea | |
.Where(path => !path.isEmpty) | |
.Select(path => { | |
int nearestSegIndex; | |
Vector2 nearestPoint2D; | |
var side = path.PointSide(Project(position), out nearestPoint2D, out nearestSegIndex); | |
var nearestPoint3D = Unproject(nearestPoint2D); | |
var segDir = path.Segment(nearestSegIndex).direction; | |
return new { | |
nearestPoint = nearestPoint3D, | |
distance = (clampedPosition - nearestPoint3D).magnitude, | |
normal = Unproject(new Vector2(-segDir.y, segDir.x)), | |
side = side | |
}; | |
}); | |
if (!positions.Any()) | |
return Vector3.zero; | |
var result = positions.MinBy(x => x.distance); | |
var pointOnBounds = result.nearestPoint; | |
return | |
smoothNormalAtCorners | |
? (clampedPosition - pointOnBounds).normalized * result.side | |
: result.normal; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment