Example #1
0
        /// <summary>
        /// Checks if there is a <see cref="Model"/> to draw and draws it with specified Effect.
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="spriteBatch"></param>
        protected virtual void DrawWithSpecificEffect(GameScene scene, Matrix view, Matrix projection, Effect effect, Action <GameObject, Effect, Matrix[], ModelMesh, GameScene> effectParams, bool applySceneLighting, string technique = null)
        {
            if (this.Model == null)
            {
                return;                                 // No model means it can't be rendered.
            }
            // copy the scale of bones from the model to apply it later.
            var transformMatrices = new Matrix[this.Model.Bones.Count];

            this.Model.CopyAbsoluteBoneTransformsTo(transformMatrices);

            var originalRastState = scene.Game.GraphicsDevice.RasterizerState;

            if (!CullingEnabled)
            {
                var newRastState = new RasterizerState
                {
                    CullMode             = CullMode.None,
                    DepthBias            = originalRastState.DepthBias,
                    DepthClipEnable      = originalRastState.DepthClipEnable,
                    FillMode             = originalRastState.FillMode,
                    MultiSampleAntiAlias = originalRastState.MultiSampleAntiAlias,
                    ScissorTestEnable    = originalRastState.ScissorTestEnable,
                    SlopeScaleDepthBias  = originalRastState.SlopeScaleDepthBias
                };

                scene.Game.GraphicsDevice.RasterizerState = newRastState;
            }

            if (technique == null)
            {
                foreach (var pass in effect.CurrentTechnique.Passes)
                {
                    foreach (var mesh in Model.Meshes)
                    {
                        foreach (var part in mesh.MeshParts)
                        {
                            part.Effect = effect;
                            effectParams.Invoke(this, part.Effect, transformMatrices, mesh, scene);
                            if (applySceneLighting)
                            {
                                scene.AddLightningToEffect(part.Effect);
                            }
                        }
                        mesh.Draw();
                    }
                }
            }
            else
            {
                foreach (var pass in effect.Techniques[technique].Passes)
                {
                    foreach (var mesh in Model.Meshes)
                    {
                        foreach (var part in mesh.MeshParts)
                        {
                            part.Effect = effect;
                            effectParams.Invoke(this, part.Effect, transformMatrices, mesh, scene);
                            if (applySceneLighting)
                            {
                                scene.AddLightningToEffect(part.Effect);
                            }
                        }
                        mesh.Draw();
                    }
                }
            }

            if (!CullingEnabled)
            {
                scene.Game.GraphicsDevice.RasterizerState = originalRastState;
            }
        }
Example #2
0
        /// <summary>
        /// Checks if there is a <see cref="Model"/> to draw and draws it.
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="spriteBatch"></param>
        protected virtual void Draw(GameScene scene, Matrix view, Matrix projection)
        {
            if (this.Model == null)
            {
                return;                                 // No model means it can't be rendered.
            }
            // copy the scale of bones from the model to apply it later.
            var transformMatrices = new Matrix[this.Model.Bones.Count];

            this.Model.CopyAbsoluteBoneTransformsTo(transformMatrices);

            var originalRastState = scene.Game.GraphicsDevice.RasterizerState;

            if (!CullingEnabled)
            {
                var newRastState = new RasterizerState
                {
                    CullMode             = CullMode.None,
                    DepthBias            = originalRastState.DepthBias,
                    DepthClipEnable      = originalRastState.DepthClipEnable,
                    FillMode             = originalRastState.FillMode,
                    MultiSampleAntiAlias = originalRastState.MultiSampleAntiAlias,
                    ScissorTestEnable    = originalRastState.ScissorTestEnable,
                    SlopeScaleDepthBias  = originalRastState.SlopeScaleDepthBias
                };

                scene.Game.GraphicsDevice.RasterizerState = newRastState;
            }

            foreach (ModelMesh mesh in this.Model.Meshes)
            {
                if (this.Effect == null)
                {
                    foreach (Effect effect in mesh.Effects)
                    {
                        if (!(effect is BasicEffect))
                        {
                            continue;
                        }

                        BasicEffect basisEffect = (BasicEffect)effect;

                        // calculating the full rotation of our object.
                        //Console.WriteLine($"POS: {this.GetHierarchyPosition().X} {this.GetHierarchyPosition().Y} {this.GetHierarchyPosition().Z}");

                        basisEffect.World = transformMatrices[mesh.ParentBone.Index]
                                            * this.ScaleMatrix
                                            * this.RotationMatrix
                                            * Matrix.CreateTranslation(this.GetHierarchyPosition());

                        basisEffect.View       = view;
                        basisEffect.Projection = projection;

                        scene.AddLightningToEffect(basisEffect);
                    }
                }
                else
                {
                    foreach (var part in mesh.MeshParts)
                    {
                        part.Effect = this.Effect;

                        this.EffectParams.Invoke(this, part.Effect, transformMatrices, mesh, scene);
                        if (ApplySceneLight)
                        {
                            scene.AddLightningToEffect(part.Effect);
                        }
                    }
                }

                mesh.Draw();
            }

            if (!CullingEnabled)
            {
                scene.Game.GraphicsDevice.RasterizerState = originalRastState;
            }
        }