Ejemplo n.º 1
0
 private Quaternion RandomYRot(ProceduralPrefab prefab)
 {
     return(prefab.randomRotation == RotationRandomness.AllAxes
         ? Quaternion.Euler(360 * (float)rnd.NextDouble(), 360 * (float)rnd.NextDouble(),
                            360 * (float)rnd.NextDouble())
         : Quaternion.Euler(0, 360 * (float)rnd.NextDouble(), 0));
 }
        IEnumerator InternalGenerate()
        {
            Debug.Log("Generating tile " + x + ", " + z);
            int counter = 0;

            float[,] ditherMap = new float[world.subTiles + 2, world.subTiles + 2];

            //List<GameObject> objs = new List<GameObject>();

            for (int i = 0; i < world.prefabs.Length; i++)
            {
                ProceduralPrefab pref = world.prefabs[i];

                if (pref.singleFixed)
                {
                    Vector3    p  = new Vector3((x + 0.5f) * world.tileSize, 0, (z + 0.5f) * world.tileSize);
                    GameObject ob = GameObject.Instantiate(pref.prefab, p, Quaternion.identity) as GameObject;
                    ob.transform.parent = root;
                }
                else
                {
                    float subSize = world.tileSize / world.subTiles;

                    for (int sx = 0; sx < world.subTiles; sx++)
                    {
                        for (int sz = 0; sz < world.subTiles; sz++)
                        {
                            ditherMap[sx + 1, sz + 1] = 0;
                        }
                    }

                    for (int sx = 0; sx < world.subTiles; sx++)
                    {
                        for (int sz = 0; sz < world.subTiles; sz++)
                        {
                            float px = x + sx / (float)world.subTiles;                          //sx / world.tileSize;
                            float pz = z + sz / (float)world.subTiles;                          //sz / world.tileSize;

                            float perl = Mathf.Pow(Mathf.PerlinNoise((px + pref.perlinOffset.x) * pref.perlinScale, (pz + pref.perlinOffset.y) * pref.perlinScale), pref.perlinPower);

                            float density = pref.density * Mathf.Lerp(1, perl, pref.perlin) * Mathf.Lerp(1, (float)rnd.NextDouble(), pref.random);
                            float fcount  = subSize * subSize * density + ditherMap[sx + 1, sz + 1];
                            int   count   = Mathf.RoundToInt(fcount);

                            // Apply dithering
                            // See http://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering
                            ditherMap[sx + 1 + 1, sz + 1 + 0] += (7f / 16f) * (fcount - count);
                            ditherMap[sx + 1 - 1, sz + 1 + 1] += (3f / 16f) * (fcount - count);
                            ditherMap[sx + 1 + 0, sz + 1 + 1] += (5f / 16f) * (fcount - count);
                            ditherMap[sx + 1 + 1, sz + 1 + 1] += (1f / 16f) * (fcount - count);

                            // Create a number of objects
                            for (int j = 0; j < count; j++)
                            {
                                // Find a random position inside the current sub-tile
                                Vector3    p  = RandomInside(px, pz);
                                GameObject ob = GameObject.Instantiate(pref.prefab, p, RandomYRot()) as GameObject;
                                ob.transform.parent = root;
                                //ob.SetActive ( false );
                                //objs.Add ( ob );
                                counter++;
                                if (counter % 2 == 0)
                                {
                                    yield return(null);
                                }
                            }
                        }
                    }
                }
            }

            ditherMap = null;

            // Wait a bit so that less tiles are being generated right now to avoid larger FPS spikes
            yield return(new WaitForSeconds(0.5f));

            //for ( int i=0;i<objs.Count;i++) {
            //	objs[i].SetActive ( true );
            //if ( i % 4 == 0 ) yield return 0;
            //}

            //Batch everything for improved performance
//			if (Application.HasProLicense ()) {
//				StaticBatchingUtility.Combine (root.gameObject);
//			}
        }