Esempio n. 1
0
    void createTerrain(Texture2D mapTexture)
    {
        clean();

        createColorMask();

        nodes = new Destroy2DNode[resolution * resolution];

        int meshCount = Mathf.FloorToInt(resolution / nodesPerMesh);

        if ((resolution % nodesPerMesh) > 0)
        {
            meshCount++;
        }
        nodeMeshes = new Destroy2DMesh[meshCount * meshCount];
        Destroy2DMesh currentMesh = null;

        //nodeValuesTexture = new Texture2D(resolution, resolution);

        float nodeSize = terrainSize / resolution;
        float uvDelta  = 1.0f / resolution;

        for (int x = 0; x < resolution; x++)
        {
            for (int y = 0; y < resolution; y++)
            {
                Destroy2DNode node = Destroy2DNode.createNewNode();
                node.terrainSize = terrainSize;

                node.nodePosition = new Vector3(x * nodeSize, y * -nodeSize, 0);

                nodes[x + y * resolution] = node;

                currentMesh = nodeMeshes[Mathf.FloorToInt(x / nodesPerMesh) + Mathf.FloorToInt(y / nodesPerMesh) * meshCount];

                if (currentMesh == null)
                {
                    currentMesh = Destroy2DMesh.instantiate();
                    currentMesh.gameObject.transform.parent        = gameObject.transform;
                    currentMesh.gameObject.transform.localPosition = node.nodePosition;

                    nodeMeshes[Mathf.FloorToInt(x / nodesPerMesh) + Mathf.FloorToInt(y / nodesPerMesh) * meshCount] = currentMesh;

                    currentMesh.meshX         = x;
                    currentMesh.meshY         = y;
                    currentMesh.meshNodeCount = nodesPerMesh;
                }

                if (node.init(mapTexture, x * uvDelta, 1 - y * uvDelta, uvDelta, nodeSize, colorValue, material))
                {
                }

                //nodeValuesTexture.SetPixel(x,-y, Color.white * (colorValue + (node.tl + node.tr + node.bl + node.br) * 0.25f));
            }
        }

        //nodeValuesTexture.Apply();

        foreach (Destroy2DMesh mesh in GetComponentsInChildren <Destroy2DMesh>())
        {
            mesh.commit(material, colliderDepth, sortingLayer, sortingOrder, this);
        }
    }
Esempio n. 2
0
    private void changeTerrain(Vector3 pos, float radius, float intensity)
    {
        int           meshCount   = Mathf.FloorToInt(resolution / nodesPerMesh);
        Destroy2DMesh currentMesh = null;

        float nodeSize   = terrainSize / resolution;
        int   radiusSize = Mathf.FloorToInt(radius / nodeSize) + 1;
        int   posX       = Mathf.FloorToInt((pos.x - transform.position.x) / nodeSize);
        int   posY       = Mathf.FloorToInt(-(pos.y - transform.position.y) / nodeSize);
        float uvDelta    = 1.0f / resolution;

        for (int x = posX - radiusSize; x < posX + radiusSize; x++)
        {
            for (int y = posY - radiusSize; y < posY + radiusSize; y++)
            {
                if ((x >= 0) && (x < resolution) && (y >= 0) && (y < resolution))
                {
                    Destroy2DNode node = nodes[x + y * resolution];

                    if (node == null)
                    {
                        node             = nodes[x + y * resolution] = Destroy2DNode.createNewNode();
                        node.terrainSize = terrainSize;
                        node.setValues(x * uvDelta, 1 - y * uvDelta, uvDelta, nodeSize);
                        node.nodePosition = new Vector3(x * nodeSize, y * -nodeSize, 0);
                    }

                    if (node != null)
                    {
                        bool shouldUpdate = false;

                        float rad_tl = ((node.nodePosition + transform.position) - pos).magnitude;
                        float rad_tr = ((node.nodePosition + transform.position + Vector3.right * nodeSize) - pos).magnitude;
                        float rad_bl = ((node.nodePosition + transform.position + Vector3.down * nodeSize) - pos).magnitude;
                        float rad_br = ((node.nodePosition + transform.position + Vector3.right * nodeSize + Vector3.down * node.size) - pos).magnitude;

                        if (rad_tl < radius)
                        {
                            node.tl     -= intensity * (1 - (rad_tl / radius));
                            shouldUpdate = true;
                        }

                        if (rad_tr < radius)
                        {
                            node.tr     -= intensity * (1 - (rad_tr / radius));
                            shouldUpdate = true;
                        }

                        if (rad_bl < radius)
                        {
                            node.bl     -= intensity * (1 - (rad_bl / radius));
                            shouldUpdate = true;
                        }

                        if (rad_br < radius)
                        {
                            node.br     -= intensity * (1 - (rad_br / radius));
                            shouldUpdate = true;
                        }

                        if (shouldUpdate)
                        {
                            node.tl = Mathf.Clamp01(node.tl);
                            node.tr = Mathf.Clamp01(node.tr);
                            node.bl = Mathf.Clamp01(node.bl);
                            node.br = Mathf.Clamp01(node.br);

                            currentMesh = nodeMeshes[Mathf.FloorToInt(x / nodesPerMesh) + Mathf.FloorToInt(y / nodesPerMesh) * meshCount];

                            currentMesh.isDirty = true;

                            Destroy2DNode otherNode = null;
                            Destroy2DMesh otherMesh = null;

                            //top left
                            if ((x > 1) && (y > 1))
                            {
                                otherNode    = nodes[(x - 1) + (y - 1) * resolution];
                                otherNode.br = node.tl;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x - 1) / nodesPerMesh) + Mathf.FloorToInt((y - 1) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            //top
                            if (y > 1)
                            {
                                otherNode    = nodes[(x) + (y - 1) * resolution];
                                otherNode.bl = node.tl;
                                otherNode.br = node.tr;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x) / nodesPerMesh) + Mathf.FloorToInt((y - 1) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            //top right
                            if ((x < (resolution - 2)) && (y > 1))
                            {
                                otherNode    = nodes[(x + 1) + (y - 1) * resolution];
                                otherNode.bl = node.tr;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x + 1) / nodesPerMesh) + Mathf.FloorToInt((y - 1) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            //right
                            if (x < (resolution - 2))
                            {
                                otherNode    = nodes[(x + 1) + (y) * resolution];
                                otherNode.tl = node.tr;
                                otherNode.bl = node.br;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x + 1) / nodesPerMesh) + Mathf.FloorToInt((y) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            //bottom right
                            if ((x < (resolution - 2)) && (y < (resolution - 2)))
                            {
                                otherNode    = nodes[(x + 1) + (y + 1) * resolution];
                                otherNode.tl = node.br;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x + 1) / nodesPerMesh) + Mathf.FloorToInt((y + 1) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            //bottom
                            if (y < (resolution - 2))
                            {
                                otherNode    = nodes[(x) + (y + 1) * resolution];
                                otherNode.tl = node.bl;
                                otherNode.tr = node.br;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x) / nodesPerMesh) + Mathf.FloorToInt((y + 1) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            //bottom left
                            if ((x > 1) && (y < (resolution - 2)))
                            {
                                otherNode    = nodes[(x - 1) + (y + 1) * resolution];
                                otherNode.tr = node.bl;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x - 1) / nodesPerMesh) + Mathf.FloorToInt((y + 1) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            //left
                            if (x > 1)
                            {
                                otherNode    = nodes[(x - 1) + (y) * resolution];
                                otherNode.tr = node.tl;
                                otherNode.br = node.bl;

                                otherNode.reInit(colorValue);

                                otherMesh         = nodeMeshes[Mathf.FloorToInt((x - 1) / nodesPerMesh) + Mathf.FloorToInt((y) / nodesPerMesh) * meshCount];
                                otherMesh.isDirty = true;
                            }

                            node.reInit(colorValue);

                            //nodeValuesTexture.SetPixel(x,-y, Color.white * (colorValue + (node.tl + node.tr + node.bl + node.br) * 0.25f));
                        }
                    }
                }
            }
        }

        //nodeValuesTexture.Apply();
    }