[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

widthWidth in pixels.
heightHeight in pixels.
depthBufferDepth buffer bits (0, 16 or 24). Note that only 24 bit depth has stencil buffer.
formatRender texture format.
readWriteColor space conversion mode.
antiAliasingNumber 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.
memorylessModeRender texture memoryless mode.
descUse this RenderTextureDesc for the settings when creating the temporary RenderTexture.

--转载请注明: http://blog.coolcoding.cn/?p=2061

发表回复

欢迎回来 (打开)

(必填)