2018-09-20 metaII 单手Scale

// Copyright © 2018, Meta Company. All rights reserved.

//

// Redistribution and use of this software (the "Software") in binary form, without modification, is

// permitted provided that the following conditions are met:

//

// 1.      Redistributions of the unmodified Software in binary form must reproduce the above

//        copyright notice, this list of conditions and the following disclaimer in the

//        documentation and/or other materials provided with the distribution.

// 2.      The name of Meta Company (“Meta”) may not be used to endorse or promote products derived

//        from this Software without specific prior written permission from Meta.

// 3.      LIMITATION TO META PLATFORM: Use of the Software is limited to use on or in connection

//        with Meta-branded devices or Meta-branded software development kits.  For example, a bona

//        fide recipient of the Software may incorporate an unmodified binary version of the

//        Software into an application limited to use on or in connection with a Meta-branded

//        device, while he or she may not incorporate an unmodified binary version of the Software

//        into an application designed or offered for use on a non-Meta-branded device.

//

// For the sake of clarity, the Software may not be redistributed under any circumstances in source

// code form, or in the form of modified binary code – and nothing in this License shall be construed

// to permit such redistribution.

//

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,

// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A

// PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL META COMPANY BE LIABLE FOR ANY DIRECT,

// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,

// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS

// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT

// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using Meta.Extensions;

using UnityEngine;

namespace Meta

{

    /// <summary>

    /// Interaction to scale model by placing two hands into the model and grabbing.

    /// </summary>

    [AddComponentMenu("Meta/Interaction/TwoHandGrabScaleInteraction")]

    public class TwoHandGrabScaleInteraction : TwoHandInteraction

    {

        /// <summary>

        /// Minimum scale

        /// </summary>

        [SerializeField]

        private Vector2 _minSize = new Vector2(.3f, .3f);

        /// <summary>

        /// Maximum scale

        /// </summary>

        [SerializeField]

        private Vector2 _maxSize = new Vector2(2, 2);

        public GameObject bottomPos;

        /// <summary>

        /// Whether to size delta for the TargetTransform should be changed. If false, the local scale will be used.

        /// </summary>

        [SerializeField]

        private bool _scaleSizeDelta = false;

        [SerializeField]

        private Vector3UnityEvent _scaleChanged = new Vector3UnityEvent();

        [Tooltip("Make objects scale exponentially using a gamma scale power rather than linearly")]

        [SerializeField]

        private bool _exponentialScaling = false;

        public new Vector3 newScaleP;

        public new Vector3 newCenterPos;

        public float localScaleY;

        private float _priorDistance;

        private RectTransform _rectTransform;

        private const float _gammaScalePower = 1.4f;

        /// <summary>

        /// Minimum scale

        /// </summary>

        public Vector2 MinSize

        {

            get { return _minSize; }

            set { _minSize = value; }

        }

        /// <summary>

        /// Maximum scale

        /// </summary>

        public Vector2 MaxSize

        {

            get { return _maxSize; }

            set { _maxSize = value; }

        }

        public Vector3UnityEvent ScaleChanged

        {

            get { return _scaleChanged; }

        }

        protected override void Engage()

        {

            _priorDistance = Vector3.Distance(FirstGrabbingHand.transform.position,SecondGrabbingHand.transform.position);

            PrepareRigidbodyForInteraction();

            base.Engage();

        }

        protected override void Disengage()

        {

            RestoreRigidbodySettingsAfterInteraction();

            base.Disengage();

        }

        protected override void Manipulate()

        {

            newCenterPos = TargetTransform.position;

            newCenterPos.y = (FirstGrabbingHand.transform.position.y + bottomPos.transform.position.y) / 2f;

            Vector3 center = newCenterPos;

            //(FirstGrabbingHand.transform.position + SecondGrabbingHand.transform.position) / 2f;

            Vector3 offset = Vector3.zero;

            //TargetTransform.position - center;

            float currentDistance = Vector3.Distance(FirstGrabbingHand.transform.position,

                                                    SecondGrabbingHand.transform.position);

            float multiplier = currentDistance / _priorDistance;

            multiplier = Mathf.Clamp(multiplier, .5f, 1.5f);

            if (_exponentialScaling)

            {

                multiplier = Mathf.Pow(multiplier, _gammaScalePower);

            }

            RectTransform rectTransform = TargetTransform as RectTransform;

            if (rectTransform != null && _scaleSizeDelta)

            {

                Vector2 newSize = rectTransform.sizeDelta * multiplier;

                if (newSize.IsNaN())

                {

                    return;

                }

                if (newSize.x < _maxSize.x && newSize.y < _maxSize.y && newSize.x > _minSize.x && newSize.y > _minSize.y)

                {

                    rectTransform.sizeDelta = newSize;

                    OnScaleChanged(newSize);

                    Move(center + (offset * multiplier));

                }

            }

            else

            {

                Vector3 newScale = TargetTransform.localScale * multiplier;

                if (newScale.IsNaN())

                {

                    return;

                }

                if (newScale.x < _maxSize.x && newScale.y < _maxSize.y && newScale.x > _minSize.x && newScale.y > _minSize.y)

                {

                    newScaleP = TargetTransform.localScale;

                    newScaleP.y = newScale.y;

                    TargetTransform.localScale = newScaleP;

                    OnScaleChanged(newScale);

                    Move(center + (offset * multiplier));

                }

            }

            _priorDistance = currentDistance;

        }

        private void OnScaleChanged(Vector3 scale)

        {

            _scaleChanged.Invoke(scale);

        }


    }


}




// Copyright © 2018, Meta Company. All rights reserved.

//

// Redistribution and use of this software (the "Software") in binary form, without modification, is

// permitted provided that the following conditions are met:

//

// 1.      Redistributions of the unmodified Software in binary form must reproduce the above

//        copyright notice, this list of conditions and the following disclaimer in the

//        documentation and/or other materials provided with the distribution.

// 2.      The name of Meta Company (“Meta”) may not be used to endorse or promote products derived

//        from this Software without specific prior written permission from Meta.

// 3.      LIMITATION TO META PLATFORM: Use of the Software is limited to use on or in connection

//        with Meta-branded devices or Meta-branded software development kits.  For example, a bona

//        fide recipient of the Software may incorporate an unmodified binary version of the

//        Software into an application limited to use on or in connection with a Meta-branded

//        device, while he or she may not incorporate an unmodified binary version of the Software

//        into an application designed or offered for use on a non-Meta-branded device.

//

// For the sake of clarity, the Software may not be redistributed under any circumstances in source

// code form, or in the form of modified binary code – and nothing in this License shall be construed

// to permit such redistribution.

//

// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,

// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A

// PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL META COMPANY BE LIABLE FOR ANY DIRECT,

// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,

// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA OR PROFITS; OR BUSINESS

// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT

// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using Meta.HandInput;

using Meta.Extensions;

using UnityEngine;

namespace Meta

{

    /// <summary>

    /// Interaction to grab the model to translate its position.

    /// </summary>

    [AddComponentMenu("Meta/Interaction/GrabInteraction")]

    public class GrabInteraction : Interaction

    {

        private HandFeature _handFeature;

        public GameObject bottomPosition;

        public Vector3 newCenterPos;

        private float _PriorDistance;

        public Vector3 newScaleP;

        private Vector3UnityEvent _scaleChanged = new Vector3UnityEvent();

        private Vector3 _localOffset;

        protected override bool CanEngage(Hand handProxy)

        {

            return GrabbingHands.Count == 1;

        }

        protected override void Engage()

        {

            _handFeature = GrabbingHands[0];

            PrepareRigidbodyForInteraction();

            _PriorDistance = Vector3.Distance(_handFeature.transform.position, bottomPosition.transform.position);

            // Store the offset of the object local to the hand feature.  This will be used to keep the object at the same distance from the hand when being moved.


            SetHandOffsets();

        }

        public Vector3UnityEvent ScaleChanged

        {

            get { return _scaleChanged; }

        }

        protected override bool CanDisengage(Hand handProxy)

        {

            if (_handFeature != null && handProxy.Palm == _handFeature)

            {

                foreach (var hand in GrabbingHands)

                {

                    if (hand != _handFeature)

                    {

                        _handFeature = hand;

                        SetHandOffsets();

                        return false;

                    }

                }

                return true;

            }

            return false;

        }

        protected override void Disengage()

        {

            Manipulate();

            RestoreRigidbodySettingsAfterInteraction();

            _handFeature = null;

        }

        protected override void Manipulate()

        {

            Move((TransformedHandPos() + bottomPosition.transform.position) / 2f);

            newCenterPos = TargetTransform.position;

            newCenterPos.y = (_handFeature.transform.position.y + bottomPosition.transform.position.y) / 2f;

            float currentDistance = Vector3.Distance(_handFeature.transform.position, bottomPosition.transform.position);

            float multiplier = currentDistance / _PriorDistance;

            multiplier = Mathf.Clamp(multiplier, 0.5f, 1.5f);

            Vector3 newScale = TargetTransform.localScale * multiplier;

            newScaleP = TargetTransform.localScale;

            newScaleP.y = newScale.y;

            TargetTransform.localScale = newScaleP;

            OnScaleChanged(newScale);

            //Vector3 center = newCenterPos;

        }

        private Vector3 TransformedHandPos()

        {

            // This complements obtaining the offset - used to convert back to world space.

            Vector3 offset = _handFeature.transform.TransformDirection(_localOffset);

            Vector3 grabPosition = _handFeature.transform.position + offset;

            return grabPosition;

        }

        private void SetHandOffsets()

        {

            _localOffset = _handFeature.transform.InverseTransformDirection(TargetTransform.position - _handFeature.Position);

            SetGrabOffset(TransformedHandPos());

        }

        private void OnScaleChanged(Vector3 scale)

        {

            _scaleChanged.Invoke(scale);

        }

    }

}




using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class ScaleController : MonoBehaviour {

    public GameObject cube;

    public GameObject top;

    public GameObject bottom;

    public Vector3 newScale;

    public float defaultDistance;


// Use this for initialization

void Start () {

        defaultDistance = Vector3.Distance(top.transform.position,  bottom.transform.position);

}

// Update is called once per frame

void Update () {

        float currentDistance = Vector3.Distance(top.transform.position, bottom.transform.position);

        float multiplier = currentDistance / defaultDistance;

        multiplier = Mathf.Clamp(multiplier, 0.5f, 1.5f);

        cube.transform.localScale = newScale;



    }

}





using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class TestTarget : MonoBehaviour {

    public GameObject cube;

    public UnityEngine.AI.NavMeshSurface naveMeshSurface;

    public bool hasPath;

    private UnityEngine.AI.NavMeshAgent agent;

    //UnityEngine.AI.NavMeshPath path;

// Use this for initialization

void Start () {

        agent = GetComponent<UnityEngine.AI.NavMeshAgent>();

        Debug.Log(hasPath);

}

// Update is called once per frame

void Update () {

        GetComponent<UnityEngine.AI.NavMeshAgent>().destination = cube.transform.position;

        if(Input.GetKeyDown(KeyCode.A))

        {

            Debug.Log("A");

            naveMeshSurface.BuildNavMesh();

        }

        //hasPath = agent.CalculatePath(cube.transform.position, path);

        if(agent.hasPath == true)

        {

            Debug.Log("A");

        }

        else

        {

            UnityEngine.AI.NavMeshHit navHit;

            if (agent.Raycast(cube.transform.position, out navHit))

            {

                agent.SetDestination(navHit.position);

            }

        }

        //if (agent.pathStatus == UnityEngine.AI.NavMeshPathStatus.PathComplete)

        //{

        //    Debug.Log("A");

        //}

        //else if (agent.pathStatus == UnityEngine.AI.NavMeshPathStatus.PathInvalid)

        //{

        //    Debug.Log("B");

        //}

        //else if (agent.pathStatus == UnityEngine.AI.NavMeshPathStatus.PathPartial)

        //{

        //    Debug.Log("C");

        //}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • 最高人民法院民一庭意见 在审理同居关系纠纷时,对当事人同居期间所得的工资、奖金和生产、经营的收益以及因继承、赠与等...
    Sunshinelawyer阅读 159评论 0 0
  • 蒙克博物馆(Munch museet)这座博物馆是为了纪念挪威著名画家爱德华•蒙克而建。馆内收藏的作品包括传世名作...
    慕溪北欧旅游阅读 585评论 0 3
  • 其实我一直都没有写读书笔记的习惯,但是2016年,受到很多人共同的影响,主要是一些公众号一些知乎回答的成功人士的谆...
    袋鼠的尾巴阅读 298评论 0 2