void RefreshMetaData()
        {
            string metaDataJson = "";

            _metaSetup = false;
            switch (_metaDataSourceType)
            {
            case MetadataSourceType.FilePath:
                if (!string.IsNullOrEmpty(_metaDataFilePath))
                {
                    //TODO: allow for more than just streaming assets
                    metaDataJson = System.IO.File.ReadAllText(Path.Combine(Application.streamingAssetsPath, _metaDataFilePath));
                }
                break;

            case MetadataSourceType.TextAsset:
                if (_metaDataFile != null)
                {
                    metaDataJson = _metaDataFile.text;
                }
                break;
            }

            //If there is no metadata, bail!
            if (metaDataJson == "")
            {
                return;
            }

            try
            {
                _metaData = Depthkit_Metadata.CreateFromJSON(metaDataJson);
            }
            catch (System.Exception)
            {
                Debug.LogError("Invaid Depthkit Metadata Format. Make sure you are using the proper metadata export from Depthkit Visualize.");
                return;
            }

            if (_collider == null)
            {
                _collider = GetComponent <BoxCollider>();
            }

            _collider.center = _metaData.boundsCenter;
            _collider.size   = _metaData.boundsSize;

            if (_renderer != null)
            {
                _renderer.Metadata = _metaData;
            }

            _metaSetup = true;
        }
Ejemplo n.º 2
0
    static void ApplyMetadata(Depthkit.Depthkit_Metadata metadata, Material material)
    {
        var pers = metadata.perspectives[0];

        material.SetVector("_Crop", pers.crop);
        material.SetVector("_ImageDimensions", pers.depthImageSize);
        material.SetVector("_FocalLength", pers.depthFocalLength);
        material.SetVector("_PrincipalPoint", pers.depthPrincipalPoint);
        material.SetFloat("_NearClip", pers.nearClip);
        material.SetFloat("_FarClip", pers.farClip);
        material.SetMatrix("_Extrinsics", pers.extrinsics);
    }
        public static Depthkit_Metadata CreateFromJSON(string jsonString)
        {
            Depthkit_Metadata metadata;
            MetadataVersion   mdVersion = JsonUtility.FromJson <MetadataVersion>(jsonString);

            // Read and upgrade old single perspective format.
            if (mdVersion._versionMajor == 0 && mdVersion._versionMinor < 3)
            {
                MetadataSinglePerspective md = JsonUtility.FromJson <MetadataSinglePerspective>(jsonString);

                //for version 1.0 (from Depthkit Visualize) fill in defaults for missing parameters
                if (mdVersion.format == "perpixel" && mdVersion._versionMinor == 1)
                {
                    //set final image dimensions
                    md.textureWidth  = (int)(md.depthImageSize.x);
                    md.textureHeight = (int)(md.depthImageSize.y) * 2;

                    //calculate bounds
                    md.boundsCenter = new Vector3(0f, 0f, (md.farClip - md.nearClip) / 2.0f + md.nearClip);
                    md.boundsSize   = new Vector3(md.depthImageSize.x * md.farClip / md.depthFocalLength.x,
                                                  md.depthImageSize.y * md.farClip / md.depthFocalLength.y,
                                                  md.farClip - md.nearClip);

                    md.numAngles = 1;

                    // check if we have a zero'd crop (is this possible?), if so default to full window
                    if (md.crop.x <= eps && md.crop.y <= eps && md.crop.z <= eps && md.crop.w <= eps)
                    {
                        md.crop = new Vector4(0.0f, 0.0f, 1.0f, 1.0f);
                    }

                    md.extrinsics = Matrix4x4.identity;
                }

                if (md.clipEpsilon < eps)
                {
                    md.clipEpsilon = 0.005f; // default depth clipping epsilon, set for older versions of metadata
                }

                metadata = Depthkit_Metadata.FromSinglePerspective(md);

                // Inverse all extrinsics matrices
                for (int i = 0; i < metadata.perspectives.Length; ++i)
                {
                    metadata.perspectives[i].extrinsics = Matrix4x4.Inverse(metadata.perspectives[i].extrinsics);
                }
            }
            else
            {
                // Read multiperspective format.
                metadata = JsonUtility.FromJson <Depthkit_Metadata>(jsonString);
                metadata.boundsCenter.z *= -1;

                for (int i = 0; i < metadata.perspectives.Length; ++i)
                {
                    //unity's coordinate space is mirrored from what Depthkit produces, apply a simple inversion here
                    Matrix4x4 mirror = Matrix4x4.TRS(Vector3.zero, Quaternion.identity, new Vector3(1.0f, 1.0f, -1.0f));

                    metadata.perspectives[i].extrinsics    = mirror * metadata.perspectives[i].extrinsics;
                    metadata.perspectives[i].extrinsicsInv = metadata.perspectives[i].extrinsics.inverse;

                    metadata.perspectives[i].cameraCenter = (metadata.perspectives[i].extrinsics * new Vector4(0.0f, 0.0f, 0.0f, 1.0f));
                    metadata.perspectives[i].cameraNormal = (metadata.perspectives[i].extrinsics * new Vector4(0.0f, 0.0f, 1.0f, 0.0f)).normalized;
                }
            }

            return(metadata);
        }