[Unity]Unity中自定义RT渲染物件,物件渲染异常的问题解决
2020/07
02
12:07
int lastLayer = actor.layer;
actor.ChangeLayer(LayersDefine.DynamicRtRender);
renderCamera.CopyFrom(mainCamera);
renderCamera.cullingMask = 1 << LayersDefine.DynamicRtRender;
renderCamera.backgroundColor = new Color(0, 0, 0, 0);
renderCamera.clearFlags = CameraClearFlags.SolidColor;
RenderTexture currRt = RenderTexture.GetTemporary (mainCamera.pixelWidth, mainCamera.pixelHeight);
renderCamera.targetTexture = currRt;
renderCamera.Render();
RenderTexture lastRt = RenderTexture.active;
RenderTexture.active = currRt;
Texture2D tex2D = new Texture2D(mainCamera.pixelWidth, mainCamera.pixelHeight);
tex2D.ReadPixels(new Rect(0, 0, mainCamera.pixelWidth, mainCamera.pixelHeight), 0, 0);
tex2D.Apply();
RenderTexture.active = lastRt;
RenderTexture.ReleaseTemporary (currRt);
image.sprite = Sprite.Create(tex2D, new Rect(0, 0, tex2D.width, tex2D.height), new Vector2(0.5f, 0.5f), 100.0f);
发现渲染次序错误
RenderTexture.GetTemporary (mainCamera.pixelWidth, mainCamera.pixelHeight, 24); // 解决问题
RenderTexture.GetTemporary
public static RenderTexture GetTemporary(int width, int height, int depthBuffer = 0, RenderTextureFormat format = RenderTextureFormat.Default, RenderTextureReadWrite readWrite = RenderTextureReadWrite.Default, int antiAliasing = 1, RenderTextureMemoryless memorylessMode = RenderTextureMemoryless.None, VRTextureUsage vrUsage = VRTextureUsage.None, bool useDynamicScale = false);
Parameters
width | Width in pixels. |
height | Height in pixels. |
depthBuffer | Depth buffer bits (0, 16 or 24). Note that only 24 bit depth has stencil buffer. |
format | Render texture format. |
readWrite | Color space conversion mode. |
antiAliasing | Number of antialiasing samples to store in the texture. Valid values are 1, 2, 4, and 8. Throws an exception if any other value is passed. |
memorylessMode | Render texture memoryless mode. |
desc | Use this RenderTextureDesc for the settings when creating the temporary RenderTexture. |
CopyRights: The Post by BY-NC-SA For Authorization,Original If Not Noted,Reprint Please Indicate From 老刘@开发笔记
Post Link: [Unity]Unity中自定义RT渲染物件,物件渲染异常的问题解决
Post Link: [Unity]Unity中自定义RT渲染物件,物件渲染异常的问题解决