Exemplo n.º 1
0
        private bool Initialized()
        {
            if (_initialized)
            {
                return(true);
            }

            Avatar = TEA_Manager.current.Avatar;

            if (null == Avatar)
            {
                return(false);
            }

            newPosition        = Avatar.transform.position;
            horizontalRotation = Avatar.transform.rotation;

            //--- paramters ---
            mainMenu   = Avatar.expressionsMenu;
            parameters = Avatar.expressionParameters;
            avatarAnim = Avatar.gameObject.GetComponent <Animator>();
            avatarAnim.runtimeAnimatorController = TEA_Manager.current.Controllers[TEA_Manager.AvatarIndex()];
            Grounded = true;
            avatarAnim.SetBool("Grounded", Grounded);

            TEA_PlayableLayerData layerData = TEA_Manager.current.LayerInfo[TEA_Manager.AvatarIndex()];

            TEA_PlayableLayerData.PlayableLayerData data = layerData.FindPlayableLayerData(VRCAvatarDescriptor.AnimLayerType.Base);
            for (int i = data.start; i < data.end; i++)
            {
                avatarAnim.SetLayerWeight(i, 1);
            }

            //--- events ---
            controlEvents = new Dictionary <VRC.SDKBase.VRC_PlayableLayerControl.BlendableLayer, TEA_PlayableLayerControl>();
            if (null == TEA_PlayableLayerControl.ApplySettings)
            {
                TEA_PlayableLayerControl.ApplySettings += TEA_PlayableLayerEvent;
            }

            _initialized = true;
            return(true);
        }
Exemplo n.º 2
0
        private void Update()
        {
            if (!Initialized())
            {
                return;
            }

            //--- controls ---
            List <VRCPlayableLayerControl.BlendableLayer> remove = new List <VRC.SDKBase.VRC_PlayableLayerControl.BlendableLayer>();

            foreach (KeyValuePair <VRCPlayableLayerControl.BlendableLayer, TEA_PlayableLayerControl> item in controlEvents)
            {
                TEA_PlayableLayerData layerData = TEA_Manager.current.LayerInfo[TEA_Manager.AvatarIndex()];
                TEA_PlayableLayerData.PlayableLayerData data = layerData.FindPlayableLayerData(TEA_PlayableLayerControl.AnimLayerType(item.Value.layer));
                float prevDur = item.Value.duration;
                item.Value.duration += Time.deltaTime;
                float normalized = item.Value.duration / item.Value.blendDuration;
                for (int i = data.start; i < data.end; i++)
                {
                    if (normalized >= 1)
                    {
                        item.Value.animator.SetLayerWeight(i, item.Value.goalWeight);
                        remove.Add(item.Value.layer);
                    }
                    else
                    {
                        float newWeight = Mathf.Lerp(item.Value.animator.GetLayerWeight(i), item.Value.goalWeight, (item.Value.duration - prevDur) / (item.Value.blendDuration - prevDur));
                        item.Value.animator.SetLayerWeight(i, newWeight);
                    }
                }
            }

            foreach (VRCPlayableLayerControl.BlendableLayer rm in remove)
            {
                controlEvents.Remove(rm);
            }

            if (CameraController.mouseIn && Input.GetMouseButton(1) && !CameraController.FreeCamera)
            {
                newPosition = Vector3.zero;
                Avatar.transform.position = newPosition;
                VelocityX = 0;
                VelocityZ = 0;
            }
            else if (null != Locomotion && CameraController.mouseIn && !CameraController.FreeCamera)
            {
                //Rotate
                float turnAmount = Mathf.Ceil(Locomotion.RotationAmount * Time.deltaTime);
                if (Input.GetKey(KeyCode.E))
                {
                    horizontalRotation *= Quaternion.Euler(Vector3.up * turnAmount);
                }
                if (Input.GetKey(KeyCode.Q))
                {
                    horizontalRotation *= Quaternion.Euler(Vector3.up * -turnAmount);
                }
                Avatar.transform.rotation = horizontalRotation;

                // --- input ---
                if (Input.GetKeyUp(KeyCode.LeftShift))
                {
                    speed++;
                }
                if (3 <= speed)
                {
                    speed = 0;
                }

                if (0 == speed)
                {
                    Locomotion.MoveType = TEA_Settings.MoveTypes.Walk;
                    velocity            = Locomotion.WalkVelocity;
                }
                else if (1 == speed)
                {
                    Locomotion.MoveType = TEA_Settings.MoveTypes.Run;
                    velocity            = Locomotion.RunVelocity;
                }
                else if (2 == speed)
                {
                    Locomotion.MoveType = TEA_Settings.MoveTypes.Sprint;
                    velocity            = Locomotion.SprintVelocity;
                }

                // Walk
                int forward  = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow) ? 1 : 0;
                int backward = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow) ? -1 : 0;
                int right    = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow) ? 1 : 0;
                int left     = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow) ? -1 : 0;
                int z        = forward + backward;
                int x        = right + left;

                if (x != 0 || z != 0)
                {
                    float   distance        = Time.deltaTime * velocity;
                    float   theta           = Mathf.Atan2(z, x);
                    float   distanceX       = Mathf.Cos(theta) * (distance);
                    float   distanceZ       = Mathf.Sin(theta) * (distance);
                    Vector3 cameraDirection = CameraController.RigCamera.transform.forward;

                    // Animator Velocities
                    if (TEA_Manager.current.ViewPort.gameObject.activeSelf)
                    {
                        newPosition = TEA_Utility.TransformPoint(Avatar.transform, new Vector3(distanceX, 0, distanceZ));
                        VelocityZ   = distanceZ / Time.deltaTime;
                    }
                    else
                    {
                        newPosition += TEA_Manager.current.CameraRigPointer.transform.right * distanceX;
                        newPosition += TEA_Manager.current.CameraRigPointer.transform.forward * distanceZ;
                        Vector3 movePoint  = TEA_Utility.InverseTransformPoint(Avatar.transform, newPosition);
                        float   aTheta     = Mathf.Deg2Rad * Avatar.transform.rotation.eulerAngles.y;
                        Vector3 movePointR = new Vector3();
                        movePointR.x = movePoint.x * Mathf.Cos(aTheta) - movePoint.z * Mathf.Sin(aTheta);
                        movePointR.z = movePoint.z * Mathf.Cos(aTheta) + movePoint.x * Mathf.Sin(aTheta);
                        VelocityX    = 0 + movePointR.x / Time.deltaTime;
                        VelocityZ    = 0 + movePointR.z / Time.deltaTime;
                    }

                    //Debug.Log($"[{x},{z}] distance[{distance}] float[{distanceX}, {distanceZ}] velocity[{VelocityX}, {VelocityZ}]");

                    // world position
                    Avatar.transform.position = newPosition;
                }
                else
                {
                    newPosition = Avatar.transform.position;
                    VelocityX   = 0;
                    VelocityZ   = 0;
                }
            }
            else
            {
                newPosition = Avatar.transform.position;
                VelocityX   = 0;
                VelocityZ   = 0;
            }
        }