Ejemplo n.º 1
0
        /// <summary>
        /// Builds polygon mesh from the provided contours.
        /// </summary>
        /// <remarks>
        /// <para>
        /// The values of the CellSize-based parameters will be converted to world units.
        /// </para>
        /// </remarks>
        /// <param name="context">The context to use for the operation.</param>
        /// <param name="contours">The contours to use to build the mesh.</param>
        /// <param name="maxVertsPerPoly">
        /// The maximum allowed vertices for a polygon.
        /// [Limits: 3 &lt;= value &lt;= <see cref="NMGen.MaxAllowedVertsPerPoly"/>]
        /// </param>
        /// <param name="walkableHeight">
        /// The walkable height used to build the contour data.
        /// [Limit: >= <see cref="NMGen.MinWalkableHeight"/>] [Units: YCellSize]
        /// </param>
        /// <param name="walkableRadius">
        /// The radius used to erode the walkable area covered by the contours.
        /// [Limit: >= 0] [Units: XZCellSize]
        /// </param>
        /// <param name="walkableStep">
        /// The walkable step used to build
        /// the contour data. [Limit: >= 0] [Units: YCellSize]</param>
        /// <returns>The generated polygon mesh, or null if there were errors.</returns>
        public static PolyMesh Build(BuildContext context, ContourSet contours
                                     , int maxVertsPerPoly, int walkableHeight, int walkableRadius, int walkableStep)
        {
            if (context == null || contours == null)
            {
                return(null);
            }

            PolyMesh result = new PolyMesh(AllocType.External);

            if (!PolyMeshEx.rcpmBuildFromContourSet(context.root
                                                    , contours.root
                                                    , maxVertsPerPoly
                                                    , ref result.root
                                                    , ref result.mMaxVerts))
            {
                return(null);
            }

            result.mWalkableHeight = walkableHeight * contours.YCellSize;
            result.mWalkableRadius = walkableRadius * contours.XZCellSize;
            result.mWalkableStep   = walkableStep * contours.YCellSize;

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets a serialized version of the mesh that can be used to
        /// recreate it later.
        /// </summary>
        /// <param name="includeBuffer">
        /// True if serialized data should include the full buffer size.  Otherwise the unused
        /// portion of the buffers will removed and the smallest possible serialized data returned.
        /// </param>
        /// <returns>A serialized version of the mesh.</returns>
        public byte[] GetSerializedData(bool includeBuffer)
        {
            if (IsDisposed)
            {
                return(null);
            }

            // Design note:  This is implemented using an interop call
            // rather than local code bacause it is much more easier to
            // serialize in C++ than it is in C#.

            IntPtr ptr      = IntPtr.Zero;
            int    dataSize = 0;

            if (!PolyMeshEx.rcpmGetSerializedData(ref root
                                                  , mMaxVerts
                                                  , mWalkableHeight
                                                  , mWalkableRadius
                                                  , mWalkableStep
                                                  , includeBuffer
                                                  , ref ptr
                                                  , ref dataSize))
            {
                return(null);
            }

            byte[] result = UtilEx.ExtractArrayByte(ptr, dataSize);

            NMGenEx.nmgFreeSerializationData(ref ptr);

            return(result);
        }
Ejemplo n.º 3
0
        private PolyMesh(SerializationInfo info, StreamingContext context)
            : base(AllocType.External)
        {
            // Note: Version compatability is handled by the interop call;

            byte[] rawData = (byte[])info.GetValue(DataKey, typeof(byte[]));

            PolyMeshEx.rcpmBuildSerializedData(rawData
                                               , rawData.Length
                                               , ref root
                                               , ref mMaxVerts
                                               , ref mWalkableHeight
                                               , ref mWalkableRadius
                                               , ref mWalkableStep);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Creates a polygon mesh from the data generated by the <see cref="GetSerializedData"/>
        /// method.
        /// </summary>
        /// <param name="serializedMesh">The serialized mesh data.</param>
        /// <returns>The new polygon mesh, or null on error.</returns>
        public static PolyMesh Create(byte[] serializedMesh)
        {
            PolyMesh result = new PolyMesh(AllocType.External);

            if (PolyMeshEx.rcpmBuildSerializedData(serializedMesh
                                                   , serializedMesh.Length
                                                   , ref result.root
                                                   , ref result.mMaxVerts
                                                   , ref result.mWalkableHeight
                                                   , ref result.mWalkableRadius
                                                   , ref result.mWalkableStep))
            {
                return(result);
            }

            return(null);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Frees all resources and marks the object as disposed.
        /// </summary>
        public override void RequestDisposal()
        {
            if (!IsDisposed)
            {
                if (ResourceType == AllocType.Local)
                {
                    Marshal.FreeHGlobal(root.areas);
                    Marshal.FreeHGlobal(root.flags);
                    Marshal.FreeHGlobal(root.polys);
                    Marshal.FreeHGlobal(root.regions);
                    Marshal.FreeHGlobal(root.verts);
                }
                else if (ResourceType == AllocType.External)
                {
                    PolyMeshEx.rcpmFreeMeshData(ref root);
                }

                root.Reset();
                mMaxVerts       = 0;
                mWalkableHeight = 0;
                mWalkableStep   = 0;
            }
        }