Exemplo n.º 1
0
        /// <summary>
        /// set all effect parameters.
        /// </summary>
        void OnEffectProcess(object sender, GameModel.RenderingCustomEffectEventArgs e)
        {
            RenderLighting lighting = e.RenderTracer.Lighting;

            e.Effect.Parameters["LightColor"].SetValue(
                lighting.diffuseColor.ToVector4());

            e.Effect.Parameters["AmbientLightColor"].SetValue(
                lighting.ambientColor.ToVector4());

            e.Effect.Parameters["Shininess"].SetValue(1.0f);

            e.Effect.Parameters["SpecularPower"].SetValue(12.0f);

            e.Effect.Parameters["EnvironmentMap"].SetValue(
                RobotGameGame.CurrentGameLevel.GameWorld.TextureCubeMap);

            e.Effect.Parameters["World"].SetValue(e.World);

            e.Effect.Parameters["View"].SetValue(e.RenderTracer.View);

            e.Effect.Parameters["Projection"].SetValue(e.RenderTracer.Projection);

            e.Effect.Parameters["LightPosition"].SetValue(
                Vector3.Negate(lighting.direction) * 1000);
        }
Exemplo n.º 2
0
        protected override void OnDraw(RenderTracer renderTracer)
        {
            RenderState renderState = renderTracer.Device.RenderState;

            //  Transform culling sphere
            if (enableCulling)
            {
                cullingSphere.Center = Vector3.Transform(cullingSphereLocalCenter,
                                                         RootAxis * Collide.TransformMatrix);

                //  Check if the the model is contained inside or intersecting
                //  with the frustum.
                ContainmentType cullingResult =
                    renderTracer.Frustum.Contains(cullingSphere);

                //  Draw a the model If inside in the frustum
                if (cullingResult == ContainmentType.Disjoint)
                {
                    return;
                }
            }

            renderState.AlphaTestEnable  = alphaTestEnable;
            renderState.AlphaBlendEnable = alphaBlendEnable;
            renderState.AlphaFunction    = alphaFunction;
            renderState.SourceBlend      = sourceBlend;
            renderState.DestinationBlend = destinationBlend;
            renderState.BlendFunction    = blendFunction;

            renderState.ReferenceAlpha         = referenceAlpha;
            renderState.DepthBufferEnable      = depthBufferEnable;
            renderState.DepthBufferWriteEnable = depthBufferWriteEnable;
            renderState.DepthBufferFunction    = depthBufferFunction;
            renderState.CullMode = cullMode;

            // Draw the model.
            for (int i = 0; i < ModelData.model.Meshes.Count; i++)
            {
                ModelMesh mesh = ModelData.model.Meshes[i];

                for (int j = 0; j < mesh.Effects.Count; j++)
                {
                    //  call a entried custom effect processing
                    if (RenderingCustomEffect != null)
                    {
                        //  Shader custom processing
                        RenderingCustomEffect(this,
                                              new RenderingCustomEffectEventArgs(renderTracer, mesh,
                                                                                 mesh.Effects[j], BoneTransforms[mesh.ParentBone.Index]));
                    }
                    else if (mesh.Effects[j] is BasicEffect)
                    {
                        BasicEffect effect = (BasicEffect)mesh.Effects[j];

                        //  Apply fog
                        if (renderTracer.Fog != null && ActiveFog)
                        {
                            RenderFog fog = renderTracer.Fog;

                            effect.FogEnabled = fog.enabled;

                            if (effect.FogEnabled)
                            {
                                effect.FogStart = fog.start;
                                effect.FogEnd   = fog.end;
                                effect.FogColor = fog.color.ToVector3();
                            }
                        }
                        else
                        {
                            effect.FogEnabled = false;
                        }

                        if (ActiveLighting)
                        {
                            //  Apply lighting
                            if (renderTracer.Lighting != null)
                            {
                                RenderLighting lighting = renderTracer.Lighting;

                                effect.LightingEnabled = lighting.enabled;

                                if (effect.LightingEnabled)
                                {
                                    effect.AmbientLightColor =
                                        lighting.ambientColor.ToVector3();

                                    effect.DirectionalLight0.Enabled = true;

                                    effect.DirectionalLight0.Direction =
                                        lighting.direction;

                                    effect.DirectionalLight0.DiffuseColor =
                                        lighting.diffuseColor.ToVector3();

                                    effect.DirectionalLight0.SpecularColor =
                                        lighting.specularColor.ToVector3();
                                }
                            }

                            if (Lighting != null)
                            {
                                effect.LightingEnabled = true;

                                for (int cnt = 0; cnt < Lighting.Length; cnt++)
                                {
                                    RenderLighting        lighting   = Lighting[cnt];
                                    BasicDirectionalLight basicLight = null;

                                    if (cnt == 0)
                                    {
                                        basicLight = effect.DirectionalLight1;
                                    }
                                    else if (cnt == 1)
                                    {
                                        basicLight = effect.DirectionalLight2;
                                    }
                                    else
                                    {
                                        continue;
                                    }

                                    if (lighting.enabled)
                                    {
                                        basicLight.Enabled = true;

                                        basicLight.Direction =
                                            lighting.direction;

                                        basicLight.DiffuseColor =
                                            lighting.diffuseColor.ToVector3();

                                        basicLight.SpecularColor =
                                            lighting.specularColor.ToVector3();
                                    }
                                    else
                                    {
                                        basicLight.Enabled = false;
                                    }
                                }
                            }

                            if (renderTracer.Lighting == null && Lighting == null)
                            {
                                effect.LightingEnabled = false;
                            }

                            //  Apply material
                            if (Material != null)
                            {
                                effect.Alpha = Material.alpha;

                                effect.DiffuseColor =
                                    Material.diffuseColor.ToVector3();

                                effect.SpecularColor =
                                    Material.specularColor.ToVector3();

                                effect.SpecularPower =
                                    Material.specularPower;

                                effect.EmissiveColor =
                                    Material.emissiveColor.ToVector3();

                                effect.VertexColorEnabled =
                                    Material.vertexColorEnabled;

                                effect.PreferPerPixelLighting =
                                    Material.preferPerPixelLighting;
                            }
                        }
                        else
                        {
                            effect.LightingEnabled = false;
                        }

                        //  Apply transform
                        effect.World      = BoneTransforms[mesh.ParentBone.Index];
                        effect.View       = renderTracer.View;
                        effect.Projection = renderTracer.Projection;
                    }
                }

                mesh.Draw();
            }
        }