Example #1
0
    private static void DispatchAddModelCallback(
        string description,
        string data,
        System.UInt32 dataSize,
        IntPtr clientData)
    {
        if (description == null)
        {
            return;
        }

        try
        {
            VLModelDeserializationStructure answerStructure =
                VLJsonUtility.FromJson <VLModelDeserializationStructure>(description);

            VLModelTrackableBehaviour trackable =
                (VLModelTrackableBehaviour)GCHandle.FromIntPtr(clientData).Target;

            // free data previously allocated/pinned
            trackable.FreeNextBinaryMemoryBlock();

            if (!String.IsNullOrEmpty(answerStructure.error))
            {
                Debug.Log("[vlUnitySDK] VLModelTrackableBehaviour.DispatchAddModelCallback: Error occurred while adding a model to the tracking system: "
                          + answerStructure.error);
            }

            if (answerStructure.result != null)
            {
                trackable.OnUpdateDeserializationResult(answerStructure.result);
            }
        }
        catch (Exception e) // Catch all exceptions, because this is a callback
                            // invoked from native code
        {
            Debug.LogError("[vlUnitySDK] " + e.GetType().Name + ": " +
                           e.Message);
        }
    }
Example #2
0
    /// <summary>
    /// Add a ModelDataDescriptors of the mesh inside the transform to the
    /// commandDescriptor
    /// </summary>
    /// <param name="transform">
    /// Transform, which is searched for possible meshes.
    /// </param>
    /// <param name="useAllChildNodes">
    /// If true: Also process child nodes, which are administered by another
    /// VLModelTrackableBehaviour
    /// </param>
    /// <param name="addDataDescriptor">
    /// Fills the ModelDataDescriptor with a DataDescriptor, a description of the
    /// data structure of the model data.
    /// </param>
    /// <param name="commandDescriptor">
    /// Reference to the command structure of the json command, which will be
    /// filled by this function.
    /// </param>
    /// <returns><c>True</c> if the model could be serialized into the visionlib.</c>False</c> if the data could not be gathered.</returns>
    private bool AddModelDescription(
        Transform transform,
        bool useAllChildNodes,
        bool addDataDescription,
        ref AddModelDataCommandDescription commandDescriptor)
    {
        // If transform is not active, do not add the model
        if (!transform.gameObject.activeInHierarchy)
        {
            return(false);
        }

        // See if another VLModelTrackableBehaviour is active in this transform. If
        // this is the case, break execution of this node and its children.
        VLModelTrackableBehaviour trackable =
            transform.GetComponent <VLModelTrackableBehaviour>();

        if (!useAllChildNodes &&
            trackable &&
            trackable != this &&
            trackable.enabled)
        {
            return(false);
        }
        if (trackable == null)
        {
            trackable = this;
        }


        Quaternion rotation    = transform.rotation;
        Vector3    globalScale = GetGlobalScale(transform);
        Vector3    position    = transform.position;

        // On HoloLens, the content node is added to the camera and thus the
        // transformation of the mesh will be changed. This change has to be
        // removed when streaming the data into the vlSDK
        Transform contentTransform = getContentTransform();

        if (contentTransform != null)
        {
            Vector3 contentGlobalScale = GetGlobalScale(contentTransform);
            rotation =
                Quaternion.Inverse(contentTransform.rotation) * rotation;
            globalScale = new Vector3(
                globalScale.x / contentGlobalScale.x,
                globalScale.y / contentGlobalScale.y,
                globalScale.z / contentGlobalScale.z);
            position =
                Quaternion.Inverse(contentTransform.rotation) *
                (position - contentTransform.position);
        }

        VLUnityCameraHelper.ToVLInPlace(ref position, ref rotation, modelTrackerBehaviour.workerBehaviour.flipCoordinateSystemHandedness);

        MeshFilter mesh = transform.GetComponent <MeshFilter>();
        string     uniqueUnityModelID = mesh.GetInstanceID().ToString();

        ModelTransform modelTransform = new ModelTransform();

        modelTransform.t = new float[] { position.x, position.y, position.z };
        modelTransform.s = new float[]
        { globalScale.x, globalScale.y, globalScale.z };
        modelTransform.q = new float[]
        { rotation.x, rotation.y, rotation.z, rotation.w };

        ModelDataDescriptor descriptor = new ModelDataDescriptor();

        descriptor.name      = uniqueUnityModelID;
        descriptor.type      = "model";
        descriptor.enabled   = trackable.useForTracking;
        descriptor.occluder  = trackable.occluder;
        descriptor.transform = modelTransform;
        if (addDataDescription)
        {
            descriptor.subModels = new BinaryDataDescriptor[]
            { CreateDataDescriptor(mesh) };
        }

        commandDescriptor.models.Add(descriptor);

        return(true);
    }