示例#1
0
    static private void CreateCubeMap(int px)
    {
        // Set reflection probe.
        GameObject       probeGo = new GameObject("CubeMap Capture Probe");
        ReflectionProbe  probe   = probeGo.AddComponent <ReflectionProbe>();
        SerializedObject probeSo = new SerializedObject(probe);

        probeGo.transform.position = Vector3.zero;
        probeGo.transform.rotation = Quaternion.identity;

        // Some Settings here if you want.
        probe.mode          = UnityEngine.Rendering.ReflectionProbeMode.Custom;
        probe.resolution    = px;
        probe.hdr           = false;
        probe.boxProjection = false;

        probeSo.Update();
        probeSo.FindProperty("m_RenderDynamicObjects").boolValue = true;
        probeSo.ApplyModifiedProperties();

        // Create default path.
        string ext  = probe.hdr ? "exr" : "png";
        string path = UnityEditor.SceneManagement.EditorSceneManager.GetActiveScene().path;

        if (!string.IsNullOrEmpty(path))
        {
            path = System.IO.Path.GetDirectoryName(path);
        }

        if (string.IsNullOrEmpty(path))
        {
            path = "Assets";
        }
        else if (System.IO.Directory.Exists(path))
        {
            System.IO.Directory.CreateDirectory(path);
        }

        // Show the file saving panel.
        string filename = "name" + ext;

        filename = System.IO.Path.GetFileNameWithoutExtension(AssetDatabase.GenerateUniqueAssetPath(System.IO.Path.Combine(path, filename)));
        path     = EditorUtility.SaveFilePanelInProject("Save CubeMap", filename, ext, "", path);

        if (!string.IsNullOrEmpty(path))
        {
            // Bake
            EditorUtility.DisplayProgressBar("CubeMap", "Baking...", 0.5f);
            if (!Lightmapping.BakeReflectionProbe(probe, path))
            {
                Debug.LogError("Failed to bake cubemap");
            }
            EditorUtility.ClearProgressBar();
        }

        GameObject.DestroyImmediate(probeGo);
    }
示例#2
0
    IEnumerator Start()
    {
        yield return(new WaitForSeconds(0.5f));

        foreach (var probe in refProbe)
        {
            Lightmapping.BakeReflectionProbe(probe, "Assets/" + probe.ToString() + ".hdr");
            HDAdditionalReflectionData addRefData = probe.gameObject.GetComponent <HDAdditionalReflectionData>();

            Texture texture = (Texture)AssetDatabase.LoadAssetAtPath("Assets/" + probe.ToString() + ".hdr", typeof(Texture));
            addRefData.bakedTexture = texture;
            yield return(null);
        }
    }
        /// <summary>
        /// Bake our global reflection probe - if no lighting baked yet and requested then a global bake is kicked off
        /// </summary>
        /// <param name="doGlobalBakeIfNecessary">If no previous bake has been done then a global bake will be kicked off</param>
        public static void BakeGlobalReflectionProbe(bool doGlobalBakeIfNecessary)
        {
            if (Lightmapping.isRunning)
            {
                return;
            }

            //Get global reflection probe
            GameObject globalReflectionProbeObj = null;

            globalReflectionProbeObj = GameObject.Find("Global Reflection Probe");
            if (globalReflectionProbeObj == null)
            {
                return;
            }

            //Get the probe itself
            ReflectionProbe probe = globalReflectionProbeObj.GetComponent <ReflectionProbe>();

            if (probe == null)
            {
                return;
            }

            //Process based on type of probe
            if (probe.mode == ReflectionProbeMode.Realtime)
            {
                probe.RenderProbe();
            }
            else if (probe.mode == ReflectionProbeMode.Baked)
            {
                if (probe.bakedTexture == null)
                {
                    if (doGlobalBakeIfNecessary)
                    {
                        BakeGlobalLighting();
                    }
                    return;
                }

                string bakedTexturePath = AssetDatabase.GetAssetPath(probe.bakedTexture);
                Lightmapping.BakeReflectionProbe(probe, bakedTexturePath);
            }
        }
示例#4
0
        /// <summary>
        /// Bake our global reflection probe - if no lighting baked yet and requested then a global bake is kicked off
        /// </summary>
        /// <param name="doGlobalBakeIfNecessary">If no previous bake has been done then a global bake will be kicked off</param>
        public static void BakeGlobalReflectionProbe(bool doGlobalBakeIfNecessary)
        {
            if (Lightmapping.isRunning)
            {
                return;
            }

            //Get global reflection probe
            ReflectionProbe[] reflectionProbes = FindObjectsOfType <ReflectionProbe>();
            if (reflectionProbes == null || reflectionProbes.Length == 0)
            {
                return;
            }

            GameObject reflectionProbeObject = GameObject.Find("Global Reflection Probe");

            if (reflectionProbeObject != null)
            {
                var probe = reflectionProbeObject.GetComponent <ReflectionProbe>();
                if (probe.mode == ReflectionProbeMode.Baked)
                {
                    if (probe.bakedTexture == null)
                    {
                        if (doGlobalBakeIfNecessary)
                        {
                            BakeGlobalLighting();
                        }
                        return;
                    }

                    BakeAmbientLight();
                    Lightmapping.BakeReflectionProbe(probe, AssetDatabase.GetAssetPath(probe.bakedTexture));
                }
                else
                {
                    probe.RenderProbe();
                }
            }
        }
示例#5
0
    public void GenerateReflectionProbes()
    {
        DayNightConfig dnc    = GameObject.FindObjectOfType <DayNightConfig>();
        GameObject     parent = Selection.activeGameObject;

        if (dnc == null || parent == null)
        {
            return;
        }

        LightmapSwitcher ls = GameObject.FindObjectOfType <LightmapSwitcher>();

        if (ls != null)
        {
            ls.initData();
        }

        FogCtrl fc = GameObject.FindObjectOfType <FogCtrl>();

        IgnoreFarClip[] ifcs = Resources.FindObjectsOfTypeAll <IgnoreFarClip> ();

        EnterBusyState("正在烘焙ReflectionProbes,请别乱动!");
        curIndex = 0;

        string productionScenePath = EditorApplication.currentScene;
        string ppath           = Path.GetDirectoryName(productionScenePath) + "/";
        string pname           = Path.GetFileNameWithoutExtension(productionScenePath);
        string lightmapTgtPath = ppath + pname + "/";

        string[] wrps = Directory.GetFiles(lightmapTgtPath, "water_reflection_probe_*.exr");
        foreach (string wrpname in wrps)
        {
            FileUtil.DeleteFileOrDirectory(wrpname);
        }
        AssetDatabase.Refresh();

        actions.Clear();

        actions.Add(delegate(int a) {
            // disable all IgnoreFarClips
            for (int i = 0; i < ifcs.Length; ++i)
            {
                IgnoreFarClip ifc       = ifcs[i];
                ifc.gameObject.isStatic = true;
                Renderer rdr            = ifc.gameObject.GetComponent <Renderer>();
                if (rdr == null)
                {
                    continue;
                }

                foreach (Material m in rdr.sharedMaterials)
                {
                    m.SetInt("_ifc", 0);
                    m.DisableKeyword("IGNORE_FAR_CLIP");
                }
            }
        });

        for (int i = 0; i < weatherNames.Length; ++i)
        {
            // create gameobject and reflection probe
            actions.Add(delegate(int a) {
                string wname        = weatherNames[curIndex];
                string goname       = "water_reflection_probe_" + wname;
                Transform probTrans = parent.transform.Find(goname);
                if (probTrans == null)
                {
                    GameObject probGO = new GameObject();
                    probGO.name       = goname;
                    probGO.transform.SetParent(parent.transform, false);
                    probTrans = probGO.transform;
                }
                ReflectionProbe rp = probTrans.GetComponent <ReflectionProbe>();
                if (rp == null)
                {
                    rp = probTrans.gameObject.AddComponent <ReflectionProbe>();
                }

                rp.size          = new Vector3(400, 200, 400);
                rp.resolution    = 128;
                rp.nearClipPlane = 0.5f;
                rp.farClipPlane  = 3000.0f;
                rp.cullingMask   = ~0;
                rp.mode          = UnityEngine.Rendering.ReflectionProbeMode.Baked;
                curProbe         = rp;

//				FVWaterCubemapLoader cl = probTrans.GetComponent<FVWaterCubemapLoader>();
//				if (cl == null)
//				{
//					cl = probTrans.gameObject.AddComponent<FVWaterCubemapLoader>();
//				}
//				curLoader = cl;
            });

//			if (Application.isPlaying)
            {
                // switch weather
                actions.Add(delegate(int a) {
                    dnc.SetToWeather(curIndex);
                });

                actions.Add(delegate(int a) {
                    if (ls != null)
                    {
                        ls.DoRefresh();
                    }
                    if (fc != null)
                    {
                        fc.DoRefresh();
                    }
                });

                actions.Add(delegate(int a) {
                    string wname  = weatherNames[curIndex];
                    string goname = "water_reflection_probe_" + wname;
                    string path   = AssetDatabase.GenerateUniqueAssetPath(Path.Combine(lightmapTgtPath, goname + ".exr"));
                    Lightmapping.BakeReflectionProbe(curProbe, path);

                    curProbe.mode = UnityEngine.Rendering.ReflectionProbeMode.Custom;
                    curProbe.customBakedTexture = UnityEditor.AssetDatabase.LoadAssetAtPath <Texture>(path);

//					curLoader.texPath = path;
                });
            }

            actions.Add(delegate(int a) {
                ++curIndex;
            });
        }

        actions.Add(delegate(int a) {
            // disable all IgnoreFarClips
            for (int i = 0; i < ifcs.Length; ++i)
            {
                IgnoreFarClip ifc       = ifcs[i];
                ifc.gameObject.isStatic = false;
                Renderer rdr            = ifc.gameObject.GetComponent <Renderer>();
                if (rdr == null)
                {
                    continue;
                }

                foreach (Material m in rdr.sharedMaterials)
                {
                    m.SetInt("_ifc", 1);
                    m.EnableKeyword("IGNORE_FAR_CLIP");
                }
            }
        });

        actions.Add(delegate(int a) {
            dnc.SetToWeather(0);
        });

        actions.Add(delegate(int a) {
            if (ls != null)
            {
                ls.DoRefresh();
            }
            if (fc != null)
            {
                fc.DoRefresh();
            }
        });

        actions.Add(delegate(int a) {
            RefreshEditorStateDisp();
        });

        haltFrame = 0;
    }