Search Unity

Windowed Mode: GetPixels is wrong in different resolutions

Discussion in 'Web' started by Blarp, Jul 23, 2021.

  1. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    270
    I'm doing a simple getpixels in a certain area of a texture, and it's pulling the wrong locations depending on your desktop's resolution.

    I am hard setting my canvas resolution in Index AND UI canvas to 1280x720.

    Using the below code, I'm capturing the screen to a texture, and then in another texture, I'm cutting it up.

    If my Windows Resolution is set different, I get different results. Anyway to resolve this issue?

    I'm not using fullscreen whatsoever, it's just a hardcoded 1280x720 canvas which should be 1280x720 in every single Desktop resolution. Even if the aspect is the same, but the desktop resos are different, it will also give mixed results.

    texture = ScreenCapture.CaptureScreenshotAsTexture(1);
    //move to the right 370, up 22, grab a square of 540x675
    Color[] c = texture.GetPixels (370, 22, 540,675);
    Texture2D m2Texture = new Texture2D (540, 675);

    m2Texture.SetPixels (c);
    m2Texture.Apply ();

    byte[] textureBytes = m2Texture.EncodeToPNG();
     
  2. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    270
    Unity is ninja adding a width/height to <canvas> even tho I hardcoded "canvas" and "#unity-canvas" to 1280x720

    Just need to figure out how to remove that

    unityninja.png
     
  3. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    270
    config.devicePixelRatio = 1; fixes this issue in index.html
     
  4. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    437
    devicePixelRatio controls the resolution scaling for HDPI displays. Setting it to 1 will force the backing resolution to be the same as the canvas size.
     
  5. Blarp

    Blarp

    Joined:
    May 13, 2014
    Posts:
    270
    If Devicepixelratio isn't in your index file, cropping textures with rect will NEVER work correctly on desktop webgl.

    Along the same lines of cropping textures, the below code uses too much memory in WebGL 1.0 Safari mobile and crashes (setpixels/getpixels). Desktop Webgl has no problems, since we aren't limited to like 500mb of ram like mobile safari. Destroying and Resetting textures doesn't make a difference as well. Full Screen screenshots are the only way on mobile webgl at the moment. I've been riding my memory usage in the profile pretty tight, so I'm well aware of the awful memory limitations in safari mobile. The webgl 1.0 limitation on safari mobile is really affecting my lighting/reflections really bad and it's insanely stressful as well. I could sit here and complain to you for hours about safari mobile, and how it's been holding Unity WebGL back for going on 10 years now since webplayer was deprecated.

    Quote me if your pals ever have a meeting with them, because I'm pretty pissed about it, and have been for a LONG time.

    //texture = ScreenCapture.CaptureScreenshotAsTexture(1);
    //Texture2D m2Texture = new Texture2D (540, 675);
    //Color[] c = texture.GetPixels (370, 22, 540,675);
    //m2Texture.SetPixels (c);
    //m2Texture.Apply ();
    //byte[] textureBytes = m2Texture.EncodeToPNG();
     
    Last edited: Jul 23, 2021
    brendanduncan_u3d likes this.
  6. brendanduncan_u3d

    brendanduncan_u3d

    Unity Technologies

    Joined:
    Jul 30, 2019
    Posts:
    437
    iOS 15 with Safari support of WebGL2 can't come soon enough!
     
    Blarp likes this.