void InitializeBlackCubemapArray()
        {
            if (m_BlackCubemapArray == null)
            {
                m_BlackCubemapArray = new CubemapArray(1, m_IBLFilterArray.Length, TextureFormat.RGBA32, false)
                {
                    hideFlags  = HideFlags.HideAndDontSave,
                    wrapMode   = TextureWrapMode.Repeat,
                    wrapModeV  = TextureWrapMode.Clamp,
                    filterMode = FilterMode.Trilinear,
                    anisoLevel = 0,
                    name       = "BlackCubemapArray"
                };

                Color32[] black = { new Color32(0, 0, 0, 0) };

                for (int element = 0; element < m_IBLFilterArray.Length; ++element)
                {
                    for (int i = 0; i < 6; i++)
                    {
                        m_BlackCubemapArray.SetPixels32(black, (CubemapFace)i, element);
                    }
                }

                m_BlackCubemapArray.Apply();
            }
        }
    void CreateCubeArrayAsset()
    {
        //Texture2DArray array = new Texture2DArray(textures[0].width, textures[0].height, textures.Length, GraphicsFormatUtility.GetGraphicsFormat(TextureFormat.ARGB32, false), TextureCreationFlags.None);
        TextureFormat tf       = cubeMaps[0].format;
        int           mipLevel = cubeMaps[0].mipmapCount;

        CubemapArray array = new CubemapArray(cubeMaps[0].width, cubeMaps.Length, texFormat, mipmaps);


        for (int i = 0; i < 6; i++) //iterate for each cube face
        {
            for (int j = 0; j < cubeMaps.Length; j++)
            {
                for (int m = 0; m < mipLevel; m++)
                {
                    CubemapFace face = (CubemapFace)i;
                    array.SetPixels(cubeMaps[j].GetPixels(face), face, j, m);
                }
            }
        }

        for (int j = 0; j < 6; j++)
        {
            //CubemapFace face = (CubemapFace)i;
            // array.SetPixels(textures[j].GetPixels(), face, j);
            for (int i = 0; i < mipLevel; i++)
            {
                //for()
                // Graphics.CopyTexture(textures[j], 0, i, array, j, i);
            }
        }

        array.Apply();
        AssetDatabase.CreateAsset(array, "Assets/CubemapArray.asset");
    }
    void Create()
    {
#if UNITY_EDITOR
        CubemapArray textureArray = new CubemapArray(6, textureList.Length, TextureFormat.RGB24, false);
        for (int i = 0; i < textureList.Length; i++)
        {
            Texture2D tex = (Texture2D)textureList[i];
            textureArray.SetPixels(tex.GetPixels(0), CubemapFace.PositiveX, 0);
        }
        textureArray.Apply();

        AssetDatabase.CreateAsset(textureArray, savePath);
        Debug.Log("Saved asset to " + savePath);
#endif
    }
    public static int Apply(IntPtr l)
    {
        int result;

        try
        {
            int num = LuaDLL.lua_gettop(l);
            if (num == 1)
            {
                CubemapArray cubemapArray = (CubemapArray)LuaObject.checkSelf(l);
                cubemapArray.Apply();
                LuaObject.pushValue(l, true);
                result = 1;
            }
            else if (num == 2)
            {
                CubemapArray cubemapArray2 = (CubemapArray)LuaObject.checkSelf(l);
                bool         updateMipmaps;
                LuaObject.checkType(l, 2, out updateMipmaps);
                cubemapArray2.Apply(updateMipmaps);
                LuaObject.pushValue(l, true);
                result = 1;
            }
            else if (num == 3)
            {
                CubemapArray cubemapArray3 = (CubemapArray)LuaObject.checkSelf(l);
                bool         updateMipmaps2;
                LuaObject.checkType(l, 2, out updateMipmaps2);
                bool makeNoLongerReadable;
                LuaObject.checkType(l, 3, out makeNoLongerReadable);
                cubemapArray3.Apply(updateMipmaps2, makeNoLongerReadable);
                LuaObject.pushValue(l, true);
                result = 1;
            }
            else
            {
                LuaObject.pushValue(l, false);
                LuaDLL.lua_pushstring(l, "No matched override function Apply to call");
                result = 2;
            }
        }
        catch (Exception e)
        {
            result = LuaObject.error(l, e);
        }
        return(result);
    }
Пример #5
0
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var width         = 64;
            var mipmapEnabled = true;
            var textureFormat = TextureFormat.RGBA32;
            var srgbTexture   = true;

            // Mark all input textures as dependency to the CubemapArray.
            // This causes the CubemapArray to get re-generated when any input texture changes or when the build target changed.
            for (var n = 0; n < m_Cubemaps.Count; ++n)
            {
                var source = m_Cubemaps[n];
                if (source != null)
                {
                    var path = AssetDatabase.GetAssetPath(source);
#if UNITY_2020_1_OR_NEWER
                    ctx.DependsOnArtifact(path);
#else
                    ctx.DependsOnSourceAsset(path);
#endif
                }
            }

#if !UNITY_2020_1_OR_NEWER
            // This value is not really used in this importer,
            // but getting the build target here will add a dependency to the current active buildtarget.
            // Because DependsOnArtifact does not exist in 2019.4, adding this dependency on top of the DependsOnSourceAsset
            // will force a re-import when the target platform changes in case it would have impacted any texture this importer depends on.
            var buildTarget = ctx.selectedBuildTarget;
#endif

            // Check if the input textures are valid to be used to build the texture array.
            var isValid = Verify(ctx, false);
            if (isValid)
            {
                // Use the texture assigned to the first slice as "master".
                // This means all other textures have to use same settings as the master texture.
                var sourceTexture = m_Cubemaps[0];
                width         = sourceTexture.width;
                textureFormat = sourceTexture.format;

                var sourceTexturePath = AssetDatabase.GetAssetPath(sourceTexture);
                var textureImporter   = (TextureImporter)AssetImporter.GetAtPath(sourceTexturePath);
                mipmapEnabled = textureImporter.mipmapEnabled;
                srgbTexture   = textureImporter.sRGBTexture;
            }

            CubemapArray cubemapArray;
            try
            {
                // Create the texture array.
                // When the texture array asset is being created, there are no input textures added yet,
                // thus we do Max(1, Count) to make sure to add at least 1 slice.
                cubemapArray = new CubemapArray(width, Mathf.Max(1, m_Cubemaps.Count), textureFormat, mipmapEnabled, !srgbTexture);
            }
            catch (System.Exception e)
            {
                Debug.LogException(e);
                ctx.LogImportError($"Import failed '{ctx.assetPath}'.", ctx.mainObject);

                isValid       = false;
                textureFormat = TextureFormat.RGBA32;
                cubemapArray  = new CubemapArray(width, Mathf.Max(1, m_Cubemaps.Count), textureFormat, mipmapEnabled, !srgbTexture);
            }

            cubemapArray.wrapMode   = m_WrapMode;
            cubemapArray.filterMode = m_FilterMode;
            cubemapArray.anisoLevel = m_AnisoLevel;

            if (isValid)
            {
                // If everything is valid, copy source textures over to the texture array.
                for (int face = 0; face < 6; face++)
                {
                    for (var n = 0; n < m_Cubemaps.Count; ++n)
                    {
                        var source = m_Cubemaps[n];
                        Graphics.CopyTexture(source, face, cubemapArray, face + (n * 6));
                    }
                }
            }
            else
            {
                // If there is any error, copy a magenta colored texture into every slice.
                // I was thinking to only make the invalid slice magenta, but then it's way less obvious that
                // something isn't right with the texture array. Thus I mark the entire texture array as broken.
                var errorTexture = new Cubemap(width, textureFormat, mipmapEnabled);
                try
                {
                    for (var face = 0; face < 6; ++face)
                    {
                        var errorPixels = errorTexture.GetPixels((CubemapFace)face);
                        for (var n = 0; n < errorPixels.Length; ++n)
                        {
                            errorPixels[n] = Color.magenta;
                        }
                        errorTexture.SetPixels(errorPixels, (CubemapFace)face);
                    }
                    errorTexture.Apply();

                    for (int face = 0; face < 6; face++)
                    {
                        for (var n = 0; n < cubemapArray.cubemapCount; ++n)
                        {
                            Graphics.CopyTexture(errorTexture, face, cubemapArray, face + (n * 6));
                        }
                    }
                }
                finally
                {
                    DestroyImmediate(errorTexture);
                }
            }

            // this should have been named "MainAsset" to be conform with Unity, but changing it now
            // would break all existing CubemapArray assets, so we don't touch it.
            cubemapArray.Apply(false, !m_IsReadable);
            ctx.AddObjectToAsset("CubemapArray", cubemapArray);
            ctx.SetMainObject(cubemapArray);

            if (!isValid)
            {
                // Run the verify step again, but this time we have the main object asset.
                // Console logs should ping the asset, but they don't in 2019.3 beta, bug?
                Verify(ctx, true);
            }
        }