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