Beispiel #1
0
        /// <summary>
        /// Applies lights to the target effect from the drawing context.
        /// </summary>
        public static void ApplyLights(DrawingContext3D context, IEffectLights effect)
        {
            // Update ambient light color
            effect.AmbientLightColor = context.ambientLightColor;

            var light0 = context.directionalLights[0];
            var light1 = context.directionalLights[1];
            var light2 = context.directionalLights[2];

            // Update the shader light parameters only when it changed.
            if (effect.DirectionalLight0.Enabled = light0.Enabled)
            {
                effect.DirectionalLight0.Direction     = light0.Direction;
                effect.DirectionalLight0.DiffuseColor  = light0.DiffuseColor;
                effect.DirectionalLight0.SpecularColor = light0.SpecularColor;
            }

            if (effect.DirectionalLight1.Enabled = light1.Enabled)
            {
                effect.DirectionalLight1.Direction     = light1.Direction;
                effect.DirectionalLight1.DiffuseColor  = light1.DiffuseColor;
                effect.DirectionalLight1.SpecularColor = light1.SpecularColor;
            }

            if (effect.DirectionalLight2.Enabled = light2.Enabled)
            {
                effect.DirectionalLight2.Direction     = light2.Direction;
                effect.DirectionalLight2.DiffuseColor  = light2.DiffuseColor;
                effect.DirectionalLight2.SpecularColor = light2.SpecularColor;
            }
        }
Beispiel #2
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;
 }
Beispiel #3
0
        internal static void EnableLighting(IEffectLights effect, float specularFraction)
        {
            int directionalLightIndex = 0;

            for (int i = 0; i < Lights.Count; i++)
            {
                // TODO: We have only made directional lighting!
                Light light = Lights[i];
                if (!light.enabled)
                {
                    continue;
                }
                switch (light.type)
                {
                case LightType.Spot:
                    break;

                case LightType.Directional:
                    switch (directionalLightIndex++)
                    {
                    case 0:
                        EnableDirectionalLight(effect.DirectionalLight0, light, specularFraction);
                        break;

                    case 1:
                        EnableDirectionalLight(effect.DirectionalLight1, light, specularFraction);
                        break;

                    case 2:
                        EnableDirectionalLight(effect.DirectionalLight2, light, specularFraction);
                        break;

                    default:
                        Debug.LogWarning("We only support up to three directional lights");
                        break;
                    }
                    break;

                case LightType.Point:
                    break;
                }
            }
            if (directionalLightIndex == 0)
            {
                effect.DirectionalLight0.Enabled = false;
            }
            if (directionalLightIndex <= 1)
            {
                effect.DirectionalLight1.Enabled = false;
            }
            if (directionalLightIndex <= 2)
            {
                effect.DirectionalLight2.Enabled = false;
            }
            effect.AmbientLightColor = RenderSettings.ambientLight;
        }
Beispiel #4
0
 internal static void EnableLighting(IEffectLights effect)
 {
     int directionalLightIndex = 0;
     for (int i = 0; i < Lights.Count; i++)
     {
         // TODO: We have only made directional lighting!
         Light light = Lights[i];
         if (!light.enabled)
         {
             continue;
         }
         switch (light.type)
         {
             case LightType.Spot:
                 break;
             case LightType.Directional:
                 switch (directionalLightIndex++)
                 {
                     case 0:
                         EnableDirectionalLight(effect.DirectionalLight0, light);
                         break;
                     case 1:
                         EnableDirectionalLight(effect.DirectionalLight1, light);
                         break;
                     case 2:
                         EnableDirectionalLight(effect.DirectionalLight2, light);
                         break;
                     default:
                         Debug.LogWarning("We only support up to three directional lights");
                         break;
                 }
                 break;
             case LightType.Point:
                 break;
         }
     }
     if (directionalLightIndex == 0)
     {
         effect.DirectionalLight0.Enabled = false;
     }
     if (directionalLightIndex <= 1)
     {
         effect.DirectionalLight1.Enabled = false;
     }
     if (directionalLightIndex <= 2)
     {
         effect.DirectionalLight2.Enabled = false;
     }
     effect.AmbientLightColor = RenderSettings.ambientLight;
 }
        public static void UpdateLighting(IEffectLights effect, SceneLight light)
        {
            effect.DirectionalLight0.Enabled       = light.DirectionalLights[0].Enabled;
            effect.DirectionalLight0.Direction     = light.DirectionalLights[0].Direction;
            effect.DirectionalLight0.DiffuseColor  = light.DirectionalLights[0].DiffuseColor * light.DirectionalLights[0].DiffuseIntensity;
            effect.DirectionalLight0.SpecularColor = light.DirectionalLights[0].SpecularColor * light.DirectionalLights[0].SpecularIntensity;

            effect.DirectionalLight1.Enabled       = light.DirectionalLights[1].Enabled;
            effect.DirectionalLight1.Direction     = light.DirectionalLights[1].Direction;
            effect.DirectionalLight1.DiffuseColor  = light.DirectionalLights[1].DiffuseColor * light.DirectionalLights[1].DiffuseIntensity;
            effect.DirectionalLight1.SpecularColor = light.DirectionalLights[1].SpecularColor * light.DirectionalLights[1].SpecularIntensity;

            effect.DirectionalLight2.Enabled       = light.DirectionalLights[2].Enabled;
            effect.DirectionalLight2.Direction     = light.DirectionalLights[2].Direction;
            effect.DirectionalLight2.DiffuseColor  = light.DirectionalLights[2].DiffuseColor * light.DirectionalLights[2].DiffuseIntensity;
            effect.DirectionalLight2.SpecularColor = light.DirectionalLights[2].SpecularColor * light.DirectionalLights[2].SpecularIntensity;
        }
        /// <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();
            }
        }
Beispiel #8
0
        public static void UpdateLighting(IEffectLights effect, SceneLight light)
        {
            effect.DirectionalLight0.Enabled = light.DirectionalLights[0].Enabled;
            effect.DirectionalLight0.Direction = light.DirectionalLights[0].Direction;
            effect.DirectionalLight0.DiffuseColor = light.DirectionalLights[0].DiffuseColor * light.DirectionalLights[0].DiffuseIntensity;
            effect.DirectionalLight0.SpecularColor = light.DirectionalLights[0].SpecularColor * light.DirectionalLights[0].SpecularIntensity;

            effect.DirectionalLight1.Enabled = light.DirectionalLights[1].Enabled;
            effect.DirectionalLight1.Direction = light.DirectionalLights[1].Direction;
            effect.DirectionalLight1.DiffuseColor = light.DirectionalLights[1].DiffuseColor * light.DirectionalLights[1].DiffuseIntensity;
            effect.DirectionalLight1.SpecularColor = light.DirectionalLights[1].SpecularColor * light.DirectionalLights[1].SpecularIntensity;

            effect.DirectionalLight2.Enabled = light.DirectionalLights[2].Enabled;
            effect.DirectionalLight2.Direction = light.DirectionalLights[2].Direction;
            effect.DirectionalLight2.DiffuseColor = light.DirectionalLights[2].DiffuseColor * light.DirectionalLights[2].DiffuseIntensity;
            effect.DirectionalLight2.SpecularColor = light.DirectionalLights[2].SpecularColor * light.DirectionalLights[2].SpecularIntensity;
        }
Beispiel #9
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;
            }
        }
        public void SetLights(IEffectLights effect)
        {
            effect.AmbientLightColor = _ambientLightColor;

            // HACK: Environent light doesn't allow lights off.
            if (!(effect is EnvironmentMapEffect))
                effect.LightingEnabled = !_lightsOff;

            SetLight(effect.DirectionalLight0, 0);
            SetLight(effect.DirectionalLight1, 1);
            SetLight(effect.DirectionalLight2, 2);
        }
Beispiel #11
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;
                }
            }
        }