Exemple #1
0
 private void ConfigureEffectLighting(IEffectLights effect)
 {
     effect.EnableDefaultLighting();
     effect.DirectionalLight0.Direction = Vector3.Backward;
     effect.DirectionalLight0.Enabled   = true;
     effect.DirectionalLight1.Enabled   = false;
     effect.DirectionalLight2.Enabled   = false;
 }
        /// <summary>
        /// Update lighting on this effect
        /// </summary>
        /// <param name="effectLights">The light effect</param>
        /// <returns>True if the lighting is enabled</returns>
        protected virtual bool UpdateLights(IEffectLights effectLights)
        {
            // Lights
            if (_enableDefaultLighting)
            {
                effectLights.EnableDefaultLighting();
                return(false);
            }
            else
            {
                effectLights.LightingEnabled   = _enableLighting;
                effectLights.AmbientLightColor = _ambientColor * _ambientIntensity;

                if (_light is SceneLight)
                {
                    UpdateLighting(effectLights, (SceneLight)_light);
                    effectLights.AmbientLightColor *= _light.AmbientColor * _light.AmbientIntensity;
                }

                return(true);
            }
        }
        /// <summary>
        /// Draws the model using the specified camera matrices and the default
        /// lighting model.
        /// </summary>
        public void Draw(Matrix world, Matrix view, Matrix projection)
        {
            foreach (var part in modelParts)
            {
                // use the new effect interfaces so this code will still work
                // even if someone changes the effect on the part to a new type.

                IEffectMatrices matrices = part.Effect as IEffectMatrices;
                if (matrices != null)
                {
                    matrices.World      = world;
                    matrices.View       = view;
                    matrices.Projection = projection;
                }

                IEffectLights lights = part.Effect as IEffectLights;
                if (lights != null)
                {
                    lights.EnableDefaultLighting();
                }

                part.Draw();
            }
        }
Exemple #4
0
        /// <summary>
        /// Update lighting on this effect
        /// </summary>
        /// <param name="effectLights">The light effect</param>
        /// <returns>True if the lighting is enabled</returns>
        protected virtual bool UpdateLights(IEffectLights effectLights)
        {
            // Lights
            if (_enableDefaultLighting)
            {
                effectLights.EnableDefaultLighting();
                return false;
            }
            else
            {
                effectLights.LightingEnabled = _enableLighting ;
                effectLights.AmbientLightColor = _ambientColor * _ambientIntensity;

                if (_light is SceneLight)
                {
                    UpdateLighting(effectLights, (SceneLight)_light);
                    effectLights.AmbientLightColor *= _light.AmbientColor * _light.AmbientIntensity;
                }

                return true;
            }
        }
Exemple #5
0
        private void DrawMesh(ModelMesh mesh, bool opaque, Matrix world, Matrix view, Matrix projection, Vector3?color)
        {
            int count = mesh.MeshParts.Count;

            for (int i = 0; i < count; i++)
            {
                ModelMeshPart part   = mesh.MeshParts[i];
                Effect        effect = part.Effect;

                Vector3     diffuseColor = Vector3.One;
                bool        textureEnabled = false, lightingEnabled = false, vertexColorEnabled = false;
                float       alpha       = 1.0f;
                BasicEffect basicEffect = effect as BasicEffect;
                if (basicEffect != null)
                {
                    if (basicEffect.Alpha < 1.0f && opaque)
                    {
                        continue;
                    }
                    if (basicEffect.Alpha == 1.0f && !opaque)
                    {
                        continue;
                    }

                    basicEffect.PreferPerPixelLighting = true;
                    basicEffect.SpecularPower          = 16;

                    if (color != null)
                    {
                        diffuseColor       = basicEffect.DiffuseColor;
                        textureEnabled     = basicEffect.TextureEnabled;
                        lightingEnabled    = basicEffect.LightingEnabled;
                        vertexColorEnabled = basicEffect.VertexColorEnabled;
                        alpha = basicEffect.Alpha;

                        basicEffect.DiffuseColor       = color.Value;
                        basicEffect.TextureEnabled     = false;
                        basicEffect.LightingEnabled    = false;
                        basicEffect.VertexColorEnabled = false;
                        basicEffect.Alpha = _options.SolidAndWireframeAlpha;
                    }
                }

                IEffectMatrices effectMatrices = effect as IEffectMatrices;
                if (effectMatrices != null)
                {
                    effectMatrices.World      = _boneTransforms[mesh.ParentBone.Index] * world;
                    effectMatrices.View       = view;
                    effectMatrices.Projection = projection;
                }
                else
                {
                    EffectParameter wvp = effect.Parameters.GetParameterBySemantic("WORLDVIEWPROJECTION");
                    if (wvp != null)
                    {
                        wvp.SetValue(_boneTransforms[mesh.ParentBone.Index] * world * view * projection);
                    }

                    EffectParameter w = effect.Parameters.GetParameterBySemantic("WORLD");
                    if (w != null)
                    {
                        w.SetValue(_boneTransforms[mesh.ParentBone.Index] * world);
                    }

                    EffectParameter v = effect.Parameters.GetParameterBySemantic("VIEW");
                    if (v != null)
                    {
                        v.SetValue(view);
                    }

                    EffectParameter p = effect.Parameters.GetParameterBySemantic("PROJECTION");
                    if (p != null)
                    {
                        p.SetValue(projection);
                    }
                }

                IEffectLights effectLights = effect as IEffectLights;
                if (effectLights != null)
                {
                    effectLights.EnableDefaultLighting();
                }

                int passes = effect.CurrentTechnique.Passes.Count;
                for (int j = 0; j < passes; j++)
                {
                    effect.CurrentTechnique.Passes[j].Apply();
                    DrawMeshPart(part);
                }

                if (basicEffect != null && color != null)
                {
                    basicEffect.DiffuseColor       = diffuseColor;
                    basicEffect.Alpha              = alpha;
                    basicEffect.TextureEnabled     = textureEnabled;
                    basicEffect.LightingEnabled    = lightingEnabled;
                    basicEffect.VertexColorEnabled = vertexColorEnabled;
                }
            }
        }