示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="field"></param>
        /// <param name="selection"></param>
        /// <returns></returns>
        public static Mesh MeshSelection(this Grid3d field, IEnumerable <int> selection)
        {
            var mesh  = new Mesh();
            var verts = mesh.Vertices;
            var faces = mesh.Faces;

            (var dx, var dy) = ((Vec2d)field.Scale * 0.5);

            // add vertices
            foreach (int index in selection)
            {
                var p = field.CoordinateAt(index);
                verts.Add(p.X - dx, p.Y - dy, p.Z);
                verts.Add(p.X + dx, p.Y - dy, p.Z);
                verts.Add(p.X - dx, p.Y + dy, p.Z);
                verts.Add(p.X + dx, p.Y + dy, p.Z);
            }

            // add faces
            for (int i = 0; i < verts.Count; i += 4)
            {
                faces.AddFace(i, i + 1, i + 3, i + 2);
            }

            return(mesh);
        }
示例#2
0
    void MakeGrid()
    {
        var colliders = _voids
                        .GetComponentsInChildren <MeshCollider>()
                        .ToArray();

        var voxelSize = float.Parse(_voxelSize);

        _grid = Grid3d.MakeGridWithVoids(colliders, voxelSize, true);

        var faces      = _grid.GetFaces().Where(f => f.IsActive);
        var graphEdges = faces.Select(f => new TaggedEdge <Voxel, Face>(f.Voxels[0], f.Voxels[1], f));
        var graph      = graphEdges.ToAdjacencyGraph <Voxel, TaggedEdge <Voxel, Face> >();

        var voxels = _grid.GetVoxels().Where(v => v.IsActive);

        var start = voxels.First();
        var end   = voxels.ElementAt(100);

        var flow = graph.MaximumFlowEdmondsKarp(_ => 1, start, end, out var predecessors, (s, e) => new TaggedEdge <Voxel, Face>(s, e, null));


        var current = end;

        _flowPath = new List <Voxel>();
        _flowPath.Add(current);

        while (predecessors(current, out var next))
        {
            current = next.GetOtherVertex(current);
            _flowPath.Add(current);
        }

        print(flow);
    }
示例#3
0
文件: Sparse.cs 项目: ADRC4/Voxel
    void MakeGrid()
    {
        var bounds = _voids
                     .GetComponentsInChildren <MeshCollider>()
                     .Select(c => c.bounds)
                     .ToArray();

        var voxelSize = float.Parse(_voxelSize);

        _grid = Grid3d.MakeGridWithBounds(bounds, voxelSize);

        //  shortest path to test if connectivity is working
        var faces      = _grid.GetFaces().Where(f => f.IsActive).ToList();
        var graphEdges = faces.Select(f => new TaggedEdge <Voxel, Face>(f.Voxels[0], f.Voxels[1], f));
        var graph      = graphEdges.ToUndirectedGraph <Voxel, TaggedEdge <Voxel, Face> >();

        var start = _grid.GetVoxels().ElementAt(2);
        var end   = _grid.GetVoxels().ElementAt(250);

        var shortest = graph.ShortestPathsDijkstra(_ => 1, start);

        shortest(end, out var path);
        var current = start;

        _flowPath = new List <Voxel>();
        _flowPath.Add(current);

        foreach (var face in path)
        {
            current = face.GetOtherVertex(current);
            _flowPath.Add(current);
        }
    }
示例#4
0
    void updateGrid()
    {
        // Remove old children from grid
        for (int y = 0; y < Grid3d.h; ++y)
        {
            for (int x = 0; x < Grid3d.w; ++x)
            {
                for (int z = 0; z < Grid3d.d; ++z)
                {
                    if (Grid3d.grid[x, y, z] != null)
                    {
                        if (Grid3d.grid[x, y, z].parent == transform)
                        {
                            Grid3d.grid[x, y, z] = null;
                        }
                    }
                }
            }
        }

        // Add new children to grid
        foreach (Transform child in transform)
        {
            Vector3 v = Grid3d.roundVec3(child.position);
            Grid3d.grid[(int)v.x, (int)v.y, (int)v.z] = child;
        }
    }
示例#5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="field"></param>
        /// <param name="selection"></param>
        /// <returns></returns>
        public static Mesh ToPolySoup(this Grid3d field, IEnumerable <int> selection)
        {
            var mesh  = new Mesh();
            var verts = mesh.Vertices;
            var faces = mesh.Faces;

            (var dx, var dy) = (field.Scale.XY * 0.5);

            // add vertices
            foreach (int index in selection)
            {
                (var x, var y, var z) = field.ToWorldSpace(index);
                verts.Add(x - dx, y - dy, z);
                verts.Add(x + dx, y - dy, z);
                verts.Add(x - dx, y + dy, z);
                verts.Add(x + dx, y + dy, z);
            }

            // add faces
            for (int i = 0; i < verts.Count; i += 4)
            {
                faces.AddFace(i, i + 1, i + 3, i + 2);
            }

            return(mesh);
        }
示例#6
0
    public Face(int x, int y, int z, Axis direction, Grid3d grid)
    {
        _grid     = grid;
        Index     = new Vector3Int(x, y, z);
        Direction = direction;
        Voxels    = GetVoxels();

        foreach (var v in Voxels.Where(v => v != null))
        {
            v.Faces.Add(this);
        }

        Center = GetCenter();

        // var center = Corner + new Vector3(x, y+0.5f, z + 0.5f) * VoxelSize;

        //Frame = new FrameElement2Node(start, end)
        //{
        //    Iy = 0.02,
        //    Iz = 0.02,
        //    A = 0.01,
        //    J = 0.05,
        //    E = 210e9,
        //    G = 70e9,
        //    ConsiderShearDeformation = false,
        //};
    }
示例#7
0
 public Voxel(Vector3Int index, Grid3d grid)
 {
     _grid    = grid;
     Index    = index;
     Center   = grid.Corner + new Vector3(index.x + 0.5f, index.y + 0.5f, index.z + 0.5f) * grid.VoxelSize;
     IsActive = !_grid.Voids.Any(IsInside);
 }
示例#8
0
    private void Start()
    {
        var bbox = new Bounds(new Vector3(5, 5, 3), new Vector3(10, 10, 6));

        _grid = new Grid3d(bbox, 1f);

        PatternTest();
    }
示例#9
0
    void MakeGrid(float x, float y, float z)
    {
        var voxelSize = float.Parse(_voxelSize);
        var bbox      = new Bounds();

        bbox.SetMinMax(new Vector3(-x * 0.5f, 0, -z * 0.5f), new Vector3(x * 0.5f, y, z * 0.5f));
        _grid = new Grid3d(bbox, voxelSize);
    }
示例#10
0
 // Use this for initialization
 void Awake()
 {
     seekerIndex = 0;
     seekerMoveControllers = new MoveController[seekers.Length];
     for (int x=0; x<seekers.Length; x++)
         seekerMoveControllers [x] = seekers [x].GetComponent<MoveController> ();
     grid = GetComponent<Grid3d> ();
 }
示例#11
0
 public Edge(int x, int y, int z, Axis direction, Grid3d grid)
 {
     _grid     = grid;
     Index     = new Vector3Int(x, y, z);
     Direction = direction;
     Center    = GetCenter();
     Voxels    = GetVoxels();
     Faces     = GetFaces();
 }
示例#12
0
 void Awake()
 {
     Instance = this;
     nodeDiameter = nodeRadius * 2;
     gridX = Mathf.RoundToInt (gridSize.x / nodeDiameter);
     gridY = Mathf.RoundToInt (gridSize.y / nodeDiameter);
     gridZ = Mathf.RoundToInt (gridSize.z / nodeDiameter);
     CreateGrid ();
 }
示例#13
0
    public Corner(Vector3Int index, Grid3d grid)
    {
        _grid    = grid;
        Index    = index;
        Position = grid.Corner + new Vector3(index.x, index.y, index.z) * grid.VoxelSize;

        Location    = new Point(Position.x, Position.z, Position.y);
        Constraints = index.y == 0 ? Constraint.Fixed : Constraint.RotationFixed;
        Loads.Add(new NodalLoad(new Force(0, 0, -2000000, 0, 0, 0)));
    }
示例#14
0
    void MakeGrid()
    {
        var colliders = _voids
                        .GetComponentsInChildren <MeshCollider>()
                        .ToArray();

        var voxelSize = float.Parse(_voxelSize);

        _grid = Grid3d.MakeGridWithVoids(colliders, voxelSize);
        Analysis();
        MakeVoxelMesh();
        ToggleVoids();
    }
示例#15
0
    void Start()
    {
        var bounds = new Bounds(new Vector3(0, 5, 0), new Vector3(15, 9, 9));

        _grid = new Grid3d(bounds, 0.8f);

        foreach (var voxel in _grid.GetVoxels())
        {
            voxel.IsActive = true;
        }

        StartCoroutine(GrowGrid());
    }
示例#16
0
    IEnumerator GrowVoxelsAnimation()
    {
        float size  = float.Parse(_voxelSize);
        var   voids = _voids.GetComponentsInChildren <MeshCollider>();

        _grid = Grid3d.MakeGridWithVoids(voids, size);
        ToggleVoids(false);

        _sequence.Clear();

        foreach (var voxel in _grid.GetFaces().Where(v => v.IsSkin))
        {
            _sequence.Add(voxel);
            yield return(null);
        }
    }
示例#17
0
    public static void setFallPoint(Transform tf)
    {
        resetFallPoint();

        foreach (Transform child in tf)
        {
            Vector3 v = Grid3d.roundVec3(child.position);


            if ((int)v.y != 0 && Grid3d.grid[(int)v.x, (int)v.y - 1, (int)v.z] == null)
            {
                fallPosition.Add(calcPutFallPoint(child.position));
            }
        }

        isUpdateFallpoint = false;
    }
示例#18
0
    void MakeGrid()
    {
        // create grid with voids
        var colliders = _voids
                        .GetComponentsInChildren <MeshCollider>()
                        .ToArray();

        _grid = new Grid3d(colliders, voxelSize);

        //sorting the Voxel
        var faces        = _grid.GetFaces().Where(f => f.IsActive);
        var graphFaces   = faces.Select(e => new TaggedEdge <Voxel, Face>(e.Voxels[0], e.Voxels[1], e));
        var graphVoxel   = graphFaces.ToUndirectedGraph <Voxel, TaggedEdge <Voxel, Face> >();
        var startVoxel   = _grid.GetVoxels().Where(v => Vector3.Distance(v.Center, Vector3.zero) < 1.5f).First();
        var shortestGrow = QuickGraph.Algorithms.AlgorithmExtensions.ShortestPathsDijkstra(graphVoxel, e => 1.0, startVoxel);

        linkFaces = faces.ToList();
        //foreach (var botomFaces in _grid.GetFaces().Where(f => f.Index.y == 0 && f.Direction == Axis.Y)) linkFaces.Add(botomFaces);

        foreach (var voxel in _grid.GetVoxels().Where(v => v.IsActive))
        {
            IEnumerable <TaggedEdge <Voxel, Face> > path;
            if (shortestGrow(voxel, out path))
            {
                voxel.Order = path.Count() + UnityEngine.Random.value * 0.9f;
            }
            activeVoxels.Add(voxel);
            voxel.IsActive = false;
        }
        activeVoxels = activeVoxels.OrderBy(v => v.Order).ToList();

        for (int i = 0; i < _grid.GetFaces().Max(f => f.Index.z) / 2; i++)
        {
            startFaces.Add(_grid.GetFaces().Where(f => f.Index.y == 0 && f.Index.x == 0 && f.Index.z == i * 2 && f.Direction == Axis.Y).First());
            startFaces.Add(_grid.GetFaces().Where(f => f.Index.y == 0 && f.Index.x == 15 && f.Index.z == i * 2 && f.Direction == Axis.Y).First());
        }

        foreach (var sf in startFaces)
        {
            var stack = Instantiate(stackPrefab, sf.Center, Quaternion.identity);
            _stackList.Add(stack);
        }
    }
示例#19
0
    bool isValidGridPos()
    {
        foreach (Transform child in transform)
        {
            Vector3 v = Grid3d.roundVec3(child.position);

            // Not inside Border?
            if (!Grid3d.insideBorder(v))
            {
                return(false);
            }

            // Block in grid cell (and not part of same group)?

            if (Grid3d.grid[(int)v.x, (int)v.y, (int)v.z] != null &&
                Grid3d.grid[(int)v.x, (int)v.y, (int)v.z].parent != transform)
            {
                return(false);
            }
        }
        return(true);
    }
 private void Start()
 {
     _grid = GetComponent <Grid3d>();
 }
示例#21
0
    // Update is called once per frame
    void fall_sequence()
    {
        switch (CameraMove3d.getPositionNumber())
        {
        case 0:
            // Move Front
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                checkMoveFront();
            }

            // Move Back
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                checkMoveBack();
            }

            // Move Left
            else if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                checkMoveLeft();
            }

            // Move Right
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                checkMoveRight();
            }


            // Rotate
            if (Input.GetKeyDown(KeyCode.A))
            {
                checkRotateXaxis(true);
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                checkRotateYaxis();
            }
            else if (Input.GetKeyDown(KeyCode.D))
            {
                checkRotateZaxis(true);
            }
            break;

        case 1:
            // Move Front
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                checkMoveRight();
            }

            // Move Back
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                checkMoveLeft();
            }

            // Move Left
            else if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                checkMoveFront();
            }

            // Move Right
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                checkMoveBack();
            }


            // Rotate
            if (Input.GetKeyDown(KeyCode.D))
            {
                checkRotateXaxis(true);
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                checkRotateYaxis();
            }
            else if (Input.GetKeyDown(KeyCode.A))
            {
                checkRotateZaxis(true);
            }
            break;

        case 2:
            // Move Front
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                checkMoveBack();
            }

            // Move Back
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                checkMoveFront();
            }

            // Move Left
            else if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                checkMoveRight();
            }

            // Move Right
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                checkMoveLeft();
            }


            // Rotate
            if (Input.GetKeyDown(KeyCode.A))
            {
                checkRotateXaxis(false);
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                checkRotateYaxis();
            }
            else if (Input.GetKeyDown(KeyCode.D))
            {
                checkRotateZaxis(false);
            }
            break;

        case 3:
            // Move Front
            if (Input.GetKeyDown(KeyCode.UpArrow))
            {
                checkMoveLeft();
            }

            // Move Back
            else if (Input.GetKeyDown(KeyCode.DownArrow))
            {
                checkMoveRight();
            }

            // Move Left
            else if (Input.GetKeyDown(KeyCode.LeftArrow))
            {
                checkMoveBack();
            }

            // Move Right
            else if (Input.GetKeyDown(KeyCode.RightArrow))
            {
                checkMoveFront();
            }


            // Rotate
            if (Input.GetKeyDown(KeyCode.D))
            {
                checkRotateXaxis(false);
            }
            else if (Input.GetKeyDown(KeyCode.S))
            {
                checkRotateYaxis();
            }
            else if (Input.GetKeyDown(KeyCode.A))
            {
                checkRotateZaxis(false);
            }
            break;
        }



        //時間経過で落ちる
        if (Time.time - lastFall >= 1.5)
        {
            // Modify position
            transform.position += new Vector3(0, -1, 0);

            // See if valid
            if (isValidGridPos())
            {
                // It's valid. Update grid.
                updateGrid();
            }
            else
            {
                // It's not valid. revert.
                transform.position += new Vector3(0, 1, 0);
                audiosource.PlayOneShot(fallSE);

                // Clear filled horizontal lines
                Grid3d.deleteFullRows();

                Spawner3d.second_ban = false;

                // Spawn next Group
                FindObjectOfType <Spawner3d>().spawnNext();

                //影を消す
                FallPoint.resetFallPoint();

                // Disable script
                enabled = false;
            }

            lastFall = Time.time;
        }

        //一気に落ちる
        else if (Input.GetKeyDown(KeyCode.Space))
        {
            int score = 0;

            while (true)
            {
                transform.position += new Vector3(0, -1, 0);

                if (!isValidGridPos())
                {
                    // It's not valid. revert.
                    transform.position += new Vector3(0, 1, 0);
                    audiosource.PlayOneShot(fallSE);
                    break;
                }
                score++;
            }
            updateGrid();

            //スコア加算
            ScoreText.addScore(score);

            // Clear filled horizontal lines
            Grid3d.deleteFullRows();

            Spawner3d.second_ban = false;

            // Spawn next Group
            FindObjectOfType <Spawner3d>().spawnNext();

            //影を消す
            FallPoint.resetFallPoint();

            // Disable script
            enabled = false;
        }
    }
示例#22
0
        public object PartOne()
        {
            var grid = Grid3d.LoadInitialState();

            return(RunSimulation(grid));
        }
示例#23
0
    void MakeGrid()
    {
        // create grid with voids
        var colliders = _voids
                        .GetComponentsInChildren <MeshCollider>()
                        .ToArray();

        var voxelSize = float.Parse(_voxelSize);

        _grid = Grid3d.MakeGridWithVoids(colliders, voxelSize);

        // select edges of boundary faces
        var edges = _grid.GetEdges().Where(e => e.ClimbableFaces.Length == 2);

        // create graph from edges
        var graphEdges = edges.Select(e => new TaggedEdge <Face, Edge>(e.ClimbableFaces[0], e.ClimbableFaces[1], e));
        var graph      = graphEdges.ToUndirectedGraph <Face, TaggedEdge <Face, Edge> >();

        // start face for shortest path
        var start = _grid.GetFaces().Where(f => f.IsSkin).Skip(10).First();

        // calculate shortest path from start face to all boundary faces
        var shortest = graph.ShortestPathsDijkstra(e => 1.0, start);

        // select an end face to draw one specific path
        var end = _grid.GetFaces().Where(f => f.IsSkin).Skip(200).First();

        shortest(end, out var endPath);

        // unsorted distinct faces of the path from start to end faces
        var endPathFaces = new HashSet <Face>(endPath.SelectMany(e => new[] { e.Source, e.Target }));

        // create a mesh face for every outer face colored based on the path length (except a solid yellow path to end face)
        var faceMeshes = new List <CombineInstance>();

        foreach (var face in _grid.GetFaces().Where(f => f.IsSkin))
        {
            float t = 1;

            if (shortest(face, out var path))
            {
                t = path.Count() * 0.04f;
                t = Mathf.Clamp01(t);
            }

            Mesh faceMesh;

            // paint face yellow if its part of the start-end path, gradient color for other faces
            if (endPathFaces.Contains(face))
            {
                faceMesh = Drawing.MakeFace(face.Center, face.Direction, _grid.VoxelSize, 0, 1);
            }
            else
            {
                faceMesh = Drawing.MakeFace(face.Center, face.Direction, _grid.VoxelSize, t);
            }

            faceMeshes.Add(new CombineInstance()
            {
                mesh = faceMesh
            });
        }

        var mesh = new Mesh()
        {
            indexFormat = UnityEngine.Rendering.IndexFormat.UInt32
        };

        mesh.CombineMeshes(faceMeshes.ToArray(), true, false, false);

        Mesh pathMesh = new Mesh();

        // draw a polyline for the start-end path
        {
            if (shortest(end, out var path))
            {
                float offset   = 0.1f;
                var   vertices = new List <Vector3>();

                var current = start;
                vertices.Add(current.Center + current.Normal * offset);

                foreach (var edge in path)
                {
                    vertices.Add(edge.Tag.Center + edge.Tag.Normal * offset);
                    current = edge.GetOtherVertex(current);
                    vertices.Add(current.Center + current.Normal * offset);
                }

                pathMesh.SetVertices(vertices);
                pathMesh.subMeshCount = 2;
                pathMesh.SetIndices(Enumerable.Range(0, vertices.Count).ToArray(), MeshTopology.LineStrip, 1);
            }
        }

        _meshes = new[] { mesh, pathMesh };
    }