internal static void RenderAndWriteToFile( HDProbe probe, string targetFile, RenderTexture cubeRT, RenderTexture planarRT ) { RenderAndWriteToFile(probe, targetFile, cubeRT, planarRT, out _, out _); }
private static void CreateAndImportDummyBakedTextureIfRequired(HDProbe probe, string bakedTexturePath) { var bytes = Texture2D.whiteTexture.EncodeToPNG(); File.WriteAllBytes(bakedTexturePath, bytes); AssetDatabase.ImportAsset(bakedTexturePath); ImportAssetAt(probe, bakedTexturePath); }
internal static void DrawHandles(HDProbeUI s, SerializedHDProbe d, Editor o) { HDProbe probe = d.target as HDProbe; var mat = Matrix4x4.TRS(probe.transform.position, probe.transform.rotation, Vector3.one); switch (EditMode.editMode) { case EditBaseShape: //important: following will init the container for box. //This must be done before drawing the contained handles if (o is PlanarReflectionProbeEditor && (InfluenceShape)d.influenceVolume.shape.intValue == InfluenceShape.Box) { //Planar need to update its transform position. //Let PlanarReflectionProbeUI.Handle do this work. break; } InfluenceVolumeUI.DrawHandles_EditBase(s.influenceVolume, d.influenceVolume, o, mat, probe); break; case EditInfluenceShape: InfluenceVolumeUI.DrawHandles_EditInfluence(s.influenceVolume, d.influenceVolume, o, mat, probe); break; case EditInfluenceNormalShape: InfluenceVolumeUI.DrawHandles_EditInfluenceNormal(s.influenceVolume, d.influenceVolume, o, mat, probe); break; case EditCenter: { if (o is PlanarReflectionProbeEditor && (InfluenceShape)d.influenceVolume.shape.intValue == InfluenceShape.Box) { //Planar need to update its transform position. //Let PlanarReflectionProbeUI.Handle do this work. break; } using (new Handles.DrawingScope(Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one))) { EditorGUI.BeginChangeCheck(); var newCapturePosition = Handles.PositionHandle(probe.transform.position, probe.transform.rotation); if (EditorGUI.EndChangeCheck()) { Vector3 newOffset = Quaternion.Inverse(probe.transform.rotation) * (newCapturePosition - probe.transform.position); Undo.RecordObjects(new Object[] { probe, probe.transform }, "Translate Influence Position"); Vector3 delta = newCapturePosition - probe.transform.position; Matrix4x4 oldLocalToWorld = Matrix4x4.TRS(probe.transform.position, probe.transform.rotation, Vector3.one); //call modification to legacy ReflectionProbe probe.influenceVolume.offset = newOffset; probe.transform.position = newCapturePosition; d.influenceVolume.offset.vector3Value -= oldLocalToWorld.inverse.MultiplyVector(delta); d.influenceVolume.Apply(); } } break; } } }
internal static void DrawGizmos(HDProbe d, GizmoType gizmoType) { HDProbeUI s; if (!HDProbeEditor.TryGetUIStateFor(d, out s)) { return; } var mat = Matrix4x4.TRS(d.transform.position, d.transform.rotation, Vector3.one); switch (EditMode.editMode) { case EditBaseShape: InfluenceVolumeUI.DrawGizmos( s.influenceVolume, d.influenceVolume, mat, InfluenceVolumeUI.HandleType.Base, InfluenceVolumeUI.HandleType.All); break; case EditInfluenceShape: InfluenceVolumeUI.DrawGizmos( s.influenceVolume, d.influenceVolume, mat, InfluenceVolumeUI.HandleType.Influence, InfluenceVolumeUI.HandleType.All); break; case EditInfluenceNormalShape: InfluenceVolumeUI.DrawGizmos( s.influenceVolume, d.influenceVolume, mat, InfluenceVolumeUI.HandleType.InfluenceNormal, InfluenceVolumeUI.HandleType.All); break; default: { var showedHandles = s.influenceVolume.showInfluenceHandles ? InfluenceVolumeUI.HandleType.All : InfluenceVolumeUI.HandleType.Base | InfluenceVolumeUI.HandleType.Influence; InfluenceVolumeUI.DrawGizmos( s.influenceVolume, d.influenceVolume, mat, InfluenceVolumeUI.HandleType.None, showedHandles); break; } } if (d.proxyVolume != null) { ReflectionProxyVolumeComponentUI.DrawGizmos_EditNone(d.proxyVolume); } }
internal static HDProbeUI CreateFor(HDProbe p) { if (p is PlanarReflectionProbe) { return(new PlanarReflectionProbeUI()); } else { return(new HDReflectionProbeUI()); } }
internal static void RenderAndWriteToFile( HDProbe probe, string targetFile, RenderTexture cubeRT, RenderTexture planarRT, out CameraSettings cameraSettings, out CameraPositionSettings cameraPositionSettings ) { var settings = probe.settings; switch (settings.type) { case ProbeSettings.ProbeType.ReflectionProbe: { var positionSettings = ProbeCapturePositionSettings.ComputeFrom(probe, null); HDRenderUtilities.Render(probe.settings, positionSettings, cubeRT, out cameraSettings, out cameraPositionSettings, forceFlipY: true, forceInvertBackfaceCulling: true, // Cubemap have an RHS standard, so we need to invert the face culling (uint)StaticEditorFlags.ReflectionProbeStatic ); HDBakingUtilities.CreateParentDirectoryIfMissing(targetFile); Checkout(targetFile); HDTextureUtilities.WriteTextureFileToDisk(cubeRT, targetFile); break; } case ProbeSettings.ProbeType.PlanarProbe: { var planarProbe = (PlanarReflectionProbe)probe; var positionSettings = ProbeCapturePositionSettings.ComputeFromMirroredReference( probe, planarProbe.referencePosition ); HDRenderUtilities.Render( settings, positionSettings, planarRT, out cameraSettings, out cameraPositionSettings ); HDBakingUtilities.CreateParentDirectoryIfMissing(targetFile); Checkout(targetFile); HDTextureUtilities.WriteTextureFileToDisk(planarRT, targetFile); var renderData = new HDProbe.RenderData(cameraSettings, cameraPositionSettings); var targetRenderDataFile = targetFile + ".renderData"; Checkout(targetRenderDataFile); HDBakingUtilities.TrySerializeToDisk(renderData, targetRenderDataFile); break; } default: throw new ArgumentOutOfRangeException(nameof(probe.settings.type)); } }
internal static void ImportAssetAt(HDProbe probe, string file) { var hd = (HDRenderPipeline)RenderPipelineManager.currentPipeline; switch (probe.settings.type) { case ProbeSettings.ProbeType.ReflectionProbe: { var importer = AssetImporter.GetAtPath(file) as TextureImporter; if (importer == null) { return; } var settings = new TextureImporterSettings(); importer.ReadTextureSettings(settings); settings.sRGBTexture = false; settings.filterMode = FilterMode.Bilinear; settings.generateCubemap = TextureImporterGenerateCubemap.AutoCubemap; settings.cubemapConvolution = TextureImporterCubemapConvolution.None; settings.seamlessCubemap = false; settings.wrapMode = TextureWrapMode.Repeat; settings.aniso = 1; importer.SetTextureSettings(settings); importer.mipmapEnabled = false; importer.textureCompression = hd.currentPlatformRenderPipelineSettings.lightLoopSettings.reflectionCacheCompressed ? TextureImporterCompression.Compressed : TextureImporterCompression.Uncompressed; importer.textureShape = TextureImporterShape.TextureCube; importer.SaveAndReimport(); break; } case ProbeSettings.ProbeType.PlanarProbe: { var importer = AssetImporter.GetAtPath(file) as TextureImporter; if (importer == null) { return; } importer.sRGBTexture = false; importer.filterMode = FilterMode.Bilinear; importer.mipmapEnabled = false; importer.textureCompression = hd.currentPlatformRenderPipelineSettings.lightLoopSettings.planarReflectionCacheCompressed ? TextureImporterCompression.Compressed : TextureImporterCompression.Uncompressed; importer.textureShape = TextureImporterShape.Texture2D; importer.SaveAndReimport(); break; } } }
internal void RegisterProbe(HDProbe probe) { var settings = probe.settings; switch (settings.mode) { case ProbeSettings.Mode.Baked: m_BakedProbes.Add(probe); break; case ProbeSettings.Mode.Realtime: switch (settings.type) { case ProbeSettings.ProbeType.PlanarProbe: if (!m_RealtimeViewDependentProbes.Contains(probe)) { m_RealtimeViewDependentProbes.Add(probe); } break; case ProbeSettings.ProbeType.ReflectionProbe: if (!m_RealtimeViewIndependentProbes.Contains(probe)) { m_RealtimeViewIndependentProbes.Add(probe); } break; } break; } switch (settings.type) { case ProbeSettings.ProbeType.PlanarProbe: { if (m_PlanarProbes.Add((PlanarReflectionProbe)probe)) { // Insert in the array // Grow the arrays if (m_PlanarProbeCount == m_PlanarProbesArray.Length) { Array.Resize(ref m_PlanarProbesArray, m_PlanarProbes.Count * 2); Array.Resize(ref m_PlanarProbeBounds, m_PlanarProbeBounds.Length * 2); } m_PlanarProbesArray[m_PlanarProbeCount] = (PlanarReflectionProbe)probe; m_PlanarProbeBounds[m_PlanarProbeCount] = ((PlanarReflectionProbe)probe).boundingSphere; ++m_PlanarProbeCount; } break; } } }
internal void UnregisterProbe(HDProbe probe) { m_BakedProbes.Remove(probe); m_RealtimeViewDependentProbes.Remove(probe); m_RealtimeViewIndependentProbes.Remove(probe); // Remove swap back if (m_PlanarProbes.Remove(probe)) { // It is best to rebuild the full array when we need it instead of doing it at each unregister. // So we mark it as dirty. m_RebuildPlanarProbeArray = true; } }
/// <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()}")); } }
static void SetupFrameSettings(HDAdditionalCameraData additionalData, HDProbe probe) { HDRenderPipelineAsset hdrp = GraphicsSettings.renderPipelineAsset as HDRenderPipelineAsset; if (probe.mode == ReflectionProbeMode.Realtime) { hdrp.GetRealtimeReflectionFrameSettings().CopyTo(additionalData.GetFrameSettings()); } else { hdrp.GetBakedOrCustomReflectionFrameSettings().CopyTo(additionalData.GetFrameSettings()); } if (probe.captureSettings.renderingPath == HDAdditionalCameraData.RenderingPath.Custom) { probe.frameSettings.Override(additionalData.GetFrameSettings()).CopyTo(additionalData.GetFrameSettings()); } }
internal static void DrawHandles(HDProbeUI s, SerializedHDProbe d, Editor o) { HDProbe probe = d.target as HDProbe; var mat = Matrix4x4.TRS(probe.transform.position, probe.transform.rotation, Vector3.one); switch (EditMode.editMode) { case EditBaseShape: InfluenceVolumeUI.DrawHandles_EditBase(s.influenceVolume, d.influenceVolume, o, mat, probe); break; case EditInfluenceShape: InfluenceVolumeUI.DrawHandles_EditInfluence(s.influenceVolume, d.influenceVolume, o, mat, probe); break; case EditInfluenceNormalShape: InfluenceVolumeUI.DrawHandles_EditInfluenceNormal(s.influenceVolume, d.influenceVolume, o, mat, probe); break; case EditCenter: { using (new Handles.DrawingScope(Matrix4x4.TRS(Vector3.zero, Quaternion.identity, Vector3.one))) { //Vector3 offsetWorld = probe.transform.position + probe.transform.rotation * probe.influenceVolume.offset; EditorGUI.BeginChangeCheck(); var newCapturePosition = Handles.PositionHandle(probe.transform.position, probe.transform.rotation); if (EditorGUI.EndChangeCheck()) { Vector3 newOffset = Quaternion.Inverse(probe.transform.rotation) * (newCapturePosition - probe.transform.position); Undo.RecordObjects(new Object[] { probe, probe.transform }, "Translate Influence Position"); Vector3 delta = newCapturePosition - probe.transform.position; Matrix4x4 oldLocalToWorld = Matrix4x4.TRS(probe.transform.position, probe.transform.rotation, Vector3.one); //call modification to legacy ReflectionProbe probe.influenceVolume.offset = newOffset; probe.transform.position = newCapturePosition; d.influenceVolume.offset.vector3Value -= oldLocalToWorld.inverse.MultiplyVector(delta); d.influenceVolume.Apply(); } } break; } } }
Texture GetTexture() { HDProbe additional = GetTarget(target); if (additional != null && additional.mode == UnityEngine.Rendering.ReflectionProbeMode.Realtime) { return(additional.realtimeTexture); } else { var p = target as ReflectionProbe; if (p != null) { return(p.texture); } } return(null); }
/// <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)); } }
internal void UnregisterProbe(HDProbe probe) { m_BakedProbes.Remove(probe); m_RealtimeViewDependentProbes.Remove(probe); m_RealtimeViewIndependentProbes.Remove(probe); // Remove swap back var index = Array.IndexOf(m_PlanarProbes, probe); if (index != -1) { if (index < m_PlanarProbeCount) { m_PlanarProbes[index] = m_PlanarProbes[m_PlanarProbeCount - 1]; m_PlanarProbeBounds[index] = m_PlanarProbeBounds[m_PlanarProbeCount - 1]; m_PlanarProbes[m_PlanarProbeCount - 1] = null; } --m_PlanarProbeCount; } }
internal static void AssignRenderData(HDProbe probe, string bakedTexturePath) { switch (probe.settings.type) { case ProbeSettings.ProbeType.PlanarProbe: { var planarProbe = (PlanarReflectionProbe)probe; var dataFile = bakedTexturePath + ".renderData"; if (File.Exists(dataFile)) { if (HDBakingUtilities.TryDeserializeFromDisk(dataFile, out HDProbe.RenderData renderData)) { HDProbeSystem.AssignRenderData(probe, renderData, ProbeSettings.Mode.Baked); EditorUtility.SetDirty(probe); } } break; } } }
static void RenderInCustomAsset(HDProbe probe, bool useExistingCustomAsset) { var provider = new TProvider(); string assetPath = null; if (useExistingCustomAsset && probe.customTexture != null && !probe.customTexture.Equals(null)) { assetPath = AssetDatabase.GetAssetPath(probe.customTexture); } if (string.IsNullOrEmpty(assetPath)) { assetPath = EditorUtility.SaveFilePanelInProject( "Save custom capture", probe.name, "exr", "Save custom capture"); } if (!string.IsNullOrEmpty(assetPath)) { var target = HDProbeSystem.CreateRenderTargetForMode( probe, ProbeSettings.Mode.Custom ); HDProbeSystem.Render( probe, null, target, out HDProbe.RenderData renderData, forceFlipY: probe.type == ProbeSettings.ProbeType.ReflectionProbe ); HDTextureUtilities.WriteTextureFileToDisk(target, assetPath); AssetDatabase.ImportAsset(assetPath); HDBakedReflectionSystem.ImportAssetAt(probe, assetPath); CoreUtils.Destroy(target); var assetTarget = AssetDatabase.LoadAssetAtPath <Texture>(assetPath); probe.SetTexture(ProbeSettings.Mode.Custom, assetTarget); probe.SetRenderData(ProbeSettings.Mode.Custom, renderData); EditorUtility.SetDirty(probe); } }
static void RenderInCustomAsset(HDProbe probe, bool useExistingCustomAsset) { var provider = new TProvider(); string assetPath = null; if (useExistingCustomAsset && probe.customTexture != null && !probe.customTexture.Equals(null)) { assetPath = AssetDatabase.GetAssetPath(probe.customTexture); } if (string.IsNullOrEmpty(assetPath)) { assetPath = EditorUtility.SaveFilePanelInProject( "Save custom capture", probe.name, "exr", "Save custom capture"); } if (!string.IsNullOrEmpty(assetPath)) { var target = (RenderTexture)HDProbeSystem.CreateRenderTargetForMode( probe, ProbeSettings.Mode.Custom ); HDBakedReflectionSystem.RenderAndWriteToFile( probe, assetPath, target, null, out var cameraSettings, out var cameraPositionSettings ); AssetDatabase.ImportAsset(assetPath); HDBakedReflectionSystem.ImportAssetAt(probe, assetPath); CoreUtils.Destroy(target); var assetTarget = AssetDatabase.LoadAssetAtPath <Texture>(assetPath); probe.SetTexture(ProbeSettings.Mode.Custom, assetTarget); probe.SetRenderData(ProbeSettings.Mode.Custom, new HDProbe.RenderData(cameraSettings, cameraPositionSettings)); EditorUtility.SetDirty(probe); } }
internal static bool TryGetUIStateFor(HDProbe p, out HDProbeUI r) { return(s_StateMap.TryGetValue(p, out r)); }
internal void RegisterProbe(HDProbe probe) { var settings = probe.settings; switch (settings.mode) { case ProbeSettings.Mode.Baked: // TODO: Remove the duplicate check // In theory, register/unregister are called by pair, never twice register in a row // So there should not any "duplicate" calls. still it happens and we must prevent // duplicate entries. if (!m_BakedProbes.Contains(probe)) { m_BakedProbes.Add(probe); } break; case ProbeSettings.Mode.Realtime: switch (settings.type) { case ProbeSettings.ProbeType.PlanarProbe: if (!m_RealtimeViewDependentProbes.Contains(probe)) { m_RealtimeViewDependentProbes.Add(probe); } break; case ProbeSettings.ProbeType.ReflectionProbe: if (!m_RealtimeViewIndependentProbes.Contains(probe)) { m_RealtimeViewIndependentProbes.Add(probe); } break; } break; } switch (settings.type) { case ProbeSettings.ProbeType.PlanarProbe: { // TODO: Remove the duplicate check // In theory, register/unregister are called by pair, never twice register in a row // So there should not any "duplicate" calls. still it happens and we must prevent // duplicate entries. if (Array.IndexOf(m_PlanarProbes, (PlanarReflectionProbe)probe) != -1) { break; } // Grow the arrays if (m_PlanarProbeCount == m_PlanarProbes.Length) { Array.Resize(ref m_PlanarProbes, m_PlanarProbes.Length * 2); Array.Resize(ref m_PlanarProbeBounds, m_PlanarProbeBounds.Length * 2); } m_PlanarProbes[m_PlanarProbeCount] = (PlanarReflectionProbe)probe; m_PlanarProbeBounds[m_PlanarProbeCount] = ((PlanarReflectionProbe)probe).boundingSphere; ++m_PlanarProbeCount; break; } } }
// void OnEnable() { myProbe = GetComponent <ReflectionProbe>(); #if ENVIRO_HDRP if (EnviroSkyMgr.instance != null && EnviroSkyMgr.instance.currentRenderPipeline == EnviroSkyMgr.EnviroRenderPipeline.HDRP) { hdprobe = GetComponent <HDProbe>(); } #endif #if ENVIRO_HDRP if (EnviroSkyMgr.instance != null && EnviroSkyMgr.instance.currentRenderPipeline == EnviroSkyMgr.EnviroRenderPipeline.HDRP) { if (hdprobe != null) { hdprobe.mode = ProbeSettings.Mode.Realtime; hdprobe.realtimeMode = ProbeSettings.RealtimeMode.OnDemand; } if (!Application.isPlaying) { paused = true; if (myProbe != null) { myProbe.enabled = false; } } else { StartCoroutine(HDRPWorkaround()); } } #else if (!standalone && myProbe != null) { myProbe.enabled = true; } if (customRendering) { myProbe.mode = ReflectionProbeMode.Custom; myProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting; CreateCubemap(); CreateTexturesAndMaterial(); CreateRenderCamera(); currentRes = myProbe.resolution; rendering = false; StartCoroutine(RefreshFirstTime()); } else { myProbe.mode = ReflectionProbeMode.Realtime; myProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting; myProbe.RenderProbe(); } #endif }
//////// void OnEnable() { myProbe = GetComponent <ReflectionProbe>(); #if ENVIRO_HDRP if (EnviroSkyMgr.instance != null) { hdprobe = GetComponent <HDProbe>(); if (!standalone && myProbe != null) { myProbe.enabled = true; } if (customRendering) { if (hdprobe != null) { hdprobe.mode = ProbeSettings.Mode.Custom; CreateCubemap(); CreateTexturesAndMaterial(); CreateRenderCamera(); currentRes = myProbe.resolution; rendering = false; StartCoroutine(RefreshFirstTime()); } } else { if (hdprobe != null) { hdprobe.mode = ProbeSettings.Mode.Realtime; hdprobe.realtimeMode = ProbeSettings.RealtimeMode.OnDemand; hdprobe.RequestRenderNextUpdate(); } } } #else if (!standalone && myProbe != null) { myProbe.enabled = true; } if (customRendering) { myProbe.mode = ReflectionProbeMode.Custom; myProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting; CreateCubemap(); CreateTexturesAndMaterial(); CreateRenderCamera(); currentRes = myProbe.resolution; rendering = false; StartCoroutine(RefreshFirstTime()); } else { myProbe.mode = ReflectionProbeMode.Realtime; myProbe.refreshMode = ReflectionProbeRefreshMode.ViaScripting; myProbe.RenderProbe(); } #endif }
void Start() { _hdProbe = GetComponent <HDProbe>(); }
static Texture GetTexture(HDReflectionProbeEditor e, Object target) { HDProbe probe = e.GetTarget(target); return(probe.texture); }