コード例 #1
0
ファイル: Node.cs プロジェクト: unitycoder/Drawing3D
        /// <summary>
        /// Writes the managed data to the native value.
        /// </summary>
        /// <param name="thisPtr">Optional pointer to the memory that will hold the native value.</param>
        /// <param name="nativeValue">Output native value</param>
        void IMarshalable <Node, AiNode> .ToNative(IntPtr thisPtr, out AiNode nativeValue)
        {
            nativeValue                = new AiNode(); // --------------------------------
            nativeValue.Name           = new AiString(m_name);
            nativeValue.Transformation = AssimpConv.ConvertTransformToAssimp(m_transform);
            nativeValue.Parent         = IntPtr.Zero;

            nativeValue.NumMeshes = (uint)m_meshes.Count;
            nativeValue.Meshes    = IntPtr.Zero;
#if WITH_NODE_METADATA
            nativeValue.MetaData = IntPtr.Zero;
#endif

            if (nativeValue.NumMeshes > 0)
            {
                nativeValue.Meshes = MemoryHelper.ToNativeArray <int>(m_meshes.ToArray());
            }

            //Now descend through the children
            nativeValue.NumChildren = (uint)m_children.Count;

            int numChildren = (int)nativeValue.NumChildren;
            int stride      = IntPtr.Size;

            IntPtr childrenPtr = IntPtr.Zero;

            if (numChildren > 0)
            {
                childrenPtr = MemoryHelper.AllocateMemory(numChildren * IntPtr.Size);

                for (int i = 0; i < numChildren; i++)
                {
                    IntPtr currPos = MemoryHelper.AddIntPtr(childrenPtr, stride * i);
                    Node   child   = m_children[i];

                    IntPtr childPtr = IntPtr.Zero;

                    //Recursively create the children and its children
                    if (child != null)
                    {
                        childPtr = ToNativeRecursive(thisPtr, child);
                    }

                    //Write the child's node ptr to our array
                    MemoryHelper.Write <IntPtr>(currPos, ref childPtr);
                }
            }

            //Finally finish writing to the native struct
            nativeValue.Children = childrenPtr;
        }
コード例 #2
0
ファイル: Node.cs プロジェクト: unitycoder/Drawing3D
        /// <summary>
        /// Reads the unmanaged data from the native value.
        /// </summary>
        /// <param name="nativeValue">Input native value</param>
        void IMarshalable <Node, AiNode> .FromNative(ref AiNode nativeValue)
        {
            m_name      = nativeValue.Name.GetString();
            m_transform = AssimpConv.ConvertTransform(nativeValue.Transformation);
            m_parent    = null;
            m_children.Clear();
            m_meshes.Clear();

            if (nativeValue.NumMeshes > 0 && nativeValue.Meshes != IntPtr.Zero)
            {
                m_meshes.AddRange(MemoryHelper.FromNativeArray <int>(nativeValue.Meshes, (int)nativeValue.NumMeshes));
            }

            if (nativeValue.NumChildren > 0 && nativeValue.Children != IntPtr.Zero)
            {
                m_children.AddRange(MemoryHelper.FromNativeArray <Node, AiNode>(nativeValue.Children, (int)nativeValue.NumChildren, true));
            }
        }
コード例 #3
0
ファイル: Node.cs プロジェクト: unitycoder/Drawing3D
        private IntPtr ToNativeRecursive(IntPtr parentPtr, Node node)
        {
            if (node == null)
            {
                return(IntPtr.Zero);
            }

            int sizeofNative = MemoryHelper.SizeOf <AiNode>();

            //Allocate the memory that will hold the node
            IntPtr nodePtr = MemoryHelper.AllocateMemory(sizeofNative);

            //First fill the native struct
            AiNode nativeValue = new AiNode();

            nativeValue.Name           = new AiString(node.m_name);
            nativeValue.Transformation = AssimpConv.ConvertTransformToAssimp(node.m_transform);
            nativeValue.Parent         = parentPtr;

            nativeValue.NumMeshes = (uint)node.m_meshes.Count;
            nativeValue.Meshes    = MemoryHelper.ToNativeArray <int>(node.m_meshes.ToArray());
#if WITH_NODE_METADATA
            nativeValue.MetaData = IntPtr.Zero;
#endif

            //Now descend through the children
            nativeValue.NumChildren = (uint)node.m_children.Count;

            int numChildren = (int)nativeValue.NumChildren;
            int stride      = IntPtr.Size;

            IntPtr childrenPtr = IntPtr.Zero;

            if (numChildren > 0)
            {
                childrenPtr = MemoryHelper.AllocateMemory(numChildren * IntPtr.Size);

                for (int i = 0; i < numChildren; i++)
                {
                    IntPtr currPos = MemoryHelper.AddIntPtr(childrenPtr, stride * i);
                    Node   child   = node.m_children[i];

                    IntPtr childPtr = IntPtr.Zero;

                    //Recursively create the children and its children
                    if (child != null)
                    {
                        childPtr = ToNativeRecursive(nodePtr, child);
                    }

                    //Write the child's node ptr to our array
                    MemoryHelper.Write <IntPtr>(currPos, ref childPtr);
                }
            }

            //Finall finish writing to the native struct, and write the whole thing to the memory we allocated previously
            nativeValue.Children = childrenPtr;

            MemoryHelper.Write <AiNode>(nodePtr, ref nativeValue);

            return(nodePtr);
        }