如何在实例化相机上应用渲染纹理(作为目标纹理)?
How to apply render texture(as target texture) on instantiated camera?
我有一个相机预制件,我在不同的位置实例化了 4 次,我想在它上面添加渲染纹理(作为目标纹理),这样我就可以采用相同的纹理并应用到一个平面上进行监视场景。不清楚的请教。我正在尝试进行监视,但不知道该怎么做,我被困在了这里。请举例说明 在此先感谢。
我觉得unity手册解释的很好https://docs.unity3d.com/Manual/class-RenderTexture.html.
更具体一点,这是一个可能的实现:
在 AssetFolder 中创建一些 RenderTexture,然后您必须 link 将它们添加到您的 Camera 脚本中以进行渲染。将此文件添加到您的 TextureRender-Camera。
using System.Collections;
using UnityEngine;
public class Camera2Texture : MonoBehaviour {
public RenderTexture[] renderTextures;
private Camera cam;
private void Awake()
{
cam = GetComponent<Camera>();
}
private void Start()
{
StartCoroutine(RenderTexturesCoroutine());
}
IEnumerator RenderTexturesCoroutine()
{
for (int i = 0; i < renderTextures.Length; i++)
{
// just move the camera a little bit and focus the center of the scene
this.transform.position += Vector3.left * 2 * i;
cam.transform.LookAt(Vector3.zero);
cam.targetTexture = renderTextures[i];
yield return new WaitForSeconds(1f);
cam.Render();
}
cam.targetTexture = null;
this.gameObject.SetActive(false);
}
}
我启动了一个协程,它每秒移动我的 TextureRender-Camera 一点点,放入数组中的下一个 RenderTexture 并渲染图像。最后我禁用了相机。这是将所有 4 个渲染纹理放在四边形上时的结果:Result
我有一个相机预制件,我在不同的位置实例化了 4 次,我想在它上面添加渲染纹理(作为目标纹理),这样我就可以采用相同的纹理并应用到一个平面上进行监视场景。不清楚的请教。我正在尝试进行监视,但不知道该怎么做,我被困在了这里。请举例说明 在此先感谢。
我觉得unity手册解释的很好https://docs.unity3d.com/Manual/class-RenderTexture.html.
更具体一点,这是一个可能的实现:
在 AssetFolder 中创建一些 RenderTexture,然后您必须 link 将它们添加到您的 Camera 脚本中以进行渲染。将此文件添加到您的 TextureRender-Camera。
using System.Collections;
using UnityEngine;
public class Camera2Texture : MonoBehaviour {
public RenderTexture[] renderTextures;
private Camera cam;
private void Awake()
{
cam = GetComponent<Camera>();
}
private void Start()
{
StartCoroutine(RenderTexturesCoroutine());
}
IEnumerator RenderTexturesCoroutine()
{
for (int i = 0; i < renderTextures.Length; i++)
{
// just move the camera a little bit and focus the center of the scene
this.transform.position += Vector3.left * 2 * i;
cam.transform.LookAt(Vector3.zero);
cam.targetTexture = renderTextures[i];
yield return new WaitForSeconds(1f);
cam.Render();
}
cam.targetTexture = null;
this.gameObject.SetActive(false);
}
}
我启动了一个协程,它每秒移动我的 TextureRender-Camera 一点点,放入数组中的下一个 RenderTexture 并渲染图像。最后我禁用了相机。这是将所有 4 个渲染纹理放在四边形上时的结果:Result