Unity Texture Texture2D RenderTexture

http://fargesportfolio.com/unity-texture-texture2d-rendertexture/

(1) Texture based on GPU

(2) RenderTexture is based on GPU

(3) Texture2D passes CPU data to GPU based on CPU Texture2D.Apply

-------------------------------------------------- ---------------------- April 20, 2020 more ---------------------- -------------------------------------------------- ---

Here to talk about Texture2Dthe Applyoperation, when we use SetPixelor SetPixelsmust call the Apply method, or otherwise display the original map, there is a link to a specific look: https://blog.csdn.net/carefreeq/article/details/52635524

The picture is stored in the memory, and the CPU points to this picture address, and the GPU displays it according to the address taken by the CPU. If the picture data is changed, there is no need to call Apply. Then the GPU does not use the instruction sent by the CPU and will not change the picture. Display. The following code, when I did not call the Apply operation, the original image is displayed, and when I call it, the display is pure red

 private void BtnClick()
    {
        Texture2D tex = new Texture2D(0, 0);
        tex.LoadImage(File.ReadAllBytes(desktopPath + "/111.jpg"));
        Color[] colors = tex.GetPixels();
        for (int i = 0; i < colors.Length; i++)
            colors[i] = Color.red;

        tex.SetPixels(colors);
        //tex.Apply();  这里更改
        rawImg.texture = tex;
    }

------------------------------------------------------------------------------------------------------------------------------------------------------------------------

/// Pass a GPU RenderTexture to CPU Texture2D
/// This opperation has heavy cost (ReadPixels)
/// We reduice the RenderTexture size first to be more efficient
 
void RenderTextureToTexture2D()
{
 
	// Please, be power of 2 for performance issue (2, 4, 8, ...)
	int downSize = 2;
 
	RenderTexture textureToDownsizeAndCopyToCPU = new RenderTexture(1920, 1080, 0);
	Texture2D newTexture2DToBeLoadedTo = new Texture2D(textureToDownsizeAndCopyToCPU.width / downSize, textureToDownsizeAndCopyToCPU.height / downSize, TextureFormat.ARGB32, false);
 
	// Load textureToDownsizeAndCopyToCPU from something you like...
 
	// Create a temporary downsized texture
	RenderTexture textureDownsized = RenderTexture.GetTemporary(textureToDownsizeAndCopyToCPU.width / downSize, textureToDownsizeAndCopyToCPU.height / downSize);
 
	// Remember currently active render texture
	RenderTexture currentActiveRT = RenderTexture.active;
 
	RenderTexture.active = textureDownsized;
 
	// Copy textureToDownsizeAndCopyToCPU to textureDownsized
	Graphics.Blit(textureToDownsizeAndCopyToCPU, textureDownsized);
 
	// Read the RenderTexture image into it
	// Pass GPU bytes to CPU
        // Very performance consuming
	newTexture2DToBeLoadedTo.ReadPixels(new Rect(0, 0, textureDownsized.width, textureDownsized.height), 0, 0, false);
	// Debug.Log(tex2D.width + " " + tex2D.height);
	// Restorie previously active render texture
	RenderTexture.active = currentActiveRT;
 
	RenderTexture.ReleaseTemporary(textureDownsized);
}
 
 
void RenderTextureFromShader()
{
        Graphics.Blit(null, RenderTextureDestination, materialShader);
}

 

1: The assignment picture is gray

Graphics.CopyTexture (or Graphics.Blit) works on the GPU side, while ReadPixels works on the CPU. This is why EncodetoJPG on the CPU works after ReadPixels instead of CopyTexture.

You must use this program to save JPG to disk

https://answers.unity.com/questions/1479723/encodetojpg-creates-gray-image-after-using-copytex.html

2: Convert RenderTexture to Texture2D

RenderTexture.active = myRenderTexture;
myTexture2D.ReadPixels(new Rect(0, 0, myRenderTexture.width, myRenderTexture.height), 0, 0);
myTexture2D.Apply();

3: Texture2D to RenderTexture

 // texRef is your Texture2D
 // You can also reduice your texture 2D that way
    RenderTexture rt = new RenderTexture(texRef.width / 2, texRef.height / 2, 0);
    RenderTexture.active = rt;
    // Copy your texture ref to the render texture
    Graphics.Blit(texRef, rt);
         
    // Now you can read it back to a Texture2D if you care
    if (tex2D == null)
         tex2D = new Texture2D(rt.width, rt.height, TextureFormat.RGBA32, true);
    tex2D.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0, false);

Guess you like

Origin blog.csdn.net/K20132014/article/details/97018446