Пример #1
0
        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (!base.ProcessNode(cmd) || textureAsset == null)
            {
                return(false);
            }

#if UNITY_EDITOR
            var importer = UnityEditor.AssetImporter.GetAtPath(UnityEditor.AssetDatabase.GetAssetPath(textureAsset));
            if (importer is UnityEditor.TextureImporter textureImporter)
            {
                normalMap = textureImporter.textureType == UnityEditor.TextureImporterType.NormalMap;
            }
#endif

            // Compressed normal maps need to be converted from AG to RG format
            if (normalMap)
            {
                if (postProcessedTexture == null)
                {
                    postProcessedTexture = new RenderTexture(1, 1, 0, GraphicsFormat.R16G16B16A16_SFloat);
                }

                if (postProcessedTexture.width != textureAsset.width || postProcessedTexture.height != textureAsset.height)
                {
                    postProcessedTexture.Release();
                    postProcessedTexture.width  = textureAsset.width;
                    postProcessedTexture.height = textureAsset.height;
                    postProcessedTexture.Create();
                }

                var blitMaterial = GetTempMaterial("Hidden/Mixture/TextureNode");
                MixtureUtils.SetTextureWithDimension(blitMaterial, "_Source", textureAsset);
                MixtureUtils.Blit(cmd, blitMaterial, textureAsset, postProcessedTexture);

                outputTexture = postProcessedTexture;
            }
            else
            {
                postProcessedTexture?.Release();
                outputTexture = textureAsset;
            }

            return(true);
        }
Пример #2
0
        protected override bool ProcessNode(CommandBuffer cmd)
        {
            if (!base.ProcessNode(cmd) || textureAsset == null)
            {
                return(false);
            }

#if UNITY_EDITOR
            var importer = UnityEditor.AssetImporter.GetAtPath(UnityEditor.AssetDatabase.GetAssetPath(textureAsset));
            if (importer is UnityEditor.TextureImporter textureImporter)
            {
                normalMap = textureImporter.textureType == UnityEditor.TextureImporterType.NormalMap;
            }
#endif

            int  targetWidth     = textureAsset.width;
            int  targetHeight    = textureAsset.height;
            int  targetDepth     = TextureUtils.GetSliceCount(textureAsset);
            bool needsTempTarget = false;
            if (!IsPowerOf2(textureAsset) && POTMode != PowerOf2Mode.None)
            {
                int maxSize = Mathf.Max(Mathf.Max(targetWidth, targetHeight), targetDepth);
                int potSize = 0;

                switch (POTMode)
                {
                case PowerOf2Mode.ScaleToNextPowerOf2:
                    potSize = Mathf.NextPowerOfTwo(maxSize);
                    break;

                default:
                    potSize = Mathf.ClosestPowerOfTwo(maxSize);
                    break;
                }
                targetWidth     = targetHeight = targetDepth = potSize;
                needsTempTarget = true;
            }
            if (normalMap)
            {
                needsTempTarget = true;
            }

            if (needsTempTarget && postProcessedTexture == null)
            {
                postProcessedTexture = new RenderTexture(1, 1, 0, GraphicsFormat.R16G16B16A16_SFloat, mipCount: textureAsset.mipmapCount)
                {
                    dimension = textureAsset.dimension, enableRandomWrite = true, volumeDepth = 1
                }
            }
            ;
            else if (!needsTempTarget)
            {
                postProcessedTexture?.Release();
                postProcessedTexture = null;
            }

            if (postProcessedTexture != null && (postProcessedTexture.width != targetWidth ||
                                                 postProcessedTexture.height != targetHeight ||
                                                 postProcessedTexture.volumeDepth != targetDepth))
            {
                postProcessedTexture.Release();
                postProcessedTexture.width       = targetWidth;
                postProcessedTexture.height      = targetHeight;
                postProcessedTexture.volumeDepth = targetDepth;
                postProcessedTexture.Create();
            }
            // TODO: same alloc as normal map + scale and crop options

            // Compressed normal maps need to be converted from AG to RG format
            if (normalMap)
            {
                // Transform normal map texture into POT
                var blitMaterial = GetTempMaterial("Hidden/Mixture/TextureNode");
                MixtureUtils.SetTextureWithDimension(blitMaterial, "_Source", textureAsset);
                blitMaterial.SetInt("_POTMode", (int)POTMode);
                MixtureUtils.Blit(cmd, blitMaterial, textureAsset, postProcessedTexture, 0);

                outputTexture = postProcessedTexture;
            }
            else if (needsTempTarget)
            {
                // Transform standard texture into POT
                var blitMaterial = GetTempMaterial("Hidden/Mixture/TextureNode");
                MixtureUtils.SetTextureWithDimension(blitMaterial, "_Source", textureAsset);
                blitMaterial.SetInt("_POTMode", (int)POTMode);
                MixtureUtils.Blit(cmd, blitMaterial, textureAsset, postProcessedTexture, 1);

                outputTexture = postProcessedTexture;
            }
            else
            {
                outputTexture = textureAsset;
            }

            if (outputTexture != null)
            {
                settings.sizeMode = OutputSizeMode.Absolute;
                settings.width    = outputTexture.width;
                settings.height   = outputTexture.height;
                settings.depth    = TextureUtils.GetSliceCount(outputTexture);
            }

            return(true);
        }