Ejemplo n.º 1
0
    public void OnClickCalibrateFacePoints()
    {
        Vector3 face;
        Vector3 leftEye;
        Vector3 rightEye;

        SMT.getFacePoints(out face, out leftEye, out rightEye);

        _calibratedFacePosition     = face;
        _calibratedLeftEyePosition  = leftEye;
        _calibratedRigthEyePosition = rightEye;
    }
Ejemplo n.º 2
0
    void trackingFacePoints()
    {
        if (SMT.isFacePointsDetected())
        {
            Vector3 face;
            Vector3 leftEye;
            Vector3 rightEye;
            SMT.getFacePoints(out face, out leftEye, out rightEye);

            // 目の間の距離
            var eyeLtoR = rightEye - leftEye;
            eyeLtoR.z = 0;
            //float eyeDistance = eyeLtoR.magnitude;
            var faceLength     = face.z * 2; // 顏の直径
            var standardLength = 0.20f;      // 20cm
            var pixParM        = standardLength / faceLength;

            var facePosition = face - _calibratedFacePosition;
            facePosition.y *= -1;
            facePosition   *= pixParM;

            var faceRotationEuler = Vector3.zero;
            if (_calibratedFacePosition.z < face.z)
            {
                facePosition.z = -1 + (_calibratedFacePosition.z / face.z);
            }
            else
            {
                facePosition.z = 0;
            }

            faceRotationEuler.z = -Mathf.Atan(eyeLtoR.y / eyeLtoR.x) * Mathf.Rad2Deg;

            var radY = facePosition.x / _faceAngleBaseDistance;
            if (Mathf.Abs(radY) > 1.0f)
            {
                radY = radY / Mathf.Abs(radY); // -1 or 1に補正
            }
            faceRotationEuler.y = -Mathf.Asin(radY) * Mathf.Rad2Deg;


            var radX = facePosition.y / _faceAngleBaseDistance;
            if (Mathf.Abs(radX) > 1.0f)
            {
                radX = radX / Mathf.Abs(radX); // -1 or 1に補正
            }
            faceRotationEuler.x = Mathf.Asin(radX) * Mathf.Rad2Deg;
            faceRotationEuler.x = faceRotationEuler.x * 2; // 縦方向の検出は弱いので大きく動くように補正.

            // 倍率適用
            facePosition      = facePosition * _translationMagnification;
            faceRotationEuler = faceRotationEuler * _rotationMagnification;
            if (faceRotationEuler.x < -75)
            {
                faceRotationEuler.x = -75;
            }
            if (faceRotationEuler.x > 75)
            {
                faceRotationEuler.x = 75;
            }
            if (faceRotationEuler.z < -75)
            {
                faceRotationEuler.z = -75;
            }
            if (faceRotationEuler.z > 75)
            {
                faceRotationEuler.z = 75;
            }

            // オフセット適用
            facePosition.y += 1.3f;

            smoothTransform(ref facePosition, ref faceRotationEuler);

            var faceRotation = Quaternion.Euler(faceRotationEuler);
            _head.transform.position = facePosition;
            _head.transform.rotation = faceRotation;

            // 顏の中央位置を自動調整
            if (_autoAdjustmentRatio > 0)
            {
                var z = _calibratedFacePosition.z; // Zは維持、XYのみずれやすいので調整する
                _calibratedFacePosition   = (_calibratedFacePosition * (1.0f - _autoAdjustmentRatio)) + (face * _autoAdjustmentRatio);
                _calibratedFacePosition.z = z;
            }
        }
    }