sirskunkalot / PlanBuild

Valheim mod enabling you to plan buildings, copy structures and terraform
Do What The F*ck You Want To Public License
11 stars 14 forks source link

Use MaterialPropertyBlocks to change the color of materials #110

Open sea212 opened 3 days ago

sea212 commented 3 days ago

As of now, the override of the WearNTear Highlight function within Extension.cs does copy materials and directly modify the color field of the materials:

https://github.com/sirskunkalot/PlanBuild/blob/02d68bb4f164f83635f88e9792210ee4e9d83712/PlanBuild/Extensions.cs#L35-L46

This leads to the creation of new material instances. Creating new instances is on one hand unnecessarily expensive in terms of computational time and in terms of memory (which can be neglected in that case) and on the other hand causes issues with material caching, as it is done in the following code snippet: https://github.com/sirskunkalot/PlanBuild/blob/02d68bb4f164f83635f88e9792210ee4e9d83712/PlanBuild/Utils/ShaderHelper.cs#L129-L132 As new instances haven new names, this caching mechanism is not only inefficient but also prone to storing wrong original material information (such as saving materials that are colored).

Solution Use MaterialPropertyBlocks (see unity documentation and explanatory article) to change the color of materials. I suggest to use the ColorfulPieces mod, which provides a clean and efficient implementation, or alternatively implement it completely by yourself (example).

Notes