IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] mi)
 {            
     IModelo model = new CustomModel(factory, mi);
     IPhysicObject po = new TriangleMeshObject(model, Vector3.Zero, Matrix.Identity, Vector3.One, MaterialDescription.DefaultBepuMaterial());            
     IShader shader = new ForwardXNABasicShader();
     ForwardMaterial dm = new ForwardMaterial(shader);
     return new IObject[] {new IObject(dm, model, po)};
 }
        public ModelLoaderData Load(Engine.GraphicFactory factory, Engine.GraphicInfo info, string Name)
        {
            modelNames.Add(Name);
            ModelLoaderData ModelLoaderData = new ModelLoaderData();
            Model           model           = factory.GetModel(Name);

            Matrix[] m = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(m);

            for (int i = 0; i < model.Meshes.Count; i++)
            {
                String name = model.Meshes[i].Name.Substring(5);

                for (int j = 0; j < model.Meshes[i].MeshParts.Count; j++)
                {
                    Matrix tr = m[model.Meshes[i].ParentBone.Index];

                    Vector3    scale;
                    Vector3    pos;
                    Quaternion ori;
                    tr.Decompose(out scale, out ori, out pos);

                    ObjectInformation mi = new ObjectInformation();
                    mi.modelName     = name;
                    mi.meshPartIndex = j;
                    mi.meshIndex     = i;
                    mi.position      = pos;
                    mi.scale         = scale;
                    mi.rotation      = ori;

                    mi.ellasticity     = -1;
                    mi.dinamicfriction = -1;
                    mi.staticfriction  = -1;
                    mi.collisionType   = "TriangleMesh";
                    mi.mass            = 10;

                    ModelBuilderHelper.Extract(m, model.Meshes[i].MeshParts[j], out mi.batchInformation);
                    mi.batchInformation.ModelLocalTransformation = m[model.Meshes[i].ParentBone.Index];

                    mi.textureInformation = new Modelo.TextureInformation(false, factory, null, null, null, null);
                    mi.textureInformation.LoadTexture();

                    Effect      effect      = model.Meshes[i].MeshParts[j].Effect;
                    BasicEffect BasicEffect = effect as BasicEffect;
                    if (BasicEffect != null)
                    {
                        mi.textureInformation.SetTexture(BasicEffect.Texture, TextureType.DIFFUSE);
                    }
                    ModelLoaderData.ModelMeshesInfo.Add(mi);
                }
            }


            return(ModelLoaderData);
        }
        public ModelLoaderData Load(Engine.GraphicFactory factory, Engine.GraphicInfo info, string Name)
        {
            modelNames.Add(Name);
            ModelLoaderData ModelLoaderData = new ModelLoaderData();
            Model model = factory.GetModel(Name);
            Matrix[] m = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(m);            
                        
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                String name = model.Meshes[i].Name.Substring(5);                

                for (int j = 0; j < model.Meshes[i].MeshParts.Count; j++)
                {                        
                        Matrix tr = m[model.Meshes[i].ParentBone.Index];

                        Vector3 scale;
                        Vector3 pos;
                        Quaternion ori;
                        tr.Decompose(out scale, out ori, out pos);

                        ObjectInformation mi = new ObjectInformation();
                        mi.modelName = name;
                        mi.meshPartIndex = j;
                        mi.meshIndex = i;
                        mi.position = pos;
                        mi.scale = scale;
                        mi.rotation = ori;

                        mi.ellasticity = -1;
                        mi.dinamicfriction = -1;
                        mi.staticfriction = -1;
                        mi.collisionType = "TriangleMesh";
                        mi.mass = 10;

                        ModelBuilderHelper.Extract(m, model.Meshes[i].MeshParts[j], out mi.batchInformation);                        
                        mi.batchInformation.ModelLocalTransformation = m[model.Meshes[i].ParentBone.Index];

                        mi.textureInformation = new Modelo.TextureInformation(false, factory, null, null, null, null);
                        mi.textureInformation.LoadTexture();

                        Effect effect = model.Meshes[i].MeshParts[j].Effect;                
                        BasicEffect BasicEffect = effect as BasicEffect;
                        if(BasicEffect != null)
                            mi.textureInformation.SetTexture(BasicEffect.Texture, TextureType.DIFFUSE);
                        ModelLoaderData.ModelMeshesInfo.Add(mi);                    
                }
            }


            return ModelLoaderData;
        }
        IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] oi)
        {
            IObject[] objs = new IObject[oi.Count()];
            int i = 0;
            foreach (var mi in oi)
            {
                mi.batchInformation.ModelLocalTransformation = Matrix.Identity;
                IModelo model = new CustomModel(factory, mi.modelName, mi.batchInformation, mi.textureInformation, mi.meshIndex, mi.meshPartIndex);
                //IPhysicObject po = new TriangleMeshObject(model, Vector3.Zero, Matrix.Identity, Vector3.One, MaterialDescription.DefaultBepuMaterial());
                GhostObject po = new GhostObject(mi.position, Matrix.CreateFromQuaternion(mi.rotation), mi.scale);
                IShader shader = new ForwardXNABasicShader();
                ForwardMaterial dm = new ForwardMaterial(shader);
                IObject obj = new IObject(dm, model, po);
                objs[i++] = obj;
            }

            return objs;
        }
        public ModelLoaderData Load(GraphicFactory factory, GraphicInfo ginfo, String Name)
        {            
            ModelLoaderData elements = new ModelLoaderData();
            Dictionary<String, XmlModelMeshInfo> infos = new Dictionary<string, XmlModelMeshInfo>();
            Dictionary<String, targetInfo> targets = new Dictionary<string, targetInfo>();
            Dictionary<String, SpotLightInformation> spotLights = new Dictionary<string, SpotLightInformation>();
            //Dictionary<String, ConstraintInformation> constraints = new Dictionary<string, ConstraintInformation>();
            Dictionary<String, CameraInfo> cameras = new Dictionary<string, CameraInfo>();
            Dictionary<String, ParticleInfo> particles = new Dictionary<string, ParticleInfo>();


            SerializerHelper.ChangeDecimalSymbolToPoint();
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(path + Name + ".xml");
            XmlNodeList worldNode = xDoc["SCENE"].ChildNodes;

            foreach (XmlNode node in worldNode)
            {


                if (node.Name == "Constraint")
                {

                    ConstraintInfo cinfo = new ConstraintInfo();


                    cinfo.Name = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);
                    XmlElement type = node["type"];
                    if (type != null)
                    {
                        cinfo.type = SerializerHelper.DeserializeAttributeBaseType<String>("value", type);
                    }

                    XmlElement child = node["child"];
                    if (child != null)
                    {
                        cinfo.bodyA = SerializerHelper.DeserializeAttributeBaseType<String>("name", child);
                    }

                    XmlElement parent = node["parent"];
                    if (parent != null)
                    {
                        cinfo.bodyB = SerializerHelper.DeserializeAttributeBaseType<String>("name", parent);
                    }

                    XmlElement breakable = node["isBreakable"];
                    if (breakable != null)
                    {

                        cinfo.breakable = SerializerHelper.DeserializeAttributeBaseType<bool>("value", breakable);

                    }



                    Vector3 pos = SerializerHelper.DeserializeVector3("position", node);
                    cinfo.Position = new Vector3(pos.X, pos.Y, pos.Z);


                    elements.ConstraintInfo.Add(cinfo);

                }
                if (node.Name == "particle")
                {
                    ParticleInfo pinfo = new ParticleInfo();
                    pinfo.Name = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);
                    pinfo.Position = SerializerHelper.DeserializeVector3("position", node);
                    pinfo.Orientation = SerializerHelper.DeserializeQuaternion("rotation", node);

                    XmlElement mass = node["type"];
                    if (mass != null)
                    {
                        pinfo.Type = SerializerHelper.DeserializeAttributeBaseType<String>("value", mass);
                    }

                    elements.ParticleInfo.Add(pinfo);
                }
                if (node.Name == "body")
                {
                    XmlModelMeshInfo info = new XmlModelMeshInfo();
                    info.modelName = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);



                    XmlElement mass = node["mass"];
                    if (mass != null)
                    {
                        info.mass = SerializerHelper.DeserializeAttributeBaseType<float>("value", mass);
                    }

                    XmlElement dfric = node["dinamicfriction"];
                    if (dfric != null)
                    {
                        info.dinamicfriction = SerializerHelper.DeserializeAttributeBaseType<float>("value", dfric);
                    }

                    XmlElement sfric = node["staticfriction"];
                    if (sfric != null)
                    {
                        info.staticfriction = SerializerHelper.DeserializeAttributeBaseType<float>("value", sfric);
                    }

                    XmlElement ellas = node["ellasticity"];
                    if (ellas != null)
                    {
                        info.ellasticity = SerializerHelper.DeserializeAttributeBaseType<float>("value", ellas);
                    }



                    XmlElement collision = node["collision"];
                    if (collision != null)
                    {
                        info.collisionType = SerializerHelper.DeserializeAttributeBaseType<String>("type", collision);

                        if (info.collisionType.Contains("Water"))
                        {

                            Vector3 pos = SerializerHelper.DeserializeVector3("position", collision);

                            float width = SerializerHelper.DeserializeAttributeBaseType<float>("value", collision["width"]);
                            float length = SerializerHelper.DeserializeAttributeBaseType<float>("value", collision["length"]);

                            info.material.extrainformation = new Dictionary<string, object>();
                            info.material.extrainformation.Add("position", pos);
                            info.material.extrainformation.Add("width", width);
                            info.material.extrainformation.Add("length", length);
                        }
                    }
                    XmlElement material = node["material"];
                    if (material == null)
                    {
                        info.material.difuseName = "white";
                    }
                    else
                    {

                        XmlElement reflect = material["reflection"];
                        if (reflect != null)
                        {
                            info.material.reflectionName = removeExtension(SerializerHelper.DeserializeAttributeBaseType<String>("name", reflect));
                        }
                        else
                        {
                            XmlElement difuse = material["diffuse"];
                            if (difuse != null)
                            {
                                info.material.difuseName = removeExtension(SerializerHelper.DeserializeAttributeBaseType<String>("name", difuse));
                            }
                            XmlElement bump = material["bump"];
                            if (bump != null)
                            {
                                info.material.bumpName = removeExtension(SerializerHelper.DeserializeAttributeBaseType<String>("name", bump));
                            }
                            XmlElement specular = material["specular"];
                            if (specular != null)
                            {
                                info.material.specularName = removeExtension(SerializerHelper.DeserializeAttributeBaseType<String>("name", specular));
                            }
                            XmlElement glow = material["glow"];
                            if (glow != null)
                            {
                                info.material.glowName = removeExtension(SerializerHelper.DeserializeAttributeBaseType<String>("name", glow));
                            }
                        }
                    }
                    infos.Add(info.modelName, info);
                }
                else if (node.Name == "pointlight")
                {
                    String name = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);
                    Vector3 pos = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    Vector3 vColor = SerializerHelper.DeserializeVector3("color", node);
                    Color color = new Color(vColor.X / 255, vColor.Y / 255, vColor.Z / 255);
                    float amount = SerializerHelper.DeserializeAttributeBaseType<float>("amount", node["multiplier"]);
                    float decay = SerializerHelper.DeserializeAttributeBaseType<float>("value", node["decay"]);
                    PointLightPE pl = new PointLightPE(pos, color, 200, amount);
                    pl.Name = name;
                    pl.UsePointLightQuadraticAttenuation = true;
                    elements.LightsInfo.Add(pl);
                }
                else if (node.Name == "spotlight")
                {
                    String name = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);
                    Vector3 pos = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    Vector3 vColor = SerializerHelper.DeserializeVector3("color", node);
                    float fallof = SerializerHelper.DeserializeBaseType<float>("fallof", node);
                    Color color = new Color(vColor.X / 255, vColor.Y / 255, vColor.Z / 255);
                    float amount = SerializerHelper.DeserializeAttributeBaseType<float>("amount", node["multiplier"]);
                    float decay = SerializerHelper.DeserializeAttributeBaseType<float>("value", node["decay"]);
                    bool castShadow = SerializerHelper.DeserializeBaseType<bool>("castShadows", node);

                    SpotLightInformation spi = new SpotLightInformation();
                    spi.angle = MathHelper.ToRadians(fallof);
                    spi.color = color;
                    spi.decay = decay;
                    spi.multiplier = amount;
                    spi.name = name;
                    spi.pos = pos;
                    spi.castShadow = castShadow;
                    spotLights.Add(spi.name, spi);


                }
                else if (node.Name == "target")
                {
                    String name = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);
                    Vector3 pos = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    targetInfo ti = new targetInfo();
                    ti.targetPos = pos;
                    ti.name = name;
                    targets.Add(ti.name, ti);
                }
                else if (node.Name == "camera")
                {
                    String name = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);
                    Vector3 pos = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    CameraInfo co = new CameraInfo();
                    co.Name = name;
                    co.Position = pos;
                    cameras.Add(co.Name, co);
                }
                else if (node.Name == "dummy")
                {
                    String name = SerializerHelper.DeserializeAttributeBaseType<String>("name", node);
                    Vector3 pos = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    DummyInfo di = new DummyInfo();
                    di.Name = name;
                    di.Position = pos;
                    elements.DummyInfo.Add(di);
                }
            }

            ///////PROCCESS LIGHTS /////////////////////
            foreach (var item in spotLights)
            {
                SpotLightInformation si = item.Value;
                targetInfo ti = targets[item.Key + ".Target"];
                SpotLightPE sl = new SpotLightPE(si.pos, Vector3.Normalize(ti.targetPos - si.pos), si.color,si.decay, (ti.targetPos - si.pos).Length() * 10f, (float)Math.Cos(si.angle / 2), si.multiplier);
                sl.CastShadown = si.castShadow;
                sl.Name = si.name;
                elements.LightsInfo.Add(sl);

            }

            ///////PROCCESS CAMERAS/////////////////////
            foreach (var item in cameras)
            {
                CameraInfo ci = item.Value;
                targetInfo ti = targets[item.Key + ".Target"];
                ci.Target = ti.targetPos;
                elements.CameraInfo.Add(ci);
            }


            Model model = factory.GetModel(modelPath + Name);
            modelNames.Add(modelPath + Name);
            Matrix[] m = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(m);

            ////////////EXTRAINDO MESHES
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                String name = model.Meshes[i].Name.Substring(5);
                if (infos.ContainsKey(name))
                {

                    for (int j = 0; j < model.Meshes[i].MeshParts.Count; j++)
                    {
                        XmlModelMeshInfo inf = infos[name];
                        Matrix tr = m[model.Meshes[i].ParentBone.Index];

                        Vector3 scale;
                        Vector3 pos;
                        Quaternion ori;
                        tr.Decompose(out scale, out ori, out pos);

                        ObjectInformation mi = new ObjectInformation();
                        mi.modelName = inf.modelName;
                        mi.meshPartIndex = j;
                        mi.meshIndex = i;
                        mi.position = pos;
                        mi.scale = scale;
                        mi.rotation = ori;
                        
                        ModelBuilderHelper.Extract(m, model.Meshes[i].MeshParts[j], out mi.batchInformation);
                        mi.ellasticity = inf.ellasticity;
                        mi.dinamicfriction = inf.dinamicfriction;
                        mi.staticfriction = inf.staticfriction;
                        mi.collisionType = inf.collisionType;
                        mi.mass = inf.mass;

                        mi.batchInformation.ModelLocalTransformation = m[model.Meshes[i].ParentBone.Index];

                        mi.textureInformation = new TextureInformation(false, factory);
                        

                        if (inf.material.reflectionName != null)
                        {
                            mi.textureInformation.SetCubeTexture(factory.GetTextureCube(texturePath + inf.material.reflectionName), TextureType.ENVIRONMENT);
                            texturesNames.Add(texturePath + inf.material.reflectionName);
                        }

                        else
                        {
                            if (inf.material.difuseName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.difuseName), TextureType.DIFFUSE);
                                texturesNames.Add(texturePath + inf.material.difuseName);
                            }

                            if (inf.material.glowName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.glowName), TextureType.GLOW);
                                texturesNames.Add(texturePath + inf.material.glowName);
                            }

                            if (inf.material.specularName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.specularName), TextureType.SPECULAR);
                                texturesNames.Add(texturePath + inf.material.specularName);
                            }

                            if (inf.material.bumpName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.bumpName), TextureType.BUMP);
                                texturesNames.Add(texturePath + inf.material.bumpName);
                            }

                        }

                        if (inf.collisionType != null)
                        {
                            if (inf.collisionType.Contains("Water"))
                            {
                                mi.extra = inf.material.extrainformation;
                            }
                        }

                        mi.textureInformation.LoadTexture();
                        elements.ModelMeshesInfo.Add(mi);
                    }
                }
            }

            SerializerHelper.ChangeDecimalSymbolToSystemDefault();
            //Clear Stuffs
            infos.Clear();
            targets.Clear();
            spotLights.Clear();
            cameras.Clear();

            return elements;
        }
 IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] oi)
 {
     foreach (var mi in oi)
     {
         mi.batchInformation.ModelLocalTransformation = mi.batchInformation.ModelLocalTransformation * Matrix.CreateScale(0.5f);
     }            
     return WorldLoader.CreateOBJ(world, factory, ginfo, oi);
 }
        /// <summary>
        /// Initializes a new instance of the <see cref="CustomModel"/> class.
        /// </summary>
        /// <param name="factory">The factory.</param>
        /// <param name="loader">The loader.</param>
        public CustomModel(GraphicFactory factory, ObjectInformation[] loader)
            : base(factory, loader[0].modelName, false)
        {
            System.Diagnostics.Debug.Assert(loader!= null);
            System.Diagnostics.Debug.Assert(loader.Count() != 0);
            BatchInformations = new BatchInformation[1][];
            BatchInformations[0] = new BatchInformation[loader.Count()];

            TextureInformations = new TextureInformation[1][];
            TextureInformations[0] = new TextureInformation[loader.Count()];

            BoundingSphere bs = new BoundingSphere();
            for (int i = 0; i < loader.Count(); i++)
            {
                BatchInformations[0][i] = loader[i].batchInformation;
                TextureInformations[0][i] = loader[i].textureInformation;

                float radius;
                Vector3 center;
                ModelBuilderHelper.ExtractModelRadiusAndCenter(BatchInformations[0][i], out radius, out center);
                bs = BoundingSphere.CreateMerged(bs, new BoundingSphere(center, radius));
            }            
            modelRadius = bs.Radius;
        }
        /// <summary>
        /// Called when an object is found
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="factory">The factory.</param>
        /// <param name="ginfo">The ginfo.</param>
        /// <param name="mi">The mi.</param>
        /// <returns></returns>
        IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] mi)
        {
            IObject[] objs = new IObject[mi.Length];
            for (int i = 0; i < mi.Length; i++)
            {
                IModelo model = new CustomModel(factory, mi[i].modelName, mi[i].batchInformation, mi[i].textureInformation);
                IPhysicObject po = new TriangleMeshObject(model, Vector3.Zero, Matrix.Identity, Vector3.One, MaterialDescription.DefaultBepuMaterial());
                DeferredCustomShader shader = new DeferredCustomShader(mi[i].HasTexture(TextureType.GLOW), mi[i].HasTexture(TextureType.BUMP), mi[i].HasTexture(TextureType.SPECULAR), mi[i].HasTexture(TextureType.PARALAX));
                DeferredMaterial dm = new DeferredMaterial(shader);
                IObject obj = new IObject(dm, model, po);

                if (mi[i].HasTexture(TextureType.BUMP))
                {
                    withBump.Add(obj);
                }

                if (mi[i].HasTexture(TextureType.SPECULAR))
                {
                    shader.SpecularPowerMapScale = 2f;
                    shader.SpecularIntensityMapScale = 0.1f;
                    withSpecular.Add(obj);
                }
                objs[i] = obj;
            }

            return objs;

        }
        /// <summary>
        /// Called when an object is found
        ///Return the object, return null to not add this object
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="factory">The factory.</param>
        /// <param name="ginfo">The ginfo.</param>
        /// <param name="mi">The mi.</param>
        /// <returns></returns>
        IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] mi)
        {

            ///Do what default would do.
            IObject[] objs = WorldLoader.CreateOBJ(world, factory, ginfo, mi);
            ///Change object property here !!!

            foreach (var obj in objs)
            {
                DeferredCustomShader cd = (obj.Material.Shader as DeferredCustomShader); ///the world loader uses deferredCustomShader for all objects
                System.Diagnostics.Debug.Assert(cd != null);
                ///if the obj does not use specular map
                if (!cd.UseSpecular)
                {
                    ///No Specular this time
                    cd.SpecularIntensity = 0f;
                }
            }
            return objs;
        }
        IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] oi)
        {
            foreach (var mi in oi)
            {
                if (!mi.HasTexture(TextureType.DIFFUSE))
                {
                    mi.textureInformation.SetTexture(factory.CreateTexture2DColor(1, 1, Color.Red), TextureType.DIFFUSE);
                }

                //if (mi.HasTexture(TextureType.BUMP))
                //{
                //    mi.textureInformation.SetNullTexture(TextureType.BUMP);
                //}    
            }           

            
            return WorldLoader.CreateOBJ(world, factory, ginfo, oi);
        }
        /// <summary>
        /// Called when an object is found
        ///Return the object, return null to not add this object
        /// </summary>
        /// <param name="world">The world.</param>
        /// <param name="factory">The factory.</param>
        /// <param name="ginfo">The ginfo.</param>
        /// <param name="mi">The mi.</param>
        /// <returns></returns>
        IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] mi)
        {            

            ///Do what default would do.
            IObject[] objs =  WorldLoader.CreateOBJ(world, factory, ginfo, mi);
            ///Change object property here !!!
            foreach (var obj in objs)
            {
                DeferredCustomShader cd = (obj.Material.Shader as DeferredCustomShader); ///the world loader uses deferredCustomShader for all objects
                System.Diagnostics.Debug.Assert(cd != null);
                ///if the obj does not use specular map
                if (!cd.UseSpecular)
                {
                    ///set a constant specular for all the object
                    cd.SpecularIntensity = 0.3f;
                    cd.SpecularPower = 150;
                }
            }
            
            ///If you want, create the object on your own, without using the World Loader,                          
            ///THIS IS WHAT WorldLoader.CreateOBJ DOES
            //IModelo model = new CustomModel(factory, mi.modelName, new BatchInformation[] { mi.batchInformation}, mi.difuse, mi.bump, mi.specular, mi.glow);
            //IPhysicObject po = new TriangleMeshObject(model, Vector3.Zero, Matrix.Identity, Vector3.One, MaterialDescription.DefaultBepuMaterial());
            //IShader shader = new DeferredCustomShader(mi.HasTexture(TextureType.GLOW), mi.HasTexture(TextureType.BUMP), mi.HasTexture(TextureType.SPECULAR), mi.HasTexture(TextureType.PARALAX));
            //DeferredMaterial dm = new DeferredMaterial(shader);
            //return new IObject(dm, model, po);

            return objs;
        }
 IObject[] wl_OnCreateIObject(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] mi)
 {            
     return WorldLoader.CreateOBJ(world, factory, ginfo, mi);
 }
        public static IObject[] CreateOBJ(IWorld world, GraphicFactory factory, GraphicInfo ginfo, ObjectInformation[] mi)
        {            

            IModelo model = new CustomModel(factory, mi);            

            MaterialDescription material;
            if (mi[0].staticfriction == -1 || mi[0].dinamicfriction == -1 || mi[0].ellasticity == -1)
            {
                material = MaterialDescription.DefaultBepuMaterial();
            }
            else
            {
                material = new MaterialDescription(mi[0].staticfriction, mi[0].dinamicfriction, mi[0].ellasticity);
            }

            IPhysicObject po;

            bool massflag = false;
            if (mi[0].mass == 0)
            {
                massflag = true;
                mi[0].mass = 0.5f;
            }

            BatchInformation binf = model.GetBatchInformation(0)[0];            

            BoundingBox bb;

            switch (mi[0].collisionType)
            {

                case "Ghost":

                   
                    po = new GhostObject(mi[0].position,Matrix.CreateFromQuaternion(mi[0].rotation), mi[0].scale);

                    break;
                case "Cylinder":

                    binf.ModelLocalTransformation = Matrix.Identity;
                    bb = ModelBuilderHelper.CreateBoundingBoxFromModel(binf);
                    Vector3 len = bb.Max - bb.Min;

                    po = new CylinderObject(mi[0].position, len.Y, len.X / 2,Vector3.Up ,mi[0].mass, Matrix.CreateFromQuaternion(mi[0].rotation), material);
                    
                    break;


                case "Sphere":
                    binf.ModelLocalTransformation = Matrix.Identity;
                    po = new SphereObject(mi[0].position, model.GetModelRadius(), mi[0].mass, mi[0].scale.X, material);
                    po.Rotation = Matrix.CreateFromQuaternion(mi[0].rotation);

                    break;


                case "Box":

                    bb = ModelBuilderHelper.CreateBoundingBoxFromModel(binf);

                    len = bb.Max - bb.Min;

                    po = new BoxObject(mi[0].position, len.X, len.Y, len.Z, mi[0].mass, mi[0].scale, Matrix.CreateFromQuaternion(mi[0].rotation), material);

                    break;

                case "Water":
                    po = new GhostObject(mi[0].position, Matrix.CreateFromQuaternion(mi[0].rotation), mi[0].scale);
                    break;
                case "TriangleMesh":
                default:
                    po = new TriangleMeshObject(model, Vector3.Zero, Matrix.Identity, new Vector3(1), material);
                    break;
            }

            po.isMotionLess = massflag;

            IShader shader = null;
#if !REACH && !WINDOWS_PHONE
            
            if (mi[0].HasTexture(TextureType.ENVIRONMENT))
            {
                shader = new DeferredEMReflectiveShader();
                (shader as DeferredEMReflectiveShader).TextureCube = mi[0].textureInformation.getCubeTexture(TextureType.ENVIRONMENT);
                
            }
            else if (mi[0].collisionType != null && mi[0].collisionType.Contains("Water"))
            {
                Vector3 position = (Vector3)(mi[0].extra["position"]);
                var width = (mi[0].extra["width"]);
                var height = (mi[0].extra["length"]);
                shader = new DeferredWaterCompleteShader((int)width,(int)height, new Plane(position.X, position.Y, position.Z, 1),10.0f);
            }
            
            else
            {
                shader = new DeferredCustomShader(mi[0].HasTexture(TextureType.GLOW), mi[0].HasTexture(TextureType.BUMP), mi[0].HasTexture(TextureType.SPECULAR), mi[0].HasTexture(TextureType.PARALAX)); 
            }
          
            DeferredMaterial dm = new DeferredMaterial(shader);
#else
            shader = new ForwardXNABasicShader();
            ForwardMaterial dm = new ForwardMaterial(shader);

#endif
            IObject ob = new IObject(dm, model, po);

            ob.Name = mi[0].modelName;

            return new IObject[] { ob };
        }
        public ModelLoaderData Load(GraphicFactory factory, GraphicInfo ginfo, String Name)
        {
            ModelLoaderData elements = new ModelLoaderData();
            Dictionary <String, XmlModelMeshInfo>     infos      = new Dictionary <string, XmlModelMeshInfo>();
            Dictionary <String, targetInfo>           targets    = new Dictionary <string, targetInfo>();
            Dictionary <String, SpotLightInformation> spotLights = new Dictionary <string, SpotLightInformation>();
            //Dictionary<String, ConstraintInformation> constraints = new Dictionary<string, ConstraintInformation>();
            Dictionary <String, CameraInfo>   cameras   = new Dictionary <string, CameraInfo>();
            Dictionary <String, ParticleInfo> particles = new Dictionary <string, ParticleInfo>();


            SerializerHelper.ChangeDecimalSymbolToPoint();
            XmlDocument xDoc = new XmlDocument();

            xDoc.Load(path + Name + ".xml");
            XmlNodeList worldNode = xDoc["SCENE"].ChildNodes;

            foreach (XmlNode node in worldNode)
            {
                if (node.Name == "Constraint")
                {
                    ConstraintInfo cinfo = new ConstraintInfo();


                    cinfo.Name = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);
                    XmlElement type = node["type"];
                    if (type != null)
                    {
                        cinfo.type = SerializerHelper.DeserializeAttributeBaseType <String>("value", type);
                    }

                    XmlElement child = node["child"];
                    if (child != null)
                    {
                        cinfo.bodyA = SerializerHelper.DeserializeAttributeBaseType <String>("name", child);
                    }

                    XmlElement parent = node["parent"];
                    if (parent != null)
                    {
                        cinfo.bodyB = SerializerHelper.DeserializeAttributeBaseType <String>("name", parent);
                    }

                    XmlElement breakable = node["isBreakable"];
                    if (breakable != null)
                    {
                        cinfo.breakable = SerializerHelper.DeserializeAttributeBaseType <bool>("value", breakable);
                    }



                    Vector3 pos = SerializerHelper.DeserializeVector3("position", node);
                    cinfo.Position = new Vector3(pos.X, pos.Y, pos.Z);


                    elements.ConstraintInfo.Add(cinfo);
                }
                if (node.Name == "particle")
                {
                    ParticleInfo pinfo = new ParticleInfo();
                    pinfo.Name        = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);
                    pinfo.Position    = SerializerHelper.DeserializeVector3("position", node);
                    pinfo.Orientation = SerializerHelper.DeserializeQuaternion("rotation", node);

                    XmlElement mass = node["type"];
                    if (mass != null)
                    {
                        pinfo.Type = SerializerHelper.DeserializeAttributeBaseType <String>("value", mass);
                    }

                    elements.ParticleInfo.Add(pinfo);
                }
                if (node.Name == "body")
                {
                    XmlModelMeshInfo info = new XmlModelMeshInfo();
                    info.modelName = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);



                    XmlElement mass = node["mass"];
                    if (mass != null)
                    {
                        info.mass = SerializerHelper.DeserializeAttributeBaseType <float>("value", mass);
                    }

                    XmlElement dfric = node["dinamicfriction"];
                    if (dfric != null)
                    {
                        info.dinamicfriction = SerializerHelper.DeserializeAttributeBaseType <float>("value", dfric);
                    }

                    XmlElement sfric = node["staticfriction"];
                    if (sfric != null)
                    {
                        info.staticfriction = SerializerHelper.DeserializeAttributeBaseType <float>("value", sfric);
                    }

                    XmlElement ellas = node["ellasticity"];
                    if (ellas != null)
                    {
                        info.ellasticity = SerializerHelper.DeserializeAttributeBaseType <float>("value", ellas);
                    }



                    XmlElement collision = node["collision"];
                    if (collision != null)
                    {
                        info.collisionType = SerializerHelper.DeserializeAttributeBaseType <String>("type", collision);

                        if (info.collisionType.Contains("Water"))
                        {
                            Vector3 pos = SerializerHelper.DeserializeVector3("position", collision);

                            float width  = SerializerHelper.DeserializeAttributeBaseType <float>("value", collision["width"]);
                            float length = SerializerHelper.DeserializeAttributeBaseType <float>("value", collision["length"]);

                            info.material.extrainformation = new Dictionary <string, object>();
                            info.material.extrainformation.Add("position", pos);
                            info.material.extrainformation.Add("width", width);
                            info.material.extrainformation.Add("length", length);
                        }
                    }
                    XmlElement material = node["material"];
                    if (material == null)
                    {
                        info.material.difuseName = "white";
                    }
                    else
                    {
                        XmlElement reflect = material["reflection"];
                        if (reflect != null)
                        {
                            info.material.reflectionName = removeExtension(SerializerHelper.DeserializeAttributeBaseType <String>("name", reflect));
                        }
                        else
                        {
                            XmlElement difuse = material["diffuse"];
                            if (difuse != null)
                            {
                                info.material.difuseName = removeExtension(SerializerHelper.DeserializeAttributeBaseType <String>("name", difuse));
                            }
                            XmlElement bump = material["bump"];
                            if (bump != null)
                            {
                                info.material.bumpName = removeExtension(SerializerHelper.DeserializeAttributeBaseType <String>("name", bump));
                            }
                            XmlElement specular = material["specular"];
                            if (specular != null)
                            {
                                info.material.specularName = removeExtension(SerializerHelper.DeserializeAttributeBaseType <String>("name", specular));
                            }
                            XmlElement glow = material["glow"];
                            if (glow != null)
                            {
                                info.material.glowName = removeExtension(SerializerHelper.DeserializeAttributeBaseType <String>("name", glow));
                            }
                        }
                    }
                    infos.Add(info.modelName, info);
                }
                else if (node.Name == "pointlight")
                {
                    String  name = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);
                    Vector3 pos  = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    Vector3      vColor = SerializerHelper.DeserializeVector3("color", node);
                    Color        color  = new Color(vColor.X / 255, vColor.Y / 255, vColor.Z / 255);
                    float        amount = SerializerHelper.DeserializeAttributeBaseType <float>("amount", node["multiplier"]);
                    float        decay  = SerializerHelper.DeserializeAttributeBaseType <float>("value", node["decay"]);
                    PointLightPE pl     = new PointLightPE(pos, color, 200, amount);
                    pl.Name = name;
                    pl.UsePointLightQuadraticAttenuation = true;
                    elements.LightsInfo.Add(pl);
                }
                else if (node.Name == "spotlight")
                {
                    String  name = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);
                    Vector3 pos  = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    Vector3 vColor     = SerializerHelper.DeserializeVector3("color", node);
                    float   fallof     = SerializerHelper.DeserializeBaseType <float>("fallof", node);
                    Color   color      = new Color(vColor.X / 255, vColor.Y / 255, vColor.Z / 255);
                    float   amount     = SerializerHelper.DeserializeAttributeBaseType <float>("amount", node["multiplier"]);
                    float   decay      = SerializerHelper.DeserializeAttributeBaseType <float>("value", node["decay"]);
                    bool    castShadow = SerializerHelper.DeserializeBaseType <bool>("castShadows", node);

                    SpotLightInformation spi = new SpotLightInformation();
                    spi.angle      = MathHelper.ToRadians(fallof);
                    spi.color      = color;
                    spi.decay      = decay;
                    spi.multiplier = amount;
                    spi.name       = name;
                    spi.pos        = pos;
                    spi.castShadow = castShadow;
                    spotLights.Add(spi.name, spi);
                }
                else if (node.Name == "target")
                {
                    String  name = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);
                    Vector3 pos  = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    targetInfo ti = new targetInfo();
                    ti.targetPos = pos;
                    ti.name      = name;
                    targets.Add(ti.name, ti);
                }
                else if (node.Name == "camera")
                {
                    String  name = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);
                    Vector3 pos  = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    CameraInfo co = new CameraInfo();
                    co.Name     = name;
                    co.Position = pos;
                    cameras.Add(co.Name, co);
                }
                else if (node.Name == "dummy")
                {
                    String  name = SerializerHelper.DeserializeAttributeBaseType <String>("name", node);
                    Vector3 pos  = SerializerHelper.DeserializeVector3("position", node);
                    pos = new Vector3(pos.X, -pos.Y, -pos.Z);
                    DummyInfo di = new DummyInfo();
                    di.Name     = name;
                    di.Position = pos;
                    elements.DummyInfo.Add(di);
                }
            }

            ///////PROCCESS LIGHTS /////////////////////
            foreach (var item in spotLights)
            {
                SpotLightInformation si = item.Value;
                targetInfo           ti = targets[item.Key + ".Target"];
                SpotLightPE          sl = new SpotLightPE(si.pos, Vector3.Normalize(ti.targetPos - si.pos), si.color, si.decay, (ti.targetPos - si.pos).Length() * 10f, (float)Math.Cos(si.angle / 2), si.multiplier);
                sl.CastShadown = si.castShadow;
                sl.Name        = si.name;
                elements.LightsInfo.Add(sl);
            }

            ///////PROCCESS CAMERAS/////////////////////
            foreach (var item in cameras)
            {
                CameraInfo ci = item.Value;
                targetInfo ti = targets[item.Key + ".Target"];
                ci.Target = ti.targetPos;
                elements.CameraInfo.Add(ci);
            }


            Model model = factory.GetModel(modelPath + Name);

            modelNames.Add(modelPath + Name);
            Matrix[] m = new Matrix[model.Bones.Count];
            model.CopyAbsoluteBoneTransformsTo(m);

            ////////////EXTRAINDO MESHES
            for (int i = 0; i < model.Meshes.Count; i++)
            {
                String name = model.Meshes[i].Name.Substring(5);
                if (infos.ContainsKey(name))
                {
                    for (int j = 0; j < model.Meshes[i].MeshParts.Count; j++)
                    {
                        XmlModelMeshInfo inf = infos[name];
                        Matrix           tr  = m[model.Meshes[i].ParentBone.Index];

                        Vector3    scale;
                        Vector3    pos;
                        Quaternion ori;
                        tr.Decompose(out scale, out ori, out pos);

                        ObjectInformation mi = new ObjectInformation();
                        mi.modelName     = inf.modelName;
                        mi.meshPartIndex = j;
                        mi.meshIndex     = i;
                        mi.position      = pos;
                        mi.scale         = scale;
                        mi.rotation      = ori;

                        ModelBuilderHelper.Extract(m, model.Meshes[i].MeshParts[j], out mi.batchInformation);
                        mi.ellasticity     = inf.ellasticity;
                        mi.dinamicfriction = inf.dinamicfriction;
                        mi.staticfriction  = inf.staticfriction;
                        mi.collisionType   = inf.collisionType;
                        mi.mass            = inf.mass;

                        mi.batchInformation.ModelLocalTransformation = m[model.Meshes[i].ParentBone.Index];

                        mi.textureInformation = new TextureInformation(false, factory);


                        if (inf.material.reflectionName != null)
                        {
                            mi.textureInformation.SetCubeTexture(factory.GetTextureCube(texturePath + inf.material.reflectionName), TextureType.ENVIRONMENT);
                            texturesNames.Add(texturePath + inf.material.reflectionName);
                        }

                        else
                        {
                            if (inf.material.difuseName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.difuseName), TextureType.DIFFUSE);
                                texturesNames.Add(texturePath + inf.material.difuseName);
                            }

                            if (inf.material.glowName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.glowName), TextureType.GLOW);
                                texturesNames.Add(texturePath + inf.material.glowName);
                            }

                            if (inf.material.specularName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.specularName), TextureType.SPECULAR);
                                texturesNames.Add(texturePath + inf.material.specularName);
                            }

                            if (inf.material.bumpName != null)
                            {
                                mi.textureInformation.SetTexture(factory.GetTexture2D(texturePath + inf.material.bumpName), TextureType.BUMP);
                                texturesNames.Add(texturePath + inf.material.bumpName);
                            }
                        }

                        if (inf.collisionType != null)
                        {
                            if (inf.collisionType.Contains("Water"))
                            {
                                mi.extra = inf.material.extrainformation;
                            }
                        }

                        mi.textureInformation.LoadTexture();
                        elements.ModelMeshesInfo.Add(mi);
                    }
                }
            }

            SerializerHelper.ChangeDecimalSymbolToSystemDefault();
            //Clear Stuffs
            infos.Clear();
            targets.Clear();
            spotLights.Clear();
            cameras.Clear();

            return(elements);
        }