Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Render Texture explanation

Discussion in 'Scripting' started by casio_2000, Aug 1, 2011.

  1. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    Hi,
    I need some help for render texture, imagine i want to blend 2 Camera. So in my scene i have two camera, (camera1) and (camera2). (camera1) have some objects and (camera2) have other objects, everything is shaded, light, shadow... If i understand correctly, i need to create a render Texture for each camera, maybe i need to use graphics.Blit to blend them? I want to Add rendertexture1 and 2, add like photoshop image process ( add, multiply....)

    Is it possible? Someone can show me a way?

    Thanks a lot
     
  2. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    If you just need to render the context of one camera on top of the other, all you need to do is to configure the depth of the cameras, set both of them to clear depth only and render the correct layers.
     
  3. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    Its not really clear, i want to Multiply one on the other. So why i need to use only the depth ?
    Do you have a bit of command reference to help me ? Im in C# for this process.

    thanks
     
  4. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    It's not really clear exactly what it is you want. Could you explain a bit more in detail and perhaps provide an illustration or example?
     
  5. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    This is a simple example, i have 2 camera, each camera with a different batch of asset (in different layer) i want to multiply on camera onver the other, with a multiply blending, to get one image. Something like that :


    Thanks for your help
     
  6. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Here's a quick example of multiply of two cameras. The source camera is multiplied onto the destination camera and the result is displayed in GUI. For clarity, the render is done on a red background and the GUI display is scaled down.

    The overlay camera needs to be set to only render objects in the overlay layer, the destination camera needs to be set to not render stuff in the overlay layer and the objects need to be placed in the correct layers ofcourse.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class Multiply : MonoBehaviour
    6. {
    7.     public Camera source, destination;
    8.    
    9.    
    10.     private RenderTexture renderTexture;
    11.     private Texture2D sourceRender, destinationRender;
    12.    
    13.    
    14.     void Start ()
    15.     {
    16.         renderTexture = new RenderTexture (Screen.width, Screen.height, 24);
    17.         sourceRender = new Texture2D (Screen.width, Screen.height);
    18.         destinationRender = new Texture2D (Screen.width, Screen.height);
    19.     }
    20.    
    21.    
    22.     void Update ()
    23.     {
    24.         RenderTexture active = RenderTexture.active;
    25.         RenderTexture.active = renderTexture;
    26.        
    27.         RenderTexture target = source.targetTexture;
    28.         source.targetTexture = renderTexture;
    29.         source.Render ();
    30.         sourceRender.ReadPixels (new Rect (0.0f, 0.0f, renderTexture.width, renderTexture.height), 0, 0);
    31.         source.targetTexture = target;
    32.        
    33.         Color background = destination.backgroundColor;
    34.         destination.backgroundColor = Color.red;
    35.         target = destination.targetTexture;
    36.         destination.targetTexture = renderTexture;
    37.         destination.Render ();
    38.         destinationRender.ReadPixels (new Rect (0.0f, 0.0f, renderTexture.width, renderTexture.height), 0, 0);
    39.         destination.targetTexture = target;
    40.         destination.backgroundColor = background;
    41.        
    42.         RenderTexture.active = active;
    43.        
    44.         Color[] sourcePixels = sourceRender.GetPixels (), destinationPixels = destinationRender.GetPixels ();
    45.        
    46.         for (int i = 0; i < sourcePixels.Length; i++)
    47.         {
    48.             destinationPixels[i] = new Color (
    49.                 (sourcePixels[i].r * destinationPixels[i].r) / 1.0f,
    50.                 (sourcePixels[i].g * destinationPixels[i].g) / 1.0f,
    51.                 (sourcePixels[i].b * destinationPixels[i].b) / 1.0f,
    52.                 (sourcePixels[i].a * destinationPixels[i].a) / 1.0f
    53.             );
    54.         }
    55.        
    56.         destinationRender.SetPixels (destinationPixels);
    57.         destinationRender.Apply ();
    58.     }
    59.    
    60.    
    61.     void OnGUI ()
    62.     {
    63.         GUI.DrawTexture (new Rect (0.0f, 0.0f, 300.0f, 300.0f), destinationRender);
    64.     }
    65. }
    https://gist.github.com/1118288
     
  7. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    Thanks a lot,
    this is really helpful.
     
  8. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    But i always getting the source camera over the source camera. I have the corrent culling mask, so im rendering the right layer, but always, the source camera is shows in red over the source camera. Its seems to not taking care of the destination camera. Im right ?
     
  9. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    Its my fault... Right on...

    Thanks
     
  10. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    Hi,
    thanks again for your help.

    It seems the multiply script not wokring correctly if my camera is set to deferred lighting. Is it possible ?
    I need to set in deferred lighting, because im using soft shader and i want the maximum quality in my game.

    Any suggestion?
     
  11. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    That shouldn't affect this script as it simply asks the camera to render what it sees to textures.

    Do note that this is an example of using RenderTextures to solve the problem where in reality your most optimal solution would be a shader. I'm unfortunately not familiar with shader syntax (one of those things it seems I just never get around to).
     
  12. casio_2000

    casio_2000

    Joined:
    May 26, 2011
    Posts:
    49
    If you want, i can build you a test scene, its really affect the inclusive exclusive light if im in deferred lighting or not. Its weird enh!

    Do you want a test seen to double check my saying?
     
  13. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    Unfortunately I don't really have the time to go through peoples individual projects.

    Make sure that all cameras are set to the same rendering mode and continue debugging from there.
     
    Centurex likes this.