// Sets up this effect and selects a technique to render with.
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            // For now, we'll accept whatever SimpleMaterial would use.
            base._SetupEffect(srs, materialData);

            return "Technique1";
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            Assert.Fatal(_texture != null, "PostProcessMaterial._SetupGlobalParameters - Texture has not been set!");

            base._SetupGlobalParameters(srs, materialData);
            _baseTextureParameter.SetValue(_texture);
        }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            if (_blendAmount < 1.0f)
            {
                srs.Gfx.Device.RenderState.AlphaBlendEnable = true;
                srs.Gfx.Device.RenderState.SourceBlend = Blend.SourceAlpha;
                srs.Gfx.Device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
            }

            return "CopyTechnique";
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            BasicEffect basicEffect = _effect.Instance as BasicEffect;
            if (basicEffect != null)
            {
                basicEffect.EnableDefaultLighting();
                basicEffect.View = srs.View;
                basicEffect.Projection = srs.Projection;
            }
            else
            {
                EffectParameter viewParam = _effect.Instance.Parameters["View"];
                viewParam.SetValue(srs.View);

                EffectParameter projectionParam = _effect.Instance.Parameters["Projection"];
                projectionParam.SetValue(srs.Projection);
            }
        }
 /// <summary>
 /// This is called once on the material per render instance batch after SetupEffect, and before any
 /// calls to SetupPass. It should be used to set the values for any effect parameters that are the
 /// same across all objects using the material. The base method does nothing.
 /// </summary>
 /// <param name="srs">The current scene render state.</param>
 /// <param name="materialData">Object specific data for the material.</param>
 protected virtual void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
 {
 }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            if ((_texture.IsNull || _texture.IsInvalid) && !string.IsNullOrEmpty(VideoFilename))
                _texture = ResourceManager.Instance.CreateResource<Texture2D>(_videoPlayer.GetTexture());

            if (_isCopyPass)
                return "CopyTechnique";

            if (!_texture.IsNull)
            {
                if (_isColorBlended)
                    return "ColorTextureBlendTechnique";

                return "TexturedTechnique";
            }

            return "ColoredTechnique";
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            if (_texture.Instance != null && _texture.Instance.IsDisposed)
                _texture.Invalidate();

            if (!_texture.IsNull)
                EffectManager.SetParameter(_baseTextureParameter, Texture.Instance);
        }
 protected override void _RenderGroup(List<RenderInstance> renderInstances, RenderMaterial material, MaterialInstanceData materialData, SceneRenderState srs, GraphicsDevice d3d)
 {
     (material as IRefractionMaterial).SetTexture(_refractionTexture);
     base._RenderGroup(renderInstances, material, materialData, srs, d3d);
 }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            srs.Gfx.Device.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
            srs.Gfx.Device.RenderState.SourceBlend = Blend.SourceAlpha;

            return base._SetupEffect(srs, materialData);
        }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            for (int i = 0; i < _textureBinds.Count; i++)
            {
                if ((_textureBinds[i]._texture.IsNull || _textureBinds[i]._texture.IsInvalid) && !string.IsNullOrEmpty(_textureBinds[i].TextureFilename))
                    _textureBinds[i]._texture = ResourceManager.Instance.LoadTexture(_textureBinds[i].TextureFilename);
            }

            if (_techniqueChain.Count < 1)
                return base._SetupEffect(srs, materialData);

            ShaderProfile shaderProfile = srs.Gfx.ShaderProfile;

            TechniqueChainEntry technique = _techniqueChain[0];
            for (int i = 1; i < _techniqueChain.Count; i++)
            {
                if ((technique.Profile > shaderProfile) || (_techniqueChain[i].Profile > technique.Profile && _techniqueChain[i].Profile <= shaderProfile))
                    technique = _techniqueChain[i];
            }

            return technique.Name;
        }
        protected override void _RenderGroup(List<RenderInstance> renderInstances, RenderMaterial material, MaterialInstanceData materialData, SceneRenderState srs, GraphicsDevice d3d)
        {
            // If the first instance is a 3D shape then make sure the
            // state is set for rendering them.
            if (renderInstances[0].Type == RenderInstance.RenderInstanceType.Mesh3D)
            {
                if (!_rendering3D)
                    _Setup3DState(srs, d3d);
            }
            else
            {
                // Restore the 2D rendering state.
                if (_rendering3D)
                    _Cleanup3DState(srs, d3d);
            }

            // Render as normal.
            base._RenderGroup(renderInstances, material, materialData, srs, d3d);
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            if (_textures[0] != null)
                EffectManager.SetParameter(_diffuseMap1Parameter, _textures[0]);
            if (_textures[1] != null)
                EffectManager.SetParameter(_diffuseMap2Parameter, _textures[1]);
            if (_textures[2] != null)
                EffectManager.SetParameter(_diffuseMap3Parameter, _textures[2]);
            if (_textures[3] != null)
                EffectManager.SetParameter(_diffuseMap4Parameter, _textures[3]);

            _mapInfoParameter.SetValue(_levelData);
        }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            // cast the material instance data
            ClipMapMaterialInstanceData data = materialData as ClipMapMaterialInstanceData;

            // get the world view index from the scene render state
            int worldViewIndex = srs.WorldViewIndex;

            // create a clip map for this world view index if none exists
            if (!_clipMaps.ContainsKey(worldViewIndex))
                _GenerateDuplicateClipMap(worldViewIndex);

            // init start level and end level
            int endLevel;
            int startLevel;

            // check if we have a material instance data
            if (data != null)
            {
                // we have material instance data:
                // grab the start level and end level
                endLevel = data.EndLevel;
                startLevel = data.StartLevel;
            }
            else
            {
                // no data:
                // set default start and end level
                endLevel = _baseClipMap.ClipStackDepth - 1;
                startLevel = (int)MathHelper.Clamp(_baseClipMap.ClipStackDepth - 4, 0, _baseClipMap.ClipStackDepth);
            }

            // get the start level to end level delta
            int delta = endLevel - startLevel;

            // set up the texture fields and map info for the shader
            int i;
            ClipStackEntry entry;

            for (i = 0; i < delta + 1 && i < 4; i++)
            {
                entry = _clipMaps[worldViewIndex].ClipLevels[endLevel - i];

                _levelData[i] = new Quaternion(
                    entry.ClipCenter.X * entry.ScaleFactor,
                    entry.ClipCenter.Y * entry.ScaleFactor,
                    entry.ScaleFactor,
                    0.0f);

                _textures[i] = entry.Texture;
            }

            // null out the end of the array so we don't set old data
            for (; i < 4; i++)
                _textures[i] = null;

            // store the number of textures used
            int texturesUsed = delta + 1;

            // choose a technique.
            if (_isLightingEnabled && srs.Gfx.ShaderProfile >= ShaderProfile.PS_2_0)
            {
                // select a shader model 2 technique with lighting
                if (texturesUsed == 1)
                    return "ClipMap1_2_lit";
                else if (texturesUsed == 2)
                    return "ClipMap2_2_lit";
                else if (texturesUsed == 3)
                    return "ClipMap3_2_lit";
                else
                    return "ClipMap4_2_lit";
            }
            else
            {
                // select a shader model 1 technique (no terrain lighting)
                if (texturesUsed == 1)
                    return "ClipMap1_1";
                else if (texturesUsed == 2)
                    return "ClipMap2_1";
                else if (texturesUsed == 3)
                    return "ClipMap3_1";
                else
                    return "ClipMap4_1";
            }
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            if (!_baseTexture.IsNull)
                EffectManager.SetParameter(_baseTextureParameter, _baseTexture.Instance);

            if (IsLightingEnabled)
            {
                bool doNormalMap = !_normalMap.IsNull;
                bool doSpecular = _specularPower > 0.0f && _specularIntensity > 0.0f && srs.Gfx.ShaderProfile >= ShaderProfile.PS_2_0;

                if (doNormalMap)
                    EffectManager.SetParameter(_normalMapParameter, _normalMap.Instance);

                if (doSpecular)
                {
                    EffectManager.SetParameter(_cameraPositionParameter, srs.CameraPosition);
                    EffectManager.SetParameter(_specularColorParameter, _specularColor);
                    EffectManager.SetParameter(_specularPowerParameter, _specularPower);
                    EffectManager.SetParameter(_specularIntensityParameter, _specularIntensity);
                }
            }
        }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            if (_baseTexture.IsNull && !string.IsNullOrEmpty(_textureFilename))
                _baseTexture = ResourceManager.Instance.LoadTexture(_textureFilename);

            if (_normalMap.IsNull && !string.IsNullOrEmpty(_normalMapFilename))
                _normalMap = ResourceManager.Instance.LoadTexture(_normalMapFilename);

            bool doNormalMap = !_normalMap.IsNull;
            bool doSpecular = _specularPower > 0.0f && _specularIntensity > 0.0f && srs.Gfx.ShaderProfile >= ShaderProfile.PS_2_0;

            if (_lightingMode == LightingMode.None)
                return "NoLightingTechnique";

            else if (_lightingMode == LightingMode.PerVertex)
            {
                if (doNormalMap)
                {
                    if (doSpecular)
                        return "VertexLightingSpecularNormalMapTechnique";
                    else
                        return "VertexLightingNormalMapTechnique";
                }
                else if (doSpecular)
                    return "VertexLightingSpecularTechnique";
                else
                    return "VertexLightingTechnique";
            }

            else
            {
                if (srs.Gfx.ShaderProfile < ShaderProfile.PS_3_0)
                {
                    if (doNormalMap)
                    {
                        if (doSpecular)
                            return "PixelLightingSpecularNormalMapTechnique";
                        else
                            return "PixelLightingNormalMapTechnique";
                    }
                    else if (doSpecular)
                        return "PixelLightingSpecularTechnique";
                    else
                        return "PixelLightingTechnique";
                }
                else
                {
                    if (doNormalMap)
                    {
                        if (doSpecular)
                            return "PixelLightingSpecularNormalMapTechnique3_0";
                        else
                            return "PixelLightingNormalMapTechnique3_0";
                    }
                    else if (doSpecular)
                        return "PixelLightingSpecularTechnique3_0";
                    else
                        return "PixelLightingTechnique3_0";
                }
            }
        }
        protected override string _SetupEffect(GarageGames.Torque.SceneGraph.SceneRenderState srs, MaterialInstanceData materialData)
        {
            if (!_cubemap.IsInitialized)
                _cubemap.Create(_isDynamic, _cubemapSize, _camera);

            return base._SetupEffect(srs, materialData);
        }
        protected override void _SetupGlobalParameters(GarageGames.Torque.SceneGraph.SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            _cubeTextureParameter.SetValue(_cubemap.Texture);
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            for (int i = 0; i < _textureBinds.Count; i++)
                EffectManager.SetParameter(_textureBinds[i].Parameter, _textureBinds[i]._texture.Instance);

            for (int i = 0; i < _floatBinds.Count; i++)
                EffectManager.SetParameter(_floatBinds[i].Parameter, _floatBinds[i].Value);

            EffectManager.SetParameter(_sasViewMatrix, srs.View);
            EffectManager.SetParameter(_sasViewInverseMatrix, Matrix.Invert(srs.View));
            EffectManager.SetParameter(_sasViewProjectionMatrix, srs.View * srs.Projection);
            EffectManager.SetParameter(_sasProjectionMatrix, srs.Projection);

            EffectManager.SetParameter(_sasTimeNow, srs.SceneGraph.CurrentTime);
            EffectManager.SetParameter(_sasTimeLast, srs.SceneGraph.LastTime);
            EffectManager.SetParameter(_sasTimeElapsed, srs.SceneGraph.CurrentTime - srs.SceneGraph.LastTime);
            EffectManager.SetParameter(_sasTimeFrameNumber, srs.SceneGraph.CurrentFrame);

            if (_viewportScaleAndOffset != null)
            {
                Viewport view = GFXDevice.Instance.Device.Viewport;
                float width = GFXDevice.Instance.Device.DepthStencilBuffer.Width;
                float height = GFXDevice.Instance.Device.DepthStencilBuffer.Height;

                Vector4 refractview = new Vector4();
                refractview.X = ((float)view.Width / width) * 0.5f;
                refractview.Y = ((float)view.Height / height) * 0.5f;
                refractview.Z = ((float)view.X / width) + refractview.X;
                refractview.W = ((float)view.Y / height) + refractview.Y;
                _viewportScaleAndOffset.SetValue(refractview);
            }

            if (_viewportUVMinMax != null)
            {
                Viewport view = GFXDevice.Instance.Device.Viewport;
                float width = GFXDevice.Instance.Device.DepthStencilBuffer.Width;
                float height = GFXDevice.Instance.Device.DepthStencilBuffer.Height;

                Vector4 uvminmax = new Vector4();
                uvminmax.X = (float)(view.X + 0.5) / width;
                uvminmax.Y = (float)(view.Y + 0.5) / height;
                uvminmax.Z = (float)(view.Width + view.X - 0.5) / width;
                uvminmax.W = (float)(view.Height + view.Y - 0.5) / height;
                _viewportUVMinMax.SetValue(uvminmax);
            }
        }
 protected override string _SetupEffect(GarageGames.Torque.SceneGraph.SceneRenderState srs, MaterialInstanceData materialData)
 {
     base._SetupEffect(srs, materialData);
     Effect.Instance.Parameters["Tint"].SetValue(tint);
     return "TintTechnique";
 }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            _bloomIntensityParameter.SetValue(_bloomIntensity);
            _baseIntensityParameter.SetValue(_baseIntensity);
            _bloomSaturationParameter.SetValue(_bloomSaturation);
            _baseSaturationParameter.SetValue(_baseSaturation);
            _texture2Parameter.SetValue(_texture2);
        }
 protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
 {
     base._SetupGlobalParameters(srs, materialData);
     EffectManager.SetParameter(_opacityParameter, _blendAmount);
     EffectManager.SetParameter(_worldViewProjectionParameter, Matrix.Identity);
 }
 protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
 {
     base._SetupGlobalParameters(srs, materialData);
     _bloomThresholdParameter.SetValue(_bloomThreshold);
 }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            EffectManager.SetParameter(_fogColorParameter, _fogColor);
            EffectManager.SetParameter(_fogNearDistanceParameter, _fogNearDistance);
            EffectManager.SetParameter(_fogFarDistanceParameter, _fogFarDistance);
            EffectManager.SetParameter(_camPosParameter, srs.CameraPosition);
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            _sampleWeights[0] = _ComputeGaussian(0);
            _sampleOffsets[0].X = 0.0f;
            _sampleOffsets[0].Y = 0.0f;

            float totalWeights = _sampleWeights[0];

            for (int i = 0; i < (_sampleCount >> 1); i++)
            {
                float weight = _ComputeGaussian(i + 1);

                _sampleWeights[i * 2 + 1] = weight;
                _sampleWeights[i * 2 + 2] = weight;

                totalWeights += weight * 2;
                float sampleOffset = i * 2 + 1.5f;

                Vector2 delta = _size * sampleOffset;
                _sampleOffsets[i * 2 + 1] = delta;
                _sampleOffsets[i * 2 + 2] = -delta;
            }

            for (int i = 0; i < _sampleCount; i++)
                _sampleWeights[i] /= totalWeights;

            _weightsParameter.SetValue(_sampleWeights);
            _offsetsParameter.SetValue(_sampleOffsets);
        }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            if (!_textureMissing && (_texture.IsNull || _texture.IsInvalid) && !string.IsNullOrEmpty(_textureFilename))
            {
                _texture = ResourceManager.Instance.LoadTexture(_textureFilename);
                if (_texture.IsNull)
                    _textureMissing = true;
            }

            if (_isCopyPass)
                return "CopyTechnique";

            if (!_texture.IsNull)
            {
                if (_isColorBlended)
                    return "ColorTextureBlendTechnique";

                return "TexturedTechnique";
            }

            return "ColoredTechnique";
        }
        protected override string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
        {
            if (_detailTex.IsNull && !string.IsNullOrEmpty(_textureFilename))
                _detailTex = ResourceManager.Instance.LoadTexture(_textureFilename);

            srs.Gfx.Device.RenderState.SourceBlend = Blend.DestinationColor;
            srs.Gfx.Device.RenderState.DestinationBlend = Blend.SourceColor;

            return base._SetupEffect(srs, materialData);
        }
 /// <summary>
 /// Resets the render instance's fields to defaults.
 /// </summary>
 public virtual void Reset()
 {
     VertexBuffer = null;
     IndexBuffer = null;
     VertexDeclaration = null;
     Material = null;
     MaterialInstanceData = null;
     Opacity = 1.0f;
     UTextureAddressMode = TextureAddressMode.Clamp;
     VTextureAddressMode = TextureAddressMode.Clamp;
     ObjectTransform = Matrix.Identity;
     SortPoint = Vector3.Zero;
     IsSortPointSet = false;
     VertexSize = 0;
     PrimitiveType = PrimitiveType.TriangleStrip;
     BaseVertex = 0;
     VertexCount = 0;
     StartIndex = 0;
     PrimitiveCount = 0;
     Type = RenderInstanceType.UndefinedType;
     MaterialSortKey = 0;
     GeometrySortKey = 0;
     IsReset = true;
 }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            if (!_detailTex.IsNull)
                _detailTexParameter.SetValue(_detailTex.Instance);

            _detailTexRepeatParameter.SetValue(_detailTexRepeat);
            _detailCenterParameter.SetValue(srs.CameraPosition);
            _detailDistanceParameter.SetValue(_detailDistance);
        }
        protected override void _SetupGlobalParameters(SceneRenderState srs, MaterialInstanceData materialData)
        {
            base._SetupGlobalParameters(srs, materialData);

            if (!_texture.IsNull)
                EffectManager.SetParameter(_baseTextureParameter, CurrentVideoFrameTexture.Instance);
        }
 /// <summary>
 /// This is called from SetupEffect prior to rendering with this material to setup any effect
 /// specific render states or properties and select the technique to use for the effect. It is
 /// not necessary to call the base method from derived classes as all it does is return the first
 /// technique in the effect's technique collection.
 /// </summary>
 /// <param name="srs">The current scene render state.</param>
 /// <param name="materialData">Object specific data for the material.</param>
 /// <returns>The name of the technique to use for the effect.</returns>
 protected virtual string _SetupEffect(SceneRenderState srs, MaterialInstanceData materialData)
 {
     return _effect.Instance.Techniques[0].Name;
 }