{"id":1951,"date":"2019-09-28T13:41:55","date_gmt":"2019-09-28T05:41:55","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1951"},"modified":"2022-01-13T14:04:23","modified_gmt":"2022-01-13T06:04:23","slug":"lightmap%e5%af%b9%e4%ba%8e%e5%8a%a8%e6%80%81%e5%8a%a0%e8%bd%bd%e9%a2%84%e8%ae%be%e4%bd%93%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1951","title":{"rendered":"Lightmap\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u9884\u8bbe\u4f53\u7684\u4f7f\u7528"},"content":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u5149\u7167\u8d34\u56fe\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u7684\u6a21\u578b\u9884\u8bbe\u4f53\u7684\u4f7f\u7528\u6559\u7a0b<\/p>\n<h2>\u4e00\u3001\u6dfb\u52a0\u5149\u7167\u8d34\u56fe\u6570\u636e\u811a\u672c\uff0c\u7528\u4e8e\u5b58\u50a8\u706f\u5149\u70d8\u7119\u540e\u7684\u5149\u7167\u8d34\u56fe\u4fe1\u606f\uff0c\u4ee5\u53ca\u8bbe\u7f6e\u5149\u7167\u8d34\u56fe\u4fe1\u606f<\/h2>\n<pre class=\"lang:c# decode:true\">using UnityEngine;\nusing System.Collections;\nusing System.Collections.Generic;\n\npublic class PrefabLightmapData : MonoBehaviour\n{\n\t[System.Serializable]\n\tstruct RendererInfo\n\t{\n\t\tpublic Renderer \trenderer;\n\t\tpublic int \t\t\tlightmapIndex;\n\t\tpublic Vector4 \t\tlightmapOffsetScale;\n\t}\n\n\t[SerializeField]\n\tRendererInfo[]\tm_RendererInfo;\n\t[SerializeField]\n\tTexture2D[] \tm_Lightmaps;\n\n\tvoid Awake ()\n\t{\n\t\tif (m_RendererInfo == null || m_RendererInfo.Length == 0)\n\t\t\treturn;\n\n\t\tvar lightmaps = LightmapSettings.lightmaps;\n\t\tvar combinedLightmaps = new LightmapData[lightmaps.Length + m_Lightmaps.Length];\n\n\t\tlightmaps.CopyTo(combinedLightmaps, 0);\n\t\tfor (int i = 0; i &lt; m_Lightmaps.Length;i++)\n\t\t{\n\t\t\tcombinedLightmaps[i+lightmaps.Length] = new LightmapData();\n\t\t\tcombinedLightmaps[i+lightmaps.Length].lightmapColor = m_Lightmaps[i];\n\t\t}\n\n\t\tApplyRendererInfo(m_RendererInfo, lightmaps.Length);\n\t\tLightmapSettings.lightmapsMode = LightmapsMode.NonDirectional;\n\t\tLightmapSettings.lightmaps = combinedLightmaps;\n\t}\n\n\t\n\tstatic void ApplyRendererInfo (RendererInfo[] infos, int lightmapOffsetIndex)\n\t{\n\t\tfor (int i=0;i&lt;infos.Length;i++)\n\t\t{\n\t\t\tvar info = infos[i];\n\t\t\tinfo.renderer.lightmapIndex = info.lightmapIndex + lightmapOffsetIndex;\n\t\t\tinfo.renderer.lightmapScaleOffset = info.lightmapOffsetScale;\n\t\t}\n\t}\n\n#if UNITY_EDITOR\n\t[UnityEditor.MenuItem(\"Assets\/Bake Prefab Lightmaps\")]\n\tstatic void GenerateLightmapInfo ()\n\t{\n\t\tif (UnityEditor.Lightmapping.giWorkflowMode != UnityEditor.Lightmapping.GIWorkflowMode.OnDemand)\n\t\t{\n\t\t\tDebug.LogError(\"ExtractLightmapData requires that you have baked you lightmaps and Auto mode is disabled.\");\n\t\t\treturn;\n\t\t}\n\t\tUnityEditor.Lightmapping.Bake();\n\n\t\tPrefabLightmapData[] prefabs = FindObjectsOfType&lt;PrefabLightmapData&gt;();\n\n\t\tforeach (var instance in prefabs)\n\t\t{\n\t\t\tvar gameObject = instance.gameObject;\n\t\t\tvar rendererInfos = new List&lt;RendererInfo&gt;();\n\t\t\tvar lightmaps = new List&lt;Texture2D&gt;();\n\t\t\t\n\t\t\tGenerateLightmapInfo(gameObject, rendererInfos, lightmaps);\n\t\t\t\n\t\t\tinstance.m_RendererInfo = rendererInfos.ToArray();\n\t\t\tinstance.m_Lightmaps = lightmaps.ToArray();\n\n\t\t\tvar targetPrefab = UnityEditor.PrefabUtility.GetPrefabParent(gameObject) as GameObject;\n\t\t\tif (targetPrefab != null)\n\t\t\t{\n\t\t\t\t\/\/UnityEditor.Prefab\n\t\t\t\tUnityEditor.PrefabUtility.ReplacePrefab(gameObject, targetPrefab);\n\t\t\t}\n\t\t}\n\t}\n\n\tstatic void GenerateLightmapInfo (GameObject root, List&lt;RendererInfo&gt; rendererInfos, List&lt;Texture2D&gt; lightmaps)\n\t{\n\t\tvar renderers = root.GetComponentsInChildren&lt;MeshRenderer&gt;();\n\t\tforeach (MeshRenderer renderer in renderers)\n\t\t{\n\t\t\tif (renderer.lightmapIndex != -1)\n\t\t\t{\n\t\t\t\tRendererInfo info = new RendererInfo();\n\t\t\t\tinfo.renderer = renderer;\n\t\t\t\tinfo.lightmapOffsetScale = renderer.lightmapScaleOffset;\n\n\t\t\t\tTexture2D lightmap = LightmapSettings.lightmaps[renderer.lightmapIndex].lightmapColor;\n\n\t\t\t\tinfo.lightmapIndex = lightmaps.IndexOf(lightmap);\n\t\t\t\tif (info.lightmapIndex == -1)\n\t\t\t\t{\n\t\t\t\t\tinfo.lightmapIndex = lightmaps.Count;\n\t\t\t\t\tlightmaps.Add(lightmap);\n\t\t\t\t}\n\n\t\t\t\trendererInfos.Add(info);\n\t\t\t}\n\t\t}\n\t}\n#endif\n\n}\n<\/pre>\n<h2>\u4e8c\u3001\u5c06\u201cPrefabLightmapData.cs\u201d\u6302\u5728\u5230\u9884\u8bbe\u4f53\u4e0a\uff0c\u5e76\u5c06\u9884\u8bbe\u4f53\u653e\u5728\u573a\u666f\u91cc\uff0c\u6267\u884c\u201cAssets\/Bake Prefab Lightmaps\u201d\u3002<\/h2>\n<h2>\u4e09\u3001\u7b49\u5f85\u70d8\u7119\u5b8c\u6210\uff0c\u67e5\u770b\u201cPrefabLightmapData.cs\u201d\u5c5e\u6027\u5df2\u7ecf\u6709\u4e86\u5149\u7167\u8d34\u56fe\u4fe1\u606f\u4e86<\/h2>\n<p><img title=\"Lightmap\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u9884\u8bbe\u4f53\u7684\u4f7f\u7528 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Lightmap\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u9884\u8bbe\u4f53\u7684\u4f7f\u7528 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" class=\"size-full wp-image-1952 alignnone\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133404.jpg\" alt=\"\" width=\"438\" height=\"347\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133404.jpg 438w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133404-300x238.jpg 300w\" sizes=\"(max-width: 438px) 100vw, 438px\" \/><\/p>\n<h2>\u56db\u3001\u79fb\u9664\u706f\u5149\uff0c\u6d4b\u8bd5\u70d8\u7119\u6548\u679c\u5373\u53ef<\/h2>\n<p><img title=\"Lightmap\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u9884\u8bbe\u4f53\u7684\u4f7f\u7528 - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Lightmap\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u9884\u8bbe\u4f53\u7684\u4f7f\u7528 - \u7b2c2\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" class=\"size-full wp-image-1953 alignnone\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133516.jpg\" alt=\"\" width=\"731\" height=\"198\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133516.jpg 731w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133516-300x81.jpg 300w\" sizes=\"(max-width: 731px) 100vw, 731px\" \/><\/p>\n<h3><\/h3>\n<p>\u4ee5\u4e0a\u6b65\u9aa4\u5728Editor\u4e0b\u8fd0\u884c\uff0c\u53ef\u67e5\u770b\u5230\u70d8\u7119\u6548\u679c\u3002\u4f46\u6253\u5305PC\u6216\u79fb\u52a8\u7aef\uff0c\u5149\u7167\u8d34\u56fe\u4f1a\u4e22\u5931\uff0c\u5c31\u9700\u8981\u6309\u4e0b\u9762\u65b9\u6cd5\u6765\u89e3\u51b3\u4e22\u5931\u95ee\u9898\u3002<\/p>\n<h2>\u5728Graphics\u8bbe\u7f6e\u754c\u9762\u6dfb\u52a0\u6a21\u578b\u6750\u8d28\u4f7f\u7528\u7684shader\uff0c\u5e76\u8c03\u6574Lightmap\u00a0Modes\u4e3a\u201cCustom\u201d<\/h2>\n<p><img title=\"Lightmap\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u9884\u8bbe\u4f53\u7684\u4f7f\u7528 - \u7b2c3\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"Lightmap\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u9884\u8bbe\u4f53\u7684\u4f7f\u7528 - \u7b2c3\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" loading=\"lazy\" class=\"size-full wp-image-1955 alignnone\" src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133917.jpg\" alt=\"\" width=\"867\" height=\"562\" srcset=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133917.jpg 867w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133917-300x194.jpg 300w, http:\/\/www.u3d8.com\/wp-content\/uploads\/2019\/09\/QQ\u622a\u56fe20190928133917-768x498.jpg 768w\" sizes=\"(max-width: 867px) 100vw, 867px\" \/><\/p>\n<p>\u81f3\u6b64\uff0c\u6559\u7a0b\u7ed3\u675f\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u6709\u7528~<\/p>\n<p>Demo\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/654306663\/LightmapForPrefab\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/github.com\/654306663\/LightmapForPrefab<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u4e3b\u8981\u8bb2\u89e3\u5149\u7167\u8d34\u56fe\u5bf9\u4e8e\u52a8\u6001\u52a0\u8f7d\u7684\u6a21\u578b\u9884\u8bbe\u4f53\u7684\u4f7f\u7528\u6559\u7a0b \u4e00\u3001\u6dfb\u52a0\u5149\u7167\u8d34\u56fe\u6570\u636e\u811a\u672c &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1951\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":1953,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[394],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1951"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1951"}],"version-history":[{"count":6,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1951\/revisions"}],"predecessor-version":[{"id":2569,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1951\/revisions\/2569"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/media\/1953"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1951"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1951"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1951"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}