Exemple #1
0
    void InstantiatePlanet()
    {
        GameObject planetObj = Instantiate(m_planetPrefab);

        planetObj.transform.position = m_position;
        planetObj.name = "Planet1";

        Planet planet = planetObj.GetComponent <Planet>();

        planet.Initalize(m_chunk_count, m_xyzResolution);

        Vector3 chunk_offset = Vector3.one * (m_xyzResolution * m_chunk_count / -2f + 0.5f * m_xyzResolution);

        for (int c_z = 0, id = 0; c_z < m_chunk_count; c_z++)
        {
            for (int c_y = 0; c_y < m_chunk_count; c_y++)
            {
                for (int c_x = 0; c_x < m_chunk_count; c_x++, id++)
                {
                    int map_offset = c_x * m_xyzResolution + c_y * m_xyzResolution * m_length + c_z * m_xyzResolution * m_length2;

                    GameObject chunkObj = Instantiate(m_planetChunkPrefab, planetObj.transform, false);
                    chunkObj.name = planetObj.name + "_chunk" + id.ToString();
                    chunkObj.transform.position = planetObj.transform.position + (chunk_offset + new Vector3(c_x * m_xyzResolution, c_y * m_xyzResolution, c_z * m_xyzResolution)) * m_scale;
                    planet.AddChunk(chunkObj);

                    PlanetChunk chunk = chunkObj.GetComponent <PlanetChunk>();
                    chunk.m_meshMaterial = m_planetMaterial;
                    chunk.m_lod1Distance = m_lod1;
                    chunk.m_lod2Distance = m_lod2;
                    chunk.m_lod3Distance = m_lod3;
                    chunk.Initalize(id, m_xyzResolution, m_scale, m_shaded, planetObj);

                    SetGeoCoords(chunk, c_x, c_y, c_z);

                    float[] densityMap = new float[m_xyzResolution * m_xyzResolution * m_xyzResolution];
                    for (int z = 0; z < m_xyzResolution; z++)
                    {
                        for (int y = 0; y < m_xyzResolution; y++)
                        {
                            for (int x = 0; x < m_xyzResolution; x++)
                            {
                                densityMap[x + y * m_xyzResolution + z * m_xyzResolution * m_xyzResolution] =
                                    m_densityMap[map_offset + x + y * m_length + z * m_length2];
                            }
                        }
                    }
                    chunk.SetDensityMap(densityMap);
                }
            }
        }
        planet.AssignChunkNeighboursAndRefresh();
    }