Runtime Texture Baking
Last updated
Last updated
For runtime texture baking, first, it is necessary to bake Shader Graph nodes into a .shader file. This is done by the Bake Shader button:
Only the nodes connected to the Shader Graph Baker are used in the generated .shader file.
After baking a .shader, it can be used in runtime scripts by creating a material using this shader. Properties of this material can be adjust in runtime too using Unity methods like SetFloat, SetVector, SetColor and etc.
Run-time API for baking material into a texture can be brought into scope with this using directive:
Unity Material class now will have 2 additional extension methods:
Creates Texture2D object and renders material into it.
resolution - Generated texture's resolution.
hasMipmap - should the created texture contain mipmaps?
linear - if baking a Normal or Linear Data type shader, then this value should be true. It is false for Color Map texture.
Renders material into the provided renderTexture. For rendering Normal and Linear Data, make sure the renderTexture is created using the linear flag.