Exemplo n.º 1
0
        public static void DrawNow(Mesh mesh, Material.Material material, Matrix4 worldMatrix = default)
        {
            if (material == null)
            {
                material = AssetDatabase.GetAsset <Material.Material>("Resources/Materials/Fallback.mat");
            }

            if (worldMatrix == default)
            {
                worldMatrix = Matrix4.Identity;
            }

            if (worldMatrix != default)
            {
                material.Shader.SetUniform("u_World", worldMatrix);
            }

            UniformBuffers.BindAll(material.Shader.Handle);
            material.Bind();
            mesh.VertexArray.Bind();
            mesh.IndexBuffer.Bind();

            GL.DrawElements(PrimitiveType.Triangles, mesh.IndexBuffer.Length, DrawElementsType.UnsignedInt, 0);

            mesh.VertexArray.Unbind();
            mesh.IndexBuffer.Unbind();
            material.Unbind();
            UniformBuffers.UnbindAll();
        }
Exemplo n.º 2
0
        public static void DrawQueue()
        {
            FrameBuffer.Bind();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            FrameBuffer.Unbind();

            fbo.Bind();
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
            fbo.Unbind();

            for (int i = 0; i < drawQueue.Count; i++)
            {
                bool isLast  = i == drawQueue.Count - 1;
                bool drawFBO = false;

                var call = (BatchDraw)drawQueue[i];

                if (!isLast)
                {
                    var callPlusOne = (BatchDraw)drawQueue[i + 1];
                    if (!call.material.Shader.IsTransparent && callPlusOne.material.Shader.IsTransparent)
                    {
                        drawFBO = true;
                    }
                }
                else
                {
                    drawFBO = true;
                }

                //Try to set this...
                call.material.Bind();
                UniformBuffers.BindAll(call.material.Shader.Handle);

                for (int j = 0; j < call.draws.Count; j++)
                {
                    if (call.draws[j].worldMatrix != default)
                    {
                        call.material.Shader.SetUniform("u_World", call.draws[j].worldMatrix);
                        call.material.Shader.BindUniform("u_World");
                    }

                    call.draws[j].mesh.VertexArray.Bind();
                    call.draws[j].mesh.IndexBuffer.Bind();

                    if (call.material.Shader.IsTransparent)
                    {
                        if (call.material.Shader.ContainsUniform("u_Src"))
                        {
                            call.material.SetScreenSourceTexture("u_Src", fbo.ColorHandle, 1);
                            call.material.Shader.BindUniform("u_Src");
                        }

                        if (call.material.Shader.ContainsUniform("u_Depth"))
                        {
                            call.material.SetScreenSourceTexture("u_Depth", fbo.DepthHandle, 2);
                            call.material.Shader.BindUniform("u_Depth");
                        }
                    }

                    FrameBuffer.Bind();

                    try
                    {
                        var length = call.draws[j].mesh.IndexBuffer.Length;
                        GL.DrawElements(PrimitiveType.Triangles, length,
                                        DrawElementsType.UnsignedInt, 0);
                    }
                    catch
                    {
                        Debug.Log("Error when trying to render an object");
                    }

                    call.draws[j].mesh.VertexArray.Unbind();
                    call.draws[j].mesh.IndexBuffer.Unbind();
                    FrameBuffer.Unbind();
                }
                call.draws.Clear();

                if (isLast)
                {
                    UniformBuffers.UnbindAll();
                    call.material.Unbind();
                }

                if (drawFBO)
                {
                    if (!isLast)
                    {
                        GL.BlitNamedFramebuffer(FrameBuffer.Handle, fbo.Handle, 0, 0, FrameBuffer.Width, FrameBuffer.Height, 0, 0, fbo.Width, fbo.Height,
                                                ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit, BlitFramebufferFilter.Nearest);
                    }
                    else
                    {
                        GL.Disable(EnableCap.DepthTest);

                        FBOMaterial.SetScreenSourceTexture("u_Src", FrameBuffer.ColorHandle, 0);
                        DrawNow(BlitMesh, FBOMaterial);

                        GL.Enable(EnableCap.DepthTest);
                        fbo.Unbind();
                    }
                }
            }

            drawQueue.Clear();
        }