示例#1
0
    public InfluenceMapSection UpdatePhysicalInfluence(Vector3 position, InfluenceMapSection section, int distance = 3)
    {
        texIsDirty = true;
        //todo store grid outside of texture, use texture for display only
        for (int i = 0; i < section.length; i++)
        {
            var   node       = section.nodes[i];
            int   pixelIndex = node.col * dimension + node.row;
            Color pixelColor = pixels[pixelIndex];
            pixels[pixelIndex] = pixelColor - new Color(0, 1, 0, node.influence);
            tex.SetPixel(node.col, node.row, pixels[pixelIndex]);
        }
        float percentX = (position.x + terrainOriginOffset) / terrainResolution;
        float percentZ = (position.z + terrainOriginOffset) / terrainResolution;

        int row = (int)(percentZ * dimension);
        int col = (int)(percentX * dimension);

        int r     = row - distance;
        int c     = col - distance;
        int count = distance * 2 + 1;

        section.Clear();

        int mapIdx = 0;

        for (int i = 0; i < count; i++)
        {
            for (int j = 0; j < count; j++)
            {
                int ci = c + i;
                int rj = r + j;

                if (ci >= 0 && ci < dimension && rj >= 0 && rj < dimension)
                {
                    int   pixelIndex         = ci * dimension + rj;
                    float normalizedDistance = 1 - ((new Vector2(col, row) - new Vector2(ci, rj)).magnitude / 5f);
                    float influence          = curve.Evaluate(normalizedDistance);

                    section.nodes[mapIdx++] = new InfluenceMapNode(ci, rj, influence);

                    Color color = pixels[pixelIndex] + new Color(0, 1, 0, influence);
                    tex.SetPixel(ci, rj, color);
                    pixels[pixelIndex] = color;
                }
            }
        }
        section.length = mapIdx;
        tex.SetPixel(col, row, new Color(1, 0, 0, 1));
        return(section);
    }
    public InfluenceMapSection UpdatePhysicalInfluence(Vector3 position, InfluenceMapSection section, int distance = 3) {
        texIsDirty = true;
        //todo store grid outside of texture, use texture for display only
        for (int i = 0; i < section.length; i++) {
            var node = section.nodes[i];
            int pixelIndex = node.col * dimension + node.row;
            Color pixelColor = pixels[pixelIndex];
            pixels[pixelIndex] = pixelColor - new Color(0, 1, 0, node.influence); 
            tex.SetPixel(node.col, node.row, pixels[pixelIndex]);
        }
        float percentX = (position.x + terrainOriginOffset) / terrainResolution;
        float percentZ = (position.z + terrainOriginOffset) / terrainResolution;

        int row = (int)(percentZ * dimension);
        int col = (int)(percentX * dimension);

        int r = row - distance;
        int c = col - distance;
        int count = distance * 2 + 1;
        section.Clear();

        int mapIdx = 0;
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count; j++) {

                int ci = c + i;
                int rj = r + j;

                if (ci >= 0 && ci < dimension && rj >= 0 && rj < dimension) {
                    int pixelIndex = ci * dimension + rj;
                    float normalizedDistance = 1 - ((new Vector2(col, row) - new Vector2(ci, rj)).magnitude / 5f);
                    float influence = curve.Evaluate(normalizedDistance);

                    section.nodes[mapIdx++] = new InfluenceMapNode(ci, rj, influence);

                    Color color = pixels[pixelIndex] + new Color(0, 1, 0, influence);
                    tex.SetPixel(ci, rj, color);
                    pixels[pixelIndex] = color;
                }
            }
        }
        section.length = mapIdx;
        tex.SetPixel(col, row, new Color(1, 0, 0, 1));
        return section;
    }