public void Bake()
        {
            if (baking)
            {
                reserveBaking = true;
                return;
            }

            var terrainPath = AssetDatabase.GetAssetPath(targetTerrain.terrainData);
            var terrainDir  = Path.GetDirectoryName(terrainPath);
            var terrainName = Path.GetFileNameWithoutExtension(terrainPath);
            var bakeDir     = Path.Combine(terrainDir, terrainName + "_MassiveGrass");

            Directory.CreateDirectory(bakeDir);
            Debug.Log("BakeDir: " + bakeDir);

            Debug.Log("Baking");
            baking = true;
            foreach (var texture2D in bakedAlphaMaps)
            {
                DestroyImmediate(texture2D);
            }

            bakedAlphaMaps.Clear();

            // Bake
            var terrainData = targetTerrain.terrainData;
            var w           = terrainData.alphamapWidth;
            var h           = terrainData.alphamapHeight;
            var layers      = terrainData.alphamapLayers;

            for (var i = 0; i < layers; i++)
            {
                var texturePath = Path.Combine(bakeDir, "alphamap" + i + ".png");
                AssetDatabase.DeleteAsset(texturePath);
                var     texture = AlphamapBaker.CreateAndBake(targetTerrain, new [] { i });
                byte [] pngData = texture.EncodeToPNG();
                File.WriteAllBytes(texturePath, pngData);
                AssetDatabase.ImportAsset(texturePath);
                Debug.Log("load " + texturePath);

                var importer = AssetImporter.GetAtPath(texturePath) as TextureImporter;
                importer.isReadable = true;
                AssetDatabase.ImportAsset(texturePath, ImportAssetOptions.ForceUpdate);

                var tex = AssetDatabase.LoadAssetAtPath <Texture2D>(texturePath);
                bakedAlphaMaps.Add(tex);
            }

            baking = false;
            Debug.Log("Baking Done");
            if (reserveBaking)
            {
                reserveBaking = false;
                Bake();
            }
        }
Ejemplo n.º 2
0
        public void Bake()
        {
            foreach (var texture2D in alphaMaps)
            {
                DestroyImmediate(texture2D);
            }
            alphaMaps.Clear();

            // Bake
            var terrainData = targetTerrain.terrainData;
            var w           = terrainData.alphamapWidth;
            var h           = terrainData.alphamapHeight;
            var layers      = terrainData.alphamapLayers;

            for (var i = 0; i < layers; i++)
            {
                var texture = AlphamapBaker.CreateAndBake(targetTerrain, new [] { i });
                alphaMaps.Add(texture);
            }
        }
Ejemplo n.º 3
0
        public void Bake()
        {
            if (baking)
            {
                reserveBaking = true;
                return;
            }

            Debug.Log("Baking");
            baking = true;
            foreach (var texture2D in bakedAlphaMaps)
            {
                DestroyImmediate(texture2D);
            }

            bakedAlphaMaps.Clear();

            // Bake
            var terrainData = targetTerrain.terrainData;
            var w           = terrainData.alphamapWidth;
            var h           = terrainData.alphamapHeight;
            var layers      = terrainData.alphamapLayers;

            for (var i = 0; i < layers; i++)
            {
                var texture = AlphamapBaker.CreateAndBake(targetTerrain, new [] { i });
                bakedAlphaMaps.Add(texture);
            }

            baking = false;
            Debug.Log("Baking Done");
            if (reserveBaking)
            {
                reserveBaking = false;
                Bake();
            }
        }