public static Bounds GetModelSpaceBounds(this SpriteRenderer spriteRenderer) { Sprite sprite = spriteRenderer.sprite; if (sprite == null) { return(BoundsExtensions.GetInvalidBoundsInstance()); } #if !UNITY_5 Vector3 modelSpaceCenter = spriteRenderer.transform.InverseTransformPoint(spriteRenderer.bounds.center); modelSpaceCenter.z = 0.0f; return(new Bounds(modelSpaceCenter, sprite.rect.size / sprite.pixelsPerUnit)); #else List <Vector2> spriteVerts = new List <Vector2>(sprite.vertices); return(Vector2Extensions.GetPointCloudAABB(spriteVerts)); #endif }
public static Bounds GetPointCloudAABB(List <Vector2> pointCloud) { if (pointCloud.Count == 0) { return(BoundsExtensions.GetInvalidBoundsInstance()); } Vector2 min = pointCloud[0]; Vector2 max = pointCloud[0]; for (int ptIndex = 1; ptIndex < pointCloud.Count; ++ptIndex) { Vector2 pt = pointCloud[ptIndex]; min = Vector2.Min(pt, min); max = Vector2.Max(pt, max); } return(new Bounds((min + max) * 0.5f, max - min)); }
/// <summary> /// Builds the camera view volume for the specified camera. Please see the comments for the /// class constructor in order to understand what the second parameter is about. /// </summary> public void BuildForCamera(Camera camera, float desiredCameraFarClipPlane) { // Store the old camera far clip plane distance. We need to do this because we will // temporarily modify the camera far clip plane to 'desiredCameraFarClipPlane' in order // to perform all the necessary calculations. float oldCameraFarClipPlane = camera.farClipPlane; AdjustCameraFarClipPlane(camera, desiredCameraFarClipPlane); // Calculate the view volume data CalculateWorldSpacePoints(camera); CalculateWorldSpacePlanes(camera); CalculateWorldSpaceVolumeEdgeRays(); // Restore the camera far clip plane to what it was before camera.farClipPlane = oldCameraFarClipPlane; // Store clip plane distances _farClipPlaneDistance = desiredCameraFarClipPlane; _nearClipPlaneDistance = camera.nearClipPlane; // Calculate near and far plane dimensions Transform cameraTransform = camera.transform; Vector3 cornerVector = TopLeftPointOnNearPlane - BottomRightPointOnNearPlane; float width = Mathf.Abs(Vector3.Dot(cornerVector, cameraTransform.right)); float height = Mathf.Abs(Vector3.Dot(cornerVector, cameraTransform.up)); _nearPlaneSize = new Vector2(width, height); cornerVector = TopLeftPointOnFarPlane - BottomRightPointOnFarPlane; width = Mathf.Abs(Vector3.Dot(cornerVector, cameraTransform.right)); height = Mathf.Abs(Vector3.Dot(cornerVector, cameraTransform.up)); _farPlaneSize = new Vector2(width, height); // Calculate the volume AABB _aabb = BoundsExtensions.FromPointCloud(new List <Vector3>(_worldSpaceVolumePoints)); }