Exemple #1
0
    static void makeStructures(PlanetData planet, IRandomGenerator gen)
    {
        planet.structuresPrefabs = new List <GameObject>();
        planet.structures        = new List <StructureInfo>();

        int[] structStartIndex = new int[planet.biomes.Length];
        for (int i = 0; i < planet.biomes.Length; i++)
        {
            structStartIndex[i] = planet.structuresPrefabs.Count;

            foreach (var s in planet.biomes[i].structures)
            {
                planet.structuresPrefabs.Add(s.prefab);
            }
        }

        var dAngle = new UniformFloatDistribution(0, 360.0f);

        for (int i = 0; i < planet.triangles.Length; i++)
        {
            int biomeId = PlanetEx.biomeIndexOfTriangle(planet, i);
            if (biomeId < 0)
            {
                continue;
            }

            if (planet.biomes[biomeId].structureDensity <= 0 || planet.biomes[biomeId].structures.Count == 0)
            {
                continue;
            }

            var t = planet.triangles[i];

            float area = MathEx.triangleArea(planet.points[t.v1].point, planet.points[t.v2].point, planet.points[t.v3].point) * planet.biomes[biomeId].structureDensity;

            float v = area - (int)area;
            if (new BernoulliDistribution(v).Next(gen))
            {
                area = (int)area + 1;
            }
            else
            {
                area = (int)area;
            }

            var p1 = planet.points[t.v1].point * (planet.points[t.v1].height + 1) * planet.scale;
            var p2 = planet.points[t.v2].point * (planet.points[t.v2].height + 1) * planet.scale;
            var p3 = planet.points[t.v3].point * (planet.points[t.v3].height + 1) * planet.scale;

            var d = new Uniform3DTriangleDistribution(p1, p2, p3);

            List <float> weights = new List <float>();
            for (int j = 0; j < planet.biomes[biomeId].structures.Count; j++)
            {
                weights.Add(planet.biomes[biomeId].structures[j].weight);
            }
            var dStructs = new DiscreteDistribution(weights);

            for (int j = 0; j < area; j++)
            {
                var pos = d.Next(gen);
                planet.structures.Add(new StructureInfo(dStructs.Next(gen) + structStartIndex[biomeId], i, pos, dAngle.Next(gen)));
            }
        }
    }