Ejemplo n.º 1
0
    public RoomGeneratorHelper(RoomSettings settings, Vector3Int targetExtent, RoomConnections connections)
    {
        m_Settings    = settings;
        m_Connections = connections;
        m_NoiseMap    = new PerlinNoise(Random.Range(int.MinValue, int.MaxValue));

        // Decide room size
        m_TraversableExtent = new Vector3Int(
            targetExtent.x - settings.m_Padding.x * 2,
            targetExtent.y,
            targetExtent.z - settings.m_Padding.z * 2
            );

        // Populate voxel data
        Vector3Int fullExtent = FullExtent;
        VoxelData  voxelData  = VoxelData.New(fullExtent.x, fullExtent.y, fullExtent.z);

        m_DoorCentre = new Vector2Int(fullExtent.x / 2, fullExtent.z / 2);

        for (int x = 0; x < fullExtent.x; ++x)
        {
            for (int z = 0; z < fullExtent.z; ++z)
            {
                int height = GetHeight(x, z);

                // Try to create a placement spot here
                if (x % m_Settings.m_PlacementFrequency == 0 && z % m_Settings.m_PlacementFrequency == 0)
                {
                    if (height == m_Settings.m_FloorHeight)
                    {
                        m_AccessibleSpots.Add(new Vector3Int(x, height, z));
                    }
                    else if (height == m_TraversableExtent.y)
                    {
                        m_InaccessibleSpots.Add(new Vector3Int(x, height, z));
                    }
                }


                for (int y = 0; y <= height; ++y)
                {
                    uint[] coloursTable = m_Settings.m_ColourIndices;

                    float rawNoise = GetRawNoise(x, z, m_Settings.m_TextureHeightFrequency, m_Settings.m_TextureNoiseScale);

                    if (rawNoise <= 0.3f && m_Settings.m_LowNoiseColourIndices.Length != 0)
                    {
                        coloursTable = m_Settings.m_LowNoiseColourIndices;
                    }
                    else if (rawNoise >= 0.6f && m_Settings.m_HighNoiseColourIndices.Length != 0)
                    {
                        coloursTable = m_Settings.m_HighNoiseColourIndices;
                    }

                    int  colourLookupIdx = Mathf.Clamp(height - y, 0, coloursTable.Length - 1);
                    uint colour          = coloursTable[colourLookupIdx];

                    voxelData.SetVoxel(new Voxel(colour), x, y, z);
                }
            }
        }

        m_Mesh      = voxelData.GenerateMesh(Vector3.zero, settings.m_Scale);
        m_Mesh.name = "GeneratedMesh";
    }