0
\$\begingroup\$

How does one gpu rotate a Texture2D or RenderTexture using Unity c#? Or, is that possible?

I think it might be something like this... I'm also trying to understand how gpu_scale seems to work here?

    // Internal unility that renders the source texture into the RTT - the scaling method itself.
    static void _gpu_scale(Texture2D src, int width, int height, FilterMode fmode = FilterMode.Trilinear)
    {
        //We need the source texture in VRAM because we render with it
        src.filterMode = fmode;
        src.Apply(true);

        //Using RTT for best quality and performance. Thanks, Unity 5
        RenderTexture rtt = new RenderTexture(width, height, 32);

        //Set the RTT in order to render to it
        Graphics.SetRenderTarget(rtt);

        //Setup 2D matrix in range 0..1, so nobody needs to care about sized
        GL.LoadPixelMatrix(0, 1, 1, 0);

        //Then clear & draw the texture to fill the entire RTT.
        GL.Clear(true, true, new Color(0, 0, 0, 0));
        Graphics.DrawTexture(new Rect(0, 0, 1, 1), src);
    }

Edit:

The input image is rotated 90 degrees ccw to fit in the left part (replacing the beige with pink dots) of this texture (no alpha is needed). If you want some numbers, it's a rect from 0,0 to new Vector2(794, 1024):

enter image description here

\$\endgroup\$
8
  • \$\begingroup\$ What exactly is the player-visible outcome you're trying to accomplish? There may be simpler routes to get there. \$\endgroup\$ Commented Apr 9, 2020 at 11:26
  • \$\begingroup\$ It's a bit complicated. I have a RenderTexture that is a specific mapping of input from the device camera, that needs to be rotated and scaled into the UV map to show up properly on a material texture. I suppose the model could have its UV and materials re-done, but I don't have access currently to computing that can do modeling. \$\endgroup\$ Commented Apr 10, 2020 at 9:34
  • \$\begingroup\$ Currently I have implemented a solution where I convert the RenderTexture to Texture2D and then convert it back to RenderTexture again and then back to Texture2D ... just wondering if there is a way to do both the rotate and remap/scale into the portion of the texture in RenderTexture space instead of converting around so much \$\endgroup\$ Commented Apr 10, 2020 at 9:35
  • \$\begingroup\$ We might be able to do this in the shader when we draw the texture on the model, just by calculating alternative sample coordinates, without spending any time actually moving the pixel data around in memory. Would that be an option in your case? If so, can you show us the model, how the texture sits across it now, and diagram for us how it needs to change? Knowing what kind of shader/material you're using on the model will help too. \$\endgroup\$ Commented Apr 10, 2020 at 12:36
  • \$\begingroup\$ i'm open to using a different shader. currently i'm using MK Toon Free. \$\endgroup\$ Commented Apr 11, 2020 at 4:39

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.