赞
踩
由于业务场景存在 光照环境设置的移植,所以对光照所有属性做读写操作,整合成工具。
序列化 Scriptable 对象的属性名称与Lighting 窗口面板上的名称大体相同,
属性顺序除逻辑需求外,大体相同。
工具使用方法:
美术调整完场景后,ReadLightingSetting ,生成如下序列化文件 LightingAsset.asset
移植时,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("read light setting");
- LightingAsset dataInfo = ScriptableObject.CreateInstance<LightingAsset>();
- if (dataInfo != null) {
- //Skybox Material
- dataInfo.SkyboxMaterial = RenderSettings.skybox;
- //Sun Source 无序列化
- // dataInfo.SunSource = RenderSettings.sun;
-
- //Realtime Lighting
- dataInfo.realtimeGI = Lightmapping.realtimeGI;
- //mixed lighting
- dataInfo.bakedGI = Lightmapping.bakedGI;
-
- //Environment Lighting
- dataInfo.ambientMode = RenderSettings.ambientMode;
-
- switch (dataInfo.ambientMode) {
- case AmbientMode.Skybox:
- dataInfo.ambientIntensity = RenderSettings.ambientIntensity;
- break;
- case AmbientMode.Trilight: //gradient
- dataInfo.ambientSkyColor = RenderSettings.ambientSkyColor;
- dataInfo.ambientEquatorColor = RenderSettings.ambientEquatorColor;
- dataInfo.ambientGroundColor = RenderSettings.ambientGroundColor;
- break;
- case AmbientMode.Flat: //color
- dataInfo.ambientColorLight = RenderSettings.ambientLight;
- break;
- }
-
- //Ambient Mode
- if (Lightmapping.realtimeGI) {
- //默认 Baked
- }
-
- //Environment Reflections
- //Source
- dataInfo.defaultReflectionMode = RenderSettings.defaultReflectionMode;
- switch (dataInfo.defaultReflectionMode) {
- case DefaultReflectionMode.Skybox:
- dataInfo.defaultReflectionResolution = RenderSettings.defaultReflectionResolution ; //Resolution
- break;
- case DefaultReflectionMode.Custom:
- dataInfo.customReflection = RenderSettings.customReflection;
- break;
- }
- dataInfo.reflectionCubemapCompression = LightmapEditorSettings.reflectionCubemapCompression; //Compression
- dataInfo.ambientIntensity = RenderSettings.reflectionIntensity;
- dataInfo.reflectionBounces = RenderSettings.reflectionBounces;
-
- if (Lightmapping.bakedGI) {
- //Lighting mode
- dataInfo.mixedLightingMode = LightmapEditorSettings.mixedBakeMode;
-
- switch (dataInfo.mixedLightingMode) {
- case MixedLightingMode.Shadowmask:
- case MixedLightingMode.IndirectOnly:
- break;
- case MixedLightingMode.Subtractive:
- dataInfo.RealtimeShadowColor = RenderSettings.subtractiveShadowColor;
- break;
- }
- }
- //Lightmapping Setting
- if (Lightmapping.bakedGI) {
- dataInfo.Lightmapper = LightmapEditorSettings.lightmapper;
- switch (dataInfo.Lightmapper) {
- case LightmapEditorSettings.Lightmapper.ProgressiveCPU:
- case LightmapEditorSettings.Lightmapper.ProgressiveGPU:
- dataInfo.defaultReflectionResolution = RenderSettings.defaultReflectionResolution; //Resolution
- dataInfo.prioritizeView = LightmapEditorSettings.prioritizeView;
- // Multiple Importance Sampling : false
- dataInfo.directSampleCount = LightmapEditorSettings.directSampleCount;
- dataInfo.indirectSampleCount = LightmapEditorSettings.indirectSampleCount;
- dataInfo.environmentSampleCount = LightmapEditorSettings.environmentSampleCount;
-
- dataInfo.Bounces = LightmapEditorSettings.bounces;
- //Filtering
- dataInfo.Filtering = LightmapEditorSettings.filteringMode;
- switch (dataInfo.Filtering) {
- case LightmapEditorSettings.FilterMode.None:
- case LightmapEditorSettings.FilterMode.Auto:
- break;
- case LightmapEditorSettings.FilterMode.Advanced:
- //Direct Filtering
- dataInfo.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 Filtering
- dataInfo.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 Occlusion
- dataInfo.AmbientOcclusion = LightmapEditorSettings.enableAmbientOcclusion;
- if (LightmapEditorSettings.enableAmbientOcclusion) {
- //Ambient Occlusion Filtering
- dataInfo.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;
- } else
- break;
- }
-
- //Indirect Resolution
- dataInfo.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;
- }
-
- }
- }
-
- AssetDatabase.DeleteAsset(light_path);
- AssetDatabase.CreateAsset(dataInfo, light_path);
- AssetDatabase.SaveAssets();
- AssetDatabase.Refresh();
- // 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));
- for (int i = 0; i < objs.Length; i++)
- {
- if(objs[i].name =="Directional Light")
- {
- RenderSettings.sun = objs[i];
- break;
- }
- }
-
- //Realtime Lighting
- Lightmapping.realtimeGI = dataInfo.realtimeGI;
- //mixed lighting
- Lightmapping.bakedGI = dataInfo.bakedGI;
-
- //Environment Lighting
- RenderSettings.ambientMode = dataInfo.ambientMode;
- switch (dataInfo.ambientMode)
- {
- case AmbientMode.Skybox:
- RenderSettings.ambientIntensity = dataInfo.ambientIntensity;
- break;
- case AmbientMode.Trilight://gradient
- RenderSettings.ambientSkyColor=dataInfo.ambientSkyColor;
- RenderSettings.ambientEquatorColor=dataInfo.ambientEquatorColor;
- RenderSettings.ambientGroundColor=dataInfo.ambientGroundColor;
- break;
- case AmbientMode.Flat://color
- RenderSettings.ambientLight=dataInfo.ambientColorLight;
- break;
- }
-
- //Ambient Mode
- if (Lightmapping.realtimeGI) {
- //默认 Baked
- }
-
- //Environment Reflections
- //Source
- RenderSettings.defaultReflectionMode =dataInfo.defaultReflectionMode;
- switch (dataInfo.defaultReflectionMode)
- {
- case DefaultReflectionMode.Skybox:
- RenderSettings.defaultReflectionResolution = dataInfo.defaultReflectionResolution; //Resolution
- break;
- case DefaultReflectionMode.Custom:
- RenderSettings.customReflection = dataInfo.customReflection;
- break;
- }
- LightmapEditorSettings.reflectionCubemapCompression = dataInfo.reflectionCubemapCompression;//Compression
- RenderSettings.reflectionIntensity = dataInfo.ambientIntensity;
- RenderSettings.reflectionBounces = dataInfo.reflectionBounces;
-
- if (Lightmapping.bakedGI)
- {
- //Lighting mode
- LightmapEditorSettings.mixedBakeMode = dataInfo.mixedLightingMode;
-
- switch (dataInfo.mixedLightingMode)
- {
- case MixedLightingMode.Shadowmask:
- case MixedLightingMode.IndirectOnly:
- break;
- case MixedLightingMode.Subtractive:
- RenderSettings.subtractiveShadowColor = dataInfo.RealtimeShadowColor;
- break;
- }
- }
- //Lightmapping Setting
- if (Lightmapping.bakedGI)
- {
- LightmapEditorSettings.lightmapper = dataInfo.Lightmapper;
- switch (dataInfo.Lightmapper)
- {
- case LightmapEditorSettings.Lightmapper.ProgressiveCPU:
- case LightmapEditorSettings.Lightmapper.ProgressiveGPU:
- RenderSettings.defaultReflectionResolution = dataInfo.defaultReflectionResolution; //Resolution
- LightmapEditorSettings.prioritizeView = dataInfo.prioritizeView;
- // Multiple Importance Sampling : false
- LightmapEditorSettings .directSampleCount= dataInfo.directSampleCount;
- LightmapEditorSettings .indirectSampleCount= dataInfo.indirectSampleCount;
- LightmapEditorSettings .environmentSampleCount= dataInfo.environmentSampleCount;
-
- LightmapEditorSettings.bounces=dataInfo.Bounces;
- //Filtering
- LightmapEditorSettings.filteringMode = dataInfo.Filtering;
- switch (dataInfo.Filtering)
- {
- case LightmapEditorSettings.FilterMode.None:
- case LightmapEditorSettings.FilterMode.Auto:
- break;
- case LightmapEditorSettings.FilterMode.Advanced:
- //Direct Filtering
- LightmapEditorSettings.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 Filtering
- LightmapEditorSettings.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 Occlusion
- LightmapEditorSettings.enableAmbientOcclusion=dataInfo.AmbientOcclusion;
- if (LightmapEditorSettings.enableAmbientOcclusion)
- {
- //Ambient Occlusion Filtering
- LightmapEditorSettings.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;
- }
- else
- break;
- }
-
- //Indirect Resolution
- LightmapEditorSettings.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 Light SunSource;
- [Space]
- 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 asset
- public override Playable CreatePlayable(PlayableGraph graph, GameObject go)
- {
-
- return Playable.Create(graph);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。