x


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.

more ▼

asked Nov 19 '09 at 08:36 AM

duhprey gravatar image

duhprey
43 1 1 7

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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!

more ▼

answered Nov 19 '09 at 12:48 PM

duck gravatar image

duck ♦♦
41k 92 148 415

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 SetBaseMapDirty() which I called after modifying the texture and that worked! So thanks for the pointers!

Nov 20 '09 at 02:27 AM duhprey
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 19 '09 at 10:21 AM

jonas echterhoff gravatar image

jonas echterhoff ♦♦
9.8k 7 23 104

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.

Nov 19 '09 at 12:57 PM jonas echterhoff ♦♦
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5099
x2210
x1478
x200
x28

asked: Nov 19 '09 at 08:36 AM

Seen: 5904 times

Last Updated: Mar 27 '10 at 11:56 AM