Unity双相机Camera,将虚拟相机视角画面显示在平面Plane上
程序员文章站
2023-12-26 23:09:03
...
Unity双相机Camera,将虚拟相机视角画面显示在平面Plane上
场景中通过添加Render Texture实现
1、场景中新建Render Texture
2、将其拖到相机的Target Texture上
3、新建Plane,将虚拟相机视角画面渲染在上面:将第一步新建的Render Texture直接拖到Plane上
4、运行,相机拍摄的画面被实时显示在Plane上
通过挂脚本实现
将脚本挂在场景中
using UnityEngine;
using System.Collections;
using System.IO;
public class GetImage : MonoBehaviour
{
public Camera mainCam; //目标摄像机
RenderTexture rt; //声明一个中间变量
Texture2D t2d ;
public GameObject pl;
void Start ()
{
t2d = new Texture2D(800,600,TextureFormat.RGB24,false);
rt = new RenderTexture(800, 600, 24);
mainCam.targetTexture = rt;
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.Space))
{
pl.GetComponent<Renderer>().material.mainTexture = rt;
//截图到t2d中
RenderTexture.active = rt;
t2d.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
t2d.Apply();
RenderTexture.active = null;
}
}
}
选择拍摄的相机与成像的平面
转载链接
https://blog.csdn.net/KillMeHealMe/article/details/102838932