Terrain Splatmap problem in Edit Mode

Hello everybody,

I’m creating a mass terrain importer for my project here, and I managed to read a folder and get all the heightmaps on it, create the terrainData, apply the heightmaps to it, add all the splatPrototypes for it, read the splatmap (a grayscale image) for each splat layer and create the float array required for SetAlphaMap method of terrainData. All works pretty nicely, the only problem is, the splatting doesn’t get applied. All I get is a terrainData saved in my asset database with all the texture set but the first texture spreading all over the terrain.

I read around that I needed to call the Flush method of the Terrain to apply the changes, but I’m creating no terrain in the scene, I’m creating terrainData objects and saving them in the AssetDatabase, so I can’t call Flush.

How can I make my splatmaps set with SetAlphaMap stick to the terrain?

This is the code snippet that creates the splats for a terrain and save it in the AssetDatabase:

float[, ,] splatarray = null;

        SplatPrototype[] prototypes = null;

        if (splats.Count > 0)

        {

            splatarray = new float[(int)heightmapsize.x, (int)heightmapsize.y, splats.Count];

            prototypes = new SplatPrototype[splats.Count];

        }

        for (int k = 0; k < splats.Count; k++)

        {

            SplatMapInfo splat = splats[k];

            // Load splatmap file

            string splatfile = string.Format(splat.Filemask, xvalue, yvalue);

            string[] splatfiles = System.IO.Directory.GetFiles(imagesPath, splatfile + ".*");

            System.Uri uri = new System.Uri(splatfiles[0]);

            WWW imageloader = new WWW(uri.AbsoluteUri);

			loadImageUrl(imageloader);

            Texture2D t = imageloader.texture;

            //imageloader.LoadImageIntoTexture(t);

            // Parse splat image and populate array

            for (int v = 0; v < heightmapsize.y; v++)

            {

                for (int u = 0; u < heightmapsize.x; u++)

                {

                    Color c = t.GetPixel(u, v);

                    splatarray[u, v, k] = c.grayscale;

                }

            }

			DestroyImmediate(t);

            // Set splat prototype for this splat layer

            SplatPrototype prototype = new SplatPrototype();

            prototype.texture = splat.texture;

            prototype.tileOffset = new Vector2(splat.TileOffsetX, splat.TileOffsetY);

            prototype.tileSize = new Vector2(splat.TileSizeX, splat.TileSizeY);

            prototypes[k] = prototype;

        }

        // If we have splats, assign them

        if (splats.Count > 0)

        {

            tdata.splatPrototypes = prototypes;

            tdata.SetAlphamaps(0, 0, splatarray);

        }

		tdata.RefreshPrototypes();

        // Save this asset to the Asset Database

        string assetname = string.Format(terrainMask, xvalue, yvalue);

        AssetDatabase.CreateAsset(tdata, "Assets/" + assetname + ".asset");

Okay, I did not get any help, and I understand this is a troublesome area and I can understand if no one knew this at all, or just there are so few who knew it that these ppl never came to see this topic, but I found the solution for this after struggling for 3 days with it.

The problem with it is the AssetDatabase.CreateAsset() method.
If you try to completely populate a terrainData object and THEN save it, it’ll discard your splatmap on it (it does something very weird I can’t explain, but the result is that you get a splatmap that covers your terrain with the 1st texture you set in the splat prototype).

All you have to do is to create your terrainData, populate the heights on it, THEN save it to the AssetDatabase, THEN retrieve another terrainData object from the saved asset with AssetDatabase.LoadAssetAtPath() and then use that loaded terrainData object to populate splatmaps, detail maps and tree instances. After this, you save the AssetDatabase by calling AssetDatabase.SaveAssets(); and then AssetDatabase.Refresh();

So, the code snippet is:

			// Create a new TerrainData object as we'll SetHeight it.
			TerrainData tdata = new TerrainData ();
			tdata.heightmapResolution = 513;
			tdata.size = terrainsize;
			tdata.alphamapResolution = 513;
			tdata.baseMapResolution = 513;
			
			// Generate the heightmap
			tdata.SetHeights (0, 0, heightmapData);



            AssetDatabase.CreateAsset(tdata, "Assets/terrain.asset");
            AssetDatabase.SaveAssets();



			// Now if we have splatmaps for this terrain, time to load them
			float[,,] splatarray = null;
			SplatPrototype[] prototypes = null;
			LoadSplats (0, 0, ref splatarray, ref prototypes);
			
            TerrainData td = (TerrainData)AssetDatabase.LoadAssetAtPath("Assets/terrain.asset", typeof(TerrainData));

		
			td.splatPrototypes = prototypes;
			td.RefreshPrototypes ();
			td.SetAlphamaps (0, 0, splatarray);

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();