using System; using JetBrains.Annotations; using UnityEngine; using UnityEngine.Experimental.Rendering; using UnityEngine.Rendering.HighDefinition; namespace UnityEditor.Rendering.HighDefinition { /// /// Provides methods to help you with lightmapping in the High Definition Render Pipeline. /// public static class LightmappingHDRP { /// /// Bakes the and updates its baked texture. /// /// Note: The update of the probe is persistent only in editor mode. /// /// The probe to bake. /// The asset path to write the baked texture to. /// The options to use for the bake. /// /// Returns null if successful. Otherwise, returns the error that occured. /// The error can be: /// * if the is invalid. /// * if the is null. /// * if the is not supported. Only /// This functional currently only supports probes. /// public static Exception BakeProbe([NotNull] HDProbe probe, [NotNull] string path, BakeProbeOptions options) { // Check arguments if (probe == null || probe.Equals(null)) return new ArgumentNullException(nameof(probe)); if (string.IsNullOrEmpty(path)) return new ArgumentException($"{nameof(path)} must not be empty or null."); // We force RGBAHalf as we don't support 11-11-10 textures (only RT) var probeFormat = GraphicsFormat.R16G16B16A16_SFloat; switch (probe) { case HDAdditionalReflectionData _: { // Get the texture size from the probe var textureSize = options.textureSize.Evaluate(probe); // Render and write var cubeRT = HDRenderUtilities.CreateReflectionProbeRenderTarget(textureSize, probeFormat); HDBakedReflectionSystem.RenderAndWriteToFile(probe, path, cubeRT); cubeRT.Release(); // Import asset at target location AssetDatabase.ImportAsset(path); HDBakedReflectionSystem.ImportAssetAt(probe, path); // Assign to the probe the baked texture var bakedTexture = AssetDatabase.LoadAssetAtPath(path); probe.SetTexture(ProbeSettings.Mode.Baked, bakedTexture); // Mark probe as dirty EditorUtility.SetDirty(probe); return null; } case PlanarReflectionProbe _: return new Exception("Planar reflection probe baking is not supported."); default: return new Exception($"Cannot handle probe type: {probe.GetType()}"); } } /// /// Represents options to use with the function. /// public struct BakeProbeOptions { /// /// Represents the size of a probe's baked texture. /// public struct TextureSize { /// /// Options for which method to use to determine the size of the baked texture. /// public enum Mode { /// /// Uses the probe's resolution as the size of the baked texture. /// UseProbeResolution, /// /// Uses the value of as the size of the baked texture. /// CustomValue } /// /// Returns a with default values. /// /// Returns a with default values. public static TextureSize NewDefault() { return new TextureSize { mode = Mode.UseProbeResolution }; } /// /// The method to use to determine the size of the baked texture. /// public Mode mode; /// /// The value used in the mode. /// public int customValue; /// /// Evaluates a probe and gets the texture size to use for baking. /// /// The probe to get the texture size for. /// Returns the size of the texture to use for the bake. /// /// When is null and the mode /// is used. /// /// When has an invalid value. public int Evaluate(HDProbe probe) { switch (mode) { case Mode.CustomValue: return customValue; case Mode.UseProbeResolution: { if (probe == null || probe.Equals(null)) throw new ArgumentNullException(nameof(probe)); return (int)probe.resolution; } default: throw new ArgumentOutOfRangeException(nameof(mode)); } } } /// /// Constructs a with default values and returns it. /// /// A with default values. public static BakeProbeOptions NewDefault() { return new BakeProbeOptions { textureSize = TextureSize.NewDefault() }; } /// /// The texture size to use for the bake. /// public TextureSize textureSize; } } }