Exemple #1
0
    private void AddNearCellVertices(int row, int col, HexXY cellCoords, MapCell cell)
    {
        MeshData mesh = selectCellMesh(cell);

        int sharedIdx = -1;

        for (int l = 0; l < vertsInStack.Count; l++)
        {
            if (vertsInStack[l].mesh == mesh)
            {
                sharedIdx = vertsInStack[l].vidx;
                break;
            }
        }

        int idx;

        if (sharedIdx == -1)
        {
            Vector2 patchOffset = patchOffsetX * patchSize * rhex + patchOffsetY * patchSize * rhey;

            idx = mesh.vertices.Count;
            Vector2 planeCoords = row * subdivRhey + col * subdivRhex;
            float   y           = cell.state == MapCell.State.Full ? 1 : 0;
            Vector3 vertex      = new Vector3(planeCoords.x, y, planeCoords.y);

            //TODO: move noise out
            float             noiseStrength = 0.1f;
            PerlinNoiseSample vNoise        = noise.Perlin2D(patchOffset + planeCoords, 0.5f);
            vertex.y += vNoise.value * noiseStrength;

            mesh.vertices.Add(vertex);
            Vector2 uvPos = patchOffset + new Vector2(planeCoords.x, planeCoords.y);
            mesh.uvs.Add(uvPos);

            Vector3 normal = new Vector3(-vNoise.derivative.x * noiseStrength, 1, -vNoise.derivative.y * noiseStrength).normalized;
            mesh.normals.Add(normal);
        }
        else
        {
            idx = sharedIdx;
        }

        vertsInStack.Add(new VertInStack()
        {
            cell = cell, mesh = mesh, vidx = idx, coords = cellCoords
        });
        CellVertInfo cvi = cellVertInfos[(cellCoords.y - cyMin) * cw + (cellCoords.x - cxMin)];

        cvi.SetMesh(mesh);
        cvi.AddIdx(row, idx);
    }
    public PerlinNoiseSample Sum2D(Vector3 point, float frequency, int octaves, float lacunarity, float persistence)
    {
        PerlinNoiseSample sum       = Perlin2D(point, frequency);
        float             amplitude = 1f;
        float             range     = 1f;

        for (int o = 1; o < octaves; o++)
        {
            frequency *= lacunarity;
            amplitude *= persistence;
            range     += amplitude;
            sum       += Perlin2D(point, frequency) * amplitude;
        }
        return(sum * (1f / range));
    }