Search Unity

Question Camera stacking and RenderTexture Problem

Discussion in 'General Graphics' started by AshwinTheGammer, Feb 28, 2023.

  1. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    I have two Cameras
    MainCamera - That renders everything except Weapon 3D model ('Weapon' layer) - Clear Flag - Skybox - Depth value - 0
    WeaponCamera - That only renders Weapon Layer - Clear Flag set to 'Depth only' - Depth Value -1

    I want to render whatever I see on a RawImage. I do this by creating renderTexture in runtime and assigning it to RawImage.
    Here is the script:

    Code (CSharp):
    1. private void Start()
    2.     {
    3. screenDisplay=GameObject.FindGameObjectWithTag("RenderScreen").GetComponent<RawImage>();
    4.         CreateRenderTexture();
    5. }
    6.  
    7. void CreateRenderTexture()
    8. {
    9.     // Get the original render texture from the original camera
    10.     RenderTexture originalRenderTexture = PlayerCamera.targetTexture;
    11.  
    12.     // Calculate the target resolution of the render texture based on the current screen resolution and the stored percentage
    13.     int targetWidth = Mathf.RoundToInt(Screen.currentResolution.width * (PlayerPrefs.GetFloat("MoveRenderTexture", 75f) / 100f));
    14.     int targetHeight = Mathf.RoundToInt(Screen.currentResolution.height * (PlayerPrefs.GetFloat("MoveRenderTexture", 75f) / 100f));
    15.  
    16.     // Create a new RenderTexture with the target resolution
    17.     renderTexture = new RenderTexture(targetWidth, targetHeight, 24);
    18.  
    19.     // Set the render texture as the target texture for the main camera
    20.     PlayerCamera.targetTexture = renderTexture;
    21.  
    22.     // Create a temporary render texture to hold the output of the weapon camera
    23.     RenderTexture weaponRenderTexture = new RenderTexture(targetWidth, targetHeight, 24);
    24.  
    25.     // Render the weapon camera to the temporary render texture
    26.     WeaponCamera.targetTexture = weaponRenderTexture;
    27.     WeaponCamera.Render();
    28.  
    29.     // Combine the main camera and weapon camera render textures
    30.     RenderTexture combinedRenderTexture = new RenderTexture(targetWidth, targetHeight, 24);
    31.     Graphics.Blit(renderTexture, combinedRenderTexture);
    32.     Graphics.Blit(weaponRenderTexture, combinedRenderTexture);
    33.  
    34.     // Assign the combined render texture to the RawImage
    35.     screenDisplay.texture = combinedRenderTexture;
    36.  
    37.     // Get the height and width of the ScreenDisplay RectTransform
    38.     RectTransform screenDisplayRect = screenDisplay.rectTransform;
    39.     float displayHeight = screenDisplayRect.rect.height;
    40.     float displayWidth = screenDisplayRect.rect.width;
    41.  
    42.     // Calculate the height of the camera view that matches the RawImage's aspect ratio
    43.     float cameraHeight = displayHeight;
    44.     float cameraWidth = displayWidth;
    45.  
    46.     // Adjust the aspect ratio of the camera
    47.     PlayerCamera.aspect = aspectRatio;
    48.  
    49.     // Reset the original render texture of the original camera
    50.     PlayerCamera.targetTexture = combinedRenderTexture;  
    51. }
    52.  
    53. //PlayerCamera = MainCamera

    But the problem is it is not rendering Weapon 3D model
    I get error Display 1 No Cameras rendering

    - A video
     
  2. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
  3. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
    You're never resetting the targetTexture of your WeaponCamera
    Because all of your cameras are rendering to RenderTextures. This means none of them are rendering to the screen.