I see there are many free outline effect shaders floating around.
but of course, my game objects have their own different materials and shaders applied already.
In order to turn outlines on and off of various objects at run time, am I just swapping the game objects material to the outline material as needed?
So I would select an object, reference its texture…pass that texture in to the temp material with outline shader, and apply that material back to the object?
does that sound about right?
I have an outline effect on game objects rendering based off a separate camera already, not shader. but its a resource killer.
The way I go about this is using material property blocks and a shared material.
Before assigning the outline material, copy the values you care about from the original material to a material property block and assign that back onto the renderer. Save a reference of the original material so you can assign it later. Then assign the outline material to the sharedMaterial of the renderer.
originalMaterial = rend.sharedMaterial; // do not use .material! that creates a new copy!
MaterialPropertyBlock matBlock = new MaterialPropertyBlock();
matBlock.SetTexture("_MainTex", originalMaterial.GetTexture("_MainTex"));
rend.SetPropertyBlock(matBlock);
rend.sharedMaterial = outlineMaterial;
When you want to go back, just assign the original material to the sharedMaterial again. The other nice thing about this approach is you only need to copy off the original material and assign the material property block once. After that you only need to swap the materials back and forth. This creates no new materials to keep track of or garbage collect!
My current setup is working fine, but, the only problem is that I have to specify the renderer to use, which is fine, but sometimes the order of materials causes the outline effect to not really show, so I added an outline index property, which, works…but again I feel like there is a better way to do this.