3D player gameobject appearing as 2D sprite at runtime

Hello,

Coming from other programming works (graph theory, combinatorial optimization), i am quite a newbie in using unity framework ! Would be great if someone could help, i spent my last 2 evenings trying to find a solution but i am stuck (with my poor skills).

I am trying to create an effect where the player gameobject (as a 3D model) appears as a 2D sprite at runtime.
I have :

  • a layer “3DObjectmasked” for the player object that i dont render on my main camera
  • create a script for my player gameobject where i want to render a 2D sprite with a temporary camera on to the raycast between the player object and main camera
    private Camera _mainCamera; // the main camera
    private Camera _renderCamera; // the camera rendering the 2D sprite
    private GameObject _object; // the player object

    // Use this for initialization
    void Start () {
        _object=GetComponent<GameObject>();
        _object.layer=LayerMask.NameToLayer("3DObjectmasked");
        _mainCamera = GameObject.FindWithTag("MainCamera").GetComponent<Camera>();
       
        _renderCamera = new Camera();
        _renderCamera.cullingMask = LayerMask.NameToLayer("3DObjectmasked");
    }
   
    // Update is called once per frame
    void Update () {
        /*
        i lack some code.
        Can i put the _renderCamera transform exactly in the raycast between _mainCamera and _object
        but zoomed so that the _object fills all the viewport of _renderCamera ? I tried a lot of things, working with bounds, distance, fieldofview...
        */

        RenderTexture currentRT = RenderTexture.active;
        RenderTexture.active = _renderCamera.targetTexture;
        _renderCamera.Render();

        Texture2D image = new Texture2D(_renderCamera.targetTexture.width, _renderCamera.targetTexture.height);
        image.ReadPixels(new Rect(0, 0, _renderCamera.targetTexture.width, _renderCamera.targetTexture.height), 0, 0);
        image.Apply();

        RenderTexture.active = currentRT;
       
        /*
        i lack some code.
        Can i put the 2D texture as a sprite for the _object (so that the object will appear as a 2D character in the 3D game for the _mainCamera) ? I was never able to put the rendered texture as the sprite for the player object
        */
    }

I tried a lot of things, checked how for now the best i can do :

  • is putting the renderimaged on another material (albedo) and put it on another game object.
  • having a sprite for the player visible (adding a sprite renderer to the gameobject) but i cant fill it with anything i am doing…

I am quite lost right now to be honest. If a kind sir or miss could give me an hint at least, i would be grateful !
Thank you for reading my post and have a good day :slight_smile:

Looks like you’re on the right track. When I do this sort of “render to a texture first, then use it,” I generally put the render camera and the object that I am “filming” somewhere far away, like at (0,-1000,0) for instance.

As far as your first question (line 19), don’t use code: just PLACE the 3D object in the right place in front of the render camera, attach the render texture to it, etc.

As to your second question (line 35) you can use the render texture on a quad, using a cutout or transparent texture, such that you move that quad around like a sprite. I don’t think there is a direct obvious way to use Sprite.Create() on a RenderTexture (but there might be; I’ll let others chime in here), but putting the rendertexture on a quad card will get you pretty much where you wanna be.

I threw together a sample setup that I commonly use for this sorta thing. The only script in there is something to spin the target 3D object while I’m filming it with the render camera. I stick it up on a quad in the main area. Let me know if it sorta makes sense. It’s a complete package with scene, assets, etc.

3447990–273084–RenderTextureSetup.unitypackage (33.8 KB)

1 Like

Thanks a lot, I will try to test new scripts with your advices when I get back from work !

After working quite some time on it, i discovered this week-end that unity has an example of out to render a camera into a 2D texture. This example is in the standard assets on the asset store (if i am not mistaken) : it’s in the folder “scenes” and the scene is called “RenderTexture”.
There is an example of a cosmonaut who is rendered on a canvas, so if anyone is interested, it’s easy to have some idea on how to do it.

1 Like

Little error, it’s in the asset bundle : “Unity Samples: UI” from unity technologies (free like the standard assets)