Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check proper usage of _new Material()_ and Renderer.material.Set*() #60

Open
4 tasks
JaXt0r opened this issue Jun 28, 2024 · 0 comments
Open
4 tasks

Check proper usage of _new Material()_ and Renderer.material.Set*() #60

JaXt0r opened this issue Jun 28, 2024 · 0 comments

Comments

@JaXt0r
Copy link
Contributor

JaXt0r commented Jun 28, 2024

When the main game is loaded (world.zen; without NPCs), we have over 1.2k Materials initialized. When looked at our mesh creation logic, we always create new Material(...) when creating a mesh or calling Renderer.mesh.Set*() when altering data which creates a clone of the Material.

TODOs:

  • Evaluate if the memory optimization is needed (e.g. on Mobile VR via Profiler)
  • Check on *MeshBuilder.cs classes where we leverage new Material() and alter instanciation of materials with same Texture to only once --> Especially useful for TextureArray (World+Items), where we have the same textures for every material and could leverage the same Material.
  • Dispose these materials (TextureManager, ...) after game stop, as Unity isn't handling it on it's own. https://docs.unity3d.com/ScriptReference/Renderer-material.html Quote: It is your responsibility to destroy the materials when the game object is being destroyed.
  • Add our usage of material/sharedMaterial as proper code comments or into the Wiki for future references.

Hints:

  • Whenever we call Renderer.material.* (calling the getter), it will automatically duplicate the material under the hood. Therefore it should be avoided at all costs and Renderer.sharedMaterial.* should be used. https://docs.unity3d.com/ScriptReference/Renderer-material.html Quote: Returns the first instantiated Material assigned to the renderer. Modifying material will change the material for this object only. If the material is used by any other renderers, this will clone the shared material and start using it from now on.

Image

@JaXt0r JaXt0r changed the title Every object creates a new Material (+Shader) instance Check proper usage of _new Material()_ and renderer. Jun 28, 2024
@JaXt0r JaXt0r changed the title Check proper usage of _new Material()_ and renderer. Check proper usage of _new Material()_ and Renderer.material.Set*() Jun 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Todo
Development

No branches or pull requests

1 participant