Ssssong += Dev

[유니티/Shader] PropertyBlock 본문

카테고리 없음

[유니티/Shader] PropertyBlock

ssong_dev 2022. 11. 17. 20:19

https://docs.unity3d.com/ScriptReference/Renderer.SetPropertyBlock.html

 

Unity - Scripting API: Renderer.SetPropertyBlock

This is recommended when only a few properties of a Material are different per object. This is more memory efficient than having one complete distinct Material per object. You can also provide a Material index (from 0 to Renderer.materials.Length-1). In th

docs.unity3d.com

 

각 렌더러나 머티리얼의 parameter를 오버라이드 해서 사용할 수 있다.

오브젝트 마다 머티리얼 프로퍼티를 약간씩 바꿔야 할 때 사용할 수 있으며

렌더러 단위 혹은 머티리얼 단위의 block이 있을 때에는 머티리얼 단위의 블럭만이 사용된다.

 

 

https://docs.unity3d.com/ScriptReference/Graphics.DrawMesh.html

 

Unity - Scripting API: Graphics.DrawMesh

DrawMesh draws a mesh for one frame. The mesh will be affected by the lights, can cast and receive shadows and be affected by Projectors - just like it was part of some game object. It can be drawn for all cameras or just for some specific camera. Use Draw

docs.unity3d.com

Graphics.DrawMesh

 

 

 

 

MaterialPropertyBlock은

같은 머티리얼을 사용하는 여러 오브젝트들에 대해서 조금씩 다른 프로퍼티 값을 할당할 때 사용한다.

Renderer마다 material의 복사 없이 material parameter를 할당하여 사용할 수 있으며

해당 블럭은 Graphics.DrawMesh 혹은 Renderer.SetPropertyBlock 복사 시 적용되므로

한 블럭을 만들어두고 모든 DrawMesh 호출에 대해 재사용하는 것이 효율적이다.

 

Renderer.SetPropertyBlock(mpb) 를 할 시 해당 렌더러의 데이터가 오버라이드되어 적용된다.

만약 이전에 있던 값을 가지지 않은 propertyBlock이 새로 적용된다면 그 값은 리셋된다.

 

 

http://thomasmountainborn.com/2016/05/25/materialpropertyblocks/

 

The magic of Material Property Blocks – Thomas Mountainborn

I’ve found MaterialProperyBlocks to be very useful, but there doesn’t seem to be a whole lot of information on them out there. I’ve decided to compile a quick post on what they do, and how best to use them – they’ll definitely become a new go-to

thomasmountainborn.com

 

따라서 만약 해당 렌더러에 mpb를 설정하는 스크립트가 여럿 있다면 이들은 서로를 방해한다!

그러니 사용할 때에 이미 세팅된 값이 있다면 이미 존재하는 property value를 먼저 가져오고 나서 값을 수정하도록 하자.

mpb는 GPU에서 회수할 필요는 없고 일반 메모리에 올라가 있다.

 

다만 mpb는 다이내믹 배칭을 깨뜨린다.

 

https://forum.unity.com/threads/learn-how-to-use-materialpropertyblocks-and-perrendererdata-for-great-performance-gains.406638/

 

 

Learn how to use MaterialPropertyBlocks and [PerRendererData] for great performance gains!

Hey there! I found that while MaterialPropertyBlocks are very useful, the documentation on them is sparse at best. I've come to use them quite often,...

forum.unity.com

 

 

propertyblock 세팅 시 바뀐 데이터는 mpb 세팅할 때 GPU에 보내지는 것이 아니다.

drawcall 때 보내지는 데이터 구성을 바꾸는 것이다.