public static ProbeCapturePositionSettings ComputeFrom(HDProbe probe, Transform reference)
        {
            var referencePosition = Vector3.zero;
            var referenceRotation = Quaternion.identity;

            if (reference != null)
            {
                referencePosition = reference.position;
                referenceRotation = reference.rotation;
            }
            else
            {
                if (probe.type == ProbeSettings.ProbeType.PlanarProbe)
                {
                    var planar = (PlanarReflectionProbe)probe;
                    return(ComputeFromMirroredReference(planar, planar.referencePosition));
                }
            }

            var result = ComputeFrom(probe, referencePosition, referenceRotation);

            // In case of probe baking, 99% of the time, orientation of the cubemap doesn't matters
            //   so, we build one without any rotation, thus we don't have to change the basis
            //   during sampling the cubemap.
            if (probe.type == ProbeSettings.ProbeType.ReflectionProbe)
            {
                result.proxyRotation = Quaternion.identity;
            }

            return(result);
        }
        private static void CreateAndImportDummyBakedTextureIfRequired(HDProbe probe, string bakedTexturePath)
        {
            var bytes = Texture2D.whiteTexture.EncodeToPNG();

            File.WriteAllBytes(bakedTexturePath, bytes);
            AssetDatabase.ImportAsset(bakedTexturePath);
            ImportAssetAt(probe, bakedTexturePath);
        }
Exemplo n.º 3
0
        internal static void RenderAndWriteToFile(
            HDProbe probe, string targetFile,
            RenderTexture cubeRT, RenderTexture planarRT
            )
        {
            var settings = probe.settings;

            switch (settings.type)
            {
            case ProbeSettings.ProbeType.ReflectionProbe:
            {
                var positionSettings = ProbeCapturePositionSettings.ComputeFrom(probe, null);
                HDRenderUtilities.Render(probe.settings, positionSettings, cubeRT,
                                         forceFlipY: true,
                                         forceInvertBackfaceCulling: true, // Cubemap have an RHS standard, so we need to invert the face culling
                                         (uint)StaticEditorFlags.ReflectionProbeStatic
                                         );
                HDBakingUtilities.CreateParentDirectoryIfMissing(targetFile);
                if (Provider.isActive && HDEditorUtils.IsAssetPath(targetFile))
                {
                    Checkout(targetFile, CheckoutMode.Both);
                }
                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 var cameraSettings, out var cameraPositionSettings
                    );
                HDBakingUtilities.CreateParentDirectoryIfMissing(targetFile);
                if (Provider.isActive && HDEditorUtils.IsAssetPath(targetFile))
                {
                    Checkout(targetFile, CheckoutMode.Both);
                }
                HDTextureUtilities.WriteTextureFileToDisk(planarRT, targetFile);
                var renderData           = new HDProbe.RenderData(cameraSettings, cameraPositionSettings);
                var targetRenderDataFile = targetFile + ".renderData";
                if (Provider.isActive && HDEditorUtils.IsAssetPath(targetRenderDataFile))
                {
                    Checkout(targetRenderDataFile, CheckoutMode.Both);
                }
                HDBakingUtilities.TrySerializeToDisk(renderData, targetRenderDataFile);
                break;
            }
            }
        }
        public static void RenderAndUpdateRealtimeRenderData(
            HDProbe probe, Transform viewerTransform
            )
        {
            var target = CreateAndSetRenderTargetIfRequired(probe, ProbeSettings.Mode.Realtime);

            Render(probe, viewerTransform, target, out HDProbe.RenderData renderData);
            AssignRenderData(probe, renderData, ProbeSettings.Mode.Realtime);
        }
 public static string GetBakedTextureFilePath(HDProbe probe)
 {
     return(GetBakedTextureFilePath(
                probe.settings.type,
                SceneObjectIDMap.GetOrCreateSceneObjectID(
                    probe.gameObject, SceneObjectCategory.ReflectionProbe
                    ),
                probe.gameObject.scene
                ));
 }
        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;
            }
            }
        }
        public static Texture CreateRenderTargetForMode(HDProbe probe, ProbeSettings.Mode targetMode)
        {
            Texture target   = null;
            var     hd       = (HDRenderPipeline)RenderPipelineManager.currentPipeline;
            var     settings = probe.settings;

            switch (targetMode)
            {
            case ProbeSettings.Mode.Realtime:
            {
                switch (settings.type)
                {
                case ProbeSettings.ProbeType.PlanarProbe:
                    target = HDRenderUtilities.CreatePlanarProbeRenderTarget(
                        (int)hd.renderPipelineSettings.lightLoopSettings.planarReflectionTextureSize
                        );
                    break;

                case ProbeSettings.ProbeType.ReflectionProbe:
                    target = HDRenderUtilities.CreateReflectionProbeRenderTarget(
                        (int)hd.renderPipelineSettings.lightLoopSettings.reflectionCubemapSize
                        );
                    break;
                }
                break;
            }

            case ProbeSettings.Mode.Baked:
            case ProbeSettings.Mode.Custom:
            {
                switch (settings.type)
                {
                case ProbeSettings.ProbeType.PlanarProbe:
                    target = HDRenderUtilities.CreatePlanarProbeRenderTarget(
                        (int)hd.renderPipelineSettings.lightLoopSettings.planarReflectionTextureSize
                        );
                    break;

                case ProbeSettings.ProbeType.ReflectionProbe:
                    target = HDRenderUtilities.CreateReflectionProbeTarget(
                        (int)hd.renderPipelineSettings.lightLoopSettings.reflectionCubemapSize
                        );
                    break;
                }
                break;
            }
            }

            return(target);
        }
        static ProbeCapturePositionSettings ComputeFrom(
            HDProbe probe,
            Vector3 referencePosition, Quaternion referenceRotation
            )
        {
            var result       = new ProbeCapturePositionSettings();
            var proxyToWorld = probe.proxyToWorld;

            result.proxyPosition     = proxyToWorld.GetColumn(3);
            result.proxyRotation     = proxyToWorld.rotation;
            result.referencePosition = referencePosition;
            result.referenceRotation = referenceRotation;
            return(result);
        }
        internal void RegisterProbe(HDProbe probe)
        {
            var settings = probe.settings;

            switch (settings.mode)
            {
            case ProbeSettings.Mode.Baked:
                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:
            {
                // 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;
            }
            }
        }
Exemplo n.º 10
0
        public static void AssignRenderData(
            HDProbe probe,
            HDProbe.RenderData renderData,
            ProbeSettings.Mode targetMode
            )
        {
            switch (targetMode)
            {
            case ProbeSettings.Mode.Baked: probe.bakedRenderData = renderData; break;

            case ProbeSettings.Mode.Custom: probe.customRenderData = renderData; break;

            case ProbeSettings.Mode.Realtime: probe.realtimeRenderData = renderData; break;
            }
        }
Exemplo n.º 11
0
        static Texture CreateAndSetRenderTargetIfRequired(HDProbe probe, ProbeSettings.Mode targetMode)
        {
            var     settings = probe.settings;
            Texture target   = probe.GetTexture(targetMode);

            if (target != null)
            {
                return(target);
            }

            target = CreateRenderTargetForMode(probe, targetMode);

            probe.SetTexture(targetMode, target);
            return(target);
        }
Exemplo n.º 12
0
        static bool DoesRealtimeProbeNeedToBeUpdated(HDProbe probe)
        {
            // Discard (real time, every frame) probe already rendered this frame
            // Discard (real time, OnEnable) probe already rendered after on enable
            if (probe.mode == ProbeSettings.Mode.Realtime)
            {
                switch (probe.realtimeMode)
                {
                case ProbeSettings.RealtimeMode.EveryFrame:
                    return(probe.lastRenderedFrame != Time.frameCount);

                case ProbeSettings.RealtimeMode.OnEnable:
                    return(!probe.wasRenderedAfterOnEnable);
                }
            }
            return(true);
        }
Exemplo n.º 13
0
        public static void Render(
            HDProbe probe, Transform viewerTransform,
            Texture outTarget, out HDProbe.RenderData outRenderData,
            bool forceFlipY = false
            )
        {
            var positionSettings = ProbeCapturePositionSettings.ComputeFrom(probe, viewerTransform);

            HDRenderUtilities.Render(
                probe.settings,
                positionSettings,
                outTarget,
                out CameraSettings cameraSettings, out CameraPositionSettings cameraPosition,
                forceFlipY: forceFlipY
                );

            outRenderData = new HDProbe.RenderData(cameraSettings, cameraPosition);
        }
Exemplo n.º 14
0
        internal static void RenderAndWriteToFile(
            HDProbe probe, string targetFile,
            RenderTexture cubeRT, RenderTexture planarRT
            )
        {
            var settings = probe.settings;

            switch (settings.type)
            {
            case ProbeSettings.ProbeType.ReflectionProbe:
            {
                var positionSettings = ProbeCapturePositionSettings.ComputeFrom(probe, null);
                HDRenderUtilities.Render(probe.settings, positionSettings, cubeRT,
                                         forceFlipY: true,
                                         forceInvertBackfaceCulling: true, // TODO: for an unknown reason, we need to invert the backface culling for baked reflection probes, Remove this
                                         (uint)StaticEditorFlags.ReflectionProbeStatic
                                         );
                HDBakingUtilities.CreateParentDirectoryIfMissing(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 cameraSettings, out CameraPositionSettings cameraPositionSettings
                    );
                HDBakingUtilities.CreateParentDirectoryIfMissing(targetFile);
                HDTextureUtilities.WriteTextureFileToDisk(planarRT, targetFile);
                var renderData = new HDProbe.RenderData(cameraSettings, cameraPositionSettings);
                HDBakingUtilities.TrySerializeToDisk(renderData, targetFile + ".renderData");
                break;
            }
            }
        }
Exemplo n.º 15
0
        public static ProbeCapturePositionSettings ComputeFromMirroredReference(
            HDProbe probe, Vector3 referencePosition
            )
        {
            var positionSettings = ComputeFrom(
                probe,
                referencePosition, Quaternion.identity
                );
            // Set proper orientation for the reference rotation
            var proxyMatrix = Matrix4x4.TRS(
                positionSettings.proxyPosition,
                positionSettings.proxyRotation,
                Vector3.one
                );
            var mirrorPosition = proxyMatrix.MultiplyPoint(probe.settings.proxySettings.mirrorPositionProxySpace);

            positionSettings.referenceRotation = Quaternion.LookRotation(mirrorPosition - positionSettings.referencePosition);
            return(positionSettings);
        }
Exemplo n.º 16
0
        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;
     }
     }
 }
        public static ProbeCapturePositionSettings ComputeFrom(HDProbe probe, Transform reference)
        {
            var referencePosition = Vector3.zero;
            var referenceRotation = Quaternion.identity;

            if (reference != null)
            {
                referencePosition = reference.position;
                referenceRotation = reference.rotation;
            }
            else
            {
                if (probe.type == ProbeSettings.ProbeType.PlanarProbe)
                {
                    var planar = (PlanarReflectionProbe)probe;
                    return(ComputeFromMirroredReference(planar, planar.referencePosition));
                }
            }

            return(ComputeFrom(probe, referencePosition, referenceRotation));
        }
        internal static void RenderAndWriteToFile(
            HDProbe probe, string targetFile,
            RenderTexture cubeRT, RenderTexture planarRT
            )
        {
            var settings = probe.settings;

            switch (settings.type)
            {
            case ProbeSettings.ProbeType.ReflectionProbe:
            {
                var positionSettings = ProbeCapturePositionSettings.ComputeFrom(probe, null);
                HDRenderUtilities.Render(probe.settings, positionSettings, cubeRT, forceFlipY: true);
                HDBakingUtilities.CreateParentDirectoryIfMissing(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 cameraSettings, out CameraPositionSettings cameraPositionSettings
                    );
                HDBakingUtilities.CreateParentDirectoryIfMissing(targetFile);
                HDTextureUtilities.WriteTextureFileToDisk(planarRT, targetFile);
                var renderData = new HDProbe.RenderData(cameraSettings, cameraPositionSettings);
                HDBakingUtilities.TrySerializeToDisk(renderData, targetFile + ".renderData");
                break;
            }
            }
        }
Exemplo n.º 20
0
 public static void UnregisterProbe(HDProbe probe) => s_Instance.UnregisterProbe(probe);
Exemplo n.º 21
0
 internal void Init(HDProbe probe)
 {
     this.m_Probe = probe;
 }