Here's what I got this far:
Unity allows to modify its render pipeline with Command Buffers.
https://blogs.unity3d.com/2015/02/06/extending-unity-5-rendering-pipeline-command-buffers/?_ga=2.221873880.1501917183.1612624884-1078945796.1596796187
I created a new CommandBuffer and added two commands to it: SetRenderTarger sets RenderTexture as target texture and Graphcs.DrawRenderer draws the content of MeshRenderer that is attached to Spine animation object into the target texture.
void Start()
{
var animation_state = anim.AnimationState;
animation_state.SetAnimation(0, "Walk", true); //start spine animation
buffer = new CommandBuffer();
buffer.SetRenderTarget(rt); //set render texture [rt]
buffer.DrawRenderer(mr, mr.material); //draw MeshRenderer [mr]
}
OnWillRenderObject apparently gets called during the rendering process so I executed CommandBuffer during it. Graphics.ExecuteCommandBuffer executes it immediately while Camera and Lightning CommandBuffer's get executed automatically during rendering so implementation of last two should be a bit different.
private void OnWillRenderObject()
{
rt.Release(); //clear the texture
mr.transform.localScale = new Vector3(100f, -100f, 1f); //switch scale to match texture pixel coordinates
Graphics.ExecuteCommandBuffer(buffer);
mr.transform.localScale = new Vector3(1f, 1f, 1f); //reset scale
}
private void OnDisable()
{
rt.Release();
}
rt.Relase() should clear the texture, I also do it on OnDisable because garbage cleaner doesn't take care of it.
This is porbably not a 100% solution as there might be some memore leakage and it might be unoptimized in general. There also an issue in that the renderer transforms Unity units directly into pixels, that means that an animation that is 3 units tall will be 3 pixels tall on the texture. The same goes for the position. That's why I changing scale before rendering which is a temporary solution and I'm looking for a correct way to do that.