我正在研究 Unity 的渲染纹理,您可以将相机的视图渲染到纹理上。然而,我注意到它并没有渲染整个相机的视图。它仅渲染相机视图的方形切片。
我正在尝试的是将相机的整个视图(16:9 长宽比)渲染到纹理(也是 16:9 长宽比)上。但现在它似乎只能将其视图的方形切片投影到方形表面上。有什么办法解决这个问题吗?
使用“RenderTexture”,您可以指定纹理大小:http://docs.unity3d.com/ScriptReference/RenderTexture.Create.html
它应该是这样的:
Camera camera = GameObject.Find("Main Camera");
int resWidth = Screen.width;
int resHeight = Screen.height;
RenderTexture rt = new RenderTexture(resWidth, resHeight, 24);
camera.targetTexture = rt; //Create new renderTexture and assign to camera
Texture2D screenShot = new Texture2D(resWidth, resHeight, TextureFormat.RGB24, false); //Create new texture
camera.Render();
RenderTexture.active = rt;
screenShot.ReadPixels(new Rect(0, 0, resWidth, resHeight), 0, 0); //Apply pixels from camera onto Texture2D
camera.targetTexture = null;
RenderTexture.active = null; //Clean
Destroy(rt); //Free memory
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)