{"id":344,"date":"2022-05-06T16:43:35","date_gmt":"2022-05-06T08:43:35","guid":{"rendered":"http:\/\/www.mapleriver.cn\/?p=344"},"modified":"2022-06-15T18:20:48","modified_gmt":"2022-06-15T10:20:48","slug":"materialpropertyblock","status":"publish","type":"post","link":"https:\/\/www.mapleriver.cn\/?p=344","title":{"rendered":"MaterialPropertyBlock"},"content":{"rendered":"<h3>1.MaterialPropertyBlock<\/h3>\n<p>&emsp;\u00a0&emsp;\u5b9a\u4e49\uff1aA block of material values to apply.\uff08\u4e00\u5757\u53ef\u4ee5\u5e94\u7528\u7684\u6750\u8d28\u5c5e\u6027\uff09<br \/>\n&emsp;\u00a0&emsp;Unity\u4e2d\u6240\u6709\u7684\u4e1c\u897f\u90fd\u662f\u901a\u8fc7\u6750\u8d28\uff08material\uff09\u6765\u8fdb\u884c\u6e32\u67d3\u7684\uff0c\u6750\u8d28\u53c8\u5f71\u54cd\u5230\u5408\u6279\u3002\u90a3\u4e48\u6709\u6ca1\u6709\u4e00\u4e2a\u65b9\u5f0f\u53c8\u8ba9\u7269\u4f53\u7528\u540c\u4e00\u4e2a\u6750\u8d28\u6e32\u67d3\uff08\u53ef\u4ee5\u5408\u6279\uff09\uff0c\u53c8\u53ef\u4ee5\u8ba9\u7269\u4f53\u8868\u73b0\u51fa\u4e0d\u540c\u7684\u6027\u8d28\u5462\uff1f\u90a3\u5c31\u8981\u7528\u5230<code>MaterialPropertyBlock<\/code>\u4e86\u3002<\/p>\n<blockquote>\n<p>&emsp;\u00a0&emsp;Use it in situations where you want to draw multiple objects with the same material, but slightly different properties. For example, if you want to slightly change the color of each mesh drawn. Changing the render state is not supported.<br \/>\n&emsp;\u00a0&emsp;\u5f53\u4f60\u60f3\u7528\u76f8\u540c\u7684\u6750\u8d28\u7ed8\u5236\u591a\u4e2a\u5bf9\u8c61\uff0c\u4f46\u6bcf\u4e2a\u5bf9\u8c61\u5c5e\u6027\u7565\u6709\u4e0d\u540c\u7684\u65f6\u5019\u5c31\u53ef\u4ee5\u4f7f\u7528MaterialPropertyBlock\u3002\u4f8b\u5982\u7a0d\u5fae\u6539\u53d8\u6bcf\u4e2a\u7f51\u683c\u7684\u989c\u8272\u3002\u4f46\u4fee\u6539\u6e32\u67d3\u72b6\u6001\u4e0d\u652f\u6301\u4fee\u6539\u6bcf\u4e2a\u7f51\u683c\u989c\u8272\u3002<br \/>\n&emsp;\u00a0&emsp;Unity's terrain engine uses MaterialPropertyBlock to draw trees; all of them use the same material, but each tree has different color, scale &amp; wind factor.<br \/>\n&emsp;\u00a0&emsp;Unity \u7684\u5730\u5f62\u5f15\u64ce\u4f7f\u7528 MaterialPropertyBlock \u7ed8\u5236\u6811\u6728\uff0c\u5b83\u4eec\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u6750\u8d28\uff0c\u4f46\u6bcf\u68f5\u6811\u90fd\u6709\u4e0d\u540c\u7684\u989c\u8272\u3001\u6bd4\u4f8b\u548c\u98ce\u7cfb\u6570\u3002<br \/>\n&emsp;\u00a0&emsp;Note that this is not compatible with SRP Batcher. Using this in the Universal Render Pipeline (URP), High Definition Render Pipeline (HDRP) or a custom render pipeline based on the Scriptable Render Pipeline (SRP) will likely result in a drop in performance.<br \/>\n&emsp;\u00a0&emsp;\u6b64\u5916\u8981\u6ce8\u610f\u7684\u662f\uff0cMaterialPropertyBlock\u4e0eSRP Batcher\u4e0d\u517c\u5bb9\uff0c\u5728URP\/HDRP\/SRP\u4e2d\u4f7f\u7528\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u4e0b\u964d\u3002\uff08\u5b98\u65b9\u5410\u69fd\uff1a\u8c01\u77e5\u9053\u4f60\u4eec\u81ea\u5b9a\u4e49\u7684\u7ba1\u7ebf\u652f\u4e0d\u652f\u6301\u6211\u4eecMaterialPropertyBlock\u5440~\uff09<\/p>\n<\/blockquote>\n<p>&emsp;\u00a0&emsp;\u4ece\u5b98\u65b9\u6587\u6863\u4e2d\u5f88\u660e\u767d\u7684\u5c31\u80fd\u770b\u51fa\u6765\uff1a<\/p>\n<ul>\n<li>Block\u662f\u51cc\u9a7e\u4e8eMaterial\u4e4b\u4e0a\u7684\uff0c\u6bd4\u5982\u4f60\u4f7f\u7528Block\u8fdb\u884c\u7ed8\u5236\u989c\u8272\u7684\u65f6\u5019\uff0c\u539f\u672cmaterial\u4e0a\u7684\u989c\u8272\u5c31\u4f1a\u5931\u6548\u3002<\/li>\n<li>\u4f7f\u7528\u4e86Block\u53ef\u4ee5\u5b9e\u73b0\u76f8\u540c\u6750\u8d28\u8868\u73b0\u51fa\u4e0d\u4e00\u6837\u7684\u6548\u679c\u3002<\/li>\n<\/ul>\n<h3>2.\u90a3\u4e48\u8fd9\u4e2a\u73a9\u610f\u513f\u5230\u5e95\u600e\u4e48\u7528\u5462\uff1f<\/h3>\n<p>&emsp;\u00a0&emsp;Block\u53ef\u4ee5\u7528\u4f5c\u7ed8\u5236Mesh\u6216\u8005\u66f4\u6539Renderer\u4e0a\u7684\u6750\u8d28\u5c5e\u6027\uff0c\u6750\u8d28\u672c\u8d28\u4e0a\u5b58\u50a8\u7684\u662fshader\u4e2d\u4f7f\u7528\u7684\u5404\u79cd\u5c5e\u6027\u3002<\/p>\n<pre><code class=\"language-C#\">\/\/\u66f4\u6539Renderer\u4e0a\u7684Block\nRenderer.SetPropertyBlock\n\/\/\u76f4\u63a5\u5728mesh\u4e0a\u7ed8\u5236\nGraphics.DrawMesh<\/code><\/pre>\n<p>&emsp;\u00a0&emsp;\u4e3e\u4e2a\u6817\u5b50\uff1a<\/p>\n<pre><code class=\"language-C#\">public class ExampleClass : MonoBehaviour\n{\n    public Mesh mesh;\n    private MeshRenderer meshRenderer;\n    public Material material;\n    private MaterialPropertyBlock block;\n    private int colorID;\n    void Start()\n    {\n        block = new MaterialPropertyBlock();\n        colorID = Shader.PropertyToID(&quot;_Color&quot;);\n    }\n\n    void Update()\n    {\n        \/\/Block\u6709\u4e24\u79cd\u8bbe\u7f6e\u65b9\u5f0f\uff1a\n        \/\/\u901a\u8fc7\u6750\u8d28\u4e0a\u7684\u5c5e\u6027\u540d\u8bbe\u7f6e\n        block.SetColor(&quot;_Color&quot;, Color.red);\n        \/\/\u901a\u8fc7\u63d0\u524d\u83b7\u53d6\u5c5e\u6027ID\u8fdb\u884c\u8bbe\u7f6e\n        block.SetColor(colorID, Color.red);\n\n        \/\/\u76f4\u63a5\u5728mesh\u4e0a\u7ed8\u5236\n        Graphics.DrawMesh(mesh, new Vector3(0, 0, 0), Quaternion.identity, material, 0, null, 0, block);\n        \/\/\u66f4\u6539Renderer\u4e0a\u7684Block\n        meshRenderer.SetPropertyBlock(block);\n    }\n}<\/code><\/pre>\n<h3>3.\u6211\u60f3\u83b7\u53d6\u4e0a\u6b21\u8bbe\u7f6e\u7684Block\u8be5\u548b\u529e\uff1f<\/h3>\n<p>&emsp;\u00a0&emsp;\u6211\u5728\u4e0a\u4e00\u5e27\u4fee\u6539\u4e86\u5f88\u591aBlock\u5c5e\u6027\uff0c\u4f46\u662f\u8fd9\u4e00\u5e27\u53ea\u60f3\u6539\u53d8\u5176\u4e2d\u7684\u67d0\u4e00\u9879\u3002<br \/>\n&emsp;\u00a0&emsp;\u6216\u8005\u662f\u4e0a\u4e00\u5e27A\u8bbe\u7f6e\u4e86\u4e00\u5927\u5806Block\u5c5e\u6027\u8fdb\u53bb\uff0c\u8fd9\u4e00\u5e27B\u8981\u4fee\u6539\u5176\u4e2d\u7684\u67d0\u4e00\u9879\u3002<br \/>\n&emsp;\u00a0&emsp;Unity\u63d0\u4f9b\u4e86\u63a5\u53e3<code>GetPropertyBlock<\/code><\/p>\n<blockquote>\n<p>public void GetPropertyBlock(MaterialPropertyBlock properties);<br \/>\npublic void GetPropertyBlock(MaterialPropertyBlock properties, int materialIndex);<\/p>\n<\/blockquote>\n<p>&emsp;\u00a0&emsp;\u4f46\u662f\u8fd9\u4e2a\u51fd\u6570\u660e\u660e\u53ebGet\uff0c\u4e3a\u4ec0\u4e48\u8fd4\u56de\u503c\u662f\u4e2avoid\u5462\uff1f<\/p>\n<blockquote>\n<p>Get per-Renderer or per-Material property block.<br \/>\nThe retrieved properties are stored in the property block passed in through &quot;properties&quot;. If no properties are set, the property block is cleared. In either case the property block you pass in is completely overwritten.<br \/>\n\u83b7\u53d6per-Renderer\u6216per-Material\u5c5e\u6027\u5757<br \/>\n\u628a\u4e4b\u524d\u5b58\u50a8\u7684\u5c5e\u6027\u901a\u8fc7\u53c2\u6570properties\u8fdb\u884c\u6062\u590d\u3002\u5982\u679c\u4e4b\u524d\u6ca1\u8bbe\u7f6eBlock\uff0c\u5c31\u628aproperties\u6e05\u7a7a\u3002\uff08\u8fd9\u4e0d\u5c31\u662f\u4e2aref\u5417\uff1f\uff09<br \/>\n\u4e0d\u7ba1\u600e\u6837\uff0c\u4f60\u4f20\u8fdb\u6765\u7684properties\u5c5e\u6027\u90fd\u4f1a\u88ab\u5b8c\u5168\u8986\u76d6\uff08\u4f55\u5fc5\u5462\uff0c\u4e3a\u5565\u4e0d\u52a0\u4e2aref\u5462\uff1f\u6211\u7784\u4e86\u4e00\u773cBlock\u4e5f\u4e0d\u662f\u7ed3\u6784\u4f53\u554a\uff09<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>1.MaterialPropertyBlock &emsp;\u00a0&emsp;\u5b9a\u4e49\uff1aA block of mate [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-344","post","type-post","status-publish","format-standard","hentry","category-unity"],"_links":{"self":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/344","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=344"}],"version-history":[{"count":3,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/344\/revisions"}],"predecessor-version":[{"id":348,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=\/wp\/v2\/posts\/344\/revisions\/348"}],"wp:attachment":[{"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mapleriver.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}