Search Unity

Question Black Hole Rendering on Limited Depth camera - How to apply renderTexture on Sprite ?

Discussion in 'General Graphics' started by sudoku1v2, May 14, 2023.

  1. sudoku1v2

    sudoku1v2

    Joined:
    Jan 2, 2018
    Posts:
    6
    I am trying to simulate a Black Hole in the Galaxy, using a dedicated shader. The light coming behind the hole is modified going to the following effect.


    A shader is applied on main camera, using the following script. The shader in included in the material myMat.
    Code (CSharp):
    1. private void OnRenderImage(RenderTexture source, RenderTexture destination)
    2.     {
    3.         if (IsBlackHoleVisible)
    4.             Graphics.Blit(source, destination, myMat);
    5.     }
    But some objects, that are in front of the black hole (blue ring) shall not be modified.

    So I have created a second camera exactly at the same position and FOV than the Main Camera, but with depth starting at BlackHole position up to infinity.
    The second camera is rendered in a target render texture. Then I want to copy part of this texture in a Sprite that is set at the BlockHole position.

    I try the following script on Sprite object. But Sprite is blank or nothing...
    Code (CSharp):
    1.    
    2.     public RenderTexture renderTexture;
    3.     public Texture2D texture2D;
    4.     public Vector2 blackholePos;
    5.     public Vector2 blackholeSize;
    6.     public SpriteRenderer mySprite;
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         //Define texture2D
    11.         texture2D = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);
    12.         //set SpriteRenderer
    13.         mySprite = GetComponent<SpriteRenderer>();
    14.     }
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         //first method tested - copy renderTexture in texture2D
    19.         Graphics.CopyTexture(renderTexture, 0, 0, (int)blackholePos.x, (int)blackholePos.y, (int)blackholeSize.x, (int)blackholeSize.y, texture2D, 0, 0, 0, 0);
    20.  
    21.         //second method tested - copy renderTexture in texture2D
    22.         RenderTexture.active = renderTexture;
    23.         texture2D.ReadPixels(new Rect(0, 0, Screen.width, Screen.height), 0, 0);
    24.         texture2D.Apply();
    25.         RenderTexture.active = null;
    26.  
    27.         //then copy the texture2D as the Sprite
    28.         mySprite.sprite = Sprite.Create(texture2D, new Rect((int)blackholePos.x, (int)blackholePos.y, (int)blackholeSize.x, (int)blackholeSize.y), new Vector2(0, 0));
    29.     }
    30.  
    How to copy only part of a renderTexture in a Texture2D and apply it on a Sprite ?
    Thanks for your help...
     
  2. sudoku1v2

    sudoku1v2

    Joined:
    Jan 2, 2018
    Posts:
    6
    I finaly manage the problem by using a Cube Mesh with a very small Z size, and not a Sprite.
    I apply on this mesh a material with my Shader. And in the shader I extrat the screen part corresponding to the area where the cube is. Simple thanks to Unity.
    You can see the result hereafter.