Search Unity

Is it possible to export terrain splat maps?

Discussion in 'Editor & General Support' started by MrTaiChi, Feb 18, 2009.

  1. MrTaiChi

    MrTaiChi

    Joined:
    Oct 24, 2008
    Posts:
    18
    Is it possible to export terrain splat maps to an image format that I can open up in Photoshop? If so, how can this be done?

    Thanks
     
  2. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    You can modify them through script. You need to drag them from the terrain asset to the script in the editor. Then you can save it, modify it or replace it.
     
  3. MrTaiChi

    MrTaiChi

    Joined:
    Oct 24, 2008
    Posts:
    18
    Can you be more specific? I see the terrain asset with the splat maps in them. Where do I drag them? What script are you talking about? Thanks.
     
  4. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    This code should save a png file to you projectfolder (parent of the assets folder).
    Then you can modify it in Photoshop. The script can also replace an existing texture. Probably used when you want to view the changes you have done to the texture in Photoshop.
    Drag the splatmap to the splat variable.
    Warning: This code is not tested and I don't know if it will compile correctly, but if not, it will probably be a small error.
    Code (csharp):
    1. var splat : Texture2D;
    2. var from : Texture2D;
    3. function save (tex : Texture2D) {
    4.     var bytes = tex.EncodeToPNG();
    5.     File.WriteAllBytes(Application.dataPath + "/../splatmap.png", bytes);
    6. }
    7.  
    8. function Replace (tex : Texture2d, from : Texture2D) {
    9.     tex.Resize (from.width, from.height, from.format, true);
    10.     pixels = from.GetPixels (0);
    11.     tex.SetPixels (pixels);
    12.     tex.Apply;
    13. }
     
  5. MrTaiChi

    MrTaiChi

    Joined:
    Oct 24, 2008
    Posts:
    18
    Thanks man. I was able to save it out. This is my test code.



    Code (csharp):
    1.  
    2. function Awake() {
    3.    
    4.     try {
    5.      
    6.        var bw : BinaryWriter = new BinaryWriter(File.Open("splatmap.png", FileMode.Create));
    7.        var pngBytes = splat.EncodeToPNG();
    8.        bw.Write(pngBytes);
    9.      
    10.        bw.Close();
    11.        
    12.     }
    13.     catch (err) {
    14.          Debug.Log("Save Error");
    15.     }  
    16. }
    17.  
     
  6. KEMBL

    KEMBL

    Joined:
    Apr 16, 2009
    Posts:
    181
    One more code example for export terrain splat texture from Assets menu. Realy you can export ordinary textures also if needs.

    How to use:

    1) Place this script on Assets\Editor dir in your project dir, call it like Export_texture.js or what ever you prefer.

    2) Click on texture for export in Project tab

    3) Go Assets / Export Texture menu

    4) File with texture appear in Assets dir with name exported_texture.png, you can change it in script body.

    Code (csharp):
    1.  
    2. // For save splatmap as PNG file.
    3. import System.IO;
    4.  
    5. @MenuItem("Assets/Export Texture")
    6.  
    7. static function Apply () {
    8.    
    9.    var texture : Texture2D = Selection.activeObject as Texture2D;
    10.    if (texture == null)
    11.    {
    12.       EditorUtility.DisplayDialog("Select Texture", "You Must Select a Texture first!", "Ok");
    13.       return;
    14.    }
    15.    
    16.    var bytes = texture.EncodeToPNG();
    17.    File.WriteAllBytes(Application.dataPath + "/exported_texture.png", bytes);
    18. }
    19.  
     
    Arkade likes this.
  7. kr1stov

    kr1stov

    Joined:
    Aug 14, 2009
    Posts:
    4
    great. really helped me. thx
     
  8. RoelBartstra

    RoelBartstra

    Joined:
    Oct 13, 2009
    Posts:
    20
    I have exported the splats but when I open them in photoshop and save them, it breaks the file. Is there any solution for this? I think it's because of the alpha...
     
  9. ThreeDeeNut

    ThreeDeeNut

    Joined:
    Apr 18, 2013
    Posts:
    10
    KEMBL, I am stuck. I can't find where the splatmap is so I can't select it and therefore I can't use your script to export it. Can you please tell me where the default location is to find the splatmap in Unity 4.2? Thanks.
     
  10. sonicviz

    sonicviz

    Joined:
    May 19, 2009
    Posts:
    1,051
    Hi,
    I just bought this, looks great.

    Dumb question:
    The main reason I bought it was to get rid of the ugly terrain tiling issue with some landscapes I bought from the asset store using default unity shader. I've run through the doc, converted the splat maps from existing terrain, and did the texture blending color and generated a new material.

    But...still getting ugly texture tiling artifacts.
    Can't see anything specific for this issue in the manual (maybe I missed it)

    Is there a simple process for doing this?

    I gather it's the Multi UV Mixing but playing with the settings doesn't seen to do anything.
    I have to recreate the material each time right?
     
    Last edited: Jun 5, 2015
  11. Deleted User

    Deleted User

    Guest

  12. Reshima

    Reshima

    Joined:
    Dec 10, 2012
    Posts:
    51
    For reference, if you need to select a terrain and export all alphamaps as PNG, here's how to do it:

    Code (CSharp):
    1. using System.IO;
    2. using UnityEditor;
    3. using UnityEngine;
    4.  
    5. public class ExportSplatmap : MonoBehaviour
    6. {
    7.     [MenuItem("Terrain/Export Splatmap...")]
    8.     static void Export()
    9.     {
    10.         Terrain terrain = Selection.activeObject as Terrain;
    11.         if (!terrain)
    12.         {
    13.             terrain = Terrain.activeTerrain;
    14.             if (!terrain)
    15.             {
    16.                 Debug.Log("Could not find any terrain. Please select or create a terrain first.");
    17.                 return;
    18.             }
    19.         }
    20.  
    21.         string path = EditorUtility.SaveFolderPanel("Choose a directory to save the alpha maps:", "", "");
    22.  
    23.         if (path != null && path.Length != 0)
    24.         {
    25.             path = path.Replace(Application.dataPath, "Assets");
    26.             TerrainData terrainData = terrain.terrainData;
    27.             int alphaMapsCount = terrainData.alphamapTextureCount;
    28.  
    29.             for (int i = 0; i < alphaMapsCount; i++)
    30.             {
    31.                 Texture2D tex = terrainData.GetAlphamapTexture(i);
    32.                 byte[] pngData = tex.EncodeToPNG();
    33.                 if (pngData != null)
    34.                 {
    35.                     File.WriteAllBytes(path + "/" + tex.name + ".png", pngData);
    36.                 }
    37.                 else
    38.                 {
    39.                     Debug.Log("Could not convert " + tex.name + " to png. Skipping saving texture.");
    40.                 }
    41.             }
    42.         }
    43.     }
    44. }
    45.  
    Each alphamap will be on a color channel (Red, Green, Blue, Alpha) of the PNG image. If the terrain has more than 4 textures, it will export additional images.