コード例 #1
0
            public void RandomScatter(int count, SpatialHash spatialHash, InstanceRandom rnd, Matrix probMatrix)
            {
                int candidatesNum = (int)(uniformity * 100);

                if (candidatesNum < 1)
                {
                    candidatesNum = 1;
                }

                for (int i = 0; i < count; i++)
                {
                    Vector2 bestCandidate = Vector3.zero;
                    float   bestDist      = 0;

                    for (int c = 0; c < candidatesNum; c++)
                    {
                        Vector2 candidate = new Vector2((spatialHash.offset.x + 1) + (rnd.Random() * (spatialHash.size - 2.01f)), (spatialHash.offset.y + 1) + (rnd.Random() * (spatialHash.size - 2.01f)));

                        //checking if candidate available here according to probability map
                        //if (probMatrix!=null && probMatrix[candidate] < rnd.Random()+0.0001f) continue;

                        //checking if candidate is the furthest one
                        float dist = spatialHash.MinDist(candidate);

                        //distance to the edge
                        float bd = (candidate.x - spatialHash.offset.x) * 2; if (bd < dist)
                        {
                            dist = bd;
                        }
                        bd = (candidate.y - spatialHash.offset.y) * 2; if (bd < dist)
                        {
                            dist = bd;
                        }
                        bd = (spatialHash.offset.x + spatialHash.size - candidate.x) * 2; if (bd < dist)
                        {
                            dist = bd;
                        }
                        bd = (spatialHash.offset.y + spatialHash.size - candidate.y) * 2; if (bd < dist)
                        {
                            dist = bd;
                        }

                        if (dist > bestDist)
                        {
                            bestDist = dist; bestCandidate = candidate;
                        }
                    }

                    if (bestDist > 0.001f)
                    {
                        spatialHash.Add(bestCandidate, 0, 0, 1);                         //adding only if some suitable candidate found
                    }
                }

                //masking
                for (int c = 0; c < spatialHash.cells.Length; c++)
                {
                    SpatialHash.Cell cell = spatialHash.cells[c];
                    for (int i = cell.objs.Count - 1; i >= 0; i--)
                    {
                        Vector2 pos = cell.objs[i].pos;

                        if (pos.x < spatialHash.offset.x + safeBorders ||
                            pos.y < spatialHash.offset.y + safeBorders ||
                            pos.x > spatialHash.offset.x + spatialHash.size - safeBorders ||
                            pos.y > spatialHash.offset.y + spatialHash.size - safeBorders)
                        {
                            cell.objs.RemoveAt(i); continue;
                        }

                        if (probMatrix != null && probMatrix[pos] < rnd.Random() + 0.0001f)
                        {
                            cell.objs.RemoveAt(i); continue;
                        }
                    }
                }
            }
コード例 #2
0
            public void Generate(SpatialHash spatialHash, int seed, Matrix probability)
            {
                InstanceRandom rnd = new InstanceRandom(seed ^ this.seed);

                RandomScatter(count, spatialHash, rnd, probability);
            }