Exemplo n.º 1
0
        /// <summary>
        /// 通过天空盒方式生成球谐光数据
        /// </summary>
        /// <param name="cubeTex"></param>
        /// <param name="rotate"></param>
        /// <returns></returns>
        public static SHData GenSHBySkyBox(Texture cubeTex, float rotate)
        {
            AmbientMode oMode = RenderSettings.ambientMode;

            RenderSettings.ambientMode = AmbientMode.Skybox;
            Material mSky = new Material(Shader.Find("Skybox/Cubemap"));

            mSky.SetTexture("_Tex", cubeTex);
            mSky.SetFloat("_Rotation", rotate);
            Material oldSky = RenderSettings.skybox;

            RenderSettings.skybox = mSky;
            DynamicGI.UpdateEnvironment();
            SphericalHarmonicsL2 sh = new SphericalHarmonicsL2();

            LightProbes.GetInterpolatedProbe(Vector3.zero, new Renderer(), out sh);
            SHData retData = CreateInstance <SHData>();

            retData.Init(sh);
            RenderSettings.skybox      = oldSky;
            RenderSettings.ambientMode = oMode;
            DynamicGI.UpdateEnvironment();

            DestroyImmediate(mSky);
            return(retData);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 使用三色渐变模式生成球谐光数据
        /// </summary>
        /// <param name="sky"></param>
        /// <param name="equator"></param>
        /// <param name="ground"></param>
        /// <returns></returns>
        public static SHData GenSHByTriLight(Color sky, Color equator, Color ground)
        {
            Color       oSkyColor, oEquatorColor, oGroundColor;
            AmbientMode oMode = RenderSettings.ambientMode;

            RenderSettings.ambientMode = AmbientMode.Trilight;
            oSkyColor     = RenderSettings.ambientSkyColor;
            oEquatorColor = RenderSettings.ambientEquatorColor;
            oGroundColor  = RenderSettings.ambientGroundColor;
            RenderSettings.ambientSkyColor     = sky;
            RenderSettings.ambientEquatorColor = equator;
            RenderSettings.ambientGroundColor  = ground;
            DynamicGI.UpdateEnvironment();
            SphericalHarmonicsL2 sh = new SphericalHarmonicsL2();

            LightProbes.GetInterpolatedProbe(Vector3.zero, new Renderer(), out sh);
            SHData retData = CreateInstance <SHData>();

            retData.Init(sh);
            RenderSettings.ambientMode         = oMode;
            RenderSettings.ambientSkyColor     = oSkyColor;
            RenderSettings.ambientEquatorColor = oEquatorColor;
            RenderSettings.ambientGroundColor  = oGroundColor;
            DynamicGI.UpdateEnvironment();

            return(retData);
        }
Exemplo n.º 3
0
        void OnGUI()
        {
            _aType = (AmbientType)EditorGUILayout.EnumPopup("AmbientType", _aType);

            switch (_aType)
            {
            case AmbientType.SkyBox:
                _map    = (Cubemap)EditorGUILayout.ObjectField("EnvTex: ", _map, typeof(Cubemap), false);
                _rotate = EditorGUILayout.Slider("EnvTexRotate", _rotate, 0, 360);
                if (GUILayout.Button("Generate!"))
                {
                    _shData = SHLightGeneratorLib.GenSHBySkyBox(_map, _rotate);
                }

                break;

            case AmbientType.TriLight:
                _skyColor     = EditorGUILayout.ColorField("Sky", _skyColor);
                _equatorColor = EditorGUILayout.ColorField("Equator", _equatorColor);
                _groundColor  = EditorGUILayout.ColorField("Ground", _groundColor);
                if (GUILayout.Button("Generate!"))
                {
                    _shData = SHLightGeneratorLib.GenSHByTriLight(_skyColor, _equatorColor, _groundColor);
                }

                break;
            }

            EditorGUILayout.Space();
            if (_shData)
            {
                GUILayout.Label("SHAr: " + _shData.SHAr);
                GUILayout.Label("SHAg: " + _shData.SHAg);
                GUILayout.Label("SHAb: " + _shData.SHAb);
                GUILayout.Label("SHBr: " + _shData.SHBr);
                GUILayout.Label("SHBg: " + _shData.SHBg);
                GUILayout.Label("SHBb: " + _shData.SHBb);
                GUILayout.Label("SHC: " + _shData.SHC);
                if (GUILayout.Button("SaveSHData"))
                {
                    string path = EditorUtility.SaveFilePanel("SaveSHData", Application.dataPath, "SHData", "asset");
                    if (!string.IsNullOrEmpty(path))
                    {
                        if (!path.Contains(Application.dataPath))
                        {
                            Debug.LogError("Invalid Path");
                            return;
                        }

                        path = "Assets" + path.Replace(Application.dataPath, "");
                        AssetDatabase.CreateAsset(_shData, path);
                        Selection.activeObject = _shData;
                    }
                }
            }
        }