Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

RenderTexture

Discussion in 'Scripting' started by blakeordway, Sep 16, 2016.

  1. blakeordway

    blakeordway

    Joined:
    Aug 9, 2016
    Posts:
    19
    I'm working in Unity 4.7 free, so I don't have access to the Render Texture of Unity 5. So, I had to create my own. I adapted scripts from the wiki, as well as what I found from some Youtube videos. I am currently having issues with timing (too much overhead), as well as the texture not being Applied.

    The scenario is this, I have a camera inside a car being used as a First Person camera, I want to show in that same camera view the view of the side mirrors (the passenger mirror and the driver side mirror) of the car. The 'feed' of the cameras is rendered as a texture on other GameObjects that are within the drivers view so he doesn't have to look over if he's trying to merge, he can see it just by looking in the top left (driver side mirror), or top right (passenger side mirror).

    This causes a lot of overhead (for one car without this, I had 4ms runtime), with 1 texture render script running, I had 18ms... I know that applying a texture all the time can be very heavy on the processor, but I feel that it's worse than it should be. My second problem is that the OnPostRender function is not being called after a certain amount of time, so the Render Texture is only the very first frame, and sometimes another one randomly. Here's the scripts that I have and if anyone has any ideas on how to fix it, I'm open and willing to listen, or if there's a better way to do this (similar to this article, that I couldn't get to work http://answers.unity3d.com/questions/421450/how-can-i-render-a-scene-to-a-surface.html)

    Script attached to gameobject that has a camera component:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RenderTexture : MonoBehaviour
    6. {
    7.     public Texture2D renderedTexture;
    8.     private Rect rectangleSize;
    9.  
    10.     void Awake()
    11.     {
    12.         renderedTexture = new Texture2D((int)camera.pixelRect.width, (int)camera.pixelRect.height);
    13.         rectangleSize = new Rect(0f, 0f, (int)camera.pixelRect.width, (int)camera.pixelRect.height);
    14.     }
    15.  
    16.     void OnPostRender()
    17.     {
    18.         Debug.Log("OnPostRender::Called");
    19.         renderedTexture.ReadPixels(rectangleSize, 0, 0, true);
    20.  
    21.         renderedTexture.Apply(); // I think the OnPostRender stops being called if this fails
    22.     }
    23. }
    24.  
    Script attached to the GameObject that needs to have that texture:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class RTtest : MonoBehaviour
    6. {
    7.     RenderTexture passengerMirrorCamera;
    8.  
    9.     void Start()
    10.     {
    11.         passengerMirrorCamera = GameObject.Find("Car 02").transform.GetChild(2).GetComponent<RenderTexture>();
    12.     }
    13.  
    14.     void Update()
    15.     {
    16.         renderer.material.mainTexture = passengerMirrorCamera.renderedTexture;
    17.     }
    18.  
    19. }
    20.  
    21.  
     
  2. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    If you use Camera.targetTexture is more fast that Texture2D.RealPixels, and needn't call OnPostRender each frame.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Camera))]
    5. public class RenderTexture : MonoBehaviour
    6. {
    7.     public MeshRenderer mr;
    8.  
    9.     void Awake()
    10.     {
    11.         Camera camera = GetComponent<Camera>();
    12.         if (mr != null) mr.material.mainTexture = camera.targetTexture;
    13.         else Debug.Log(this.GetType().Name + " MeshRenderer null");
    14.     }
    15. }
    In this system, you need two cameras. Download link: https://ufile.io/a3e26
     
  3. ob103ninja

    ob103ninja

    Joined:
    Nov 19, 2014
    Posts:
    45
    Couldn't you, well, update unity?
     
  4. blakeordway

    blakeordway

    Joined:
    Aug 9, 2016
    Posts:
    19
    Not at this current time. We have plans to soon, but for now we have to stick with 4.7.1.

    Could you explain why I would need two cameras? Would I need one where I want to display the image, and then one where I'm getting the feed from?
     
  5. IsGreen

    IsGreen

    Joined:
    Jan 17, 2014
    Posts:
    206
    https://docs.unity3d.com/Manual/class-RenderTexture.html

    Example
    A very quick way to make a live arena-camera in your game:

    1. Create a new Render Texture asset using Assets->Create->Render Texture.
    2. Create a new Camera using GameObject > Create General > Camera.
    3. Assign the Render Texture to the Target Texture of the new Camera.
    4. Create a wide, tall and thin box
    5. Drag the Render Texture onto it to create a Material that uses the render texture.
    6. Enter Play Mode, and observe that the box’s texture is updated in real-time based on the new Camera’s output