Instancing

Instancing is an optimization technique to render a large number of objects with different transforms but share the same material and model. InstancedModel class enables instancing for both models and primitives.

InstancedModel.Template specifies the target model and material to be instanced. It can be a Model, a Primitive or anything that implements ISupportInstancing. The following example shows how to make use of the Cross primitive to create a grass field:

image

<InstancedModel ViewDistance="20">        
    <DistributionMap.DistributionMap>
        <DistributionMap Step="1" RandomizeRotation="True" VerticalScale="0.5~2" HorizontalScale="0.6~1.4" Density="10000" Texture="{ExternalReference Assets/box.dds}" />
    </DistributionMap.DistributionMap>
    <Cross Tessellation="2">
        <MaterialGroup Texture="{ContentReference Assets/fgrass}" TwoSided="True">
            <DiffuseMaterialPart />
            <AlphaTestMaterialPart ReferenceAlpha="200" />
        </MaterialGroup>
    </Cross>        
</InstancedModel>

 

The ViewDistance property defines the visible range of each instance. The DistributionMap is a built-in XAML attached property that automatically generates an array of transformation matrices based on a gray-scale texture that describes the distribution of the grass. Notice the ExternalReference tag used by the Texture property, it is different from a ContentReference. A ContentReference refers to  a compiled asset, while an ExternalReference loads the referenced asset and compile into a different object format. In this case, the “box.dds” file is loaded and compiled into a transform matrix array.

To extend beyond the capability of a distribution map, create a custom attached property and set the transforms of the instanced model using InstancedModel.SetTransforms method.

An instanced model has to be rendered using a MaterialGroup that contains an InstancedMaterialPart. When authoring an instanced model in XAML, the InstancedMaterialPart is optional since a default one will be added to the group if it is not found at build time.

Last edited Sep 28, 2012 at 9:15 AM by yufeih, version 9

Comments

No comments yet.