unity3d - Stack overflow with C# and Mono, recursive function -


i'm using unity3d based on mono framework , c#. function below throw stack overflows. cannot figure out these come from; nowadays tends happen infinite loops, mine isn't. works fine 8x8x16 voxels, crashes @ 16x16x32 voxels. put lock in because figure might happen function fired second time before first finished.

int floatingvoxelsflood(int x, int y, int z, ref bool[] voxelsattached) {     if (getpixel(x, y, z).a > 0.5f) return 0;      int id = getpixelid(x, y, z);     if (voxelsattached[id]) return 0;      voxelsattached[id] = true;      int count = 1;      int minx = x-1;     int maxx = x+1;     if (minx >= 0)          count += floatingvoxelsflood(minx, y, z, ref voxelsattached);     if (maxx < volumewidth)          count += floatingvoxelsflood(maxx, y, z, ref voxelsattached);      int miny = y-1;     int maxy = y+1;     if (miny >= 0)          count += floatingvoxelsflood(x, miny, z, ref voxelsattached);     if (maxy < volumeheight)          count += floatingvoxelsflood(x, maxy, z, ref voxelsattached);      int minz = z-1;     int maxz = z+1;     if (minz >= 0)          count += floatingvoxelsflood(x, y, minz, ref voxelsattached);     if (maxz < volumedepth)          count += floatingvoxelsflood(x, y, maxz, ref voxelsattached);      return count; } 

voxelsattached set false @ start. getpixel().a returns if voxel needs used.

how can solve stack overflow?

the 1 below not overflow stack.

int floatingvoxelsflood(int x, int y, int z, ref bool[] voxelsattached) {     if (getpixel(x, y, z).a > 0.5f) return 0;      queue<pixel> queue = new queue<pixel>();     queue.enqueue(new pixel(x, y, z));      int count = 0;     while(queue.count > 0) {         pixel p = queue.dequeue();          int id = getpixelid(p.x, p.y, p.z);         if (!voxelsattached[id] && getpixel(p.x, p.y, p.z).a < 0.5f) {             count++;             voxelsattached[id] = true;              int minx = p.x-1;             int maxx = p.x+1;             if (minx >= 0)                  queue.enqueue(new pixel(minx, p.y, p.z));             if (maxx < volumewidth)                  queue.enqueue(new pixel(maxx, p.y, p.z));              int miny = p.y-1;             int maxy = p.y+1;             if (miny >= 0)                  queue.enqueue(new pixel(p.x, miny, p.z));             if (maxy < volumeheight)                  queue.enqueue(new pixel(p.x, maxy, p.z));              int minz = p.z-1;             int maxz = p.z+1;             if (minz >= 0)                  queue.enqueue(new pixel(p.x, p.y, minz));             if (maxz < volumedepth)                  queue.enqueue(new pixel(p.x, p.y, maxz));         }     }      return count; } 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -