The code included in this post has been taken out because it completely solved the problem,
Only actual things @svan did was globally declaring variable tex w/ PGraphics tex;, plus adding tex.beginDraw(); & tex.endDraw(); to the OP’s already provided code!
, instead of just providing pre-made answers.
There’s nothing to provide but link to very basic requirements for dealing w/ PGraphics objects.