コード例 #1
0
ファイル: DrawableHexSphere.cs プロジェクト: sin-us/sdhk
            public static IntersectionCheckNode CreateFromTiles(ICollection <CustomTile> tiles, float maxRadius)
            {
                IntersectionCheckNode result = new IntersectionCheckNode();

                var initialBoundingBox = new BoundingBox(new Vector3(-maxRadius, -maxRadius, -maxRadius), new Vector3(maxRadius, maxRadius, maxRadius));

                foreach (CustomTile t in tiles)
                {
                    result.AddTile(t, 0, ref initialBoundingBox);
                }

                return(result);
            }
コード例 #2
0
ファイル: DrawableHexSphere.cs プロジェクト: sin-us/sdhk
            private void AddTile(CustomTile tile, int currentDepth, ref BoundingBox box)
            {
                BoundingBox = box;

                if (currentDepth >= MaxDepth)
                {
                    if (Tiles == null)
                    {
                        Tiles = new List <CustomTile>();
                    }

                    Tiles.Add(tile);
                }
                else
                {
                    var dividedBoxes = DivideBoundingBox(ref box, currentDepth);

                    var leftBoundingBox  = dividedBoxes.Item1;
                    var rightBoundingBox = dividedBoxes.Item2;

                    var leftContainment  = leftBoundingBox.Contains(tile.BoundingBox);
                    var rightContainment = rightBoundingBox.Contains(tile.BoundingBox);

                    if (leftContainment == ContainmentType.Contains || leftContainment == ContainmentType.Intersects)
                    {
                        if (Left == null)
                        {
                            Left = new IntersectionCheckNode();
                        }
                        Left.AddTile(tile, currentDepth + 1, ref leftBoundingBox);
                    }

                    if (rightContainment == ContainmentType.Contains || rightContainment == ContainmentType.Intersects)
                    {
                        if (Right == null)
                        {
                            Right = new IntersectionCheckNode();
                        }
                        Right.AddTile(tile, currentDepth + 1, ref rightBoundingBox);
                    }
                }
            }
コード例 #3
0
ファイル: DrawableHexSphere.cs プロジェクト: sin-us/sdhk
        private void InitializeVertices()
        {
            var vertices = new List <VertexPositionColorNormal>();


            foreach (var t in Tiles)
            {
                int startingVerticesLength = vertices.Count;

                // "0-level" tiles (bottom)
                for (int j = 0; j < t.Corners.Length - 2; ++j)
                {
                    Vector3 a = new Vector3(t.Corners[j + 2].X * Radius, t.Corners[j + 2].Y * Radius, t.Corners[j + 2].Z * Radius);
                    Vector3 b = new Vector3(t.Corners[j + 1].X * Radius, t.Corners[j + 1].Y * Radius, t.Corners[j + 1].Z * Radius);
                    Vector3 c = new Vector3(t.Corners[0].X * Radius, t.Corners[0].Y * Radius, t.Corners[0].Z * Radius);

                    Vector3 normA = t.Corners[j + 2].V.ToVector3();
                    Vector3 normB = t.Corners[j + 1].V.ToVector3();
                    Vector3 normC = t.Corners[0].V.ToVector3();

                    vertices.Add(CreateVertex(a, normA, t.Height));
                    vertices.Add(CreateVertex(b, normB, t.Height));
                    vertices.Add(CreateVertex(c, normC, t.Height));
                }

                // "Raised" tiles (top)
                float height = t.IsWater ? (float)(Radius + WaterHeight * GroundHeight) : (float)(Radius + t.Height * GroundHeight);
                for (int j = 0; j < t.Corners.Length - 2; ++j)
                {
                    vertices.Add(CreateVertex(new Vector3(t.Corners[j + 2].X * height, t.Corners[j + 2].Y * height, t.Corners[j + 2].Z * height), t));
                    vertices.Add(CreateVertex(new Vector3(t.Corners[j + 1].X * height, t.Corners[j + 1].Y * height, t.Corners[j + 1].Z * height), t));
                    vertices.Add(CreateVertex(new Vector3(t.Corners[0].X * height, t.Corners[0].Y * height, t.Corners[0].Z * height), t));
                }

                // Sides of raised tiles
                for (int i = 0; i < t.Corners.Length; ++i)
                {
                    // Two points of "0-level" edge
                    var a1 = new Vector3(t.Corners[i].X * Radius, t.Corners[i].Y * Radius, t.Corners[i].Z * Radius);
                    var a2 = new Vector3(t.Corners[(i + 1) % t.Corners.Length].X * Radius, t.Corners[(i + 1) % t.Corners.Length].Y * Radius, t.Corners[(i + 1) % t.Corners.Length].Z * Radius);

                    // Two points of "height-level" edge
                    var b1 = new Vector3(t.Corners[i].X * height, t.Corners[i].Y * height, t.Corners[i].Z * height);
                    var b2 = new Vector3(t.Corners[(i + 1) % t.Corners.Length].X * height, t.Corners[(i + 1) % t.Corners.Length].Y * height, t.Corners[(i + 1) % t.Corners.Length].Z * height);

                    var normal = (a2 - b1) * (a2 - a1);
                    normal = new Vector3(t.X, t.Y, t.Z);
                    normal.Normalize();

                    vertices.Add(CreateVertex(b1, normal, t.Height));
                    vertices.Add(CreateVertex(a2, normal, t.Height));
                    vertices.Add(CreateVertex(a1, normal, t.Height));

                    vertices.Add(CreateVertex(b1, normal, t.Height));
                    vertices.Add(CreateVertex(b2, normal, t.Height));
                    vertices.Add(CreateVertex(a2, normal, t.Height));
                }


                t.VerticeIndices       = Enumerable.Repeat(startingVerticesLength, vertices.Count - startingVerticesLength).Select((value, index) => value + index).ToArray();
                t.TopHexVerticeIndices = t.VerticeIndices.Skip((t.Corners.Length - 2) * 3).Take((t.Corners.Length - 2) * 3).ToArray();
                t.BoundingBox          = BoundingBox.CreateFromPoints(t.TopHexVerticeIndices.Select(vi => vertices[vi].Position));
            }

            _intersectionChecker = IntersectionCheckNode.CreateFromTiles(Tiles, Radius + GroundHeight);
            _vertices            = vertices.ToArray();
        }