Bilinear resizing : Can't resize past 512

I can reduce the size of textures past 512 - but I can’t increase them back (game will just hang).
So a 1024x1024 texture can reduce to 256x256.
But a 256x256 texture can not increase to 1024x1024
but a 256x256 texture can increase to 512x512

a 2x2 can increase to 512x512, and vice versa. Hell, a 1024x1024 can reduce to 2x2.

script is WIP, haven’t cleaned one of the textures and need to add credit (written by me, but carbon copy concept from someone else)

function ResizeTexture(tex : Texture2D , width : int , height : int ) : Texture2D{
    var resizedImage : Texture2D = Texture2D(width, height);
    var bTemp : Texture2D = Instantiate(tex, Vector3.zero, Quaternion.identity);
    var fraction_x : float;
    var fraction_y : float;
    var one_minus_x : float;
    var one_minus_y : float;
    //----
    var ceil_x : int ;
    var ceil_y : int;
    var floor_x : int;
    var floor_y : int;
    //----
    var c1 = Color();
    var c2 = Color();
    var c3 = Color();
    var c4 = Color();
    var red : float;
    var blue : float;
    var green : float;
    //-----
    var b1 : float;
    var b2 : float;
    
    var tempw : float = bTemp.width * 1.0;
    var temph : float = width * 1.0;
    var nXFactor : float = tempw / temph ;
    
    tempw  = bTemp.height * 1.0;
    temph = height * 1.0;
    var nYFactor : float = tempw / temph;
    
    for (x = 0 ; x < width ; ++x){
        for (y = 0 ; y < height ; ++y){
            floor_x = Mathf.Floor(x * nXFactor);
            floor_y = Mathf.Floor(y * nYFactor);
            ceil_x = floor_x + 1;
            if (ceil_x >= bTemp.width){
                ceil_x = floor_x;
            }
            ceil_y = floor_y + 1;
            if (ceil_y >= bTemp.height){
                ceil_y = floor_y;
            }
            
            fraction_x = x * nXFactor - floor_x;
            fraction_y = y * nYFactor - floor_y;
            one_minus_x = 1.0 - fraction_x ;
            one_minus_y = 1.0 - fraction_y ;
            //-----
            c1 = bTemp.GetPixel(floor_x, floor_y);
            c2 = bTemp.GetPixel(ceil_x, floor_y);
            c3 = bTemp.GetPixel(floor_x, ceil_y);
            c4 = bTemp.GetPixel(ceil_x, ceil_y);
            //------
            //blue
            b1 = (one_minus_x * c1.b + fraction_x * c2.b);
            b2 = (one_minus_x * c3.b + fraction_x * c4.b);
            blue = (one_minus_y * b1 + fraction_y * b2);
            //green
            b1 = (one_minus_x * c1.g + fraction_x * c2.g);
            b2 = (one_minus_x * c3.g + fraction_x * c4.g);
            green = (one_minus_y * b1 + fraction_y * b2);
            //red
            b1 = (one_minus_x * c1.r + fraction_x * c2.r);
            b2 = (one_minus_x * c3.r + fraction_x * c4.r);
            red = (one_minus_y * b1 + fraction_y * b2);
            //------
            resizedImage.SetPixel(x,y, Color(red, green, blue));
        }
        resizedImage.Apply();
        
    }
    return resizedImage;
}

Thsi one is very unclean, but does it’s job for testing.

var tex1 = 
    var tex1 : Texture2D = [b]PlaceTextureHere[/b]
    var tex : Texture2D[] = new Texture2D[16]; 
    var size : int = Mathf.Min(tex1.width , tex1.height); //gets minimum size from it
    var i2 = 0;
    tex[0] = tex1;
    File.WriteAllBytes(Application.dataPath + "tex[" + 0 + "].png", tex[0].EncodeToPNG());
    for (i = size / 2 ; i >= 2 ; i /= 4){
        i2 += 1;
        tex[i2] = ResizeTexture(tex[i2 - 1] , i , i );
    }
    for (i = 2 ; i <= size / 2 ; i *= 4){
//Warning : Textures past 512 will cause game to hang/crash.
        tex[i2] = ResizeTexture(tex[i2], tex[0].width, tex[0].height);
        File.WriteAllBytes(Application.dataPath + "tex[" + i2 + "].png", tex[i2].EncodeToPNG());
        i2 -= 1;
    }

Bump

Bump.

Have you tried putting print statements in the code to see where the hang occurs? (For example, does it fail on the Apply call or during the inner/outer loop?)

Figured it out a few hours ago - I had tried debugging it but the editor would freeze so badly no debug (Debug.Log(string)) would come from it. When looking over it again I made a subconscious edit without noticing it.

// resizedImage.Apply();
        
    }
    resizedImage.Apply();
    return resizedImage;

I am going to be submitting this to the showcase section, so full code will be there.