Exemplo n.º 1
0
        //// user world pos & rot
        //private Vector3[] userWorldPos = null;
        //private Quaternion[] userWorldRot = null;


        void Start()
        {
            kinectManager = KinectManager.Instance;
            numSensors    = kinectManager ? kinectManager.GetSensorCount() : 0;

            if (numSensors > 1)
            {
                ShowDebugMessage(numSensors + " sensors found. Starting multi-camera setup...");
            }
            else
            {
                ShowErrorMessage(numSensors + " sensors found. No need for multi-camera setup.");
                return;
            }

            // user meshes per camera
            cameraUserMesh = new GameObject[numSensors];

            // init camera pos & rot
            savedCamPos = new List <Vector3> [numSensors];
            savedCamRot = new List <Quaternion> [numSensors];

            // init multi-camera pose
            multiCamPose.version  = 1;
            multiCamPose.camPose  = new KinectInterop.CameraPose[numSensors];
            multiCamPose.settings = new string[numSensors];

            for (int i = 0; i < numSensors; i++)
            {
                KinectInterop.SensorData sensorData = kinectManager.GetSensorData(i);

                if (sensorData != null && sensorData.sensorInterface != null)
                {
                    multiCamPose.camPose[i]             = new KinectInterop.CameraPose();
                    multiCamPose.camPose[i].sensorType  = (int)sensorData.sensorInterface.GetSensorPlatform();
                    multiCamPose.camPose[i].sensorIndex = ((DepthSensorBase)sensorData.sensorInterface).deviceIndex;
                    multiCamPose.camPose[i].sensorId    = sensorData.sensorInterface.GetSensorDeviceId();

                    DepthSensorBase.BaseSensorSettings settings = sensorData.sensorInterface.GetSensorSettings(null);
                    multiCamPose.settings[i] = JsonUtility.ToJson(settings);

                    if (i != 0)
                    {
                        // reset poses of all non-first cameras
                        sensorData.sensorInterface.SetSensorToWorldMatrix(Vector3.zero, Quaternion.identity, true);
                    }
                }

                savedCamPos[i] = new List <Vector3>();
                savedCamRot[i] = new List <Quaternion>();

                if (userMeshPrefab != null)
                {
                    cameraUserMesh[i]      = Instantiate(userMeshPrefab, transform);
                    cameraUserMesh[i].name = ((KinectInterop.DepthSensorPlatform)multiCamPose.camPose[i].sensorType).ToString() + multiCamPose.camPose[i].sensorIndex + "Mesh";

                    UserMeshRendererGpu meshRenderer = cameraUserMesh[i].GetComponent <UserMeshRendererGpu>();
                    if (meshRenderer != null)
                    {
                        meshRenderer.sensorIndex = i;
                    }
                }
            }

            //userWorldPos = new Vector3[numSensors];
            //userWorldRot = new Quaternion[numSensors];

            // initial user rotation
            initialUserRot = Quaternion.Euler(0f, 180f, 0f);  // always mirrored

            // start co-routine
            bStopCoroutine = false;
            StartCoroutine(EstimateCameraPoses());
        }
        void Start()
        {
            kinectManager = KinectManager.Instance;
            numSensors    = kinectManager ? kinectManager.GetSensorCount() : 0;
            numJoints     = trackedBodyJoint.Length;

            if (numSensors > 1)
            {
                ShowDebugMessage(numSensors + " sensors found. Starting multi-camera setup...");
            }
            else
            {
                ShowErrorMessage(numSensors + " sensor(s) found. No need for multi-camera setup.");
                return;
            }

            // user meshes & body transforms per camera
            if (userMeshPrefab != null)
            {
                cameraUserMesh = new GameObject[numSensors];
            }
            if (bodyTransformPrefab != null)
            {
                bodyTransforms = new Transform[numSensors];
            }

            // maximum saved samples
            maxSavedSamples = numberOfSamples;
            numMvaSamples   = Mathf.Min(Mathf.Max(Mathf.RoundToInt(maxSavedSamples * optimizeForPercentage), 1), maxSavedSamples);
            //Debug.Log("OptForPerc: " + numMvaSamples);

            minPoseError = new float[numSensors];
            minPoseIndex = new int[numSensors];

            // init user pos & rot
            userBodyPose = new UserBodyPose[numSensors];

            // init camera pos & rot
            savedCamPos   = new List <Vector3> [numSensors];
            savedCamRot   = new List <Quaternion> [numSensors];
            savedUserPose = new List <UserBodyPose> [numSensors];

            // init multi-camera pose
            multiCamPose.version  = 1;
            multiCamPose.camPose  = new KinectInterop.CameraPose[numSensors];
            multiCamPose.settings = new string[numSensors];

            for (int i = 0; i < numSensors; i++)
            {
                KinectInterop.SensorData sensorData = kinectManager.GetSensorData(i);

                if (sensorData != null && sensorData.sensorInterface != null)
                {
                    multiCamPose.camPose[i]             = new KinectInterop.CameraPose();
                    multiCamPose.camPose[i].sensorType  = (int)sensorData.sensorInterface.GetSensorPlatform();
                    multiCamPose.camPose[i].sensorIndex = ((DepthSensorBase)sensorData.sensorInterface).deviceIndex;
                    multiCamPose.camPose[i].sensorId    = sensorData.sensorInterface.GetSensorDeviceId();

                    DepthSensorBase.BaseSensorSettings settings = sensorData.sensorInterface.GetSensorSettings(null);
                    multiCamPose.settings[i] = JsonUtility.ToJson(settings);

                    // set master-sub frame sync
                    sensorData.sensorInterface.EnableSensorSync(sensorData, useSynchronizedSamples);
                    if (sensorData.sensorInterface.IsSensorMaster())
                    {
                        masterIndex = i;
                    }

                    if (i == 0)
                    {
                        // first camera
                        Transform sensorTrans = kinectManager.GetSensorTransform(0);
                        refCameraPos = sensorTrans.position;
                        refCameraRot = sensorTrans.rotation;

                        // src camera matrix
                        srcCameraMatrix = Matrix4x4.TRS(refCameraPos, refCameraRot, Vector3.one);
                    }

                    // reset poses of all cameras
                    sensorData.sensorInterface.SetSensorToWorldMatrix(Vector3.zero, Quaternion.identity, true);
                }

                userBodyPose[i] = new UserBodyPose(numJoints);

                savedCamPos[i]   = new List <Vector3>();
                savedCamRot[i]   = new List <Quaternion>();
                savedUserPose[i] = new List <UserBodyPose>();

                if (bodyTransformPrefab != null)
                {
                    GameObject userBodyTransform = Instantiate(bodyTransformPrefab, Vector3.zero, Quaternion.identity);
                    userBodyTransform.name = "BodyTransform" + i;

                    Renderer renderer = userBodyTransform.GetComponent <Renderer>();
                    if (renderer != null)
                    {
                        renderer.material.color = bodyTransformColor[i % bodyTransformColor.Length];
                    }

                    bodyTransforms[i] = userBodyTransform.transform;
                }

                if (userMeshPrefab != null)
                {
                    cameraUserMesh[i]      = Instantiate(userMeshPrefab, transform);
                    cameraUserMesh[i].name = ((KinectInterop.DepthSensorPlatform)multiCamPose.camPose[i].sensorType).ToString() + multiCamPose.camPose[i].sensorIndex + "Mesh";

                    UserMeshRendererGpu userMeshRenderer = cameraUserMesh[i].GetComponent <UserMeshRendererGpu>();
                    if (userMeshRenderer != null)
                    {
                        userMeshRenderer.sensorIndex = i;
                    }
                    else
                    {
                        SceneMeshRendererGpu sceneMeshRenderer = cameraUserMesh[i].GetComponent <SceneMeshRendererGpu>();
                        if (sceneMeshRenderer != null)
                        {
                            sceneMeshRenderer.sensorIndex = i;
                        }
                    }

                    if (bodyTransforms != null && bodyTransforms[i] != null)
                    {
                        bodyTransforms[i].parent = cameraUserMesh[i].transform;
                    }
                }

                //minPoseError[i] = float.MaxValue;
            }

            //userWorldPos = new Vector3[numSensors];
            //userWorldRot = new Quaternion[numSensors];

            // initial user rotation
            initialUserRot = Quaternion.Euler(0f, 180f, 0f);  // always mirrored

            if (updatePanel)
            {
                // hide the update panel
                updatePanel.gameObject.SetActive(false);

                sensorRotTransform = new Vector3[numSensors];
                sensorRotAdjust    = new Vector3[numSensors];

                sensorPosTransform = new Vector3[numSensors];
                sensorPosAdjust    = new Vector3[numSensors];

                if (sensorDropdown)
                {
                    // create list of sensor indices (and exclude the 1st one)
                    List <string> alSensorOptions = new List <string>();
                    for (int i = 1; i < numSensors; i++)
                    {
                        alSensorOptions.Add(i.ToString());
                    }

                    sensorDropdown.ClearOptions();
                    sensorDropdown.AddOptions(alSensorOptions);
                }
            }

            bool bConfigLoaded = false;

            if (kinectManager.useMultiCamConfig)
            {
                // load the config file
                bConfigLoaded = LoadMultiCamConfig();

                if (bConfigLoaded)
                {
                    // show cam-pose adjustment panel
                    ShowUpdatePanel();
                }
            }

            if (!bConfigLoaded)
            {
                // start auto-calibration co-routine
                bStopCoroutine = false;
                StartCoroutine(AutoCalibrateCameras());
            }
        }