当前位置:   article > 正文

Unity Lighting Window 属性的序列化与反序列化_unity lightingdata.asset 序列化

unity lightingdata.asset 序列化

由于业务场景存在 光照环境设置的移植,所以对光照所有属性做读写操作,整合成工具。

序列化 Scriptable 对象的属性名称与Lighting 窗口面板上的名称大体相同,

属性顺序除逻辑需求外,大体相同。

工具使用方法:

美术调整完场景后,ReadLightingSetting ,生成如下序列化文件 LightingAsset.asset

移植时,write 读取配置,自动烘焙场景

 

具体代码:

编辑器工具类:LightingSetting.cs

  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor;
  4. using UnityEngine.Rendering;
  5. using Object = UnityEngine.Object;
  6. /// <summary>
  7. /// 使用RenderSettings、 LightmapEditorSettings 和 Lightmapping API。
  8. /// </summary>
  9. public class LightingSetting
  10. {
  11. static string real_path = Application.dataPath+"/Lighting/Assets/LightingAsset.asset";
  12. static string light_path = "Assets/Lighting/Assets/LightingAsset.asset";
  13. [MenuItem("LightLing/ReadLightingSetting")]
  14. public static void ReadLightingSetting()
  15. {
  16. Debug.Log("read light setting");
  17. LightingAsset dataInfo = ScriptableObject.CreateInstance<LightingAsset>();
  18. if (dataInfo != null) {
  19. //Skybox Material
  20. dataInfo.SkyboxMaterial = RenderSettings.skybox;
  21. //Sun Source 无序列化
  22. // dataInfo.SunSource = RenderSettings.sun;
  23. //Realtime Lighting
  24. dataInfo.realtimeGI = Lightmapping.realtimeGI;
  25. //mixed lighting
  26. dataInfo.bakedGI = Lightmapping.bakedGI;
  27. //Environment Lighting
  28. dataInfo.ambientMode = RenderSettings.ambientMode;
  29. switch (dataInfo.ambientMode) {
  30. case AmbientMode.Skybox:
  31. dataInfo.ambientIntensity = RenderSettings.ambientIntensity;
  32. break;
  33. case AmbientMode.Trilight: //gradient
  34. dataInfo.ambientSkyColor = RenderSettings.ambientSkyColor;
  35. dataInfo.ambientEquatorColor = RenderSettings.ambientEquatorColor;
  36. dataInfo.ambientGroundColor = RenderSettings.ambientGroundColor;
  37. break;
  38. case AmbientMode.Flat: //color
  39. dataInfo.ambientColorLight = RenderSettings.ambientLight;
  40. break;
  41. }
  42. //Ambient Mode
  43. if (Lightmapping.realtimeGI) {
  44. //默认 Baked
  45. }
  46. //Environment Reflections
  47. //Source
  48. dataInfo.defaultReflectionMode = RenderSettings.defaultReflectionMode;
  49. switch (dataInfo.defaultReflectionMode) {
  50. case DefaultReflectionMode.Skybox:
  51. dataInfo.defaultReflectionResolution = RenderSettings.defaultReflectionResolution ; //Resolution
  52. break;
  53. case DefaultReflectionMode.Custom:
  54. dataInfo.customReflection = RenderSettings.customReflection;
  55. break;
  56. }
  57. dataInfo.reflectionCubemapCompression = LightmapEditorSettings.reflectionCubemapCompression; //Compression
  58. dataInfo.ambientIntensity = RenderSettings.reflectionIntensity;
  59. dataInfo.reflectionBounces = RenderSettings.reflectionBounces;
  60. if (Lightmapping.bakedGI) {
  61. //Lighting mode
  62. dataInfo.mixedLightingMode = LightmapEditorSettings.mixedBakeMode;
  63. switch (dataInfo.mixedLightingMode) {
  64. case MixedLightingMode.Shadowmask:
  65. case MixedLightingMode.IndirectOnly:
  66. break;
  67. case MixedLightingMode.Subtractive:
  68. dataInfo.RealtimeShadowColor = RenderSettings.subtractiveShadowColor;
  69. break;
  70. }
  71. }
  72. //Lightmapping Setting
  73. if (Lightmapping.bakedGI) {
  74. dataInfo.Lightmapper = LightmapEditorSettings.lightmapper;
  75. switch (dataInfo.Lightmapper) {
  76. case LightmapEditorSettings.Lightmapper.ProgressiveCPU:
  77. case LightmapEditorSettings.Lightmapper.ProgressiveGPU:
  78. dataInfo.defaultReflectionResolution = RenderSettings.defaultReflectionResolution; //Resolution
  79. dataInfo.prioritizeView = LightmapEditorSettings.prioritizeView;
  80. // Multiple Importance Sampling : false
  81. dataInfo.directSampleCount = LightmapEditorSettings.directSampleCount;
  82. dataInfo.indirectSampleCount = LightmapEditorSettings.indirectSampleCount;
  83. dataInfo.environmentSampleCount = LightmapEditorSettings.environmentSampleCount;
  84. dataInfo.Bounces = LightmapEditorSettings.bounces;
  85. //Filtering
  86. dataInfo.Filtering = LightmapEditorSettings.filteringMode;
  87. switch (dataInfo.Filtering) {
  88. case LightmapEditorSettings.FilterMode.None:
  89. case LightmapEditorSettings.FilterMode.Auto:
  90. break;
  91. case LightmapEditorSettings.FilterMode.Advanced:
  92. //Direct Filtering
  93. dataInfo.DirectDenoiser = LightmapEditorSettings.denoiserTypeDirect;
  94. dataInfo.DirectFilter = LightmapEditorSettings.filterTypeDirect;
  95. switch (dataInfo.DirectFilter) {
  96. case LightmapEditorSettings.FilterType.Gaussian:
  97. dataInfo.filteringGaussRadiusDirect = LightmapEditorSettings.filteringGaussRadiusDirect;
  98. break;
  99. case LightmapEditorSettings.FilterType.ATrous:
  100. dataInfo.filteringAtrousPositionSigmaDirect = LightmapEditorSettings.filteringAtrousPositionSigmaDirect;
  101. break;
  102. case LightmapEditorSettings.FilterType.None:
  103. break;
  104. }
  105. //Indirect Filtering
  106. dataInfo.DirectDenoiser = LightmapEditorSettings.denoiserTypeIndirect;
  107. dataInfo.InirectFilter = LightmapEditorSettings.filterTypeIndirect;
  108. switch (dataInfo.InirectFilter) {
  109. case LightmapEditorSettings.FilterType.Gaussian:
  110. dataInfo.filteringGaussRadiusIndirect = LightmapEditorSettings.filteringGaussRadiusIndirect;
  111. break;
  112. case LightmapEditorSettings.FilterType.ATrous:
  113. dataInfo.filteringAtrousPositionSigmaIndirect = LightmapEditorSettings.filteringAtrousPositionSigmaIndirect;
  114. break;
  115. case LightmapEditorSettings.FilterType.None:
  116. break;
  117. }
  118. //Ambient Occlusion
  119. dataInfo.AmbientOcclusion = LightmapEditorSettings.enableAmbientOcclusion;
  120. if (LightmapEditorSettings.enableAmbientOcclusion) {
  121. //Ambient Occlusion Filtering
  122. dataInfo.AmbientOcclutionDenoiser = LightmapEditorSettings.denoiserTypeAO;
  123. dataInfo.AmbientOcclutionFilter = LightmapEditorSettings.filterTypeAO;
  124. switch (dataInfo.AmbientOcclutionFilter) {
  125. case LightmapEditorSettings.FilterType.Gaussian:
  126. dataInfo.filteringGaussRadiusAO = LightmapEditorSettings.filteringGaussRadiusAO;
  127. break;
  128. case LightmapEditorSettings.FilterType.ATrous:
  129. dataInfo.filteringAtrousPositionSigmaAO = LightmapEditorSettings.filteringAtrousPositionSigmaAO;
  130. break;
  131. case LightmapEditorSettings.FilterType.None:
  132. break;
  133. }
  134. break;
  135. } else
  136. break;
  137. }
  138. //Indirect Resolution
  139. dataInfo.IndirectResolution = LightmapEditorSettings.realtimeResolution;
  140. dataInfo.LightmapResolution = LightmapEditorSettings.bakeResolution;
  141. dataInfo.LightmapPadding = LightmapEditorSettings.padding;
  142. dataInfo.LightmapSize = LightmapEditorSettings.maxAtlasSize;
  143. dataInfo.CompressLightmaps = LightmapEditorSettings.textureCompression;
  144. if (LightmapEditorSettings.enableAmbientOcclusion) {
  145. dataInfo.AOMaxDistance = LightmapEditorSettings.aoMaxDistance;
  146. dataInfo.AOInDirectContribution = LightmapEditorSettings.aoExponentIndirect;
  147. dataInfo.AODirectContribution = LightmapEditorSettings.aoExponentDirect;
  148. }
  149. dataInfo.DirectionalMode = LightmapEditorSettings.lightmapsMode;
  150. dataInfo.IndirectIntensity = Lightmapping.indirectOutputScale;
  151. dataInfo.AlbedoBoost = Lightmapping.bounceBoost;
  152. break;
  153. case LightmapEditorSettings.Lightmapper.Enlighten:
  154. dataInfo.customReflection = RenderSettings.customReflection;
  155. break;
  156. }
  157. }
  158. }
  159. AssetDatabase.DeleteAsset(light_path);
  160. AssetDatabase.CreateAsset(dataInfo, light_path);
  161. AssetDatabase.SaveAssets();
  162. AssetDatabase.Refresh();
  163. // Debug.LogError(LightmapEditorSettings.trainingDataDestination);
  164. // Debug.LogError(Lightmapping.lightingDataAsset);
  165. Debug.Log("Read complete");
  166. }
  167. [MenuItem("LightLing/WriteLightingSetting")]
  168. public static void SettingLightPCConfig()
  169. {
  170. Debug.Log("path:"+light_path);
  171. LightingAsset dataInfo = AssetDatabase.LoadAssetAtPath(light_path, typeof(ScriptableObject)) as LightingAsset;
  172. if (dataInfo != null)
  173. {
  174. // Lightmapping.lightingDataAsset = dataInfo.lightingDataAsset;//自动bake 关联
  175. RenderSettings.skybox = dataInfo.SkyboxMaterial;
  176. Light[] objs = (Light[])Object.FindObjectsOfType(typeof(Light));
  177. for (int i = 0; i < objs.Length; i++)
  178. {
  179. if(objs[i].name =="Directional Light")
  180. {
  181. RenderSettings.sun = objs[i];
  182. break;
  183. }
  184. }
  185. //Realtime Lighting
  186. Lightmapping.realtimeGI = dataInfo.realtimeGI;
  187. //mixed lighting
  188. Lightmapping.bakedGI = dataInfo.bakedGI;
  189. //Environment Lighting
  190. RenderSettings.ambientMode = dataInfo.ambientMode;
  191. switch (dataInfo.ambientMode)
  192. {
  193. case AmbientMode.Skybox:
  194. RenderSettings.ambientIntensity = dataInfo.ambientIntensity;
  195. break;
  196. case AmbientMode.Trilight://gradient
  197. RenderSettings.ambientSkyColor=dataInfo.ambientSkyColor;
  198. RenderSettings.ambientEquatorColor=dataInfo.ambientEquatorColor;
  199. RenderSettings.ambientGroundColor=dataInfo.ambientGroundColor;
  200. break;
  201. case AmbientMode.Flat://color
  202. RenderSettings.ambientLight=dataInfo.ambientColorLight;
  203. break;
  204. }
  205. //Ambient Mode
  206. if (Lightmapping.realtimeGI) {
  207. //默认 Baked
  208. }
  209. //Environment Reflections
  210. //Source
  211. RenderSettings.defaultReflectionMode =dataInfo.defaultReflectionMode;
  212. switch (dataInfo.defaultReflectionMode)
  213. {
  214. case DefaultReflectionMode.Skybox:
  215. RenderSettings.defaultReflectionResolution = dataInfo.defaultReflectionResolution; //Resolution
  216. break;
  217. case DefaultReflectionMode.Custom:
  218. RenderSettings.customReflection = dataInfo.customReflection;
  219. break;
  220. }
  221. LightmapEditorSettings.reflectionCubemapCompression = dataInfo.reflectionCubemapCompression;//Compression
  222. RenderSettings.reflectionIntensity = dataInfo.ambientIntensity;
  223. RenderSettings.reflectionBounces = dataInfo.reflectionBounces;
  224. if (Lightmapping.bakedGI)
  225. {
  226. //Lighting mode
  227. LightmapEditorSettings.mixedBakeMode = dataInfo.mixedLightingMode;
  228. switch (dataInfo.mixedLightingMode)
  229. {
  230. case MixedLightingMode.Shadowmask:
  231. case MixedLightingMode.IndirectOnly:
  232. break;
  233. case MixedLightingMode.Subtractive:
  234. RenderSettings.subtractiveShadowColor = dataInfo.RealtimeShadowColor;
  235. break;
  236. }
  237. }
  238. //Lightmapping Setting
  239. if (Lightmapping.bakedGI)
  240. {
  241. LightmapEditorSettings.lightmapper = dataInfo.Lightmapper;
  242. switch (dataInfo.Lightmapper)
  243. {
  244. case LightmapEditorSettings.Lightmapper.ProgressiveCPU:
  245. case LightmapEditorSettings.Lightmapper.ProgressiveGPU:
  246. RenderSettings.defaultReflectionResolution = dataInfo.defaultReflectionResolution; //Resolution
  247. LightmapEditorSettings.prioritizeView = dataInfo.prioritizeView;
  248. // Multiple Importance Sampling : false
  249. LightmapEditorSettings .directSampleCount= dataInfo.directSampleCount;
  250. LightmapEditorSettings .indirectSampleCount= dataInfo.indirectSampleCount;
  251. LightmapEditorSettings .environmentSampleCount= dataInfo.environmentSampleCount;
  252. LightmapEditorSettings.bounces=dataInfo.Bounces;
  253. //Filtering
  254. LightmapEditorSettings.filteringMode = dataInfo.Filtering;
  255. switch (dataInfo.Filtering)
  256. {
  257. case LightmapEditorSettings.FilterMode.None:
  258. case LightmapEditorSettings.FilterMode.Auto:
  259. break;
  260. case LightmapEditorSettings.FilterMode.Advanced:
  261. //Direct Filtering
  262. LightmapEditorSettings.denoiserTypeDirect = dataInfo.DirectDenoiser;
  263. LightmapEditorSettings.filterTypeDirect = dataInfo.DirectFilter;
  264. switch (dataInfo.DirectFilter)
  265. {
  266. case LightmapEditorSettings.FilterType.Gaussian:
  267. LightmapEditorSettings.filteringGaussRadiusDirect = dataInfo.filteringGaussRadiusDirect;
  268. break;
  269. case LightmapEditorSettings.FilterType.ATrous:
  270. LightmapEditorSettings.filteringAtrousPositionSigmaDirect = dataInfo.filteringAtrousPositionSigmaDirect;
  271. break;
  272. case LightmapEditorSettings.FilterType.None:
  273. break;
  274. }
  275. //Indirect Filtering
  276. LightmapEditorSettings.denoiserTypeIndirect = dataInfo.DirectDenoiser;
  277. LightmapEditorSettings.filterTypeIndirect = dataInfo.InirectFilter;
  278. switch (dataInfo.InirectFilter)
  279. {
  280. case LightmapEditorSettings.FilterType.Gaussian:
  281. LightmapEditorSettings.filteringGaussRadiusIndirect = dataInfo.filteringGaussRadiusIndirect;
  282. break;
  283. case LightmapEditorSettings.FilterType.ATrous:
  284. LightmapEditorSettings.filteringAtrousPositionSigmaIndirect=dataInfo.filteringAtrousPositionSigmaIndirect;
  285. break;
  286. case LightmapEditorSettings.FilterType.None:
  287. break;
  288. }
  289. //Ambient Occlusion
  290. LightmapEditorSettings.enableAmbientOcclusion=dataInfo.AmbientOcclusion;
  291. if (LightmapEditorSettings.enableAmbientOcclusion)
  292. {
  293. //Ambient Occlusion Filtering
  294. LightmapEditorSettings.denoiserTypeAO = dataInfo.AmbientOcclutionDenoiser;
  295. LightmapEditorSettings.filterTypeAO = dataInfo.AmbientOcclutionFilter;
  296. switch (dataInfo.AmbientOcclutionFilter)
  297. {
  298. case LightmapEditorSettings.FilterType.Gaussian:
  299. LightmapEditorSettings.filteringGaussRadiusAO = dataInfo.filteringGaussRadiusAO;
  300. break;
  301. case LightmapEditorSettings.FilterType.ATrous:
  302. LightmapEditorSettings.filteringAtrousPositionSigmaAO=dataInfo.filteringAtrousPositionSigmaAO;
  303. break;
  304. case LightmapEditorSettings.FilterType.None:
  305. break;
  306. }
  307. break;
  308. }
  309. else
  310. break;
  311. }
  312. //Indirect Resolution
  313. LightmapEditorSettings.realtimeResolution=dataInfo.IndirectResolution;
  314. LightmapEditorSettings.bakeResolution = dataInfo.LightmapResolution;
  315. LightmapEditorSettings.padding = dataInfo.LightmapPadding;
  316. LightmapEditorSettings.maxAtlasSize = dataInfo.LightmapSize;
  317. LightmapEditorSettings.textureCompression=dataInfo.CompressLightmaps;
  318. if (LightmapEditorSettings.enableAmbientOcclusion)
  319. {
  320. LightmapEditorSettings.aoMaxDistance = dataInfo.AOMaxDistance;
  321. LightmapEditorSettings.aoExponentIndirect = dataInfo.AOInDirectContribution;
  322. LightmapEditorSettings.aoExponentDirect = dataInfo.AODirectContribution;
  323. }
  324. LightmapEditorSettings.lightmapsMode=dataInfo.DirectionalMode;
  325. Lightmapping.indirectOutputScale=dataInfo.IndirectIntensity;
  326. Lightmapping.bounceBoost=dataInfo.AlbedoBoost;
  327. break;
  328. case LightmapEditorSettings.Lightmapper.Enlighten:
  329. RenderSettings.customReflection = dataInfo.customReflection;
  330. break;
  331. }
  332. }
  333. Debug.Log("baking...");
  334. Lightmapping.Bake();
  335. Debug.Log("baking completed");
  336. }
  337. else
  338. {
  339. Debug.LogError("not found Lighting assets");
  340. return;
  341. }
  342. }
  343. }

 

 

Scriptable 灯光数据:LightingAsset.asset

  1. using System;
  2. using UnityEditor;
  3. using UnityEditor.AddressableAssets.Build;
  4. using UnityEditor.AddressableAssets.Settings;
  5. using UnityEngine;
  6. using UnityEngine.Playables;
  7. using UnityEngine.Rendering;
  8. [Serializable]
  9. [CreateAssetMenu(fileName = "LightingAsset", menuName = "Creat Lighting Asset")]
  10. public class LightingAsset : PlayableAsset
  11. {
  12. public LightingDataAsset lightingDataAsset;
  13. public Material SkyboxMaterial;
  14. public Light SunSource;
  15. [Space]
  16. public int defaultReflectionResolution;
  17. public AmbientMode ambientMode;
  18. public float ambientIntensity;
  19. [Space]
  20. public Color ambientSkyColor;
  21. public Color ambientEquatorColor;
  22. public Color ambientGroundColor;
  23. [Space]
  24. public Color ambientColorLight;
  25. [Space]
  26. public DefaultReflectionMode defaultReflectionMode;
  27. public ReflectionCubemapCompression reflectionCubemapCompression;
  28. public float reflectionIntensity;
  29. public int reflectionBounces;
  30. public Cubemap customReflection;
  31. [Space]
  32. public bool realtimeGI;
  33. public bool bakedGI;
  34. public MixedLightingMode mixedLightingMode;
  35. public Color RealtimeShadowColor;
  36. [Space]
  37. public LightmapEditorSettings.Lightmapper Lightmapper;
  38. public bool prioritizeView;
  39. public bool multipleSampling;
  40. public int directSampleCount;
  41. public int indirectSampleCount;
  42. public int environmentSampleCount;
  43. public int LightProbeSampleMultiplier;
  44. public int Bounces;
  45. public LightmapEditorSettings.FilterMode Filtering;
  46. [Space]
  47. public LightmapEditorSettings.DenoiserType DirectDenoiser;
  48. public LightmapEditorSettings.FilterType DirectFilter;
  49. public int filteringGaussRadiusDirect;
  50. public float filteringAtrousPositionSigmaDirect;
  51. [Space]
  52. public LightmapEditorSettings.DenoiserType IndirectDenoiser;
  53. public LightmapEditorSettings.FilterType InirectFilter;
  54. public int filteringGaussRadiusIndirect;
  55. public float filteringAtrousPositionSigmaIndirect;
  56. [Space]
  57. public LightmapEditorSettings.DenoiserType AmbientOcclutionDenoiser;
  58. public LightmapEditorSettings.FilterType AmbientOcclutionFilter;
  59. public int filteringGaussRadiusAO;
  60. public float filteringAtrousPositionSigmaAO;
  61. [Space]
  62. public float IndirectResolution;
  63. public float LightmapResolution;
  64. public int LightmapPadding=2;
  65. public int LightmapSize=1024;
  66. public bool CompressLightmaps;
  67. [Space]
  68. public bool AmbientOcclusion;
  69. public float AOMaxDistance;
  70. public float AOInDirectContribution;
  71. public float AODirectContribution;
  72. [Space]
  73. public LightmapsMode DirectionalMode;
  74. public float IndirectIntensity;
  75. public float AlbedoBoost;
  76. // Factory method that generates a playable based on this asset
  77. public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
  78. {
  79. return Playable.Create(graph);
  80. }
  81. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/637079
推荐阅读
相关标签
  

闽ICP备14008679号