Search Unity

Question Difference in Scene and Game view when using multiple cameras

Discussion in '2D' started by nTu4Ka, Apr 27, 2023.

  1. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Hello,


    I'm working on 2D game with multiple characters.
    I want to implement character layering in following way: lower layer camera render everything on its layer into RenderTexture. Then this RenderTexture is assigned to SpriteRenderer on next layer and is used as background for second layer. This is the only solution I found to blend each character effects only with everything below him (and not on top).

    Here is the issue.
    When I play the game I see everything as expected in Scene view as well as each camera previews.
    But the Game view shows me only character rendered on top layer and camera background color instead of previous layer RenderTexture.

    Camera depths seem to be configured properly. The lower the layer - the smaller depth number (0 for static background, 10 for top most characters).

    Does anyone has any ideas?

    I attached camera configurations.
    RenderTextures are created, assigned and updated at runtime.

    Start()
    RenderTexture LayerRender = new RenderTexture(Screen.Width, Screen.Height, 0, RenderTextureFormat.Default);
    GetComponent<Camera>().targetTexture = LayerRender;

    Update()
    for (int i = 0; i < 2; i++) LayerCamera.Render();



    Kind regards,
    nTu4Ka.
     

    Attached Files:

  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    223

    Can't you just use the sorting group component for rendering sprites on top or below of each other?
     
  3. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    I need to blend them. E.g. multiply color blending.
    As far as I know I cannot sample pixel color in shader of another object (object on layer below).
     
  4. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Looks interesting. I wasn't aware of this. Will look into this one.
     
    venediklee likes this.
  5. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    i think that produces funky results for example when you change transparency
    sounds like you are making some mistake on your camera render texture that is not rendering the background as transparent

    the first thing that i have to say is that you cant apply the render texture to a sprite renderer, you would need to create a sprite first.

    Anyway you can check out this thread:

    https://forum.unity.com/threads/how-to-render-a-reflection-mirror.1219998/
     
  6. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Thank you!

    I assigned rendered texture to the material following way:
    Code (CSharp):
    1. GetComponent<Renderer>().material.mainTexture = RenderTexture;
    It seemed like it worked. I could see rendered texture on the material (not in sprite field of course).
    I could also see everything in action in scene view.

    As an alternative solution I tried creating and assigning sprites but the game became significantly slow almost slide show.


    Code (CSharp):
    1. LayerTexture = new Texture2D(Instance.ScreenWidth, Instance.ScreenHeight);
    2. LayerTexture.ReadPixels(new Rect(0, 0, Instance.ScreenWidth, Instance.ScreenHeight), 0, 0);
    3. LayerTexture.Apply();
    4.  
    5. // Assign the Texture2D to a SpriteRenderer
    6. NextLayerRenderer.sprite = Sprite.Create(LayerTexture, new Rect(0, 0, Instance.ScreenWidth, Instance.ScreenHeight), new Vector2(0.5f, 0.5f));
    I'll try Kurt-Dekker package and see the implementation.
    Thank you!
     
    Last edited: Apr 30, 2023
  7. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Interesting solution.
    I tried something like this. The issues is - I'm rendering characters in world space and RawImage is a screen space component. If I understand correctly.
    I think I'll try the option rendering on a quad.

    I'm still confused why everting looks good in scene view but not in game view.
     
  8. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    I figured out why I was seeing everything in scene view but not in game view.
    Game view is based on cameras while scene view shows all the objects as they are.

    So looks like I don't completely understand how camera rendering works and how to set up multiple cameras rendering queue.
     
  9. nTu4Ka

    nTu4Ka

    Joined:
    Jan 27, 2014
    Posts:
    69
    Aaand after this I think I resolved my issue.

    I changed SpriteRenderer to RawImage to assign RenderTexture directly and Canvas to Screen Space - Camera.

    Thank you @venediklee and @karderos for your involvement and ideas.
     
    Last edited: May 14, 2023
    venediklee likes this.