Example #1
0
        /// <summary>
        /// Reset all the data
        /// </summary>
        public void Clear()
        {
            for (int i = 0; i < buckets.Length; i++)
            {
                buckets[i] = Invalid;
            }

            poolHead = 0;

            this.bounds = new BBox2i(Vector2i.Max, Vector2i.Min);
        }
Example #2
0
        /// <summary>
        /// Reset all the data
        /// </summary>
        public void Clear()
        {
            for (int i = 0; i < bucketsSize; i++)
            {
                buckets[i] = 0xff;
            }

            poolHead = 0;

            this.bounds = new BBox2i(Vector2i.Max, Vector2i.Min);
        }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProximityGrid" /> class.
        /// </summary>
        /// <param name="poolSize">The size of the item array</param>
        /// <param name="cellSize">The size of each cell</param>
        public ProximityGrid(int poolSize, float cellSize)
        {
            this.cellSize    = cellSize;
            this.invCellSize = 1.0f / cellSize;

            //allocate hash buckets
            this.bucketsSize = MathHelper.NextPowerOfTwo(poolSize);
            this.buckets     = new int[this.bucketsSize];

            //allocate pool of items
            this.poolSize = poolSize;
            this.poolHead = 0;
            this.pool     = new Item[this.poolSize];

            this.bounds = new BBox2i(Vector2i.Max, Vector2i.Min);

            Clear();
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ProximityGrid{T}"/> class.
        /// </summary>
        /// <param name="poolSize">The size of the item array</param>
        /// <param name="cellSize">The size of each cell</param>
        public ProximityGrid(int poolSize, float cellSize)
        {
            this.cellSize    = cellSize;
            this.invCellSize = 1.0f / cellSize;

            //allocate hash buckets
            this.buckets = new int[MathHelper.NextPowerOfTwo(poolSize)];

            //allocate pool of items
            this.poolHead = 0;
            this.pool     = new Item[poolSize];
            for (int i = 0; i < this.pool.Length; i++)
            {
                this.pool[i] = new Item();
            }

            this.bounds = new BBox2i(Vector2i.Max, Vector2i.Min);

            Clear();
        }
Example #5
0
		/// <summary>
		/// Initializes a new instance of the <see cref="PolyMeshDetail"/> class.
		/// </summary>
		/// <remarks>
		/// <see cref="PolyMeshDetail"/> uses a <see cref="CompactHeightfield"/> to add in details to a
		/// <see cref="PolyMesh"/>. This detail is triangulated into a new mesh and can be used to approximate height in the walkable
		/// areas of a scene.
		/// </remarks>
		/// <param name="mesh">The <see cref="PolyMesh"/>.</param>
		/// <param name="compactField">The <see cref="CompactHeightfield"/> used to add height detail.</param>
		/// <param name="sampleDist">The sampling distance.</param>
		/// <param name="sampleMaxError">The maximum sampling error allowed.</param>
		public PolyMeshDetail(PolyMesh mesh, CompactHeightfield compactField, float sampleDist, float sampleMaxError)
		{
			if (mesh.VertCount == 0 || mesh.PolyCount == 0)
				return;

			Vector3 origin = mesh.Bounds.Min;

			int maxhw = 0, maxhh = 0;

			BBox2i[] bounds = new BBox2i[mesh.PolyCount];
			Vector3[] poly = new Vector3[mesh.NumVertsPerPoly];

			var storedVertices = new List<Vector3>();
			var storedTriangles = new List<TriangleData>();

			//find max size for polygon area
			for (int i = 0; i < mesh.PolyCount; i++)
			{
				var p = mesh.Polys[i];

				int xmin = compactField.Width;
				int xmax = 0;
				int zmin = compactField.Length;
				int zmax = 0;

				for (int j = 0; j < mesh.NumVertsPerPoly; j++)
				{
					var pj = p.Vertices[j];
					if (pj == PolyMesh.NullId)
						break;

					var v = mesh.Verts[pj];

					xmin = Math.Min(xmin, v.X);
					xmax = Math.Max(xmax, v.X);
					zmin = Math.Min(zmin, v.Z);
					zmax = Math.Max(zmax, v.Z);
				}

				xmin = Math.Max(0, xmin - 1);
				xmax = Math.Min(compactField.Width, xmax + 1);
				zmin = Math.Max(0, zmin - 1);
				zmax = Math.Min(compactField.Length, zmax + 1);

				if (xmin >= xmax || zmin >= zmax)
					continue;

				maxhw = Math.Max(maxhw, xmax - xmin);
				maxhh = Math.Max(maxhh, zmax - zmin);

				bounds[i] = new BBox2i(xmin, zmin, xmax, zmax);
			}

			HeightPatch hp = new HeightPatch(0, 0, maxhw, maxhh);

			this.meshes = new MeshData[mesh.PolyCount];

			for (int i = 0; i < mesh.PolyCount; i++)
			{
				var p = mesh.Polys[i];

				//store polygon vertices for processing
				int npoly = 0;
				for (int j = 0; j < mesh.NumVertsPerPoly; j++)
				{
					int pvi = p.Vertices[j];
					if (pvi == PolyMesh.NullId)
						break;

					PolyVertex pv = mesh.Verts[pvi];
					Vector3 v = new Vector3(pv.X, pv.Y, pv.Z);
					v.X *= mesh.CellSize;
					v.Y *= mesh.CellHeight;
					v.Z *= mesh.CellSize;
					poly[j] = v;
					npoly++;
				}

				//get height data from area of polygon
				BBox2i bound = bounds[i];
				hp.Resize(bound.Min.X, bound.Min.Y, bound.Max.X - bound.Min.X, bound.Max.Y - bound.Min.Y);
				GetHeightData(compactField, p, npoly, mesh.Verts, mesh.BorderSize, hp);

				List<Vector3> tempVerts = new List<Vector3>();
				List<TriangleData> tempTris = new List<TriangleData>(128);
				List<EdgeInfo> edges = new List<EdgeInfo>(16);
				List<SamplingData> samples = new List<SamplingData>(128);
				BuildPolyDetail(poly, npoly, sampleDist, sampleMaxError, compactField, hp, tempVerts, tempTris, edges, samples);

				//more detail verts
				for (int j = 0; j < tempVerts.Count; j++)
				{
					Vector3 tv = tempVerts[j];

					Vector3 v;
					v.X = tv.X + origin.X;
					v.Y = tv.Y + origin.Y + compactField.CellHeight;
					v.Z = tv.Z + origin.Z;

					tempVerts[j] = v;
				}

				for (int j = 0; j < npoly; j++)
				{
					Vector3 po = poly[j];

					po.X += origin.X;
					po.Y += origin.Y;
					po.Z += origin.Z;

					poly[j] = po;
				}

				//save data
				this.meshes[i].VertexIndex = storedVertices.Count;
				this.meshes[i].VertexCount = tempVerts.Count;
				this.meshes[i].TriangleIndex = storedTriangles.Count;
				this.meshes[i].TriangleCount = tempTris.Count;

				//store vertices
				storedVertices.AddRange(tempVerts);
				
				//store triangles
				for (int j = 0; j < tempTris.Count; j++)
				{
					storedTriangles.Add(new TriangleData(tempTris[j], tempVerts, poly, npoly));
				}
			}

			this.verts = storedVertices.ToArray();
			this.tris = storedTriangles.ToArray();
		}