Exemple #1
0
        /*
         * This function gathers material data from the objects in the scene graph
         * rooted by the given AV object.  This will create associations between
         * the material and texturing properties and the objects that use them,
         * which then can be browsed or searched using the collection's other functions.
         * \param[in] scene_root The root node of the scene to gather material
         * information from.
         */
        public void GatherMaterials(NiAVObject scene_root)
        {
            if (scene_root == null)
            {
                throw new Exception("MatTexCollection::GatherMaterials was called on a null scene root.");
            }
            //Check and see if this object is a geometry object
            if (scene_root.IsDerivedType(NiGeometry.TYPE))
            {
                //Check and see if this geometry's unique combination of material and texture properties has already been found
                if (GetMaterialIndex(scene_root) == NO_MATERIAL)
                {
                    //Material was not already found.  Look for material/texture related properties.
                    var mat     = scene_root.GetPropertyByType(NiMaterialProperty.TYPE);
                    var texing  = scene_root.GetPropertyByType(NiTexturingProperty.TYPE);
                    var tex     = scene_root.GetPropertyByType(NiTextureProperty.TYPE);
                    var multi   = scene_root.GetPropertyByType(NiMultiTextureProperty.TYPE);
                    var spec    = scene_root.GetPropertyByType(NiSpecularProperty.TYPE);
                    var alpha   = scene_root.GetPropertyByType(NiAlphaProperty.TYPE);
                    var stencil = scene_root.GetPropertyByType(NiStencilProperty.TYPE);
                    //Make sure at least one isn't null
                    if (mat != null || texing != null || tex != null || multi != null || stencil != null)
                    {
                        //One isn't null, so create a new Material
                        var matC     = mat as NiMaterialProperty;
                        var texingC  = texing as NiTexturingProperty;
                        var texC     = tex as NiTextureProperty;
                        var multiC   = multi as NiMultiTextureProperty;
                        var specC    = spec as NiSpecularProperty;
                        var alphaC   = alpha as NiAlphaProperty;
                        var stencilC = stencil as NiStencilProperty;
                        //First, check if the material's textures have been found yet
                        if (texingC != null)
                        {
                            for (var i = 0; i < 8; ++i)
                            {
                                if (texingC.HasTexture(i))
                                {
                                    var td = texingC.GetTexture(i);
                                    if (td.source != null)
                                    {
                                        var index = GetTextureIndex(td.source);
                                        if (index == NO_TEXTURE)
                                        {
                                            //Texture has not yet been found.  Create a new one.
                                            textures.Add(new TextureWrapper(td.source));
                                        }
                                    }
                                }
                            }
                        }
                        else if (texC != null)
                        {
                            var image = texC.GetImage();
                            if (image != null)
                            {
                                var index = GetTextureIndex(image);
                                if (index == NO_TEXTURE)
                                {
                                    //Texture has not yet been found.  Create a new one.
                                    textures.Add(new TextureWrapper(image));
                                }
                            }
                        }
                        //TODO: Implement this for NiMultiTextureProperty as well
                        materials.push_back(new MaterialWrapper(matC, texingC, texC, multiC, specC, alphaC, stencilC, this));
                    }
                }
                //Done with this branch, so return.
                return;
            }

            //If this object is a NiNode, then call this function on its children
            var node = scene_root as NiNode;

            if (node != null)
            {
                var children = node.GetChildren();
            }
            for (var i = 0; i < children.Length; ++i)
            {
                GatherMaterials(children[i]);
            }
        }