/// <summary> /// recomputes flattening if axis to be flattened has changed. /// </summary> protected virtual void UpdateFlattenedAxis() { // Find the maximum size of the new bounds float maxAxisThickness = Mathf.Max(Mathf.Max(targetBoundsLocalScale.x, targetBoundsLocalScale.y), targetBoundsLocalScale.z); FlattenModeEnum newFlattenedAxis = FlattenModeEnum.DoNotFlatten; switch (flattenPreference) { case FlattenModeEnum.DoNotFlatten: // Do nothing break; case FlattenModeEnum.FlattenAuto: // Flattening order of preference - z, y, x if (Mathf.Abs(targetBoundsLocalScale.z / maxAxisThickness) < flattenAxisThreshold) { newFlattenedAxis = FlattenModeEnum.FlattenZ; targetBoundsLocalScale.z = flattenedAxisThickness * maxAxisThickness; } else if (Mathf.Abs(targetBoundsLocalScale.y / maxAxisThickness) < flattenAxisThreshold) { newFlattenedAxis = FlattenModeEnum.FlattenY; targetBoundsLocalScale.y = flattenedAxisThickness * maxAxisThickness; } else if (Mathf.Abs(targetBoundsLocalScale.x / maxAxisThickness) < flattenAxisThreshold) { newFlattenedAxis = FlattenModeEnum.FlattenX; targetBoundsLocalScale.x = flattenedAxisThickness * maxAxisThickness; } break; case FlattenModeEnum.FlattenX: newFlattenedAxis = FlattenModeEnum.FlattenX; targetBoundsLocalScale.x = flattenedAxisThickness * maxAxisThickness; break; case FlattenModeEnum.FlattenY: newFlattenedAxis = FlattenModeEnum.FlattenY; targetBoundsLocalScale.y = flattenedAxisThickness * maxAxisThickness; break; case FlattenModeEnum.FlattenZ: newFlattenedAxis = FlattenModeEnum.FlattenZ; targetBoundsLocalScale.z = flattenedAxisThickness * maxAxisThickness; break; } FlattenedAxis = newFlattenedAxis; }
protected virtual void RefreshTargetBounds() { if (target == null) { targetBoundsWorldCenter = Vector3.zero; targetBoundsLocalScale = Vector3.one; return; } // Get the new target bounds boundsPoints.Clear(); switch (BoundsCalculationMethod) { case BoundsCalculationMethodEnum.RendererBounds: default: Renderer[] renderers = target.GetComponentsInChildren <Renderer>(); for (int i = 0; i < renderers.Length; ++i) { var rendererObj = renderers[i]; if (rendererObj.gameObject.layer == IgnoreLayer) { continue; } rendererObj.bounds.GetCornerPositionsFromRendererBounds(ref corners); boundsPoints.AddRange(corners); } break; case BoundsCalculationMethodEnum.MeshFilterBounds: MeshFilter[] meshFilters = target.GetComponentsInChildren <MeshFilter>(); for (int i = 0; i < meshFilters.Length; i++) { var meshFilterObj = meshFilters[i]; if (meshFilterObj.gameObject.layer == IgnoreLayer) { continue; } Bounds meshBounds = meshFilterObj.sharedMesh.bounds; meshBounds.GetCornerPositions(meshFilterObj.transform, ref corners); boundsPoints.AddRange(corners); } RectTransform[] rectTransforms = target.GetComponentsInChildren <RectTransform>(); for (int i = 0; i < rectTransforms.Length; i++) { rectTransforms[i].GetWorldCorners(rectTransformCorners); boundsPoints.AddRange(rectTransformCorners); } break; } if (boundsPoints.Count > 0) { // We now have a list of all points in world space // Translate them all to local space for (int i = 0; i < boundsPoints.Count; i++) { boundsPoints[i] = target.transform.InverseTransformPoint(boundsPoints[i]); } // Encapsulate the points with a local bounds localTargetBounds.center = boundsPoints[0]; localTargetBounds.size = Vector3.zero; foreach (Vector3 point in boundsPoints) { localTargetBounds.Encapsulate(point); } } // Store the world center of the target bb targetBoundsWorldCenter = target.transform.TransformPoint(localTargetBounds.center); // Store the local scale of the target bb targetBoundsLocalScale = localTargetBounds.size; targetBoundsLocalScale.Scale(target.transform.localScale); // Find the maximum size of the new bounds float maxAxisThickness = Mathf.Max(Mathf.Max(targetBoundsLocalScale.x, targetBoundsLocalScale.y), targetBoundsLocalScale.z); // Now check our flatten behavior FlattenModeEnum newFlattenedAxis = FlattenModeEnum.DoNotFlatten; switch (FlattenPreference) { case FlattenModeEnum.DoNotFlatten: // Do nothing break; case FlattenModeEnum.FlattenAuto: // Flattening order of preference - z, y, x if (Mathf.Abs(targetBoundsLocalScale.z / maxAxisThickness) < FlattenAxisThreshold) { newFlattenedAxis = FlattenModeEnum.FlattenZ; targetBoundsLocalScale.z = FlattenedAxisThickness * maxAxisThickness; } else if (Mathf.Abs(targetBoundsLocalScale.y / maxAxisThickness) < FlattenAxisThreshold) { newFlattenedAxis = FlattenModeEnum.FlattenY; targetBoundsLocalScale.y = FlattenedAxisThickness * maxAxisThickness; } else if (Mathf.Abs(targetBoundsLocalScale.x / maxAxisThickness) < FlattenAxisThreshold) { newFlattenedAxis = FlattenModeEnum.FlattenX; targetBoundsLocalScale.x = FlattenedAxisThickness * maxAxisThickness; } break; case FlattenModeEnum.FlattenX: newFlattenedAxis = FlattenModeEnum.FlattenX; targetBoundsLocalScale.x = FlattenedAxisThickness * maxAxisThickness; break; case FlattenModeEnum.FlattenY: newFlattenedAxis = FlattenModeEnum.FlattenY; targetBoundsLocalScale.y = FlattenedAxisThickness * maxAxisThickness; break; case FlattenModeEnum.FlattenZ: newFlattenedAxis = FlattenModeEnum.FlattenZ; targetBoundsLocalScale.z = FlattenedAxisThickness * maxAxisThickness; break; } FlattenedAxis = newFlattenedAxis; }