本文整理汇总了C#中UnityEngine.Bounds.SqrDistance方法的典型用法代码示例。如果您正苦于以下问题:C# Bounds.SqrDistance方法的具体用法?C# Bounds.SqrDistance怎么用?C# Bounds.SqrDistance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UnityEngine.Bounds
的用法示例。
在下文中一共展示了Bounds.SqrDistance方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CustomCull
/// <summary>
/// Checks to see if object is inside the view frustum of any of the LOS cameras.
/// Ideally should be called in OnWillRenderObject, but it's to late to disable renderer..
/// Early outs when visible to one camera.
/// </summary>
private static bool CustomCull(Bounds meshBounds, int layerMask)
{
//Get list of cube sources
List<LOSSourceCube> losCubeSources = LOSManager.Instance.LOSSourcesCube;
for (int i = 0; i < losCubeSources.Count; ++i)
{
LOSSourceCube losSourceCube = losCubeSources[i];
Camera currentCamera = losSourceCube.SourceCamera;
if (losSourceCube.IsVisible && currentCamera != null)
{
float squaredDistance = currentCamera.farClipPlane * currentCamera.farClipPlane;
if (meshBounds.SqrDistance(losSourceCube.transform.position) <= squaredDistance)
{
if (CheckRayCast(currentCamera, meshBounds, layerMask))
return true;
}
}
}
// Get list of sources.
List<LOSSource> losSources = LOSManager.Instance.LOSSources;
for (int i = 0; i < losSources.Count; ++i)
{
LOSSource losSource = losSources[i];
Camera currentCamera = losSource.SourceCamera;
if (losSource.IsVisible && currentCamera != null)
{
Plane[] cameraPlanes = losSource.FrustumPlanes;
if (GeometryUtility.TestPlanesAABB(cameraPlanes, meshBounds))
{
if (CheckRayCast(currentCamera, meshBounds, layerMask))
return true;
}
}
}
return false;
}
示例2: AvoideVessel
public static bool AvoideVessel(VesselWrapper vsl, Vector3d dir, float dist, Vector3d dV,
float r2, Bounds exhaust2, Transform refT2,
out Vector3d maneuver, float threshold = -1)
{
maneuver = Vector3d.zero;
//filter vessels on non-collision courses
var dVn = dV.normalized;
var cosA = Mathf.Clamp(Vector3.Dot(dir, dVn), -1, 1);
var collision_dist = threshold.Equals(0)?
2*dist-vsl.Geometry.DistToBounds(vsl.Physics.wCoM+dir*dist)-
Mathf.Sqrt(exhaust2.SqrDistance(refT2.InverseTransformPoint(vsl.Physics.wCoM))) :
Mathf.Max(vsl.Geometry.R, dist-vsl.Geometry.DistToBounds(vsl.Physics.wCoM+dir*dist)) +
Mathf.Max(r2, dist-Mathf.Sqrt(exhaust2.SqrDistance(refT2.InverseTransformPoint(vsl.Physics.wCoM))));
// vsl.Log("R {}, R2 {}; r {}, r2 {}", vsl.Geometry.R, r2,
// dist-vsl.DistToBounds(vsl.wCoM+dir*dist),
// dist-Mathf.Sqrt(exhaust2.SqrDistance(refT2.InverseTransformPoint(vsl.wCoM))));//debug
// Utils.Log("{}: cosA: {}, threshold {}", vsl.vessel.vesselName, cosA, threshold);//debug
if(cosA <= 0) goto check_distance;
if(threshold < 0) threshold = CPS.MinDistance;
var sinA = Mathf.Sqrt(1-cosA*cosA);
var min_separation = dist*sinA;
var sep_threshold = collision_dist+threshold;
// Utils.Log("{}: min_sep > sep_thresh: {} > {}, threshold {}",
// vsl.vessel.vesselName, min_separation, sep_threshold, threshold);//debug
if(min_separation > sep_threshold) goto check_distance;
//calculate time to collision
var vDist = 0f;
var alpha = Mathf.Acos(cosA);
var dVm = dV.magnitude;
if(sinA <= 0) vDist = dist-sep_threshold;
else if(dist > sep_threshold) vDist = sep_threshold*Mathf.Sin(Mathf.Asin(min_separation/sep_threshold)-alpha)/sinA;
var vTime = Utils.ClampL(vDist, 0.1f)/dVm;
// Utils.Log("{}: vTime > SafeTime: {} > {}",
// vsl.vessel.vesselName, vTime, CPS.SafeTime/Utils.Clamp(Mathf.Abs(Vector3.Dot(vsl.wMaxAngularA, dVn)), 0.01f, 1f));//debug
if(vTime > SafeTime(vsl, dVn)) goto check_distance;
//calculate maneuver
Vector3d side;
if(cosA < 0.9) side = (dVn*cosA-dir).normalized;
else if(Math.Abs(Vector3d.Dot(dVn, vsl.Physics.Up)) < 0.9)
side = Vector3d.Cross(dVn, vsl.Physics.Up).normalized;
else side = Vector3d.Cross(dVn, vsl.OnPlanetParams.Fwd).normalized;
// if(dist > sep_threshold)
// {
// var beta = Mathf.Asin(sep_threshold/dist)-alpha;
// maneuver = (Mathf.Sin(beta)*side + dVn*(Mathf.Cos(beta)-1)).normalized;
// }
// else
maneuver = side;
maneuver += vsl.Physics.Up*Mathf.Sign(Vector3.Dot(dir, vsl.Physics.Up))*(min_separation/sep_threshold-1);
maneuver *= (sep_threshold-min_separation) / Math.Sqrt(vTime);
// maneuver = (-vsl.Up*Mathf.Sign(Vector3.Dot(dir, vsl.Up))*(1-min_separation/sep_threshold) +
// (dVn*cosA-dir).normalized).normalized * (sep_threshold-min_separation) / vTime;
// vsl.Log("\ndist {}\ndV {}\nmaneuver: {}\n" +
// "vTime {}, vDist {}, min sep {}, sep_thresh {}",
// dir*dist, dV, maneuver, vTime, vDist, min_separation, sep_threshold);//debug
#if DEBUG
Collided |= dist < collision_dist;
#endif
//if distance is not safe, correct course anyway
check_distance:
var collision = !maneuver.IsZero();
dist -= collision_dist;
if(dist < threshold)
{
var dist_to_safe = Utils.ClampH(dist-threshold, -0.01f);
var dc = dir*dist_to_safe;
if(vsl.HorizontalSpeed.NeededVector.sqrMagnitude > CPS.LatAvoidMinVelSqr)
{
var lat_avoid = Vector3d.Cross(vsl.Physics.Up, vsl.HorizontalSpeed.NeededVector.normalized);
dc = Vector3d.Dot(dc, lat_avoid) >= 0?
lat_avoid*dist_to_safe :
lat_avoid*-dist_to_safe;
}
if(dc.sqrMagnitude > 0.25) maneuver += dc/CPS.SafeTime*2;
// vsl.Log("adding safe distance correction: {}", dc/CPS.SafeTime*2);//debug
}
// #if DEBUG
// Dir = dir*dist;
// DeltaV = dV;
// Maneuver = maneuver;
// #endif
return collision;
}
示例3: MinDistanceToAABB
public static float MinDistanceToAABB(Vector3 vec, Vector3 pMin, Vector3 pMax)
{
var b = new Bounds();
b.SetMinMax(pMin, pMax);
// inside?
if (b.Contains(vec)) return 0f;
else return Mathf.Sqrt(b.SqrDistance(vec));
}