// texture generation of puzzle pieces
    IEnumerator DrawingCells()
    {
        camDraw.enabled = true;

        // remove the texture, if was generated earlier
        if (texPuzzle != null)
        {
            DestroyImmediate(texPuzzle);
        }

        // exhibiting the pieces in the right position for generating camera
        int camPosX = (int)camDraw.transform.localPosition.x;

        foreach (CellGroup group in groups.Values)
        {
            DC_Cell data = group.cells[0].data;
            float   posX = camPosX + data.posX * data.sizeCellX * 2 - (data.sizeBoardX - 1) * data.sizeCellX;
            float   posY = -data.posY * data.sizeCellY * 2 + (data.sizeBoardY - 1) * data.sizeCellY;
            group.transform.localPosition = new Vector3(posX, posY, 0);
        }

        yield return(new WaitForSeconds(0.1f));

        // save the resulting image in the texture
        RenderTexture.active = rTex;
        texPuzzle            = new Texture2D(sizeBoardX * sizeCellX * 2, sizeBoardY * sizeCellY * 2, TextureFormat.ARGB32, false);
        texPuzzle.ReadPixels(new Rect(0, 0, sizeBoardX * sizeCellX * 2, sizeBoardY * sizeCellY * 2), 0, 0);
        texPuzzle.Apply();
        RenderTexture.active = null;

        camDraw.enabled = false;

        // We remove the helper objects and assign-generated texture
        foreach (CellGroup group in groups.Values)
        {
            Destroy(group.cells[0].draw.gameObject);
            group.cells[0].mesh.gameObject.SetActive(true);
            group.cells[0].mesh.sharedMaterial.mainTexture = texPuzzle;
        }

        // mix pieces
        for (int i = 0; i < 100; i++)
        {
            curGroup = groups[Random.Range(0, groups.Count)];
            SortgroupsUp();
        }

        curGroup = null;

        // set in the received position
        for (int y = 0; y < sizeBoardY; y++)
        {
            for (int x = 0; x < sizeBoardX; x++)
            {
                groups [x + y * sizeBoardX].transform.localPosition = new Vector3(
                    x * sizeCellX - (sizeBoardX - 0) * sizeCellX / 2 + sizeCellX / 2,
                    y * sizeCellY - (sizeBoardY - 0) * sizeCellY / 2 + sizeCellY / 2, 0);
            }
        }
    }
    void BoardCreate()
    {
        groups = new Dictionary <int, CellGroup>();
        Lib.RemoveObjects(rootCells);

        // create groups for each piece and places them in the starting position
        for (int y = 0; y < sizeBoardY; y++)
        {
            for (int x = 0; x < sizeBoardX; x++)
            {
                CellGroup group = Lib.AddObject <CellGroup>(prefGroup, rootCells, true);
                group.index = x + y * sizeBoardX;
                Cell cell = group.cells[0];

                DC_Cell data = new DC_Cell();
                data.posX       = x;
                data.posY       = y;
                data.sizeBoardX = sizeBoardX;
                data.sizeBoardY = sizeBoardY;
                data.sizePictX  = pictBack.localScale.x;
                data.sizePictY  = pictBack.localScale.y;
                data.sizeCellX  = sizeCellX;
                data.sizeCellY  = sizeCellY;

                cell.SetData(data);
                cell.draw.SetData(data);
                group.UpdateIndex();
                groups.Add(group.index, group);
                cell.mesh.gameObject.SetActive(false);
            }
        }

        // generates texture with pieces of puzzle
        StartCoroutine(DrawingCells());
    }
    // texture generation of puzzle pieces
    IEnumerator DrawingCells()
    {
        camDraw.enabled = true;

        // remove the texture, if was generated earlier
        if (texPuzzle != null)
        {
            DestroyImmediate(texPuzzle);
        }

        // exhibiting the pieces in the right position for generating camera
        int camPosX = (int)camDraw.transform.localPosition.x;

        foreach (CellGroup group in groups.Values)
        {
            DC_Cell data = group.cells[0].data;
            float   posX = camPosX + data.posX * data.sizeCellX * 2 - (sizeBoardX - 1) * data.sizeCellX;
            float   posY = -data.posY * data.sizeCellY * 2 + (sizeBoardY - 1) * data.sizeCellY;
            group.transform.localPosition = new Vector3(posX, posY, 0);
        }

        yield return(new WaitForSeconds(0.1f));

        // save the resulting image in the texture
        RenderTexture.active = rTex;
        texPuzzle            = new Texture2D(sizeBoardX * sizeCellX * 2, sizeBoardY * sizeCellY * 2, TextureFormat.ARGB32, false);
        texPuzzle.ReadPixels(new Rect(0, 0, sizeBoardX * sizeCellX * 2, sizeBoardY * sizeCellY * 2), 0, 0);
        texPuzzle.Apply();
        RenderTexture.active = null;

        camDraw.enabled = false;

        // We remove the helper objects and assign-generated texture
        foreach (CellGroup group in groups.Values)
        {
            Destroy(group.cells[0].draw.gameObject);
            group.cells[0].mesh.gameObject.SetActive(true);
            group.cells[0].mesh.sharedMaterial.mainTexture = texPuzzle;

            // Changing shader to transparent to avoid using light
            // group.cells[0].mesh.sharedMaterial.shader = Shader.Find("Unlit/Transparent");
        }

        // placement of pieces in a random position
        foreach (CellGroup group in groups.Values)
        {
            if (Random.Range(0, 2) == 0)
            {
                group.transform.localPosition = new Vector3(Random.Range(250, 450), Random.Range(-250, 250), -group.index * 2);
            }
            else
            {
                group.transform.localPosition = new Vector3(Random.Range(-450, -250), Random.Range(-250, 250), -group.index * 2);
            }

            group.UpdateCellPos();
        }
    }
Esempio n. 4
0
    public Vector3 curPos;              // the coordinates of the current position of a piece

    // a piece of data installation
    public void SetData(DC_Cell data)
    {
        this.data = data;

        rightPos = new Vector3(data.posX * data.sizeCellX - data.sizeBoardX * data.sizeCellX / 2 + data.sizeCellX / 2,
                               -data.posY * data.sizeCellY + data.sizeBoardY * data.sizeCellY / 2 - data.sizeCellY / 2, 0);

        // the size and displacement of the textures in the mesh
        mesh.transform.localScale = new Vector3(data.sizeCellX * 2, data.sizeCellY * 2, 1);
        mesh.sharedMaterial       = new Material(mesh.sharedMaterial);
        mesh.sharedMaterial.SetTextureScale("_MainTex", new Vector2(1f / data.sizeBoardX, 1f / data.sizeBoardY));
        mesh.sharedMaterial.SetTextureOffset("_MainTex", new Vector2(data.posX * 1f / data.sizeBoardX,
                                                                     1 - (data.posY + 1) * 1f / data.sizeBoardY));
    }
Esempio n. 5
0
    public float outline;                       // offset outline

    public void SetData(DC_Cell data)
    {
        this.data = data;
        dataDraw  = new DC_CellDraw();

        foreach (Transform child in transform)
        {
            child.localPosition = child.localPosition * outline;
            child.localScale    = new Vector3(data.sizeCellX * 2, data.sizeCellY * 2, 1);
            MeshRenderer meshChild = child.GetComponent <MeshRenderer>();
            meshChild.sharedMaterial = new Material(meshChild.sharedMaterial);
        }

        mesh.sharedMaterial.SetTextureScale("_MainTex", new Vector2(data.sizeCellX / data.sizePictX * 2, data.sizeCellY / (float)data.sizePictY * 2));
        mesh.sharedMaterial.SetTextureOffset("_MainTex", new Vector2(data.posX * 1f / data.sizeBoardX - 1f / data.sizeBoardX / 2f,
                                                                     (data.posY + 1) * -1f / data.sizeBoardY - 1f / data.sizeBoardY / 2f));
    }