コード例 #1
0
        private void RenderAll(Material overrideMaterial)
        {
            currentMesh   = null;
            currentShader = null;

            foreach (var shaderPair in renderers)
            {
                if (!engine.shaderManager.GetShaderByHandle(shaderPair.Key).WriteMask&& overrideMaterial != null)
                {
                    continue;
                }

                if (overrideMaterial != null)
                {
                    currentShader = engine.shaderManager.GetShaderByHandle(overrideMaterial.ShaderHandle);
                }
                else
                {
                    currentShader = engine.shaderManager.GetShaderByHandle(shaderPair.Key);
                }
                currentShader.Activate();

                foreach (var materialPair in shaderPair.Value)
                {
                    SetZWrite(materialPair.Key.Shader.ZWrite);

                    foreach (var texturePair in materialPair.Key.textures)
                    {
                        texturePair.Value.Activate(texturePair.Key);
                    }

                    if (overrideMaterial == null)
                    {
                        foreach (var bufferKeyPair in materialPair.Key.structuredBuffers)
                        {
                            bufferKeyPair.Value.Activate(bufferKeyPair.Key);
                        }

                        foreach (var bufferKeyPair in materialPair.Key.structuredPixelsBuffers)
                        {
                            bufferKeyPair.Value.Activate(bufferKeyPair.Key);
                        }

                        foreach (var bufferKeyPair in materialPair.Key.structuredVertexBuffers)
                        {
                            bufferKeyPair.Value.Activate(bufferKeyPair.Key);
                        }
                    }

                    foreach (var meshPair in materialPair.Value)
                    {
                        if (currentMesh != meshPair.Key)
                        {
                            meshPair.Key.VerticesBuffer.Activate(0);
                            meshPair.Key.IndicesBuffer.Activate(0);
                            currentMesh = meshPair.Key;
                        }

                        if (currentShader.Instancing)
                        {
                            if (instancesArray.Length < meshPair.Value.Count)
                            {
                                instancesArray = new Matrix[meshPair.Value.Count];
                            }

                            for (int i = 0; i < meshPair.Value.Count; ++i)
                            {
                                instancesArray[i] = meshPair.Value[i].LocalToWorldMatrix;
                            }
                            instancesBuffer.UpdateBuffer(instancesArray);
                            instancesBuffer.Activate(1);

                            engine.Device.DrawIndexedInstanced(meshPair.Key.IndexCount, meshPair.Value.Count, 0, 0, 0);
                        }
                        else
                        {
                            foreach (var instance in meshPair.Value)
                            {
                                objectData.WorldMatrix = instance.LocalToWorldMatrix;
                                objectBuffer.UpdateBuffer(ref objectData);
                                engine.Device.DrawIndexed(meshPair.Key.IndexCount, 0, 0);
                            }
                        }
                    }
                }
            }
        }