Unity Products:Amplify Impostors/Custom Baking
The Amplify Impostors baker usually gathers the deferred GBuffer information that contains every material data for a typical PBR rendering. This, however, might not always be necessary or might not even be enough. In some cases the object you want to create the impostor for has some special realtime shader effect that a simple PBR rendering doesn't cover. In other cases you might just want to reduce the amount of data being generated because you have higher platform constraints (ie: mobile). The idea behind custom baking is allowing you to tell the tool what you want to bake and how you want to render it on a case by case scenario.
Creating these shaders can become quite difficult so we have included in our package the necessary templates and nodes which can be used with our own Amplify Shader Editor plugin, making the process a lot easier and faster. The package also provides a simple sample and this page explains the process of creating it (we assume the use of Amplify Shader Editor).
This tutorial demonstrates the process of creating the same custom impostor found in the samples. The intention is to create the impostor for an object that contains a special dynamic shader. This shader adds a procedural layer of paint on top of the object. This layer of paint is procedural in the sense that it uses the object position to calculate a pseudo-random color for the paint. If we were to bake a regular impostor for this object we would lose this procedural behavior and end up with a static version instead.
Notice how the left real object changes color while the right impostor stays a static red.
To accomplish this two special shaders are needed. One is a shader that will be used instead of the original shader of your object material to bake the different texture maps. This shader contains the different outputs that will be used to generate the different textures and what their channels contain. We'll can call this the "bake shader". The other shader will use the generated textures and render the impostor correctly with the intended custom effect you might be looking for. This is the shader you will use in your product that creates the impostor effect. We'll call this one the "runtime shader". Finally we need to setup the outputs and link the shaders to them, we do this using a preset asset file that you can create a reference in the impostor component.
NOTE: If you are using ASE make sure to import the Impostors package AFTER the shader editor.
Step 1 - Create the bake shader
- Right click in your project and create a new Impostor Bake shader.
- With the shader open in the editor start by placing the texture samplers and pack them to the chosen output and make sure the property names match the original shader.
- Then create the graph nodes and connections that correspond to the static parts that we can bake into maps, in our case that's everything except the procedural color part. Also, because we'll later need the special mask to recreate the final procedural effect and because we want to save some texture memory we will pack it along side the remaining data and connect it to the same single output.
- Add the "Pack Normal Depth" shader function node which will transform the normals into the necessary space and create the depth information in the alpha channel.
- (optional) If your object shader uses a "Cutout" effect make sure to mimic it by subtracting the clip mask value and connecting it to the "Clip" output
Step 2 - Create the runtime shader
- Right click in your project and create a new Impostor Runtime shader.
- In the editor add a new "Amplify Impostor node", connect the Albedo, World Normal and Alpha outputs to the available inputs in the master node. Hit Save.
NOTE: Doing this should compile an Impostor compatible shader that uses the most important data from the bake, however we still need customize it further to support the intended procedural effect.
- In our case we want to fetch a custom map not available in the original outputs, we do this by selecting the Impostor node and changing the "Extra Samplers" field to the appropriate number (in our case 1) and connecting a Texture Object node with the same name as the output map name created in Step 2. In this case it's called "_Mask".
- We now need to create the procedural logic from the original shader which uses the object XZ position in a wave function to generate a color.
- Finally filter the procedural color to the mask from the extra output and connect the appropriate channels to their outputs. Save.
Step 3 - Setup your impostor and bake
- Create a new preset by going to "Assets > Create > Amplify Impostor Bake Preset" or hitting the "New" button in the impostor component. Select it in the chosen project location.
- Assign the shaders to the preset and add or remove outputs until you have the same number of usable outputs from the shader created in [Step 1].
- Configure the outputs to represent the data they hold. In this case the first output holds albedo color data in RGB so we mark it as sRGB color space. Do make sure to mark the output that contains the alpha channel, in this case it's the first one so we leave the radio button as is.
- Now, simply select the new preset in the impostor component, override the fields if necessary and hit "Bake Impostor"