- Home /
Modifying Terrain Splat Texture at Runtime
I'm trying to create a Conway's Game Of Life as a texture on the terrain (the life in this case is "grass"). I attempted creating a script to modify the texture and dragged SplatAlpha 0 from the terrainData into the texture. This however, doesn't appear to modify the splat properly until I go back into the texture painter and suddenly the changes pop in. This may also be causing Unity to crash...
Is there a proper way to do this?
Thanks.
Answer by duck · Nov 19, 2009 at 12:48 PM
First of all (and it sounds like you probably know this), there's no need to actually modify any Texture objects in order to modify the splat maps on a terrain. They're stored internally as a float[,,] array, and you can edit these values directly.
So, assuming you're calling:
terrainData.SetAlphamaps(0, 0, yourNewSplatmapData);
(where yourNewSplatmapData is a float[,,] with valid data inside), I think you then need to do this:
terrain.terrainData = terrainData;
terrain.Flush();
Note - because these are undocumented commands, and UT may upgrade or change the terrain engine in the future, these functions may not work in future versions of the webplayer plugin!
good luck!
I didn't know about that! Thanks. I've been coding unityscript in gvim and can't easily see undocumented functions...
I started to try this, but after I setup a C# version I found SetBase$$anonymous$$apDirty() which I called after modifying the texture and that worked! So thanks for the pointers!
Answer by jonas-echterhoff · Nov 19, 2009 at 10:21 AM
Some thoughts, without having tried this:
-Unity only uses the splat maps directly for rendering for close by terrain segments. For further away parts, it uses a base map, which is precomposed from the splat maps. I don't think there is a runtime API which lets you regenerate the base map. You could, however change the base map distance to be very high, using Terrain.basemapDistance.
-Make sure you are actually modifying the splat maps (by rendering them onto some object). You have to call Texture2D.Apply() after using SetPixels.
Duck's answer below actually makes much more sense. I thought the SetAlphamaps() method was only available to the editor, but he is right, that is the best way to modify splat maps.
Your answer
Follow this Question
Related Questions
Assigning UV Map to model at runtime 0 Answers
How can I perform some action based on the terrain texture currently under my player? 4 Answers
How to automatically apply different textures on terrain based on height? 5 Answers
Change terrain texture colour 1 Answer
Unity Terrain Texture Resolutions 1 Answer