private void TrySetDefaultMaterial() { if (wireframeMaterial == null) { wireframeMaterial = VisualUtils.CreateDefaultMaterial(); } }
internal void Create(ref Vector3[] boundsCorners, Transform parent, bool isFlattened) { // create corners for (int i = 0; i < boundsCorners.Length; ++i) { GameObject corner = new GameObject { name = "corner_" + i.ToString() }; corner.transform.parent = parent; corner.transform.localPosition = boundsCorners[i]; GameObject visualsScale = new GameObject(); visualsScale.name = "visualsScale"; visualsScale.transform.parent = corner.transform; visualsScale.transform.localPosition = Vector3.zero; // Compute mirroring scale { Vector3 p = boundsCorners[i]; visualsScale.transform.localScale = new Vector3(Mathf.Sign(p[0]), Mathf.Sign(p[1]), Mathf.Sign(p[2])); } Bounds visualBounds = CreateVisual(visualsScale, isFlattened); var invScale = visualBounds.size.x == 0.0f ? 0.0f : config.HandleSize / visualBounds.size.x; VisualUtils.AddComponentsToAffordance(corner, new Bounds(visualBounds.center * invScale, visualBounds.size * invScale), HandlePrefabCollider.Box, CursorContextInfo.CursorAction.Scale, config.ColliderPadding, parent, config.DrawTetherWhenManipulating); handles.Add(corner.transform); } VisualUtils.HandleIgnoreCollider(config.HandlesIgnoreCollider, handles); objectsChangedEvent.Invoke(this); }
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 SetHighlighted() { //update the box material to the grabbed material if (boxDisplay != null) { VisualUtils.ApplyMaterialToAllRenderers(boxDisplay, config.BoxGrabbedMaterial); } }
protected void UpdateColliderBounds() { foreach (var handle in handles) { var handleBounds = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject); UpdateColliderBounds(handle, handleBounds.size); } }
internal void ResetVisibility(bool activate) { //set box display visibility if (boxDisplay != null) { boxDisplay.SetActive(activate); VisualUtils.ApplyMaterialToAllRenderers(boxDisplay, config.BoxMaterial); } }
internal void UpdateLinkPositions(ref Vector3[] boundsCorners) { if (boundsCorners != null) { for (int i = 0; i < links.Count; ++i) { links[i].transform.position = VisualUtils.GetLinkPosition(i, ref boundsCorners); } } }
private void CreateHandles(Transform parent, bool drawManipulationTether) { for (int i = 0; i < edgeCenters.Length; ++i) { GameObject midpoint = new GameObject(); midpoint.name = "midpoint_" + i.ToString(); midpoint.transform.position = edgeCenters[i]; midpoint.transform.parent = parent; GameObject midpointVisual; GameObject prefabType = config.HandlePrefab; if (prefabType != null) { midpointVisual = GameObject.Instantiate(prefabType); } else { midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere); GameObject.Destroy(midpointVisual.GetComponent <SphereCollider>()); } // Align handle with its edge assuming that the prefab is initially aligned with the up direction if (edgeAxes[i] == CardinalAxisType.X) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.right); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } else if (edgeAxes[i] == CardinalAxisType.Z) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.forward); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual); float maxDim = Mathf.Max( Mathf.Max(midpointBounds.size.x, midpointBounds.size.y), midpointBounds.size.z); float invScale = config.HandleSize / maxDim; midpointVisual.name = "visuals"; midpointVisual.transform.parent = midpoint.transform; midpointVisual.transform.localScale = new Vector3(invScale, invScale, invScale); midpointVisual.transform.localPosition = Vector3.zero; VisualUtils.AddComponentsToAffordance(midpoint, new Bounds(midpointBounds.center * invScale, midpointBounds.size * invScale), config.RotationHandlePrefabColliderType, CursorContextInfo.CursorAction.Rotate, config.ColliderPadding, parent, drawManipulationTether); handles.Add(midpoint.transform); if (config.HandleMaterial != null) { VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial); } } }
/// <inheritdoc/> internal override void CalculateHandlePositions(ref Vector3[] boundsCorners) { if (boundsCorners != null && HandlePositions != null) { for (int i = 0; i < HandlePositions.Length; ++i) { HandlePositions[i] = VisualUtils.GetLinkPosition(i, ref boundsCorners); } UpdateHandles(); } }
internal void ResetHandleVisibility(bool isVisible) { if (handles != null) { for (int i = 0; i < handles.Count; ++i) { handles[i].gameObject.SetActive(isVisible && IsVisible(handles[i])); VisualUtils.ApplyMaterialToAllRenderers(handles[i].gameObject, BaseConfig.HandleMaterial); } } }
internal protected void TrySetDefaultMaterial() { if (handleMaterial == null) { handleMaterial = VisualUtils.CreateDefaultMaterial(); } if (handleGrabbedMaterial == null && handleGrabbedMaterial != handleMaterial) { handleGrabbedMaterial = VisualUtils.CreateDefaultMaterial(); } }
internal void CreateLinks(ref Vector3[] boundsCorners, Transform parent, Vector3 currentBoundsExtents) { // create links if (links != null) { GameObject link; Vector3 linkDimensions = GetLinkDimensions(currentBoundsExtents); for (int i = 0; i < VisualUtils.EdgeAxisType.Length; ++i) { if (config.WireframeShape == WireframeType.Cubic) { link = GameObject.CreatePrimitive(PrimitiveType.Cube); Object.Destroy(link.GetComponent <BoxCollider>()); } else { link = GameObject.CreatePrimitive(PrimitiveType.Cylinder); Object.Destroy(link.GetComponent <CapsuleCollider>()); } link.name = "link_" + i.ToString(); CardinalAxisType axisType = VisualUtils.EdgeAxisType[i]; float wireframeEdgeRadius = config.WireframeEdgeRadius; if (axisType == CardinalAxisType.Y) { link.transform.localScale = new Vector3(wireframeEdgeRadius, linkDimensions.y, wireframeEdgeRadius); link.transform.Rotate(new Vector3(0.0f, 90.0f, 0.0f)); } else if (axisType == CardinalAxisType.Z) { link.transform.localScale = new Vector3(wireframeEdgeRadius, linkDimensions.z, wireframeEdgeRadius); link.transform.Rotate(new Vector3(90.0f, 0.0f, 0.0f)); } else//X { link.transform.localScale = new Vector3(wireframeEdgeRadius, linkDimensions.x, wireframeEdgeRadius); link.transform.Rotate(new Vector3(0.0f, 0.0f, 90.0f)); } link.transform.position = VisualUtils.GetLinkPosition(i, ref boundsCorners); link.transform.parent = parent; Renderer linkRenderer = link.GetComponent <Renderer>(); if (config.WireframeMaterial != null) { linkRenderer.material = config.WireframeMaterial; } link.SetActive(config.ShowWireFrame); links.Add(new Link(link.transform, axisType)); } } }
internal void CalculateEdgeCenters(ref Vector3[] boundsCorners) { if (boundsCorners != null && edgeCenters != null) { for (int i = 0; i < edgeCenters.Length; ++i) { edgeCenters[i] = VisualUtils.GetLinkPosition(i, ref boundsCorners); } } UpdateHandles(); }
private void UpdateBounds() { if (TargetBounds != null) { Vector3 newExtents = CalculateBoundsExtents(); if (newExtents != Vector3.zero) { currentBoundsExtents = newExtents; VisualUtils.GetCornerPositionsFromBounds(new Bounds(Vector3.zero, currentBoundsExtents * 2.0f), ref boundsCorners); } } }
protected void UpdateBaseMaterial() { if (handles != null) { for (int i = 0; i < handles.Count; ++i) { if (handles[i] != highlightedHandle) { VisualUtils.ApplyMaterialToAllRenderers(handles[i].gameObject, BaseConfig.HandleMaterial); } } } }
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; } }
internal void Reset(bool visible) { isVisible = visible; // Set box display visibility if (boxDisplay != null) { bool activate = config.BoxMaterial != null && isVisible; // only set active if material is set boxDisplay.SetActive(activate); if (activate) { VisualUtils.ApplyMaterialToAllRenderers(boxDisplay, config.BoxMaterial); } } }
protected void ResetHandles() { if (handles != null) { for (int i = 0; i < handles.Count; ++i) { bool isVisible = IsVisible(handles[i]); handles[i].gameObject.SetActive(isVisible); if (isVisible) { VisualUtils.ApplyMaterialToAllRenderers(handles[i].gameObject, BaseConfig.HandleMaterial); } } } highlightedHandle = null; }
internal void Reset(bool areHandlesActive, FlattenModeType flattenAxis) { IsActive = areHandlesActive; ResetHandles(); if (IsActive) { 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 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); } } } }
internal void SetHighlighted(Transform handleToHighlight) { // turn off all handles that aren't the handle we want to highlight if (handles != null) { for (int i = 0; i < handles.Count; ++i) { if (handles[i] != handleToHighlight) { handles[i].gameObject.SetActive(false); } else { VisualUtils.ApplyMaterialToAllRenderers(handles[i].gameObject, BaseConfig.HandleGrabbedMaterial); } } } }
private Vector3 CalculateBoundsExtents() { // 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(); // apply flattening return(VisualUtils.FlattenBounds(boundsExtents, flattenAxis)); }
private void UpdateColliderType() { foreach (var handle in handles) { // remove old colliders bool shouldCreateNewCollider = false; var oldBoxCollider = handle.GetComponent <BoxCollider>(); if (oldBoxCollider != null && config.RotationHandlePrefabColliderType == HandlePrefabCollider.Sphere) { shouldCreateNewCollider = true; Object.Destroy(oldBoxCollider); } var oldSphereCollider = handle.GetComponent <SphereCollider>(); if (oldSphereCollider != null && config.RotationHandlePrefabColliderType == HandlePrefabCollider.Box) { shouldCreateNewCollider = true; Object.Destroy(oldSphereCollider); } if (shouldCreateNewCollider) { // attach new collider var handleBounds = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject); var invScale = handleBounds.size.x == 0.0f ? 0.0f : config.HandleSize / handleBounds.size.x; Vector3 colliderSizeScaled = handleBounds.size * invScale; Vector3 colliderCenterScaled = handleBounds.center * invScale; if (config.RotationHandlePrefabColliderType == HandlePrefabCollider.Box) { BoxCollider collider = handle.gameObject.AddComponent <BoxCollider>(); collider.size = colliderSizeScaled; collider.center = colliderCenterScaled; collider.size += config.ColliderPadding; } else { SphereCollider sphere = handle.gameObject.AddComponent <SphereCollider>(); sphere.center = colliderCenterScaled; sphere.radius = colliderSizeScaled.x * 0.5f; sphere.radius += VisualUtils.GetMaxComponent(config.ColliderPadding); } } } }
/// <summary> /// Creates the corner visual and returns the bounds of the created visual /// </summary> /// <param name="parent">parent of visual</param> /// <param name="isFlattened">instantiate in flattened mode - slate</param> /// <returns>bounds of the created visual</returns> private Bounds CreateVisual(GameObject parent, bool isFlattened) { // figure out which prefab to instantiate GameObject cornerVisual = null; areHandlesFlattened = isFlattened; GameObject prefabType = isFlattened ? config.HandleSlatePrefab : config.HandlePrefab; if (prefabType == null) { // instantiate default prefab, a cube. Remove the box collider from it cornerVisual = GameObject.CreatePrimitive(PrimitiveType.Cube); cornerVisual.transform.parent = parent.transform; cornerVisual.transform.localPosition = Vector3.zero; GameObject.Destroy(cornerVisual.GetComponent <BoxCollider>()); } else { cornerVisual = GameObject.Instantiate(prefabType, parent.transform); } if (isFlattened) { // Rotate 2D slate handle asset for proper orientation cornerVisual.transform.Rotate(0, 0, -90); } cornerVisual.name = visualsName; // this is the size of the corner visuals var cornerbounds = VisualUtils.GetMaxBounds(cornerVisual); float maxDim = Mathf.Max(Mathf.Max(cornerbounds.size.x, cornerbounds.size.y), cornerbounds.size.z); cornerbounds.size = maxDim * Vector3.one; // we need to multiply by this amount to get to desired scale handle size var invScale = cornerbounds.size.x == 0.0f ? 0.0f : config.HandleSize / cornerbounds.size.x; cornerVisual.transform.localScale = new Vector3(invScale, invScale, invScale); VisualUtils.ApplyMaterialToAllRenderers(cornerVisual, config.HandleMaterial); return(cornerbounds); }
private Bounds CreateVisual(int handleIndex, GameObject parent) { GameObject midpointVisual; GameObject prefabType = config.HandlePrefab; if (prefabType != null) { midpointVisual = Object.Instantiate(prefabType); } else { midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere); Object.Destroy(midpointVisual.GetComponent <SphereCollider>()); } // Align handle with its edge assuming that the prefab is initially aligned with the up direction if (edgeAxes[handleIndex] == CardinalAxisType.X) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.right); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } else if (edgeAxes[handleIndex] == CardinalAxisType.Z) { Quaternion realignment = Quaternion.FromToRotation(Vector3.up, Vector3.forward); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; } Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual); float maxDim = VisualUtils.GetMaxComponent(midpointBounds.size); float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim; midpointVisual.name = "visuals"; midpointVisual.transform.parent = parent.transform; midpointVisual.transform.localScale = new Vector3(invScale, invScale, invScale); midpointVisual.transform.localPosition = Vector3.zero; if (config.HandleMaterial != null) { VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial); } return(midpointBounds); }
protected override void UpdateColliderBounds(Transform handle, Vector3 visualSize) { var invScale = visualSize.x == 0.0f ? 0.0f : config.HandleSize / visualSize.x; GetVisual(handle).transform.localScale = new Vector3(invScale, invScale, invScale); Vector3 colliderSizeScaled = visualSize * invScale; if (config.RotationHandlePrefabColliderType == HandlePrefabCollider.Box) { BoxCollider collider = handle.gameObject.GetComponent <BoxCollider>(); collider.size = colliderSizeScaled; collider.size += BaseConfig.ColliderPadding; } else { SphereCollider collider = handle.gameObject.GetComponent <SphereCollider>(); collider.radius = colliderSizeScaled.x * 0.5f; collider.radius += VisualUtils.GetMaxComponent(config.ColliderPadding); } }
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); } } } }
private void CreateHandles(Transform parent) { for (int i = 0; i < edgeCenters.Length; ++i) { GameObject midpoint = new GameObject(); midpoint.name = "midpoint_" + i.ToString(); midpoint.transform.position = edgeCenters[i]; midpoint.transform.parent = parent; Bounds midpointBounds = CreateVisual(i, midpoint); float maxDim = VisualUtils.GetMaxComponent(midpointBounds.size); float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim; VisualUtils.AddComponentsToAffordance(midpoint, new Bounds(midpointBounds.center * invScale, midpointBounds.size * invScale), config.RotationHandlePrefabColliderType, CursorContextInfo.CursorAction.Rotate, config.ColliderPadding, parent, config.DrawTetherWhenManipulating); handles.Add(midpoint.transform); } VisualUtils.HandleIgnoreCollider(config.HandlesIgnoreCollider, handles); objectsChangedEvent.Invoke(this); }
private Bounds CreateVisual(int handleIndex, GameObject parent) { GameObject midpointVisual; GameObject prefabType = config.HandlePrefab; if (prefabType != null) { midpointVisual = Object.Instantiate(prefabType); } else { midpointVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere); if (config.HandlePrefabColliderType != HandlePrefabCollider.Sphere) { Object.Destroy(midpointVisual.GetComponent <SphereCollider>()); } } Quaternion realignment = GetRotationRealignment(handleIndex); midpointVisual.transform.localRotation = realignment * midpointVisual.transform.localRotation; Bounds midpointBounds = VisualUtils.GetMaxBounds(midpointVisual); float maxDim = VisualUtils.GetMaxComponent(midpointBounds.size); float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim; midpointVisual.name = visualsName; midpointVisual.transform.parent = parent.transform; midpointVisual.transform.localScale = new Vector3(invScale, invScale, invScale); midpointVisual.transform.localPosition = Vector3.zero; if (config.HandleMaterial != null) { VisualUtils.ApplyMaterialToAllRenderers(midpointVisual, config.HandleMaterial); } return(midpointBounds); }
internal void HandleIgnoreCollider(Collider handlesIgnoreCollider) { VisualUtils.HandleIgnoreCollider(handlesIgnoreCollider, handles); }