Esempio n. 1
0
        private bool ApplyShaders(RenderType renderType, IRenderModule renderModule, Matrix localWorldMatrix, Matrix?view, Matrix viewProjection, MeshLibrary meshLib, int index, int outlineId, bool outlined)
        {
            if (renderType == RenderType.Opaque ||
                renderType == RenderType.ShadowLinear ||
                renderType == RenderType.ShadowOmnidirectional ||
                renderType == RenderType.SubsurfaceScattering ||
                renderType == RenderType.Forward)
            {
                renderModule.Apply(localWorldMatrix, view, viewProjection);
            }
            else if (renderType == RenderType.Hologram)
            {
                Shaders.HologramEffectParameter_World.SetValue(localWorldMatrix);
                Shaders.HologramEffectParameter_WorldViewProj.SetValue(localWorldMatrix * viewProjection);

                Shaders.HologramEffect.CurrentTechnique.Passes[0].Apply();
            }
            else if (renderType == RenderType.IdRender || renderType == RenderType.IdOutline)
            {
                Shaders.IdRenderEffectParameterWorldViewProj.SetValue(localWorldMatrix * viewProjection);

                int id = meshLib.GetWorldMatrices()[index].Id;

                if (renderType == RenderType.IdRender)
                {
                    Shaders.IdRenderEffectParameterColorId.SetValue(IdGenerator.GetColorFromId(id).ToVector4());

                    Shaders.IdRenderEffectDrawId.Apply();
                }
                if (renderType == RenderType.IdOutline)
                {
                    //Is this the Id we want to outline?
                    if (id == outlineId)
                    {
                        graphicsDevice.RasterizerState = RasterizerState.CullNone;

                        Shaders.IdRenderEffectParameterWorld.SetValue(localWorldMatrix);

                        if (outlined)
                        {
                            Shaders.IdRenderEffectDrawOutline.Apply();
                        }
                        else
                        {
                            Shaders.IdRenderEffectDrawId.Apply();
                        }
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
Esempio n. 2
0
        public void Draw(RenderType renderType, Matrix viewProjection, bool lightViewPointChanged = false, bool hasAnyObjectMoved = false, bool outlined = false, int outlineId = 0, Matrix?view = null, IRenderModule renderModule = null)
        {
            SetBlendAndRasterizerState(renderType);

            if (renderType == RenderType.ShadowLinear || renderType == RenderType.ShadowOmnidirectional)
            {
                //For shadowmaps we need to find out whether any object has moved and if so if it is rendered. If yes, redraw the whole frame, if no don't do anything
                if (!CheckShadowMapUpdateNeeds(lightViewPointChanged, hasAnyObjectMoved))
                {
                    return;
                }
            }

            for (int index1 = 0; index1 < Index; index1++)
            {
                MaterialLibrary matLib = MaterialLib[index1];

                if (matLib.Index < 1)
                {
                    continue;
                }

                //if none of this materialtype is drawn continue too!
                bool isUsed = false;

                for (int i = 0; i < matLib.Index; i++)
                {
                    MeshLibrary meshLib = matLib.GetMeshLibrary()[i];

                    //If it's set to "not rendered" skip
                    for (int j = 0; j < meshLib.Rendered.Length; j++)
                    {
                        if (meshLib.Rendered[j])
                        {
                            isUsed = true;
                            //if (meshLib.GetWorldMatrices()[j].HasChanged)
                            //    hasAnyObjectMoved = true;
                        }

                        if (isUsed)    // && hasAnyObjectMoved)
                        {
                            break;
                        }
                    }
                }

                if (!isUsed)
                {
                    continue;
                }

                //Count the draws of different materials!

                MaterialEffect material = matLib.GetMaterial();

                //Check if alpha or opaque!
                if (renderType == RenderType.Opaque && material.IsTransparent || renderType == RenderType.Opaque && material.Type == MaterialEffect.MaterialTypes.ForwardShaded)
                {
                    continue;
                }
                if (renderType == RenderType.Hologram && material.Type != MaterialEffect.MaterialTypes.Hologram)
                {
                    continue;
                }
                if (renderType != RenderType.Hologram && material.Type == MaterialEffect.MaterialTypes.Hologram)
                {
                    continue;
                }

                if (renderType == RenderType.SubsurfaceScattering &&
                    material.Type != MaterialEffect.MaterialTypes.SubsurfaceScattering)
                {
                    continue;
                }

                if (renderType == RenderType.Forward &&
                    material.Type != MaterialEffect.MaterialTypes.ForwardShaded)
                {
                    continue;
                }

                //Set the appropriate Shader for the material
                if (renderType == RenderType.ShadowOmnidirectional || renderType == RenderType.ShadowLinear)
                {
                    if (!material.HasShadow)
                    {
                        continue;
                    }
                }

                if (renderType != RenderType.IdRender && renderType != RenderType.IdOutline)
                {
                    GameStats.MaterialDraws++;
                }

                PerMaterialSettings(renderType, material, renderModule);

                for (int i = 0; i < matLib.Index; i++)
                {
                    MeshLibrary meshLib = matLib.GetMeshLibrary()[i];

                    //Initialize the mesh VB and IB
                    graphicsDevice.SetVertexBuffer(meshLib.GetMesh().VertexBuffer);
                    graphicsDevice.Indices = (meshLib.GetMesh().IndexBuffer);
                    int primitiveCount = meshLib.GetMesh().PrimitiveCount;
                    int vertexOffset   = meshLib.GetMesh().VertexOffset;
                    //int vCount = meshLib.GetMesh().NumVertices;
                    int startIndex = meshLib.GetMesh().StartIndex;

                    //Now draw the local meshes!
                    for (int index = 0; index < meshLib.Index; index++)
                    {
                        //If it's set to "not rendered" skip
                        //if (!meshLib.GetWorldMatrices()[index].Rendered) continue;
                        if (!meshLib.Rendered[index])
                        {
                            continue;
                        }

                        Matrix localWorldMatrix = meshLib.GetWorldMatrices()[index].World;

                        if (!ApplyShaders(renderType, renderModule, localWorldMatrix, view, viewProjection, meshLib, index,
                                          outlineId, outlined))
                        {
                            continue;
                        }
                        GameStats.MeshDraws++;

                        graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, vertexOffset, startIndex,
                                                             primitiveCount);
                    }
                }

                //Reset to
                if (material.RenderCClockwise)
                {
                    graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
                }
            }
        }