Пример #1
0
        protected override void OnBeginApply(DrawingContext context, Material previousMaterial)
        {
            var previousAlphaTestMaterial = previousMaterial as AlphaTestMaterial;

            if (previousAlphaTestMaterial == null)
            {
                effect.View       = context.View;
                effect.Projection = context.Projection;

                MaterialHelper.ApplyFog(context as DrawingContext3D, effect);
            }

            if (alpha != Constants.Alpha)
            {
                effect.Alpha = alpha;
            }
            if (diffuseColor.HasValue)
            {
                effect.DiffuseColor = diffuseColor.Value;
            }
            if (referenceAlpha.HasValue)
            {
                effect.ReferenceAlpha = referenceAlpha.Value;
            }
            if (alphaFunction.HasValue)
            {
                effect.AlphaFunction = alphaFunction.Value;
            }

            if (previousAlphaTestMaterial == null || previousTexture != texture)
            {
                previousTexture = effect.Texture = texture;
            }

            effect.World = world;
            effect.VertexColorEnabled = VertexColorEnabled;

            if (SamplerState != null)
            {
                GraphicsDevice.SamplerStates[0] = SamplerState;
            }

            effect.CurrentTechnique.Passes[0].Apply();
        }
Пример #2
0
        protected override void OnBeginApply(DrawingContext context, Material previousMaterial)
        {
            // Check if the previous material used is a also a BasicMaterial. If so, we can
            // guarantee that the current shader has all the correct global parameters, and
            // there's no need to set them again.
            var previousBasicMaterial = previousMaterial as BasicMaterial;

            if (previousBasicMaterial == null || previousBasicMaterial.lightingEnabled != lightingEnabled)
            {
                // Update parameters that are global and shared between different instances.
                // This includes view projection matrices and global directional lights.
                effect.View       = context.View;
                effect.Projection = context.Projection;

                var context3D = context as DrawingContext3D;
                if (lightingEnabled)
                {
                    MaterialHelper.ApplyLights(context3D, effect);
                }
                MaterialHelper.ApplyFog(context3D, effect);
            }

            // Update per instance shader parameters only when the value does not equal to
            // the default one. Remember to reset it back to defaults during EndApply.
            // Since most objects will have their materials left at the default state,
            // most of these parameter won't be updated to the shader.
            if (alpha != Constants.Alpha)
            {
                effect.Alpha = alpha;
            }
            if (diffuseColor.HasValue)
            {
                effect.DiffuseColor = diffuseColor.Value;
            }
            if (emissiveColor.HasValue)
            {
                effect.EmissiveColor = emissiveColor.Value;
            }
            if (specularColor.HasValue)
            {
                effect.SpecularColor = specularColor.Value;
            }
            if (specularPower.HasValue)
            {
                effect.SpecularPower = specularPower.Value;
            }

            // Setting the texture parameter has a small overhead, so compare it upfront.
            if (previousBasicMaterial == null || texture != previousTexture)
            {
                previousTexture = effect.Texture = texture;
            }

            // Update shader parameters that are always different for each instance.
            effect.World = world;

            // Update shader parameters that has little or no overhead.
            effect.TextureEnabled         = texture != null;
            effect.LightingEnabled        = lightingEnabled;
            effect.PreferPerPixelLighting = preferPerPixelLighting;
            effect.VertexColorEnabled     = vertexColorEnabled;

            // Finally apply the shader.
            pass.Apply();

            // Update sampler state
            if (samplerState != null)
            {
                context.graphics.SamplerStates[0] = samplerState;
            }
        }