示例#1
0
        private void CreateJumbledGrid(out Topology topology, out IVertexAttribute <Vector3> vertexPositions)
        {
            Vector3[] vertexPositionsArray;
            var       surface = RectangularHexGrid.Create(
                HexGridDescriptor.CreateSideUp(true, HexGridAxisStyles.StaggeredSymmetric),
                Vector3.zero, Quaternion.identity,
                false, false,
                new IntVector2(11, 10));

            topology        = surface.CreateManifold(out vertexPositionsArray);
            vertexPositions = PositionalVertexAttribute.Create(surface, vertexPositionsArray);

            var regularityWeight = 0.5f;
            var equalAreaWeight  = 1f - regularityWeight;

            var regularityRelaxedVertexPositions = new Vector3[topology.vertices.Count].AsVertexAttribute();
            var equalAreaRelaxedVertexPositions  = new Vector3[topology.vertices.Count].AsVertexAttribute();
            var relaxedVertexPositions           = regularityRelaxedVertexPositions;
            var faceCentroids = PositionalFaceAttribute.Create(surface, topology.internalFaces.Count);
            var vertexAreas   = new float[topology.vertices.Count].AsVertexAttribute();

            FaceAttributeUtility.CalculateFaceCentroidsFromVertexPositions(topology.internalFaces, vertexPositions, faceCentroids);
            VertexAttributeUtility.CalculateVertexAreasFromVertexPositionsAndFaceCentroids(topology.vertices, vertexPositions, faceCentroids, vertexAreas);

            var totalArea = 0f;

            foreach (var vertexArea in vertexAreas)
            {
                totalArea += vertexArea;
            }

            var localTopology        = topology;
            var localVertexPositions = vertexPositions;

            Func <float> relaxIterationFunction = () =>
            {
                PlanarManifoldUtility.RelaxVertexPositionsForRegularity(localTopology, localVertexPositions, false, regularityRelaxedVertexPositions);
                PlanarManifoldUtility.RelaxVertexPositionsForEqualArea(localTopology, localVertexPositions, totalArea, false, equalAreaRelaxedVertexPositions, faceCentroids, vertexAreas);
                for (int i = 0; i < relaxedVertexPositions.Count; ++i)
                {
                    relaxedVertexPositions[i] = regularityRelaxedVertexPositions[i] * regularityWeight + equalAreaRelaxedVertexPositions[i] * equalAreaWeight;
                }
                var relaxationAmount = PlanarManifoldUtility.CalculateRelaxationAmount(localVertexPositions, relaxedVertexPositions);
                for (int i = 0; i < localVertexPositions.Count; ++i)
                {
                    localVertexPositions[i] = relaxedVertexPositions[i];
                }
                return(relaxationAmount);
            };

            Func <bool> repairFunction = () =>
            {
                return(PlanarManifoldUtility.ValidateAndRepair(localTopology, surface.normal, localVertexPositions, 0.5f, false));
            };

            Action relaxationLoopFunction = TopologyRandomizer.CreateRelaxationLoopFunction(20, 20, 0.95f, relaxIterationFunction, repairFunction);

            TopologyRandomizer.Randomize(
                topology, 1, 0.2f,
                3, 3, 5, 7, false,
                XorShift128Plus.Create(23478),
                relaxationLoopFunction);
        }
示例#2
0
        public void StartNewGame()
        {
            DestroyOldGame();

            Vector3[] vertexPositionsArray;

            if (squaresToggle.isOn)
            {
                IntVector2 boardSize;
                if (smallToggle.isOn)
                {
                    boardSize = new IntVector2(9, 9);
                }
                else if (mediumToggle.isOn)
                {
                    boardSize = new IntVector2(13, 13);
                }
                else
                {
                    boardSize = new IntVector2(19, 19);
                }
                _surface         = RectangularQuadGrid.Create(Vector2.right, Vector2.up, Vector3.zero, Quaternion.identity, false, false, boardSize);
                _topology        = ((RectangularQuadGrid)_surface).CreateManifold(out vertexPositionsArray);
                _vertexPositions = PositionalVertexAttribute.Create(_surface, vertexPositionsArray);
            }
            else if (hexesToggle.isOn)
            {
                IntVector2 boardSize;
                if (smallToggle.isOn)
                {
                    boardSize = new IntVector2(9, 9);
                }
                else if (mediumToggle.isOn)
                {
                    boardSize = new IntVector2(13, 13);
                }
                else
                {
                    boardSize = new IntVector2(19, 19);
                }
                _surface = RectangularHexGrid.Create(
                    HexGridDescriptor.CreateCornerUp(true, HexGridAxisStyles.StaggeredSymmetric),
                    Vector3.zero, Quaternion.identity,
                    false, false,
                    boardSize);
                _topology        = ((RectangularHexGrid)_surface).CreateManifold(out vertexPositionsArray);
                _vertexPositions = PositionalVertexAttribute.Create(_surface, vertexPositionsArray);
            }
            else
            {
                IntVector2 boardSize;
                if (smallToggle.isOn)
                {
                    boardSize = new IntVector2(9, 9);
                }
                else if (mediumToggle.isOn)
                {
                    boardSize = new IntVector2(13, 13);
                }
                else
                {
                    boardSize = new IntVector2(19, 19);
                }
                _surface = RectangularHexGrid.Create(
                    HexGridDescriptor.CreateCornerUp(true, HexGridAxisStyles.StaggeredSymmetric),
                    Vector3.zero, Quaternion.identity,
                    false, false,
                    boardSize);
                _topology        = ((RectangularHexGrid)_surface).CreateManifold(out vertexPositionsArray);
                _vertexPositions = PositionalVertexAttribute.Create(_surface, vertexPositionsArray);

                var regularityWeight = 0.5f;
                var equalAreaWeight  = 1f - regularityWeight;

                var regularityRelaxedVertexPositions = new Vector3[_topology.vertices.Count].AsVertexAttribute();
                var equalAreaRelaxedVertexPositions  = new Vector3[_topology.vertices.Count].AsVertexAttribute();
                var relaxedVertexPositions           = regularityRelaxedVertexPositions;
                var faceCentroids = PositionalFaceAttribute.Create(_surface, _topology.internalFaces.Count);
                var vertexAreas   = new float[_topology.vertices.Count].AsVertexAttribute();

                FaceAttributeUtility.CalculateFaceCentroidsFromVertexPositions(_topology.internalFaces, _vertexPositions, faceCentroids);
                VertexAttributeUtility.CalculateVertexAreasFromVertexPositionsAndFaceCentroids(_topology.vertices, _vertexPositions, faceCentroids, vertexAreas);

                var totalArea = 0f;
                foreach (var vertexArea in vertexAreas)
                {
                    totalArea += vertexArea;
                }

                Func <float> relaxIterationFunction = () =>
                {
                    PlanarManifoldUtility.RelaxVertexPositionsForRegularity(_topology, _vertexPositions, true, regularityRelaxedVertexPositions);
                    PlanarManifoldUtility.RelaxVertexPositionsForEqualArea(_topology, _vertexPositions, totalArea, true, equalAreaRelaxedVertexPositions, faceCentroids, vertexAreas);
                    for (int i = 0; i < relaxedVertexPositions.Count; ++i)
                    {
                        relaxedVertexPositions[i] = regularityRelaxedVertexPositions[i] * regularityWeight + equalAreaRelaxedVertexPositions[i] * equalAreaWeight;
                    }
                    var relaxationAmount = PlanarManifoldUtility.CalculateRelaxationAmount(_vertexPositions, relaxedVertexPositions);
                    for (int i = 0; i < _vertexPositions.Count; ++i)
                    {
                        _vertexPositions[i] = relaxedVertexPositions[i];
                    }
                    return(relaxationAmount);
                };

                Func <bool> repairFunction = () =>
                {
                    return(PlanarManifoldUtility.ValidateAndRepair(_topology, _surface.normal, _vertexPositions, 0.5f, true));
                };

                Action relaxationLoopFunction = TopologyRandomizer.CreateRelaxationLoopFunction(20, 20, 0.95f, relaxIterationFunction, repairFunction);

                TopologyRandomizer.Randomize(
                    _topology, 1, 0.1f,
                    3, 3, 5, 7, true,
                    _random,
                    relaxationLoopFunction);
            }

            _facePositions = PositionalFaceAttribute.Create(_surface, _topology.internalFaces.Count);
            FaceAttributeUtility.CalculateFaceCentroidsFromVertexPositions(_topology.internalFaces, _vertexPositions, _facePositions);

            _innerAngleBisectors = EdgeAttributeUtility.CalculateFaceEdgeBisectorsFromVertexPositions(_topology.internalFaces, PlanarSurface.Create(Vector3.zero, Quaternion.identity), _vertexPositions);

            _faceBoardStates = new BoardState[_topology.internalFaces.Count].AsFaceAttribute();

            foreach (var face in _topology.internalFaces)
            {
                _faceBoardStates[face] = BoardState.Empty;
            }

            _facePieces = new Transform[_topology.internalFaces.Count].AsFaceAttribute();

            _partitioning        = UniversalFaceSpatialPartitioning.Create(_surface, _topology, _vertexPositions);
            _picker.partitioning = _partitioning;
            _picker.enabled      = true;

            var centerVertexNormal = _surface.normal.normalized;

            var triangulation = new SeparatedFacesUmbrellaTriangulation(2,
                                                                        (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.position = _vertexPositions[edge];
                vertexAttributes.normal   = (_vertexPositions[edge] + _surface.normal * 5f - _facePositions[edge.nearFace]).normalized;
                vertexAttributes.uv       = new Vector2(0.25f, 0f);
                vertexAttributes.Advance();

                vertexAttributes.position = _vertexPositions[edge] + _innerAngleBisectors[edge] * 0.05f;
                vertexAttributes.normal   = (vertexAttributes.position + _surface.normal * 5f - _facePositions[edge.nearFace]).normalized;
                vertexAttributes.uv       = new Vector2(0.25f, 0.5f);
                vertexAttributes.Advance();
            },
                                                                        (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.position = _facePositions[face];
                vertexAttributes.normal   = centerVertexNormal;
                vertexAttributes.uv       = new Vector2(0.25f, 1f);
                vertexAttributes.Advance();
            });

            _dynamicMesh = DynamicMesh.Create(
                _topology.enumerableInternalFaces,
                DynamicMesh.VertexAttributes.Position |
                DynamicMesh.VertexAttributes.Normal |
                DynamicMesh.VertexAttributes.UV,
                triangulation);

            foreach (var mesh in _dynamicMesh.submeshes)
            {
                var meshObject = Instantiate(meshFilterRendererPrefab);
                meshObject.mesh = mesh;
                meshObject.transform.SetParent(gameBoardMeshes);
            }

            _gameBoardBounds = new Bounds(Vector3.zero, Vector3.zero);
            foreach (var vertex in _topology.vertices)
            {
                _gameBoardBounds.Encapsulate(_vertexPositions[vertex]);
            }

            AdjustCamera();

            var pickerCollider = GetComponent <BoxCollider>();

            pickerCollider.center = _gameBoardBounds.center;
            pickerCollider.size   = _gameBoardBounds.size;

            _whiteCount = 0;
            _blackCount = 0;
            _moveCount  = 0;

            whiteCountText.text = _whiteCount.ToString();
            blackCountText.text = _blackCount.ToString();

            _gameActive = true;
            _turn       = BoardState.Black;
        }