Beispiel #1
0
 public static void DrawModel(_Model model, Vector3 position, Vector3 rotation, Color color, _Texture textureInfo)
 {
     DrawModel(model, position, rotation, Vector3.One, color, textureInfo);
 }
Beispiel #2
0
        public static void DrawModel(_Model model, Vector3 position, Vector3 rotation, Vector3 scale, Color color, _Texture textureInfo)
        {
            CheckInitialized();

            if (model == null)
            {
                return;
            }

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

            Matrix m = Matrix.CreateRotationZ(rotation.Y) * Matrix.CreateRotationY(rotation.X) * Matrix.CreateRotationX(rotation.Z) *
                       Matrix.CreateScale(scale.X, scale.Z, scale.Y) * Matrix.CreateTranslation(new Vector3(position.X, position.Z, position.Y));

            int i = 0;

            foreach (ModelMesh mesh in model.Model.Meshes)
            {
                foreach (Effect effect in mesh.Effects)
                {
                    setEffectMatrices(effect, transforms[mesh.ParentBone.Index] * m, View, Projection);

                    if (effect.GetType() == typeof(BasicEffect))
                    {
                        if (EnableLighting)
                        {
                            (effect as BasicEffect).EnableDefaultLighting();
                        }
                        (effect as BasicEffect).DiffuseColor = color.ToVector3();
                        continue;
                    }

                    setEffectDiffuseColor(effect, color);

                    bool textureDataSet = false;
                    if (i < textureInfo.Overrides.Length)
                    {
                        textureDataSet = true;
                        if (effect.Parameters["xTexture"] != null)
                        {
                            effect.Parameters["xTexture"].SetValue(textureInfo.Overrides[i]);
                        }
                    }
                    else
                    {
                        if (i < model.Textures.Length)
                        {
                            textureDataSet = true;
                            if (effect.Techniques["TechniqueTextured"] != null)
                            {
                                effect.CurrentTechnique = effect.Techniques["TechniqueTextured"];
                            }
                            if (effect.Parameters["xTexture"] != null)
                            {
                                effect.Parameters["xTexture"].SetValue(model.Textures[i]);
                            }
                        }
                        else
                        if (effect.Techniques["TechniqueNonTextured"] != null)
                        {
                            effect.CurrentTechnique = effect.Techniques["TechniqueNonTextured"];
                        }
                    }

                    if (textureDataSet)
                    {
                        if (i < textureInfo.NormalMaps.Length)
                        {
                            if (effect.Techniques["TechniqueTexturedNormalMap"] != null)
                            {
                                effect.CurrentTechnique = effect.Techniques["TechniqueTexturedNormalMap"];
                            }
                            if (effect.Parameters["xNormalMap"] != null)
                            {
                                effect.Parameters["xNormalMap"].SetValue(textureInfo.NormalMaps[i]);
                            }
                        }

                        if (effect.Parameters["xTextureOffset"] != null)
                        {
                            effect.Parameters["xTextureOffset"].SetValue(textureInfo.Offset);
                        }
                        if (effect.Parameters["xTextureScale"] != null)
                        {
                            effect.Parameters["xTextureScale"].SetValue(textureInfo.Scale);
                        }

                        i++;
                    }

                    if (EnableLighting)
                    {
                        if (effect.Parameters["xLights"] != null)
                        {
                            int          maxLightCount = effect.Parameters["xLights"].Elements.Count;
                            PointLight[] rangeLights   = (from light in Lights
                                                          let dist = (position - light.Position).Length()
                                                                     where dist < light.FallOff
                                                                     orderby dist ascending
                                                                     select light).Take(maxLightCount).ToArray();
                            for (int j = 0; j < maxLightCount; j++)
                            {
                                EffectParameter effParam = effect.Parameters["xLights"].Elements[j];

                                if (j >= rangeLights.Length)
                                {
                                    for (int k = j; k < maxLightCount; k++)
                                    {
                                        effParam.StructureMembers["Initialized"].SetValue(false);
                                    }
                                    break;
                                }

                                PointLight light = rangeLights[j];

                                effParam.StructureMembers["Initialized"].SetValue(light.initialized);
                                effParam.StructureMembers["Position"].SetValue(light.Position);
                                effParam.StructureMembers["Power"].SetValue(light.Power);
                                effParam.StructureMembers["FallOff"].SetValue(light.FallOff);
                                effParam.StructureMembers["Color"].SetValue(light.Color.ToVector4());
                            }
                        }
                    }
                }

                mesh.Draw();
            }
        }
Beispiel #3
0
 public static void DrawModel(_Model model, Vector3 position, _Texture textureInfo)
 {
     DrawModel(model, position, Vector3.Zero, Vector3.One, Color.White, textureInfo);
 }