Exemple #1
0
        public void SetParameters(Material material)
        {
            if (material.InternalEffect != null)
            {
                if (material.InternalEffect is BasicEffect)
                {
                    BasicEffect be = (BasicEffect)material.InternalEffect;

                    alphaEffect.Alpha              = be.Alpha;
                    alphaEffect.DiffuseColor       = be.DiffuseColor;
                    alphaEffect.Texture            = be.Texture;
                    alphaEffect.VertexColorEnabled = be.VertexColorEnabled;
                }
                else
                {
                    Log.Write("Passed internal effect is not BasicEffect, so we can not apply the " +
                              "effect to this shader", Log.LogLevel.Warning);
                }
            }
            else
            {
                alphaEffect.Alpha        = material.Diffuse.W;
                alphaEffect.DiffuseColor = Vector3Helper.GetVector3(material.Diffuse);
                alphaEffect.Texture      = material.Texture;
            }
        }
 public virtual void SetParameters(GoblinXNA.Graphics.Environment environment)
 {
     basicEffect.FogEnabled = environment.FogEnabled;
     if (environment.FogEnabled)
     {
         basicEffect.FogStart = environment.FogStartDistance;
         basicEffect.FogEnd   = environment.FogEndDistance;
         basicEffect.FogColor = Vector3Helper.GetVector3(environment.FogColor);
     }
 }
        public void SetParameters(Material material)
        {
            if (material.InternalEffect != null)
            {
                if (material.InternalEffect is BasicEffect)
                {
                    internalEffect       = (BasicEffect)material.InternalEffect;
                    internalEffect.Alpha = originalAlphas[internalEffect] * material.Diffuse.W;
                    if (MixMaterialDiffuseWithTexture)
                    {
                        internalEffect.DiffuseColor *= Vector3Helper.GetVector3(material.Diffuse);
                    }

                    if (lightsChanged)
                    {
                        internalEffect.LightingEnabled        = basicEffect.LightingEnabled;
                        internalEffect.PreferPerPixelLighting = basicEffect.PreferPerPixelLighting;
                        internalEffect.AmbientLightColor      = basicEffect.AmbientLightColor;
                        if (basicEffect.LightingEnabled)
                        {
                            DirectionalLight[] lights = { internalEffect.DirectionalLight0,
                                                          internalEffect.DirectionalLight1, internalEffect.DirectionalLight2 };

                            int numLightSource = lightSources.Count;
                            for (int i = 0; i < numLightSource; i++)
                            {
                                lights[i].Enabled       = true;
                                lights[i].DiffuseColor  = Vector3Helper.GetVector3(lightSources[i].Diffuse);
                                lights[i].Direction     = lightSources[i].Direction;
                                lights[i].SpecularColor = Vector3Helper.GetVector3(lightSources[i].Specular);
                            }
                            for (int i = numLightSource; i < MaxLights; i++)
                            {
                                lights[i].Enabled = false;
                            }
                        }
                    }
                }
                else
                {
                    Log.Write("Passed internal effect is not BasicEffect, so we can not apply the " +
                              "effect to this shader", Log.LogLevel.Warning);
                }
            }
            else
            {
                basicEffect.Alpha          = material.Diffuse.W;
                basicEffect.DiffuseColor   = Vector3Helper.GetVector3(material.Diffuse);
                basicEffect.SpecularColor  = Vector3Helper.GetVector3(material.Specular);
                basicEffect.EmissiveColor  = Vector3Helper.GetVector3(material.Emissive);
                basicEffect.SpecularPower  = material.SpecularPower;
                basicEffect.TextureEnabled = material.HasTexture;
                basicEffect.Texture        = material.Texture;
            }
        }
        public void SetParameters(Material material)
        {
            if (material.InternalEffect != null)
            {
                if (material.InternalEffect is SkinnedEffect)
                {
                    internalEffect       = (SkinnedEffect)material.InternalEffect;
                    internalEffect.Alpha = originalAlphas[alphaIndexer] * material.Diffuse.W;

                    internalEffect.PreferPerPixelLighting = skinnedEffect.PreferPerPixelLighting;
                    internalEffect.AmbientLightColor      = skinnedEffect.AmbientLightColor;

                    if (lightsChanged)
                    {
                        if (lightSources.Count > 0)
                        {
                            DirectionalLight[] lights = { internalEffect.DirectionalLight0,
                                                          internalEffect.DirectionalLight1, internalEffect.DirectionalLight2 };

                            int numLightSource = lightSources.Count;
                            for (int i = 0; i < numLightSource; i++)
                            {
                                lights[i].Enabled       = true;
                                lights[i].DiffuseColor  = Vector3Helper.GetVector3(lightSources[i].Diffuse);
                                lights[i].Direction     = lightSources[i].Direction;
                                lights[i].SpecularColor = Vector3Helper.GetVector3(lightSources[i].Specular);
                            }
                        }
                    }

                    alphaIndexer = (alphaIndexer + 1) % originalAlphas.Length;
                }
                else
                {
                    Log.Write("Passed internal effect is not BasicEffect, so we can not apply the " +
                              "effect to this shader", Log.LogLevel.Warning);
                }
            }
            else
            {
                skinnedEffect.Alpha         = material.Diffuse.W;
                skinnedEffect.DiffuseColor  = Vector3Helper.GetVector3(material.Diffuse);
                skinnedEffect.SpecularColor = Vector3Helper.GetVector3(material.Specular);
                skinnedEffect.EmissiveColor = Vector3Helper.GetVector3(material.Emissive);
                skinnedEffect.SpecularPower = material.SpecularPower;
                skinnedEffect.Texture       = material.Texture;
            }
        }
        public void SetParameters(List <LightNode> globalLights, List <LightNode> localLights)
        {
            bool ambientSet = false;

            ClearBasicEffectLights();
            lightSources.Clear();
            LightNode lNode             = null;
            Vector4   ambientLightColor = new Vector4(0, 0, 0, 1);

            // traverse the local lights in reverse order in order to get closest light sources
            // in the scene graph
            for (int i = localLights.Count - 1; i >= 0; i--)
            {
                lNode = localLights[i];
                // only set the ambient light color if it's not set yet and not the default color (0, 0, 0, 1)
                if (!ambientSet && (!lNode.AmbientLightColor.Equals(ambientLightColor)))
                {
                    ambientLightColor = lNode.AmbientLightColor;
                    ambientSet        = true;
                }

                if (lightSources.Count < MaxLights)
                {
                    // skip the light source if not enabled or not a directional light
                    if (!lNode.LightSource.Enabled || (lNode.LightSource.Type != LightType.Directional))
                    {
                        continue;
                    }

                    LightSource source = new LightSource();
                    source.Diffuse   = lNode.LightSource.Diffuse;
                    source.Direction = lNode.LightSource.TransformedDirection;
                    source.Specular  = lNode.LightSource.Specular;

                    lightSources.Add(source);

                    // If there are already 3 lights, then skip other lights
                    if (lightSources.Count >= MaxLights)
                    {
                        break;
                    }
                }
            }

            // Next, traverse the global lights in normal order
            for (int i = 0; i < globalLights.Count; i++)
            {
                lNode = globalLights[i];
                // only set the ambient light color if it's not set yet and not the default color (0, 0, 0, 1)
                if (!ambientSet && (!lNode.AmbientLightColor.Equals(ambientLightColor)))
                {
                    ambientLightColor = lNode.AmbientLightColor;
                    ambientSet        = true;
                }

                if (lightSources.Count < MaxLights)
                {
                    // skip the light source if not enabled or not a directional light
                    if (!lNode.LightSource.Enabled || (lNode.LightSource.Type != LightType.Directional))
                    {
                        continue;
                    }

                    LightSource source = new LightSource();
                    source.Diffuse   = lNode.LightSource.Diffuse;
                    source.Direction = lNode.LightSource.TransformedDirection;
                    source.Specular  = lNode.LightSource.Specular;

                    lightSources.Add(source);

                    // If there are already 3 lights, then skip other lights
                    if (lightSources.Count >= MaxLights)
                    {
                        break;
                    }
                }
            }

            ambientLight = Vector3Helper.GetVector3(ambientLightColor);

            if (lightSources.Count > 0)
            {
                DirectionalLight[] lights = { basicEffect.DirectionalLight0,
                                              basicEffect.DirectionalLight1, basicEffect.DirectionalLight2 };

                bool atLeastOneLight = false;
                int  numLightSource  = lightSources.Count;
                for (int i = 0; i < numLightSource; i++)
                {
                    lights[i].Enabled       = true;
                    lights[i].DiffuseColor  = Vector3Helper.GetVector3(lightSources[i].Diffuse);
                    lights[i].Direction     = lightSources[i].Direction;
                    lights[i].SpecularColor = Vector3Helper.GetVector3(lightSources[i].Specular);
                    atLeastOneLight         = true;
                }

                basicEffect.LightingEnabled = atLeastOneLight;
            }

            basicEffect.AmbientLightColor = ambientLight;
            lightsChanged = true;
        }