MinionsArt made this extremely cool shader whereby blocks appear and disappear with vertex animation. After playing with it for a few minutes, I of course got the over-ambitious idea that I should use it to have ruined structures rebuilding themselves, but this would require more than the world space translation and scaling happening in the original shader, so I fell down a rabbit hole of Matrix transformations, and emerged with a shader that can in fact do precisely that. I also addressed a few issues in the original shader (MinionsArt is much more skilled than I am, so I mean no disrespect!) and adapted it to better suit my deferred workflow.
How To Use
Unpack the attached Shaders.zip, put the files in your project maintaining the directory structure. Alternatively, get the files from the Lib and SurfVert folders in my repo here. Again, maintaining directory structure.
- You can now set the axes on which the non-matrix translation and scaling happens, which is useful if you (like me!) have Z-up models.
- Since I added matrix scaling, the non-matrix scaling from the original shader is somewhat redundant, but I have kept it just in case someone preferred it. It can be enabled/disabled with a keyword toggle to save on GPU resources.
- In her post MinionsArt mentions not being able to see anything with the shader while in edit mode. I surfaced the _Moved property in the editor so you can toy with it to see the animation for yourself without having to test it at runtime. (shader is still instanced!)
- There's more than one way to skin a cat and I prefer to handle material toggles in such a way that the keyword and the property associated with the keyword are the same in order to reduce possible keyword cruft, so all the toggles are done the way I like them.
- removed #pragma multi_compile_instancing because in a surface shader this is ignored, as all surface shaders get instancing variants compiled unless you add a pre-compiler directive otherwise
- All of the keywords are now local because I have a huge problem with going over global keyword limits in my main RSA projct and now set all keywords local unless I know for sure I'll need them to be global.
- The entire effect requires a keyword I added, _EFFECT_ON to be enabled. That way if you have the blocks appear and want them to stay, you can disable the keyword and save some processing power (since the calculations are inside an #ifdef block)
Optional reading, tips specific to my common libraries (unrelated to the MinionsArt shader):
- Mine uses the standard lighting model and is all set up for Pabst Blue Ribbon! (also known as Physically Based Rendering...) Accordingly it has a keyword to toggle the use of a normal map to save resources; this is kinda clumsy as-is, it makes more sense when you have a custom editor, but I have things I gotta get done and I don't need this shader for my game, so you'll have to live without a custom editor to auto check it when you drag a normal map into the slot.
- Along with this using the Standard lighting model, you get a taste of my full power as I have very nicely included part of my own personal common library in this shader, as you can see in the surface function. This also means that for this shader to work, you must follow the file/directory structure as indicated here.
- As part of getting a taste of my full power you have a ton of options for metallic/glossiness maps which may seem confusing without a custom editor. I'll probably explain all this in a future tutorial installment. Basically, you get a dropdown to pick what kind of shininess map you have, and the sampled texture is applied accordingly. If you don't know what kind of map you have, check the file name. If you're still not sure, if it's black and white, try metallic and specular. If it's gray, try roughness. If it's colorful, try RMA.
I've not seen this feature in other shaders, but my common shaders have an optional second glossmap for if you have a metallic map which ONLY has metallic data, and you also have a specular or roughness map. (like all the PBR materials from Textures.com. Argh! Would it kill you to stick the specmap in the alpha channel?!) This second map has to be enabled with a keyword toggle 'cuz I didn't make a custom editor, and even if it is enabled, it only has any effect if the first map is a metallic map.
- Another feature that may seem weird is FLASH_COLOR and BLOCK_COLOR, these are colors that can be changed via material property block and affect the emissive and albedo colors respectively. I use this specifically for making environment objects flash to the beat of music. You can use it for this too, or totally ignore them, the shader works either way. Accordingly, GET_BLOCK_COLOR and GET_FLASH_COLOR are custom macros for retrieving these colors, and as my Lite edition of my common library includes alternate functions that omit them, you may remove them if you want.
Let me know if you need help or have any questions!