示例#1
0
        protected void Start()
        {
            var hexGridSurface = RectangularHexGrid.Create(
                HexGridDescriptor.CreateSideUp(true, HexGridAxisStyles.StaggeredSymmetric),
                Vector3.zero, Quaternion.Euler(90f, 0f, 0f),
                false, false,
                new IntVector2(topologyWidth, topologyHeight));

            _surface = hexGridSurface;

            Vector3[] vertexPositionsArray;
            _topology            = hexGridSurface.CreateManifold(out vertexPositionsArray);
            _vertexPositions     = vertexPositionsArray.AsVertexAttribute();
            _facePositions       = FaceAttributeUtility.CalculateFaceCentroidsFromVertexPositions(_topology.internalFaces, _vertexPositions);
            _faceCornerBisectors = EdgeAttributeUtility.CalculateFaceEdgeBisectorsFromVertexPositions(_topology.internalFaces, _vertexPositions, _facePositions);
            _faceBlockedStates   = new bool[_topology.internalFaces.Count].AsFaceAttribute();

            var triangulation = new SeparatedFacesUmbrellaTriangulation(2,
                                                                        (Topology.FaceEdge edge, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.position = _vertexPositions[edge];
                vertexAttributes.normal   = Vector3.up;
                vertexAttributes.color    = borderColor;
                vertexAttributes.Advance();

                vertexAttributes.position = _vertexPositions[edge] + _faceCornerBisectors[edge] * 0.05f;
                vertexAttributes.normal   = (vertexAttributes.position + Vector3.up - _facePositions[edge.nearFace]).normalized;
                vertexAttributes.color    = normalColor;
                vertexAttributes.Advance();
            },
                                                                        (Topology.Face face, DynamicMesh.IIndexedVertexAttributes vertexAttributes) =>
            {
                vertexAttributes.position = _facePositions[face];
                vertexAttributes.normal   = Vector3.up;
                vertexAttributes.color    = normalColor;
                vertexAttributes.Advance();
            });

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

            foreach (var mesh in _dynamicMesh.submeshes)
            {
                var meshFilter = Instantiate(submeshPrefab);
                meshFilter.mesh = mesh;
                meshFilter.transform.SetParent(transform, false);
            }

            var partioning = UniversalFaceSpatialPartitioning.Create(_surface, _topology, _vertexPositions);
            var picker     = GetComponent <FaceSpatialPartitioningPicker>();

            picker.partitioning = partioning;
        }
示例#2
0
        private void CreateHexGrid2(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);
        }
示例#3
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);
        }