Terrain Splatting in code

Jan 22, 2013 at 12:27 AM


I was just wondering if it's possible to implement terrain splatting as shown in the terrain documentation via c# rather than xaml? I want to load different maps and currently I'm limited to materials defined in the xaml.

If so could you provide some sample code of how it would look like?


Jan 22, 2013 at 3:16 AM

You are not able to create a MaterialGroup or MaterialPaintGroup in code at game runtime, because the shader needs to be compiled at build time.

However, you can load a MaterialGroup and then modify the textures and properties in C#.

Using the MaterialGroup.Find method, you are able to retrieve the contained MaterialPart, and modify the textures associated with that particular MaterialPart just like how it's done in XAML.

Let me know if it doesn't work out.

Jan 22, 2013 at 4:06 AM
Edited Jan 22, 2013 at 4:07 AM

That would be sufficient as I all I want to do is specify the MaskTextures and set the MaskTextureScale property. I tried to get the properties but wasn't able to work out how to access or set them.

My xaml looks like:


    <Surface Name="Terrain" 

        <MaterialGroup MaterialPaintGroup.MaskTextureScale="32, 32" Name="TerrainSplatter">

                    <ExternalReference />

                <DiffuseMaterialPart Texture="{ContentReference Textures/board2}" DiffuseColor=".6,.6,.6" />
                <SpecularMaterialPart SpecularColor="0.2, 0.5, 0.5" SpecularPower="64" />

                <DiffuseMaterialPart Texture="{ContentReference Textures/water}" />
                <SpecularMaterialPart SpecularColor="0.5, 0.5, 0.5" SpecularPower="64" />
                <NormalMapMaterialPart NormalMap="{ContentReference Textures/water_n}" />


I tried to get the material group using:


var matGroup = scene.FindName<MaterialGroup>("TerrainSplatter");


but it doesn't seem to expose the properties I need. Maybe I'm doing it wrong.

Are you able to provide any examples that show how to access and modify the MaskTextures and MaskTextureScale values?

Jan 22, 2013 at 9:22 AM

I'm not sure if you are able to find the material using


var matGroup = scene.FindName<MaterialGroup>("TerrainSplatter");


A safer way is to Find the Surface first then access the Material property of the surface.


Use the static MaterialPaintGroup.GetMaskTextures/SetMaskTextures and MaterialPaintGroup.GetMaskTextureScale/SetMaskTextureScale

Note that the mask textures are baked into the 4 channels when you get the the textures at runtime.