Texture2D CreateTexture(Terrain t, Texture2D tex, string texNamePostfix, Color defaultColor)
        {
            // find/create manager
            var mgr = t.GetComponent <MicroSplatTerrain>();

            if (mgr == null)
            {
                return(null);
            }

            // if we still don't have a texture, create one
            if (tex == null)
            {
                tex = new Texture2D(t.terrainData.alphamapWidth, t.terrainData.alphamapHeight, TextureFormat.RGBA32, false, true);

                for (int x = 0; x < tex.width; ++x)
                {
                    for (int y = 0; y < tex.height; ++y)
                    {
                        tex.SetPixel(x, y, defaultColor);
                    }
                }
                tex.Apply();
                tex.wrapMode = TextureWrapMode.Clamp;
                var path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData);
                path += "/" + t.name + texNamePostfix + ".png";
                tex   = SaveTexture(path, tex);
                mgr.Sync();
            }


            return(tex);
        }
Пример #2
0
        void CreateTexture(Terrain t, string texNamePrefix = "", Material customMat = null)
        {
            // find/create manager
            var mgr = t.GetComponent <MicroSplatTerrain>();

            if (mgr == null)
            {
                return;
            }
            Texture2D tex = mgr.streamTexture;

            // if we still don't have a texture, create one
            if (tex == null)
            {
                tex = new Texture2D(t.terrainData.alphamapWidth, t.terrainData.alphamapHeight, TextureFormat.ARGB32, false, true);
                mgr.streamTexture = tex;
                Color c = new Color(0, 0, 0, 0);
                for (int x = 0; x < tex.width; ++x)
                {
                    for (int y = 0; y < tex.height; ++y)
                    {
                        tex.SetPixel(x, y, c);
                    }
                }
                tex.Apply();
                tex.wrapMode = TextureWrapMode.Clamp;
                var path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData);
                path += "/" + t.name + "_stream_data.png";
                mgr.streamTexture = SaveTexture(path, tex);
            }

            mgr.Sync();
        }
        Texture2D CreateTexture(Terrain t, Texture2D tex, string texNamePostfix, Color defaultColor, bool linear)
        {
            // find/create manager
            var mgr = t.GetComponent <MicroSplatTerrain>();

            if (mgr == null)
            {
                return(null);
            }

            // if we still don't have a texture, create one
            if (tex == null)
            {
                tex = new Texture2D(t.terrainData.alphamapWidth, t.terrainData.alphamapHeight, TextureFormat.RGBA32, false, linear);

                for (int x = 0; x < tex.width; ++x)
                {
                    for (int y = 0; y < tex.height; ++y)
                    {
                        tex.SetPixel(x, y, defaultColor);
                    }
                }
                tex.Apply();
                tex.wrapMode = TextureWrapMode.Clamp;
                var path = MicroSplatUtilities.RelativePathFromAsset(t.terrainData);
                path += "/" + t.name + texNamePostfix + ".png";
                tex   = SaveTexture(path, tex);
                var ai = AssetImporter.GetAtPath(path);
                var ti = ai as TextureImporter;
                if (ti != null)
                {
                    ti.sRGBTexture        = !linear;
                    ti.textureCompression = TextureImporterCompression.Uncompressed;
                    var ftm = ti.GetDefaultPlatformTextureSettings();
                    ftm.format = TextureImporterFormat.RGBA32;
                    ti.SetPlatformTextureSettings(ftm);

                    ti.mipmapEnabled = true;
                    ti.wrapMode      = TextureWrapMode.Clamp;
                    ti.SaveAndReimport();
                }
                mgr.Sync();
                EditorUtility.SetDirty(mgr);
            }


            return(tex);
        }
        void CompressTerrainSplats(MicroSplatTerrain t)
        {
            int splatCount = t.terrain.terrainData.alphamapTextureCount;

            // write out
            for (int i = 0; i < splatCount; ++i)
            {
                var tex  = t.terrain.terrainData.GetAlphamapTexture(i);
                var path = MicroSplatUtilities.RelativePathFromAsset(t);
                path += "/" + t.name + "_splat" + i + ".tga";
                System.IO.File.WriteAllBytes(path, tex.EncodeToTGA());
            }

            AssetDatabase.Refresh();
            // load and adjust importer
            for (int i = 0; i < splatCount; ++i)
            {
                var path = MicroSplatUtilities.RelativePathFromAsset(t);
                path += "/" + t.name + "_splat" + i + ".tga";

                var tex = CompressTexture(path, false);

                if (i == 0)
                {
                    t.customControl0 = tex;
                }
                else if (i == 1)
                {
                    t.customControl1 = tex;
                }
                else if (i == 2)
                {
                    t.customControl2 = tex;
                }
                else if (i == 3)
                {
                    t.customControl3 = tex;
                }
                else if (i == 4)
                {
                    t.customControl4 = tex;
                }
                else if (i == 5)
                {
                    t.customControl5 = tex;
                }
                else if (i == 6)
                {
                    t.customControl6 = tex;
                }
                else if (i == 7)
                {
                    t.customControl7 = tex;
                }
            }
            EditorUtility.SetDirty(t);

            MicroSplatKeywords keywords = MicroSplatUtilities.FindOrCreateKeywords(t.templateMaterial);

            if (!keywords.IsKeywordEnabled("_CUSTOMSPLATTEXTURES"))
            {
                keywords.EnableKeyword("_CUSTOMSPLATTEXTURES");
                MicroSplatShaderGUI.MicroSplatCompiler compiler = new MicroSplatShaderGUI.MicroSplatCompiler();
                compiler.Compile(t.templateMaterial);
                MicroSplatTerrain.SyncAll();
            }

            // destructive operation
            t.terrain.terrainData.alphamapResolution = 16;
        }