private void ScaleObject(ProximityState state, Transform scaleVisual, float objectSize, bool lerp = false)
        {
            float targetScale = 1.0f, weight = 0.0f;

            switch (state)
            {
            case ProximityState.FullsizeNoProximity:
                targetScale = config.FarScale;
                weight      = lerp ? config.FarGrowRate : 1.0f;
                break;

            case ProximityState.MediumProximity:
                targetScale = config.MediumScale;
                weight      = lerp ? config.MediumGrowRate : 1.0f;
                break;

            case ProximityState.CloseProximity:
                targetScale = config.CloseScale;
                weight      = lerp ? config.CloseGrowRate : 1.0f;
                break;
            }

            float maxScaleAxis  = VisualUtils.GetMaxComponent(scaleVisual.localScale);
            float newLocalScale = (maxScaleAxis * (1.0f - weight)) + (objectSize * targetScale * weight);

            scaleVisual.localScale = new Vector3(newLocalScale, newLocalScale, newLocalScale);
        }
Beispiel #2
0
        protected override void UpdateColliderBounds(Transform handle, Vector3 visualSize)
        {
            float maxDim   = VisualUtils.GetMaxComponent(visualSize);
            float invScale = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;

            GetVisual(handle).transform.localScale = new Vector3(invScale, invScale, invScale);
            BoxCollider collider     = handle.gameObject.GetComponent <BoxCollider>();
            Vector3     colliderSize = visualSize * invScale;

            collider.size   = colliderSize;
            collider.size  += BaseConfig.ColliderPadding;
            collider.center = Vector3.zero;
        }
        private void UpdateColliderType()
        {
            foreach (var handle in handles)
            {
                // remove old colliders
                bool shouldCreateNewCollider = false;
                var  oldBoxCollider          = handle.GetComponent <BoxCollider>();

                // Caution, Destroy() will destroy one frame later.
                // Do not check later for presence this frame!
                if (oldBoxCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Sphere)
                {
                    shouldCreateNewCollider = true;
                    Object.Destroy(oldBoxCollider);
                }

                var oldSphereCollider = handle.GetComponent <SphereCollider>();
                if (oldSphereCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Box)
                {
                    shouldCreateNewCollider = true;
                    Object.Destroy(oldSphereCollider);
                }

                if (shouldCreateNewCollider)
                {
                    // attach new collider
                    var     handleBounds         = VisualUtils.GetMaxBounds(GetVisual(handle).gameObject);
                    float   maxDim               = VisualUtils.GetMaxComponent(handleBounds.size);
                    float   invScale             = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;
                    Vector3 colliderSizeScaled   = handleBounds.size * invScale;
                    Vector3 colliderCenterScaled = handleBounds.center * invScale;
                    if (config.HandlePrefabColliderType == 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  = VisualUtils.GetMaxComponent(colliderSizeScaled) * 0.5f;
                        sphere.radius += VisualUtils.GetMaxComponent(config.ColliderPadding);
                    }
                }
            }
        }
        /// <summary>
        /// Creates the corner visual and returns the bounds of the created visual
        /// </summary>
        /// <param name="handleIndex">cornerIndex</param>
        /// <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(int handleIndex, GameObject parent, bool isFlattened)
        {
            // figure out which prefab to instantiate
            GameObject handleVisual = null;

            areHandlesFlattened = isFlattened;
            GameObject prefabType = isFlattened ? config.HandleSlatePrefab : config.HandlePrefab;

            if (prefabType == null)
            {
                // instantiate default prefab, a cube with box collider
                handleVisual = GameObject.CreatePrimitive(PrimitiveType.Cube);

                // deactivate collider on visuals and register for deletion - actual collider
                // of handle is attached to the handle gameobject, not the visual
                var collider = handleVisual.GetComponent <BoxCollider>();
                collider.enabled = false;
                UnityEngine.Object.Destroy(collider);
            }
            else
            {
                handleVisual = GameObject.Instantiate(prefabType, parent.transform);
            }

            // this is the size of the corner visuals
            var   handleVisualBounds = VisualUtils.GetMaxBounds(handleVisual);
            float maxDim             = VisualUtils.GetMaxComponent(handleVisualBounds.size);
            float invScale           = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;

            handleVisual.name                    = visualsName;
            handleVisual.transform.parent        = parent.transform;
            handleVisual.transform.localScale    = new Vector3(invScale, invScale, invScale);
            handleVisual.transform.localPosition = Vector3.zero;
            handleVisual.transform.localRotation = Quaternion.identity;


            Quaternion realignment = GetRotationRealignment(handleIndex, isFlattened);

            parent.transform.localRotation = realignment;

            if (config.HandleMaterial != null)
            {
                VisualUtils.ApplyMaterialToAllRenderers(handleVisual, config.HandleMaterial);
            }

            return(handleVisualBounds);
        }
        private Bounds CreateVisual(int handleIndex, GameObject parent)
        {
            GameObject handleVisual;
            GameObject prefabType = config.HandlePrefab;

            if (prefabType != null)
            {
                handleVisual = Object.Instantiate(prefabType);
            }
            else
            {
                handleVisual = GameObject.CreatePrimitive(PrimitiveType.Sphere);
                // We only want the Primitive sphere mesh, but CreatePrimitive will
                // give us a sphere collider too. Remove the sphere collider here
                // so we can manually add our own properly configured collider later.
                var collider = handleVisual.GetComponent <SphereCollider>();
                collider.enabled = false;

                // Caution, Destroy() will destroy one frame later.
                // Do not check later for presence this frame!
                Object.Destroy(collider);
            }

            // handleVisualBounds are returned in handleVisual-local space.
            Bounds handleVisualBounds = VisualUtils.GetMaxBounds(handleVisual);
            float  maxDim             = VisualUtils.GetMaxComponent(handleVisualBounds.size);
            float  invScale           = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;

            handleVisual.name                    = visualsName;
            handleVisual.transform.parent        = parent.transform;
            handleVisual.transform.localScale    = new Vector3(invScale, invScale, invScale);
            handleVisual.transform.localPosition = Vector3.zero;
            handleVisual.transform.localRotation = Quaternion.identity;

            Quaternion realignment = GetRotationRealignment(handleIndex);

            parent.transform.localRotation = realignment;

            if (config.HandleMaterial != null)
            {
                VisualUtils.ApplyMaterialToAllRenderers(handleVisual, config.HandleMaterial);
            }

            return(handleVisualBounds);
        }
        private void UpdateColliderType()
        {
            foreach (var handle in handles)
            {
                // remove old colliders
                bool shouldCreateNewCollider = false;
                var  oldBoxCollider          = handle.GetComponent <BoxCollider>();
                if (oldBoxCollider != null && config.HandlePrefabColliderType == HandlePrefabCollider.Sphere)
                {
                    shouldCreateNewCollider = true;
                    Object.Destroy(oldBoxCollider);
                }

                var oldSphereCollider = handle.GetComponent <SphereCollider>();
                if (oldSphereCollider != null && config.HandlePrefabColliderType == 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.HandlePrefabColliderType == 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);
                    }
                }
            }
        }
        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.HandlePrefabColliderType == 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);
            }
        }
        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);
                // deactivate collider on visuals and register for deletion - actual collider
                // of handle is attached to the handle gameobject, not the visual
                var collider = midpointVisual.GetComponent <SphereCollider>();
                collider.enabled = false;
                Object.Destroy(collider);
            }

            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);
        }
        private void CreateHandles(Transform parent)
        {
            for (int i = 0; i < HandlePositions.Length; ++i)
            {
                GameObject handle = new GameObject();
                handle.name = HandlePositionDescription + "_" + i.ToString();
                handle.transform.position = HandlePositions[i];
                handle.transform.parent   = parent;

                Bounds midpointBounds = CreateVisual(i, handle);
                float  maxDim         = VisualUtils.GetMaxComponent(midpointBounds.size);
                float  invScale       = maxDim == 0.0f ? 0.0f : config.HandleSize / maxDim;
                VisualUtils.AddComponentsToAffordance(handle, new Bounds(midpointBounds.center * invScale, midpointBounds.size * invScale),
                                                      config.HandlePrefabColliderType, CursorContextInfo.CursorAction.Rotate, config.ColliderPadding, parent, config.DrawTetherWhenManipulating);

                handles.Add(handle.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);
        }