private void UpdateBounds() { if (cachedTargetCollider != null) { // Store current rotation then zero out the rotation so that the bounds // are computed when the object is in its 'axis aligned orientation'. Quaternion currentRotation = targetObject.transform.rotation; targetObject.transform.rotation = Quaternion.identity; Physics.SyncTransforms(); // Update collider bounds Vector3 boundsExtents = cachedTargetCollider.bounds.extents; // After bounds are computed, restore rotation... targetObject.transform.rotation = currentRotation; Physics.SyncTransforms(); if (boundsExtents != Vector3.zero) { if (flattenAxis == FlattenModeType.FlattenAuto) { float min = Mathf.Min(boundsExtents.x, Mathf.Min(boundsExtents.y, boundsExtents.z)); flattenAxis = min.Equals(boundsExtents.x) ? FlattenModeType.FlattenX : (min.Equals(boundsExtents.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ); } boundsExtents.x = flattenAxis == FlattenModeType.FlattenX ? 0.0f : boundsExtents.x; boundsExtents.y = flattenAxis == FlattenModeType.FlattenY ? 0.0f : boundsExtents.y; boundsExtents.z = flattenAxis == FlattenModeType.FlattenZ ? 0.0f : boundsExtents.z; currentBoundsExtents = boundsExtents; GetCornerPositionsFromBounds(new Bounds(Vector3.zero, boundsExtents * 2.0f), ref boundsCorners); CalculateEdgeCenters(); } } }
/// <summary> /// Flattens the given extents according to the passed flattenAxis. The flattenAxis value will be replaced by flattenValue. /// </summary> /// <param name="extents">The original extents (unflattened).</param> /// <param name="flattenAxis">The axis to flatten.</param> /// <param name="flattenValue">The value to flatten the flattenAxis to.</param> /// <returns>New extents with flattened axis.</returns> static internal Vector3 FlattenBounds(Vector3 extents, FlattenModeType flattenAxis, float flattenValue = 0.0f) { Vector3 boundsExtents = extents; if (boundsExtents != Vector3.zero) { if (flattenAxis == FlattenModeType.FlattenAuto) { if (boundsExtents.x < boundsExtents.y && boundsExtents.x < boundsExtents.z) { flattenAxis = FlattenModeType.FlattenX; } else if (boundsExtents.y < boundsExtents.z) { flattenAxis = FlattenModeType.FlattenY; } else { flattenAxis = FlattenModeType.FlattenZ; } } boundsExtents.x = (flattenAxis == FlattenModeType.FlattenX) ? flattenValue : boundsExtents.x; boundsExtents.y = (flattenAxis == FlattenModeType.FlattenY) ? flattenValue : boundsExtents.y; boundsExtents.z = (flattenAxis == FlattenModeType.FlattenZ) ? flattenValue : boundsExtents.z; } return(boundsExtents); }
private void UpdateBounds() { if (TargetBounds != null) { // Store current rotation then zero out the rotation so that the bounds // are computed when the object is in its 'axis aligned orientation'. Quaternion currentRotation = Target.transform.rotation; Target.transform.rotation = Quaternion.identity; UnityPhysics.SyncTransforms(); // Update collider bounds Vector3 boundsExtents = TargetBounds.bounds.extents; // After bounds are computed, restore rotation... Target.transform.rotation = currentRotation; UnityPhysics.SyncTransforms(); if (boundsExtents != Vector3.zero) { if (flattenAxis == FlattenModeType.FlattenAuto) { float min = Mathf.Min(boundsExtents.x, Mathf.Min(boundsExtents.y, boundsExtents.z)); flattenAxis = (min == boundsExtents.x) ? FlattenModeType.FlattenX : ((min == boundsExtents.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ); } boundsExtents.x = (flattenAxis == FlattenModeType.FlattenX) ? 0.0f : boundsExtents.x; boundsExtents.y = (flattenAxis == FlattenModeType.FlattenY) ? 0.0f : boundsExtents.y; boundsExtents.z = (flattenAxis == FlattenModeType.FlattenZ) ? 0.0f : boundsExtents.z; currentBoundsExtents = boundsExtents; GetCornerPositionsFromBounds(new Bounds(Vector3.zero, boundsExtents * 2.0f), ref boundsCorners); boundsUpdated.Invoke(); } } }
private Vector3 GetBoxDisplayScale(Vector3 currentBoundsExtents, FlattenModeType flattenAxis) { // When a box is flattened one axis is normally scaled to zero, this doesn't always work well with visuals so we take // that flattened axis and re-scale it to the flattenAxisDisplayScale. Vector3 displayScale = VisualUtils.FlattenBounds(currentBoundsExtents, flattenAxis, config.FlattenAxisDisplayScale); return(2.0f * displayScale); }
internal void Update(Transform parent, Vector3 boundsExtents, FlattenModeType flattenAxis) { if (boxDisplay != null) { Vector3 rootScale = parent.lossyScale; Vector3 invRootScale = new Vector3(1.0f / rootScale.x, 1.0f / rootScale.y, 1.0f / rootScale.z); // Compute the local scale that produces the desired world space size boxDisplay.transform.localScale = Vector3.Scale(GetBoxDisplayScale(boundsExtents, flattenAxis), invRootScale); } }
internal void UpdateDisplay(Vector3 boundsExtents, FlattenModeType flattenAxis) { if (boxDisplay != null) { Transform parent = boxDisplay.transform.parent; boxDisplay.transform.parent = null; boxDisplay.transform.localScale = GetBoxDisplayScale(boundsExtents, flattenAxis); boxDisplay.transform.parent = parent; } }
private void RecaculateBounds() { // Make sure that the bounds of all child objects are up to date before we compute bounds Physics.SyncTransforms(); BoundBoxCollider = GetComponent <BoxCollider>(); if (BoundBoxCollider == null) { Debug.Log("Error! Please Add BoxCollider And Adjust Size For BoundingBoxGameobject"); return; } // Store current rotation then zero out the rotation so that the bounds // are computed when the object is in its 'axis aligned orientation'. Quaternion currentRotation = transform.rotation; transform.rotation = Quaternion.identity; Physics.SyncTransforms(); // Update collider bounds currentBoundsExtents = BoundBoxCollider.bounds.extents; // After bounds are computed, restore rotation... transform.rotation = currentRotation; Physics.SyncTransforms(); if (currentBoundsExtents != Vector3.zero) { if (FlattenAxis == FlattenModeType.FlattenAuto) { float min = Mathf.Min(currentBoundsExtents.x, Mathf.Min(currentBoundsExtents.y, currentBoundsExtents.z)); flattenAxis = (min == currentBoundsExtents.x) ? FlattenModeType.FlattenX : ((min == currentBoundsExtents.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ); } currentBoundsExtents.x = (flattenAxis == FlattenModeType.FlattenX) ? 0.0f : currentBoundsExtents.x; currentBoundsExtents.y = (flattenAxis == FlattenModeType.FlattenY) ? 0.0f : currentBoundsExtents.y; currentBoundsExtents.z = (flattenAxis == FlattenModeType.FlattenZ) ? 0.0f : currentBoundsExtents.z; Transform existContainerTransform = this.transform.Find(GetType().ToString()); if (existContainerTransform != null) { #if UNITY_EDITOR GameObject.Destroy(existContainerTransform.gameObject); #else GameObject.DestroyImmediate(existContainerTransform.gameObject); #endif } BoundingBoxContainer = new GameObject(GetType().ToString()).transform; BoundingBoxContainer.parent = transform; BoundingBoxContainer.position = BoundBoxCollider.bounds.center; BoundingBoxContainer.localRotation = Quaternion.identity; } }
internal void AddBoxDisplay(Transform parent, Vector3 currentBoundsExtents, FlattenModeType flattenAxis) { // This has to be cube even in flattened mode as flattened box display can still have a thickness of flattenAxisDisplayScale boxDisplay = GameObject.CreatePrimitive(PrimitiveType.Cube); Object.Destroy(boxDisplay.GetComponent <Collider>()); boxDisplay.name = "box display"; cachedFlattenMode = flattenAxis; cachedExtents = currentBoundsExtents; Reset(isVisible); boxDisplay.transform.localScale = GetBoxDisplayScale(currentBoundsExtents, flattenAxis); boxDisplay.transform.parent = parent; }
private Vector3 GetBoxDisplayScale(Vector3 currentBoundsExtents, FlattenModeType flattenAxis) { // When a box is flattened one axis is normally scaled to zero, this doesn't always work well with visuals so we take // that flattened axis and re-scale it to the flattenAxisDisplayScale. Vector3 displayScale = currentBoundsExtents; displayScale.x = (flattenAxis == FlattenModeType.FlattenX) ? flattenAxisDisplayScale : displayScale.x; displayScale.y = (flattenAxis == FlattenModeType.FlattenY) ? flattenAxisDisplayScale : displayScale.y; displayScale.z = (flattenAxis == FlattenModeType.FlattenZ) ? flattenAxisDisplayScale : displayScale.z; return(2.0f * displayScale); }
internal void Reset(bool areHandlesActive, FlattenModeType flattenAxis) { IsActive = areHandlesActive; ResetHandles(); bool isFlattened = flattenAxis != FlattenModeType.DoNotFlatten; if (areHandlesFlattened != isFlattened) { areHandlesFlattened = isFlattened; // we have to recreate visuals in this case as flattened scale handles will use a different prefab RecreateVisuals(); } }
internal void AddBoxDisplay(Transform parent, Vector3 currentBoundsExtents, FlattenModeType flattenAxis) { if (config.BoxMaterial != null) { // this has to be cube even in flattened mode as flattened box display can still have a thickness of flattenAxisDisplayScale boxDisplay = GameObject.CreatePrimitive(PrimitiveType.Cube); GameObject.Destroy(boxDisplay.GetComponent <Collider>()); boxDisplay.name = "bounding box"; VisualUtils.ApplyMaterialToAllRenderers(boxDisplay, config.BoxMaterial); boxDisplay.transform.localScale = GetBoxDisplayScale(currentBoundsExtents, flattenAxis); boxDisplay.transform.parent = parent; } }
private void UpdateBounds() { Vector3 boundsSize = Vector3.zero; Vector3 centroid = Vector3.zero; //store current rotation then zero out the rotation so that the bounds //are computed when the object is in its 'axis aligned orientation'. Quaternion currentRotation = targetObject.transform.rotation; targetObject.transform.rotation = Quaternion.identity; if (cachedTargetCollider != null) { Bounds colliderBounds = cachedTargetCollider.bounds; boundsSize = colliderBounds.extents; centroid = colliderBounds.center; } //after bounds are computed, restore rotation... targetObject.transform.rotation = currentRotation; if (boundsSize != Vector3.zero) { if (flattenAxis == FlattenModeType.FlattenAuto) { float min = Mathf.Min(boundsSize.x, Mathf.Min(boundsSize.y, boundsSize.z)); flattenAxis = min.Equals(boundsSize.x) ? FlattenModeType.FlattenX : (min.Equals(boundsSize.y) ? FlattenModeType.FlattenY : FlattenModeType.FlattenZ); } boundsSize.x = flattenAxis == FlattenModeType.FlattenX ? 0.0f : boundsSize.x; boundsSize.y = flattenAxis == FlattenModeType.FlattenY ? 0.0f : boundsSize.y; boundsSize.z = flattenAxis == FlattenModeType.FlattenZ ? 0.0f : boundsSize.z; currentBoundsSize = boundsSize; boundsCentroid = centroid; boundsCorners[0] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z - currentBoundsSize.z); boundsCorners[1] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z - currentBoundsSize.z); boundsCorners[2] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z - currentBoundsSize.z); boundsCorners[3] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z - currentBoundsSize.z); boundsCorners[4] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z + currentBoundsSize.z); boundsCorners[5] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y - currentBoundsSize.y, centroid.z + currentBoundsSize.z); boundsCorners[6] = centroid - new Vector3(centroid.x + currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z + currentBoundsSize.z); boundsCorners[7] = centroid - new Vector3(centroid.x - currentBoundsSize.x, centroid.y + currentBoundsSize.y, centroid.z + currentBoundsSize.z); CalculateEdgeCenters(); } }
private void Flatten(FlattenModeType flattenAxis) { if (flattenAxis == FlattenModeType.FlattenX) { flattenedHandles = new int[] { 0, 4, 2, 6 }; } else if (flattenAxis == FlattenModeType.FlattenY) { flattenedHandles = new int[] { 1, 3, 5, 7 }; } else if (flattenAxis == FlattenModeType.FlattenZ) { flattenedHandles = new int[] { 9, 10, 8, 11 }; } }
/// <summary> /// Returns the flatten indices to the corresponding flattenAxis mode. /// </summary> /// <param name="flattenAxis">Flatten axis mode that should be converted to indices.</param> /// <returns>Flattened indices.</returns> internal static List <int> GetFlattenedIndices(FlattenModeType flattenAxis, CardinalAxisType[] axisArray) { List <int> flattenedIndices = new List <int>(); for (int i = 0; i < axisArray.Length; ++i) { if ((flattenAxis == FlattenModeType.FlattenX && axisArray[i] == CardinalAxisType.X) || (flattenAxis == FlattenModeType.FlattenY && axisArray[i] == CardinalAxisType.Y) || (flattenAxis == FlattenModeType.FlattenZ && axisArray[i] == CardinalAxisType.Z)) { flattenedIndices.Add(i); } } return(flattenedIndices); }
internal void Reset(bool areHandlesActive, FlattenModeType flattenAxis) { IsActive = areHandlesActive; ResetHandles(); if (IsActive && handleAxes.Length == handles.Count) { List <int> flattenedHandles = VisualUtils.GetFlattenedIndices(flattenAxis, handleAxes); if (flattenedHandles != null) { for (int i = 0; i < flattenedHandles.Count; ++i) { handles[flattenedHandles[i]].gameObject.SetActive(false); } } } }
internal void AddBoxDisplay(Transform parent, Vector3 currentBoundsExtents, FlattenModeType flattenAxis) { if (boxMaterial != null) { bool isFlattened = flattenAxis != FlattenModeType.DoNotFlatten; boxDisplay = GameObject.CreatePrimitive(isFlattened ? PrimitiveType.Quad : PrimitiveType.Cube); GameObject.Destroy(boxDisplay.GetComponent <Collider>()); boxDisplay.name = "bounding box"; BoundsControlVisualUtils.ApplyMaterialToAllRenderers(boxDisplay, boxMaterial); boxDisplay.transform.localScale = GetBoxDisplayScale(currentBoundsExtents, flattenAxis); boxDisplay.transform.parent = parent; } }
internal void Reset(bool areHandlesActive, FlattenModeType flattenAxis) { IsActive = areHandlesActive; cachedFlattenAxis = flattenAxis; if (IsActive) { ResetHandles(); int[] flattenedHandles = VisualUtils.GetFlattenedIndices(flattenAxis); if (flattenedHandles != null) { for (int i = 0; i < flattenedHandles.Length; ++i) { handles[flattenedHandles[i]].gameObject.SetActive(false); } } } }
/// <summary> /// Returns the flatten indices to the corresponding flattenAxis mode. /// </summary> /// <param name="flattenAxis">Flatten axis mode that should be converted to indices.</param> /// <returns>Flattened indices.</returns> internal static int[] GetFlattenedIndices(FlattenModeType flattenAxis) { if (flattenAxis == FlattenModeType.FlattenX) { return(flattenedIndicesX); } else if (flattenAxis == FlattenModeType.FlattenY) { return(flattenedIndicesY); } else if (flattenAxis == FlattenModeType.FlattenZ) { return(flattenedIndicesZ); } return(null); }
/// <summary> /// Flattens the given extents according to the passed flattenAxis. The flattenAxis value will be replaced by flattenValue. /// </summary> /// <param name="extents">The original extents (unflattened).</param> /// <param name="flattenAxis">The axis to flatten.</param> /// <param name="flattenValue">The value to flatten the flattenAxis to.</param> /// <returns>New extents with flattened axis.</returns> static internal Vector3 FlattenBounds(Vector3 extents, FlattenModeType flattenAxis, float flattenValue = 0.0f) { Vector3 boundsExtents = extents; if (boundsExtents != Vector3.zero) { if (flattenAxis == FlattenModeType.FlattenAuto) { flattenAxis = DetermineAxisToFlatten(boundsExtents); } boundsExtents.x = (flattenAxis == FlattenModeType.FlattenX) ? flattenValue : boundsExtents.x; boundsExtents.y = (flattenAxis == FlattenModeType.FlattenY) ? flattenValue : boundsExtents.y; boundsExtents.z = (flattenAxis == FlattenModeType.FlattenZ) ? flattenValue : boundsExtents.z; } return(boundsExtents); }
/// <summary> /// Returns the flatten indices to the corresponding flattenAxis mode. /// </summary> /// <param name="flattenAxis">Flatten axis mode that should be converted to indices.</param> /// <returns>Flattened indices.</returns> internal static List <int> GetFlattenedIndices(FlattenModeType flattenAxis, CardinalAxisType[] axisArray) { Debug.Assert(flattenAxis != FlattenModeType.FlattenAuto, "FlattenAuto passed to GetFlattenedIndices. Resolve FlattenAuto into an actual axis before calling."); List <int> flattenedIndices = new List <int>(); for (int i = 0; i < axisArray.Length; ++i) { if ((flattenAxis == FlattenModeType.FlattenX && axisArray[i] == CardinalAxisType.X) || (flattenAxis == FlattenModeType.FlattenY && axisArray[i] == CardinalAxisType.Y) || (flattenAxis == FlattenModeType.FlattenZ && axisArray[i] == CardinalAxisType.Z)) { flattenedIndices.Add(i); } } return(flattenedIndices); }
internal void Reset(bool isVisible, FlattenModeType flattenAxis) { cachedFlattenAxis = flattenAxis; if (links != null) { for (int i = 0; i < links.Count; ++i) { links[i].transform.gameObject.SetActive(isVisible && config.ShowWireFrame); } List <int> flattenedHandles = VisualUtils.GetFlattenedIndices(cachedFlattenAxis, VisualUtils.EdgeAxisType); if (flattenedHandles != null) { for (int i = 0; i < flattenedHandles.Count; ++i) { links[flattenedHandles[i]].transform.gameObject.SetActive(false); } } } }
internal void UpdateFlattenAxis(FlattenModeType flattenAxis) { cachedFlattenMode = flattenAxis; UpdateDisplay(cachedExtents, flattenAxis); }
private void CreatBoundingBoxRoot(FlattenModeType flattenMode) { RecaculateBounds(); }