Ejemplo n.º 1
0
        /// <summary>
        /// Reads the unmanaged data from the native value.
        /// </summary>
        /// <param name="nativeValue">Input native value</param>
        void IMarshalable <Metadata, AiMetadata> .FromNative(ref AiMetadata nativeValue)
        {
            Clear();

            if (nativeValue.NumProperties == 0 || nativeValue.keys == IntPtr.Zero || nativeValue.Values == IntPtr.Zero)
            {
                return;
            }

            AiString[]        keys    = MemoryHelper.FromNativeArray <AiString>(nativeValue.keys, (int)nativeValue.NumProperties);
            AiMetadataEntry[] entries = MemoryHelper.FromNativeArray <AiMetadataEntry>(nativeValue.Values, (int)nativeValue.NumProperties);

            for (int i = 0; i < nativeValue.NumProperties; i++)
            {
                String          key   = keys[i].GetString();
                AiMetadataEntry entry = entries[i];

                if (String.IsNullOrEmpty(key) || entry.Data == IntPtr.Zero)
                {
                    continue;
                }

                Object data = null;
                switch (entry.DataType)
                {
                case MetaDataType.Bool:
                    data = MemoryHelper.Read <bool>(entry.Data);
                    break;

                case MetaDataType.Float:
                    data = MemoryHelper.Read <float>(entry.Data);
                    break;

                case MetaDataType.Double:
                    data = MemoryHelper.Read <double>(entry.Data);
                    break;

                case MetaDataType.Int32:
                    data = MemoryHelper.Read <int>(entry.Data);
                    break;

                case MetaDataType.String:
                    AiString aiString = MemoryHelper.Read <AiString>(entry.Data);
                    data = aiString.GetString();
                    break;

                case MetaDataType.UInt64:
                    data = MemoryHelper.Read <UInt64>(entry.Data);
                    break;

                case MetaDataType.Vector3D:
                    data = MemoryHelper.Read <Vector3D>(entry.Data);
                    break;
                }

                if (data != null)
                {
                    Add(key, new Entry(entry.DataType, data));
                }
            }
        }
Ejemplo n.º 2
0
        /// <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 <Metadata, AiMetadata> .ToNative(IntPtr thisPtr, out AiMetadata nativeValue)
        {
            nativeValue = new AiMetadata();
            nativeValue.NumProperties = (uint)Count;

            AiString[]        keys    = new AiString[Count];
            AiMetadataEntry[] entries = new AiMetadataEntry[Count];
            int index = 0;

            foreach (KeyValuePair <String, Entry> kv in this)
            {
                AiMetadataEntry entry = new AiMetadataEntry();
                entry.DataType = kv.Value.DataType;

                switch (kv.Value.DataType)
                {
                case MetaDataType.Bool:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(bool));
                    bool boolValue = (bool)kv.Value.Data;
                    MemoryHelper.Write <bool>(entry.Data, ref boolValue);
                    break;

                case MetaDataType.Float:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(float));
                    float floatValue = (float)kv.Value.Data;
                    MemoryHelper.Write <float>(entry.Data, ref floatValue);
                    break;

                case MetaDataType.Double:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(double));
                    double doubleValue = (double)kv.Value.Data;
                    MemoryHelper.Write <double>(entry.Data, ref doubleValue);
                    break;

                case MetaDataType.Int32:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(int));
                    int intValue = (int)kv.Value.Data;
                    MemoryHelper.Write <int>(entry.Data, ref intValue);
                    break;

                case MetaDataType.String:
                    entry.Data = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <AiString>());
                    AiString aiStringValue = new AiString(kv.Value.Data as String);
                    MemoryHelper.Write <AiString>(entry.Data, ref aiStringValue);
                    break;

                case MetaDataType.UInt64:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(UInt64));
                    UInt64 uint64Value = (UInt64)kv.Value.Data;
                    MemoryHelper.Write <UInt64>(entry.Data, ref uint64Value);
                    break;

                case MetaDataType.Vector3D:
                    entry.Data = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <Vector3D>());
                    Vector3D vectorValue = (Vector3D)kv.Value.Data;
                    MemoryHelper.Write <Vector3D>(entry.Data, ref vectorValue);
                    break;
                }

                keys[index]    = new AiString(kv.Key);
                entries[index] = entry;
                index++;
            }

            nativeValue.keys   = MemoryHelper.ToNativeArray <AiString>(keys);
            nativeValue.Values = MemoryHelper.ToNativeArray <AiMetadataEntry>(entries);
        }
Ejemplo n.º 3
0
        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;

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

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

            //If has metadata, create it, otherwise it should be NULL
            if (m_metaData.Count > 0)
            {
                nativeValue.MetaData = MemoryHelper.ToNativePointer <Metadata, AiMetadata>(m_metaData);
            }

            //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);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{Bone, AiBone}.ToNative"/>.
        /// </summary>
        /// <param name="nativeValue">Native value to free</param>
        /// <param name="freeNative">True if the unmanaged memory should be freed, false otherwise.</param>
        public static void FreeNative(IntPtr nativeValue, bool freeNative)
        {
            if (nativeValue == IntPtr.Zero)
            {
                return;
            }

            AiBone aiBone     = MemoryHelper.Read <AiBone>(nativeValue);
            int    numWeights = MemoryHelper.Read <int>(MemoryHelper.AddIntPtr(nativeValue, MemoryHelper.SizeOf <AiString>()));
            IntPtr weightsPtr = MemoryHelper.AddIntPtr(nativeValue, MemoryHelper.SizeOf <AiString>() + sizeof(uint));

            if (aiBone.NumWeights > 0 && aiBone.Weights != IntPtr.Zero)
            {
                MemoryHelper.FreeMemory(aiBone.Weights);
            }

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }
        /// <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 <MeshAnimationAttachment, AiAnimMesh> .ToNative(IntPtr thisPtr, out AiAnimMesh nativeValue)
        {
            nativeValue.Name          = new AiString(m_name);
            nativeValue.Vertices      = IntPtr.Zero;
            nativeValue.Normals       = IntPtr.Zero;
            nativeValue.Tangents      = IntPtr.Zero;
            nativeValue.BiTangents    = IntPtr.Zero;
            nativeValue.Colors        = new AiMeshColorArray();
            nativeValue.TextureCoords = new AiMeshTextureCoordinateArray();
            nativeValue.NumVertices   = (uint)VertexCount;
            nativeValue.Weight        = m_weight;

            if (VertexCount > 0)
            {
                //Since we can have so many buffers of Vector3D with same length, lets re-use a buffer
                Vector3D[] copy = new Vector3D[VertexCount];

                nativeValue.Vertices = MemoryHelper.ToNativeArray <Vector3D>(CopyTo(m_vertices, copy));

                if (HasNormals)
                {
                    nativeValue.Normals = MemoryHelper.ToNativeArray <Vector3D>(CopyTo(m_normals, copy));
                }

                if (HasTangentBasis)
                {
                    nativeValue.Tangents   = MemoryHelper.ToNativeArray <Vector3D>(CopyTo(m_tangents, copy));
                    nativeValue.BiTangents = MemoryHelper.ToNativeArray <Vector3D>(CopyTo(m_bitangents, copy));
                }

                //Vertex Color channels
                for (int i = 0; i < m_colors.Length; i++)
                {
                    List <Color4D> list = m_colors[i];

                    if (list == null || list.Count == 0)
                    {
                        nativeValue.Colors[i] = IntPtr.Zero;
                    }
                    else
                    {
                        nativeValue.Colors[i] = MemoryHelper.ToNativeArray <Color4D>(list.ToArray());
                    }
                }

                //Texture coordinate channels
                for (int i = 0; i < m_texCoords.Length; i++)
                {
                    List <Vector3D> list = m_texCoords[i];

                    if (list == null || list.Count == 0)
                    {
                        nativeValue.TextureCoords[i] = IntPtr.Zero;
                    }
                    else
                    {
                        nativeValue.TextureCoords[i] = MemoryHelper.ToNativeArray <Vector3D>(CopyTo(list, copy));
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <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>
        unsafe void IMarshalable <Metadata, AiMetadata> .ToNative(IntPtr thisPtr, out AiMetadata nativeValue)
        {
            nativeValue = new AiMetadata();
            nativeValue.NumProperties = (uint)Count;

            AiString[]        keys    = new AiString[Count];
            AiMetadataEntry[] entries = new AiMetadataEntry[Count];
            int index = 0;

            foreach (KeyValuePair <String, Entry> kv in this)
            {
                AiMetadataEntry entry = new AiMetadataEntry();
                entry.DataType = kv.Value.DataType;

                switch (kv.Value.DataType)
                {
                case MetaDataType.Bool:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(bool));
                    bool boolValue = (bool)kv.Value.Data;
                    *((bool *)entry.Data) = boolValue;
                    break;

                case MetaDataType.Float:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(float));
                    float floatValue = (float)kv.Value.Data;
                    *((float *)entry.Data) = floatValue;
                    break;

                case MetaDataType.Int:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(int));
                    int intValue = (int)kv.Value.Data;
                    *((int *)entry.Data) = intValue;
                    break;

                case MetaDataType.String:
                    entry.Data = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <AiString>());
                    AiString aiStringValue = new AiString(kv.Value.Data as String);
                    *((AiString *)entry.Data) = aiStringValue;
                    break;

                case MetaDataType.UInt64:
                    entry.Data = MemoryHelper.AllocateMemory(sizeof(UInt64));
                    UInt64 uint64Value = (UInt64)kv.Value.Data;
                    *((ulong *)entry.Data) = uint64Value;
                    break;

                case MetaDataType.Vector3D:
                    entry.Data = MemoryHelper.AllocateMemory(MemoryHelper.SizeOf <Vector3>());
                    Vector3 vectorValue = (Vector3)kv.Value.Data;
                    *((Vector3 *)entry.Data) = vectorValue;
                    break;
                }

                keys[index]    = new AiString(kv.Key);
                entries[index] = entry;
                index++;
            }

            nativeValue.keys   = MemoryHelper.ToNativeArray <AiString>(keys);
            nativeValue.Values = MemoryHelper.ToNativeArray <AiMetadataEntry>(entries);
        }
Ejemplo n.º 7
0
        /// <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 <Scene, AiScene> .ToNative(IntPtr thisPtr, out AiScene nativeValue)
        {
            nativeValue.Flags       = m_flags;
            nativeValue.Materials   = IntPtr.Zero;
            nativeValue.RootNode    = IntPtr.Zero;
            nativeValue.Meshes      = IntPtr.Zero;
            nativeValue.Lights      = IntPtr.Zero;
            nativeValue.Cameras     = IntPtr.Zero;
            nativeValue.Textures    = IntPtr.Zero;
            nativeValue.Animations  = IntPtr.Zero;
            nativeValue.PrivateData = IntPtr.Zero;

            nativeValue.NumMaterials  = (uint)MaterialCount;
            nativeValue.NumMeshes     = (uint)MeshCount;
            nativeValue.NumLights     = (uint)LightCount;
            nativeValue.NumCameras    = (uint)CameraCount;
            nativeValue.NumTextures   = (uint)TextureCount;
            nativeValue.NumAnimations = (uint)AnimationCount;

            //Write materials
            if (nativeValue.NumMaterials > 0)
            {
                nativeValue.Materials = MemoryHelper.ToNativeArray <Material, AiMaterial>(m_materials.ToArray(), true);
            }

            //Write scenegraph
            if (m_rootNode != null)
            {
                nativeValue.RootNode = MemoryHelper.ToNativePointer <Node, AiNode>(m_rootNode);
            }

            //Write meshes
            if (nativeValue.NumMeshes > 0)
            {
                nativeValue.Meshes = MemoryHelper.ToNativeArray <Mesh, AiMesh>(m_meshes.ToArray(), true);
            }

            //Write lights
            if (nativeValue.NumLights > 0)
            {
                nativeValue.Lights = MemoryHelper.ToNativeArray <Light, AiLight>(m_lights.ToArray(), true);
            }

            //Write cameras
            if (nativeValue.NumCameras > 0)
            {
                nativeValue.Cameras = MemoryHelper.ToNativeArray <Camera, AiCamera>(m_cameras.ToArray(), true);
            }

            //Write textures
            if (nativeValue.NumTextures > 0)
            {
                nativeValue.Textures = MemoryHelper.ToNativeArray <EmbeddedTexture, AiTexture>(m_textures.ToArray(), true);
            }

            //Write animations
            if (nativeValue.NumAnimations > 0)
            {
                nativeValue.Animations = MemoryHelper.ToNativeArray <Animation, AiAnimation>(m_animations.ToArray(), true);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Frees unmanaged memory created by <see cref="IMarshalable{Mesh, AiMesh}.ToNative"/>.
        /// </summary>
        /// <param name="nativeValue">Native value to free</param>
        /// <param name="freeNative">True if the unmanaged memory should be freed, false otherwise.</param>
        public unsafe static void FreeNative(IntPtr nativeValue, bool freeNative)
        {
            if (nativeValue == IntPtr.Zero)
            {
                return;
            }

            AiMesh aiMesh = *((AiMesh *)nativeValue);

            if (aiMesh.NumVertices > 0)
            {
                if (aiMesh.Vertices != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Vertices);
                }

                if (aiMesh.Normals != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Normals);
                }

                if (aiMesh.Tangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.Tangents);
                }

                if (aiMesh.BiTangents != IntPtr.Zero)
                {
                    MemoryHelper.FreeMemory(aiMesh.BiTangents);
                }

                //Vertex Color channels
                for (int i = 0; i < aiMesh.Colors.Length; i++)
                {
                    IntPtr colorPtr = aiMesh.Colors[i];

                    if (colorPtr != IntPtr.Zero)
                    {
                        MemoryHelper.FreeMemory(colorPtr);
                    }
                }

                //Texture coordinate channels
                for (int i = 0; i < aiMesh.TextureCoords.Length; i++)
                {
                    IntPtr texCoordsPtr = aiMesh.TextureCoords[i];

                    if (texCoordsPtr != IntPtr.Zero)
                    {
                        MemoryHelper.FreeMemory(texCoordsPtr);
                    }
                }
            }

            //Faces
            if (aiMesh.NumFaces > 0 && aiMesh.Faces != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiFace>(aiMesh.Faces, (int)aiMesh.NumFaces, Face.FreeNative);
            }

            //Bones
            if (aiMesh.NumBones > 0 && aiMesh.Bones != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiBone>(aiMesh.Bones, (int)aiMesh.NumBones, Bone.FreeNative, true);
            }

            //Attachment meshes
            if (aiMesh.NumAnimMeshes > 0 && aiMesh.AnimMeshes != IntPtr.Zero)
            {
                MemoryHelper.FreeNativeArray <AiAnimMesh>(aiMesh.AnimMeshes, (int)aiMesh.NumAnimMeshes, MeshAnimationAttachment.FreeNative, true);
            }

            if (freeNative)
            {
                MemoryHelper.FreeMemory(nativeValue);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Reads the unmanaged data from the native value.
        /// </summary>
        /// <param name="nativeValue">Input native value</param>
        void IMarshalable <Mesh, AiMesh> .FromNative(ref AiMesh nativeValue)
        {
            ClearBuffers();

            int vertexCount = (int)nativeValue.NumVertices;

            m_name          = nativeValue.Name.GetString();
            m_materialIndex = (int)nativeValue.MaterialIndex;

            //Load Per-vertex components
            if (vertexCount > 0)
            {
                //Positions
                if (nativeValue.Vertices != IntPtr.Zero)
                {
                    m_vertices.AddRange(MemoryHelper.FromNativeArray <Vector3>(nativeValue.Vertices, vertexCount));
                }

                //Normals
                if (nativeValue.Normals != IntPtr.Zero)
                {
                    m_normals.AddRange(MemoryHelper.FromNativeArray <Vector3>(nativeValue.Normals, vertexCount));
                }

                //Tangents
                if (nativeValue.Tangents != IntPtr.Zero)
                {
                    m_tangents.AddRange(MemoryHelper.FromNativeArray <Vector3>(nativeValue.Tangents, vertexCount));
                }

                //BiTangents
                if (nativeValue.BiTangents != IntPtr.Zero)
                {
                    m_bitangents.AddRange(MemoryHelper.FromNativeArray <Vector3>(nativeValue.BiTangents, vertexCount));
                }

                //Vertex Color channels
                for (int i = 0; i < nativeValue.Colors.Length; i++)
                {
                    IntPtr colorPtr = nativeValue.Colors[i];

                    if (colorPtr != IntPtr.Zero)
                    {
                        m_colors[i].AddRange(MemoryHelper.FromNativeArray <Color4>(colorPtr, vertexCount));
                    }
                }

                //Texture coordinate channels
                for (int i = 0; i < nativeValue.TextureCoords.Length; i++)
                {
                    IntPtr texCoordsPtr = nativeValue.TextureCoords[i];

                    if (texCoordsPtr != IntPtr.Zero)
                    {
                        m_texCoords[i].AddRange(MemoryHelper.FromNativeArray <Vector3>(texCoordsPtr, vertexCount));
                    }
                }

                //UV components for each tex coordinate channel
                for (int i = 0; i < nativeValue.NumUVComponents.Length; i++)
                {
                    m_texComponentCount[i] = (int)nativeValue.NumUVComponents[i];
                }
            }

            //Faces
            if (nativeValue.NumFaces > 0 && nativeValue.Faces != IntPtr.Zero)
            {
                m_faces.AddRange(MemoryHelper.FromNativeArray <Face, AiFace>(nativeValue.Faces, (int)nativeValue.NumFaces));
            }

            //Bones
            if (nativeValue.NumBones > 0 && nativeValue.Bones != IntPtr.Zero)
            {
                m_bones.AddRange(MemoryHelper.FromNativeArray <Bone, AiBone>(nativeValue.Bones, (int)nativeValue.NumBones, true));
            }

            //Attachment meshes
            if (nativeValue.NumAnimMeshes > 0 && nativeValue.AnimMeshes != IntPtr.Zero)
            {
                m_meshAttachments.AddRange(MemoryHelper.FromNativeArray <MeshAnimationAttachment, AiAnimMesh>(nativeValue.AnimMeshes, (int)nativeValue.NumAnimMeshes, true));
            }
        }
Ejemplo n.º 10
0
        /// <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 <Mesh, AiMesh> .ToNative(IntPtr thisPtr, out AiMesh nativeValue)
        {
            nativeValue.Name            = new AiString(m_name);
            nativeValue.Vertices        = IntPtr.Zero;
            nativeValue.Normals         = IntPtr.Zero;
            nativeValue.Tangents        = IntPtr.Zero;
            nativeValue.BiTangents      = IntPtr.Zero;
            nativeValue.AnimMeshes      = IntPtr.Zero;
            nativeValue.Bones           = IntPtr.Zero;
            nativeValue.Faces           = IntPtr.Zero;
            nativeValue.Colors          = new AiMeshColorArray();
            nativeValue.TextureCoords   = new AiMeshTextureCoordinateArray();
            nativeValue.NumUVComponents = new AiMeshUVComponentArray();
            nativeValue.PrimitiveTypes  = m_primitiveType;
            nativeValue.MaterialIndex   = (uint)m_materialIndex;
            nativeValue.NumVertices     = (uint)VertexCount;
            nativeValue.NumBones        = (uint)BoneCount;
            nativeValue.NumFaces        = (uint)FaceCount;
            nativeValue.NumAnimMeshes   = (uint)MeshAnimationAttachmentCount;

            if (nativeValue.NumVertices > 0)
            {
                //Since we can have so many buffers of Vector3 with same length, lets re-use a buffer
                Vector3[] copy = new Vector3[nativeValue.NumVertices];

                nativeValue.Vertices = MemoryHelper.ToNativeArray <Vector3>(CopyTo(m_vertices, copy));

                if (HasNormals)
                {
                    nativeValue.Normals = MemoryHelper.ToNativeArray <Vector3>(CopyTo(m_normals, copy));
                }

                if (HasTangentBasis)
                {
                    nativeValue.Tangents   = MemoryHelper.ToNativeArray <Vector3>(CopyTo(m_tangents, copy));
                    nativeValue.BiTangents = MemoryHelper.ToNativeArray <Vector3>(CopyTo(m_bitangents, copy));
                }

                //Vertex Color channels
                for (int i = 0; i < m_colors.Length; i++)
                {
                    List <Color4> list = m_colors[i];

                    if (list == null || list.Count == 0)
                    {
                        nativeValue.Colors[i] = IntPtr.Zero;
                    }
                    else
                    {
                        nativeValue.Colors[i] = MemoryHelper.ToNativeArray <Color4>(list.ToArray());
                    }
                }

                //Texture coordinate channels
                for (int i = 0; i < m_texCoords.Length; i++)
                {
                    List <Vector3> list = m_texCoords[i];

                    if (list == null || list.Count == 0)
                    {
                        nativeValue.TextureCoords[i] = IntPtr.Zero;
                    }
                    else
                    {
                        nativeValue.TextureCoords[i] = MemoryHelper.ToNativeArray <Vector3>(CopyTo(list, copy));
                    }
                }

                //UV components for each tex coordinate channel
                for (int i = 0; i < m_texComponentCount.Length; i++)
                {
                    nativeValue.NumUVComponents[i] = (uint)m_texComponentCount[i];
                }
            }

            //Faces
            if (nativeValue.NumFaces > 0)
            {
                nativeValue.Faces = MemoryHelper.ToNativeArray <Face, AiFace>(m_faces.ToArray());
            }

            //Bones
            if (nativeValue.NumBones > 0)
            {
                nativeValue.Bones = MemoryHelper.ToNativeArray <Bone, AiBone>(m_bones.ToArray(), true);
            }

            //Attachment meshes
            if (nativeValue.NumAnimMeshes > 0)
            {
                nativeValue.AnimMeshes = MemoryHelper.ToNativeArray <MeshAnimationAttachment, AiAnimMesh>(m_meshAttachments.ToArray());
            }
        }
Ejemplo n.º 11
0
        private static unsafe byte[] SetMaterialString(String value, byte[] existing)
        {
            if (String.IsNullOrEmpty(value))
            {
                return(null);
            }

            int stringSize = Encoding.UTF8.GetByteCount(value);

            if (stringSize < 0)
            {
                return(null);
            }

            int size = stringSize + 1 + sizeof(int);

            byte[] data = existing;

            if (existing == null || existing.Length != size)
            {
                data = new byte[size];

                fixed(byte *bytePtr = &data[0])
                {
                    MemoryHelper.Write <int>(new IntPtr(bytePtr), ref stringSize);
                    byte[] utfBytes = Encoding.UTF8.GetBytes(value);
                    MemoryHelper.Write <byte>(new IntPtr(bytePtr + sizeof(int)), utfBytes, 0, utfBytes.Length);
                    //Last byte should be zero
                }

                return(data);
        }

        #region IMarshalable Implementation

        /// <summary>
        /// Gets if the native value type is blittable (that is, does not require marshaling by the runtime, e.g. has MarshalAs attributes).
        /// </summary>
        bool IMarshalable <MaterialProperty, AiMaterialProperty> .IsNativeBlittable
        {
            get { return(true); }
        }

        /// <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 <MaterialProperty, AiMaterialProperty> .ToNative(IntPtr thisPtr, out AiMaterialProperty nativeValue)
        {
            nativeValue.Key        = new AiString(m_name);
            nativeValue.Type       = m_type;
            nativeValue.Index      = (uint)m_texIndex;
            nativeValue.Semantic   = m_texType;
            nativeValue.Data       = IntPtr.Zero;
            nativeValue.DataLength = 0;

            if (m_rawValue != null)
            {
                nativeValue.DataLength = (uint)m_rawValue.Length;
                nativeValue.Data       = MemoryHelper.ToNativeArray <byte>(m_rawValue);
            }
        }

        /// <summary>
        /// Reads the unmanaged data from the native value.
        /// </summary>
        /// <param name="nativeValue">Input native value</param>
        void IMarshalable <MaterialProperty, AiMaterialProperty> .FromNative(ref AiMaterialProperty nativeValue)
        {
            m_name     = nativeValue.Key.GetString();
            m_type     = nativeValue.Type;
            m_texIndex = (int)nativeValue.Index;
            m_texType  = nativeValue.Semantic;
            m_rawValue = null;

            if (nativeValue.DataLength > 0 && nativeValue.Data != IntPtr.Zero)
                m_rawValue = MemoryHelper.FromNativeArray <byte>(nativeValue.Data, (int)nativeValue.DataLength); }
        }