//Transforms the root node of the scene and writes it back to the native structure private bool TransformScene(IntPtr scene) { BuildMatrix(); try { if (!m_scaleRot.IsIdentity) { AiScene aiScene = MemoryHelper.MarshalStructure <AiScene>(scene); if (aiScene.RootNode == IntPtr.Zero) { return(false); } IntPtr matrixPtr = MemoryHelper.AddIntPtr(aiScene.RootNode, MemoryHelper.SizeOf <AiString>()); //Skip over Node Name Matrix4x4 matrix = MemoryHelper.Read <Matrix4x4>(matrixPtr); //Get the root transform matrix = matrix * m_scaleRot; //Transform //Write back to unmanaged mem MemoryHelper.Write <Matrix4x4>(matrixPtr, ref matrix); return(true); } } catch (Exception) { } return(false); }
/// <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.Name = new AiString(m_name); nativeValue.Transformation = m_transform; nativeValue.Parent = IntPtr.Zero; nativeValue.NumMeshes = (uint)m_meshes.Count; nativeValue.Meshes = IntPtr.Zero; 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); } 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; }
private static unsafe String GetMaterialString(byte[] matPropData) { if (matPropData == null) { return(String.Empty); fixed(byte *ptr = &matPropData[0]) { //String is stored as 32 bit length prefix THEN followed by zero-terminated UTF8 data (basically need to reconstruct an AiString) AiString aiString; aiString.Length = new UIntPtr((uint)MemoryHelper.Read <int>(new IntPtr(ptr))); //Memcpy starting at dataPtr + sizeof(int) for length + 1 (to account for null terminator) MemoryHelper.CopyMemory(new IntPtr(aiString.Data), MemoryHelper.AddIntPtr(new IntPtr(ptr), sizeof(int)), (int)aiString.Length.ToUInt32() + 1); return(aiString.GetString()); } }
/// <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); } }
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); }