/// <summary> /// Bakes the <paramref name="probe" /> and updates its baked texture. /// /// Note: The update of the probe is persistent only in editor mode. /// </summary> /// <param name="probe">The probe to bake.</param> /// <param name="path">The asset path to write the baked texture to.</param> /// <param name="options">The options to use for the bake.</param> /// <returns> /// Returns <c>null</c> if successful. Otherwise, returns the error that occured. /// The error can be: /// * <see cref="ArgumentException" /> if the <paramref name="path" /> is invalid. /// * <see cref="ArgumentNullException" /> if the <paramref name="probe" /> is <c>null</c>. /// * <see cref="Exception" /> if the <paramref name="probe" /> is not supported. Only /// This functional currently only supports <see cref="HDAdditionalReflectionData" /> probes. /// </returns> 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, null); 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 <Texture>(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()}")); } }
/// <summary> /// Evaluates a probe and gets the texture size to use for baking. /// </summary> /// <param name="probe">The probe to get the texture size for.</param> /// <returns>Returns the size of the texture to use for the bake.</returns> /// <exception cref="ArgumentNullException"> /// When <paramref name="probe" /> is <c>null</c> and the mode /// <see cref="Mode.UseProbeResolution" /> is used. /// </exception> /// <exception cref="ArgumentOutOfRangeException">When <see cref="mode" /> has an invalid value.</exception> 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)); } }