在Unity3d中将完整的相机视图(16:9)渲染到纹理上 [英] Rendering complete camera view(16:9) onto a texture in Unity3d
问题描述
我在玩Unity的渲染纹理,您可以在其中将相机的视图渲染到纹理上.但是,我注意到它不能渲染整个相机的视图.它只会渲染相机视图的一个正方形切片.
I was playing around with Unity's render textures where you can render a camera's view onto a texture. However, I noticed that it doesn't render the entire camera's view. It only renders a square slice of the camera's view.
我正在尝试的是将相机的整个视图(长宽比为16:9)渲染到纹理(也是长宽比为16:9)上.但是现在,它似乎只能将其视图的正方形切片投影在正方形表面上.有什么解决办法吗?
What I'm trying is to get the entire view of the camera(16:9 aspect ratio) rendered onto a texture(also 16:9 aspect ratio). But right now it only seems to be able to project a square slice of its view on a square surface. Is there any kind of solution to this?
推荐答案
With 'RenderTexture' you can specify your texture size: 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
这篇关于在Unity3d中将完整的相机视图(16:9)渲染到纹理上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!