由于业务场景存在 光照环境设置的移植,所以对光照所有属性做读写操作,整合成工具。
序列化 Scriptable 对象的属性名称与Lighting 窗口面板上的名称大体相同,
属性顺序除逻辑需求外,大体相同。
工具使用方法:
美术调整完场景后,ReadLightingSetting ,生成如下序列化文件
移植时,write 读取配置,自动烘焙场景
具体代码:
编辑器工具类:LightingSetting.cs
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEngine.Rendering;
using Object = UnityEngine.Object;/// <summary>
/// 使用RenderSettings、 LightmapEditorSettings 和 Lightmapping API。
/// </summary>
public class LightingSetting
{static string real_path = Application.dataPath+"/Lighting/Assets/LightingAsset.asset";static string light_path = "Assets/Lighting/Assets/LightingAsset.asset";[MenuItem("LightLing/ReadLightingSetting")]public static void ReadLightingSetting(){Debug.Log("create light asset ... ");if (File.Exists(real_path))File.Delete(real_path);LightingAsset lightmapAsset = ScriptableObject.CreateInstance<LightingAsset>();AssetDatabase.CreateAsset(lightmapAsset, light_path);Debug.Log("read light setting");LightingAsset dataInfo = AssetDatabase.LoadAssetAtPath(light_path, typeof(ScriptableObject)) as LightingAsset;if (dataInfo != null) {dataInfo.SkyboxMaterial = RenderSettings.skybox;//Realtime LightingdataInfo.realtimeGI = Lightmapping.realtimeGI;//Environment LightingdataInfo.ambientMode = RenderSettings.ambientMode;switch (dataInfo.ambientMode) {case AmbientMode.Skybox:dataInfo.ambientIntensity = RenderSettings.ambientIntensity;break;case AmbientMode.Trilight: //gradientdataInfo.ambientSkyColor = RenderSettings.ambientSkyColor;dataInfo.ambientEquatorColor = RenderSettings.ambientEquatorColor;dataInfo.ambientGroundColor = RenderSettings.ambientGroundColor;break;case AmbientMode.Flat: //colordataInfo.ambientColorLight = RenderSettings.ambientLight;break;}// Realtime LightingdataInfo.realtimeGI = Lightmapping.realtimeGI;//Ambient Modeif (Lightmapping.realtimeGI) {//默认 Baked}//Environment Reflections//SourcedataInfo.defaultReflectionMode = RenderSettings.defaultReflectionMode;switch (dataInfo.defaultReflectionMode) {case DefaultReflectionMode.Skybox:dataInfo.defaultReflectionResolution = RenderSettings.defaultReflectionResolution ; //Resolutionbreak;case DefaultReflectionMode.Custom:dataInfo.customReflection = RenderSettings.customReflection;break;}dataInfo.reflectionCubemapCompression = LightmapEditorSettings.reflectionCubemapCompression; //CompressiondataInfo.ambientIntensity = RenderSettings.reflectionIntensity;dataInfo.reflectionBounces = RenderSettings.reflectionBounces;//mixed lightingdataInfo.bakedGI = Lightmapping.bakedGI;if (Lightmapping.bakedGI) {//Lighting modedataInfo.mixedLightingMode = LightmapEditorSettings.mixedBakeMode;switch (dataInfo.mixedLightingMode) {case MixedLightingMode.Shadowmask:case MixedLightingMode.IndirectOnly:break;case MixedLightingMode.Subtractive:dataInfo.RealtimeShadowColor = RenderSettings.subtractiveShadowColor;break;}}//Lightmapping Settingif (Lightmapping.bakedGI) {dataInfo.Lightmapper = LightmapEditorSettings.lightmapper;switch (dataInfo.Lightmapper) {case LightmapEditorSettings.Lightmapper.ProgressiveCPU:case LightmapEditorSettings.Lightmapper.ProgressiveGPU:dataInfo.defaultReflectionResolution = RenderSettings.defaultReflectionResolution; //ResolutiondataInfo.prioritizeView = LightmapEditorSettings.prioritizeView;////// Multiple Importance Sampling : falsedataInfo.directSampleCount = LightmapEditorSettings.directSampleCount;dataInfo.indirectSampleCount = LightmapEditorSettings.indirectSampleCount;dataInfo.environmentSampleCount = LightmapEditorSettings.environmentSampleCount;dataInfo.Bounces = LightmapEditorSettings.bounces;//FilteringdataInfo.Filtering = LightmapEditorSettings.filteringMode;switch (dataInfo.Filtering) {case LightmapEditorSettings.FilterMode.None:case LightmapEditorSettings.FilterMode.Auto:break;case LightmapEditorSettings.FilterMode.Advanced://Direct FilteringdataInfo.DirectDenoiser = LightmapEditorSettings.denoiserTypeDirect;dataInfo.DirectFilter = LightmapEditorSettings.filterTypeDirect;switch (dataInfo.DirectFilter) {case LightmapEditorSettings.FilterType.Gaussian:dataInfo.filteringGaussRadiusDirect = LightmapEditorSettings.filteringGaussRadiusDirect;break;case LightmapEditorSettings.FilterType.ATrous:dataInfo.filteringAtrousPositionSigmaDirect = LightmapEditorSettings.filteringAtrousPositionSigmaDirect;break;case LightmapEditorSettings.FilterType.None:break;}//Indirect FilteringdataInfo.DirectDenoiser = LightmapEditorSettings.denoiserTypeIndirect;dataInfo.InirectFilter = LightmapEditorSettings.filterTypeIndirect;switch (dataInfo.InirectFilter) {case LightmapEditorSettings.FilterType.Gaussian:dataInfo.filteringGaussRadiusIndirect = LightmapEditorSettings.filteringGaussRadiusIndirect;break;case LightmapEditorSettings.FilterType.ATrous:dataInfo.filteringAtrousPositionSigmaIndirect = LightmapEditorSettings.filteringAtrousPositionSigmaIndirect;break;case LightmapEditorSettings.FilterType.None:break;}//Ambient OcclusiondataInfo.AmbientOcclusion = LightmapEditorSettings.enableAmbientOcclusion;if (LightmapEditorSettings.enableAmbientOcclusion) {//Ambient Occlusion FilteringdataInfo.AmbientOcclutionDenoiser = LightmapEditorSettings.denoiserTypeAO;dataInfo.AmbientOcclutionFilter = LightmapEditorSettings.filterTypeAO;switch (dataInfo.AmbientOcclutionFilter) {case LightmapEditorSettings.FilterType.Gaussian:dataInfo.filteringGaussRadiusAO = LightmapEditorSettings.filteringGaussRadiusAO;break;case LightmapEditorSettings.FilterType.ATrous:dataInfo.filteringAtrousPositionSigmaAO = LightmapEditorSettings.filteringAtrousPositionSigmaAO;break;case LightmapEditorSettings.FilterType.None:break;}break;} elsebreak;}//Indirect ResolutiondataInfo.IndirectResolution = LightmapEditorSettings.realtimeResolution;dataInfo.LightmapResolution = LightmapEditorSettings.bakeResolution;dataInfo.LightmapPadding = LightmapEditorSettings.padding;dataInfo.LightmapSize = LightmapEditorSettings.maxAtlasSize;dataInfo.CompressLightmaps = LightmapEditorSettings.textureCompression;if (LightmapEditorSettings.enableAmbientOcclusion) {dataInfo.AOMaxDistance = LightmapEditorSettings.aoMaxDistance;dataInfo.AOInDirectContribution = LightmapEditorSettings.aoExponentIndirect;dataInfo.AODirectContribution = LightmapEditorSettings.aoExponentDirect;}dataInfo.DirectionalMode = LightmapEditorSettings.lightmapsMode;dataInfo.IndirectIntensity = Lightmapping.indirectOutputScale;dataInfo.AlbedoBoost = Lightmapping.bounceBoost;break;case LightmapEditorSettings.Lightmapper.Enlighten:dataInfo.customReflection = RenderSettings.customReflection;break;}}}
// Debug.LogError(LightmapEditorSettings.trainingDataDestination);
// Debug.LogError(Lightmapping.lightingDataAsset);Debug.Log("Read complete");}[MenuItem("LightLing/WriteLightingSetting")]public static void SettingLightPCConfig(){Debug.Log("path:"+light_path);LightingAsset dataInfo = AssetDatabase.LoadAssetAtPath(light_path, typeof(ScriptableObject)) as LightingAsset;if (dataInfo != null){
// Lightmapping.lightingDataAsset = dataInfo.lightingDataAsset;//自动bake 关联RenderSettings.skybox = dataInfo.SkyboxMaterial; Light[] objs = (Light[])Object.FindObjectsOfType(typeof(Light));RenderSettings.sun = objs[0];//Realtime LightingLightmapping.realtimeGI = dataInfo.realtimeGI;//Environment LightingRenderSettings.ambientMode = dataInfo.ambientMode;switch (dataInfo.ambientMode){case AmbientMode.Skybox:RenderSettings.ambientIntensity = dataInfo.ambientIntensity;break;case AmbientMode.Trilight://gradientRenderSettings.ambientSkyColor=dataInfo.ambientSkyColor;RenderSettings.ambientEquatorColor=dataInfo.ambientEquatorColor;RenderSettings.ambientGroundColor=dataInfo.ambientGroundColor;break;case AmbientMode.Flat://colorRenderSettings.ambientLight=dataInfo.ambientColorLight;break;}// Realtime LightingLightmapping.realtimeGI = dataInfo.realtimeGI;//Ambient Modeif (Lightmapping.realtimeGI) {//默认 Baked}//Environment Reflections//SourceRenderSettings.defaultReflectionMode =dataInfo.defaultReflectionMode;switch (dataInfo.defaultReflectionMode){case DefaultReflectionMode.Skybox:RenderSettings.defaultReflectionResolution = dataInfo.defaultReflectionResolution; //Resolutionbreak;case DefaultReflectionMode.Custom:RenderSettings.customReflection = dataInfo.customReflection;break;}LightmapEditorSettings.reflectionCubemapCompression = dataInfo.reflectionCubemapCompression;//CompressionRenderSettings.reflectionIntensity = dataInfo.ambientIntensity;RenderSettings.reflectionBounces = dataInfo.reflectionBounces;//mixed lightingLightmapping.bakedGI = dataInfo.bakedGI;if (Lightmapping.bakedGI){//Lighting modeLightmapEditorSettings.mixedBakeMode = dataInfo.mixedLightingMode;switch (dataInfo.mixedLightingMode){case MixedLightingMode.Shadowmask:case MixedLightingMode.IndirectOnly:break;case MixedLightingMode.Subtractive:RenderSettings.subtractiveShadowColor = dataInfo.RealtimeShadowColor;break;}}//Lightmapping Settingif (Lightmapping.bakedGI){LightmapEditorSettings.lightmapper = dataInfo.Lightmapper;switch (dataInfo.Lightmapper){case LightmapEditorSettings.Lightmapper.ProgressiveCPU:case LightmapEditorSettings.Lightmapper.ProgressiveGPU:RenderSettings.defaultReflectionResolution = dataInfo.defaultReflectionResolution; //ResolutionLightmapEditorSettings.prioritizeView = dataInfo.prioritizeView;////// Multiple Importance Sampling : falseLightmapEditorSettings .directSampleCount= dataInfo.directSampleCount;LightmapEditorSettings .indirectSampleCount= dataInfo.indirectSampleCount;LightmapEditorSettings .environmentSampleCount= dataInfo.environmentSampleCount;LightmapEditorSettings.bounces=dataInfo.Bounces;//FilteringLightmapEditorSettings.filteringMode = dataInfo.Filtering;switch (dataInfo.Filtering){case LightmapEditorSettings.FilterMode.None:case LightmapEditorSettings.FilterMode.Auto:break;case LightmapEditorSettings.FilterMode.Advanced://Direct FilteringLightmapEditorSettings.denoiserTypeDirect = dataInfo.DirectDenoiser;LightmapEditorSettings.filterTypeDirect = dataInfo.DirectFilter;switch (dataInfo.DirectFilter){case LightmapEditorSettings.FilterType.Gaussian:LightmapEditorSettings.filteringGaussRadiusDirect = dataInfo.filteringGaussRadiusDirect;break;case LightmapEditorSettings.FilterType.ATrous:LightmapEditorSettings.filteringAtrousPositionSigmaDirect = dataInfo.filteringAtrousPositionSigmaDirect;break;case LightmapEditorSettings.FilterType.None:break;}//Indirect FilteringLightmapEditorSettings.denoiserTypeIndirect = dataInfo.DirectDenoiser;LightmapEditorSettings.filterTypeIndirect = dataInfo.InirectFilter;switch (dataInfo.InirectFilter){case LightmapEditorSettings.FilterType.Gaussian:LightmapEditorSettings.filteringGaussRadiusIndirect = dataInfo.filteringGaussRadiusIndirect;break;case LightmapEditorSettings.FilterType.ATrous:LightmapEditorSettings.filteringAtrousPositionSigmaIndirect=dataInfo.filteringAtrousPositionSigmaIndirect;break;case LightmapEditorSettings.FilterType.None:break;}//Ambient OcclusionLightmapEditorSettings.enableAmbientOcclusion=dataInfo.AmbientOcclusion;if (LightmapEditorSettings.enableAmbientOcclusion){//Ambient Occlusion FilteringLightmapEditorSettings.denoiserTypeAO = dataInfo.AmbientOcclutionDenoiser;LightmapEditorSettings.filterTypeAO = dataInfo.AmbientOcclutionFilter;switch (dataInfo.AmbientOcclutionFilter){case LightmapEditorSettings.FilterType.Gaussian:LightmapEditorSettings.filteringGaussRadiusAO = dataInfo.filteringGaussRadiusAO;break;case LightmapEditorSettings.FilterType.ATrous:LightmapEditorSettings.filteringAtrousPositionSigmaAO=dataInfo.filteringAtrousPositionSigmaAO;break;case LightmapEditorSettings.FilterType.None:break;}break;}elsebreak;}//Indirect ResolutionLightmapEditorSettings.realtimeResolution=dataInfo.IndirectResolution;LightmapEditorSettings.bakeResolution = dataInfo.LightmapResolution;LightmapEditorSettings.padding = dataInfo.LightmapPadding;LightmapEditorSettings.maxAtlasSize = dataInfo.LightmapSize;LightmapEditorSettings.textureCompression=dataInfo.CompressLightmaps;if (LightmapEditorSettings.enableAmbientOcclusion){LightmapEditorSettings.aoMaxDistance = dataInfo.AOMaxDistance;LightmapEditorSettings.aoExponentIndirect = dataInfo.AOInDirectContribution;LightmapEditorSettings.aoExponentDirect = dataInfo.AODirectContribution;}LightmapEditorSettings.lightmapsMode=dataInfo.DirectionalMode;Lightmapping.indirectOutputScale=dataInfo.IndirectIntensity;Lightmapping.bounceBoost=dataInfo.AlbedoBoost;break;case LightmapEditorSettings.Lightmapper.Enlighten:RenderSettings.customReflection = dataInfo.customReflection;break;}}Debug.Log("baking...");Lightmapping.Bake();Debug.Log("baking completed");}else{Debug.LogError("not found Lighting assets");return;}}}
Scriptable 灯光数据:LightingAsset.asset
using System;
using UnityEditor;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Rendering;[Serializable]
[CreateAssetMenu(fileName = "LightingAsset", menuName = "Creat Lighting Asset")]
public class LightingAsset : PlayableAsset
{public LightingDataAsset lightingDataAsset;public Material SkyboxMaterial;public int defaultReflectionResolution; public AmbientMode ambientMode; public float ambientIntensity;[Space]public Color ambientSkyColor;public Color ambientEquatorColor;public Color ambientGroundColor;[Space]public Color ambientColorLight;[Space]public DefaultReflectionMode defaultReflectionMode; public ReflectionCubemapCompression reflectionCubemapCompression; public float reflectionIntensity; public int reflectionBounces;public Cubemap customReflection;[Space]public bool realtimeGI;public bool bakedGI;public MixedLightingMode mixedLightingMode;public Color RealtimeShadowColor;[Space]public LightmapEditorSettings.Lightmapper Lightmapper;public bool prioritizeView;public bool multipleSampling;public int directSampleCount;public int indirectSampleCount;public int environmentSampleCount;public int LightProbeSampleMultiplier;public int Bounces;public LightmapEditorSettings.FilterMode Filtering;[Space]public LightmapEditorSettings.DenoiserType DirectDenoiser;public LightmapEditorSettings.FilterType DirectFilter;public int filteringGaussRadiusDirect;public float filteringAtrousPositionSigmaDirect;[Space]public LightmapEditorSettings.DenoiserType IndirectDenoiser;public LightmapEditorSettings.FilterType InirectFilter;public int filteringGaussRadiusIndirect;public float filteringAtrousPositionSigmaIndirect;[Space]public LightmapEditorSettings.DenoiserType AmbientOcclutionDenoiser;public LightmapEditorSettings.FilterType AmbientOcclutionFilter;public int filteringGaussRadiusAO;public float filteringAtrousPositionSigmaAO;[Space]public float IndirectResolution;public float LightmapResolution;public int LightmapPadding=2;public int LightmapSize=1024;public bool CompressLightmaps;[Space]public bool AmbientOcclusion;public float AOMaxDistance;public float AOInDirectContribution;public float AODirectContribution;[Space]public LightmapsMode DirectionalMode;public float IndirectIntensity;public float AlbedoBoost;// Factory method that generates a playable based on this assetpublic override Playable CreatePlayable(PlayableGraph graph, GameObject go){return Playable.Create(graph);}
}