public void Render(GraphicsDevice graphicsDevice, Camera camera, Matrix worldMatrix) { // Render scene. graphicsDevice.BlendState = BlendState.Opaque; graphicsDevice.DepthStencilState = DepthStencilState.Default; graphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; graphicsDevice.SamplerStates[0] = SamplerStateUtility.ShadowMap; _meshEffect.VisualizeCascades = _settings.VisualizeCascades; _meshEffect.FilterAcrossCascades = _settings.FilterAcrossCascades; _meshEffect.FilterSize = _settings.FixedFilterSize; _meshEffect.Bias = _settings.Bias; _meshEffect.OffsetScale = _settings.OffsetScale; _meshEffect.ViewProjection = camera.ViewProjection; _meshEffect.CameraPosWS = camera.Position; _meshEffect.ShadowMap = _shadowMap; _meshEffect.LightDirection = _settings.LightDirection; _meshEffect.LightColor = _settings.LightColor; _boundingFrustum.Matrix = camera.ViewProjection; // Draw all meshes. foreach (var mesh in _scene.Meshes) { if (!_boundingFrustum.Intersects(mesh.BoundingSphere)) { continue; } foreach (var meshPart in mesh.MeshParts) { if (meshPart.PrimitiveCount > 0) { var basicEffect = (BasicEffect)meshPart.Effect; _meshEffect.DiffuseColor = basicEffect.DiffuseColor; _meshEffect.World = _sceneTransforms[mesh.ParentBone.Index] * worldMatrix; _meshEffect.Apply(); graphicsDevice.SetVertexBuffer(meshPart.VertexBuffer); graphicsDevice.Indices = meshPart.IndexBuffer; graphicsDevice.DrawIndexedPrimitives( PrimitiveType.TriangleList, meshPart.VertexOffset, 0, meshPart.NumVertices, meshPart.StartIndex, meshPart.PrimitiveCount); } } } }
private void Draw( CommandEncoder commandEncoder, MeshEffect meshEffect, EffectPipelineStateHandle pipelineStateHandle, IEnumerable <ModelMeshMaterialPass> materialPasses, RenderInstanceData instanceData) { commandEncoder.SetVertexBuffer(2, instanceData.WorldBuffer); if (Skinned) { meshEffect.SetSkinningBuffer(instanceData.SkinningBuffer); meshEffect.SetNumBones(NumBones); } meshEffect.SetSkinningEnabled(Skinned); meshEffect.SetMaterials(_materialsBuffer); meshEffect.SetTextures(_textures); commandEncoder.SetVertexBuffer(0, _vertexBuffer); foreach (var materialPass in materialPasses) { meshEffect.SetTextureIndices(materialPass.TextureIndicesBuffer); meshEffect.SetMaterialIndices(materialPass.MaterialIndicesBuffer); meshEffect.SetNumTextureStages(materialPass.NumTextureStages); commandEncoder.SetVertexBuffer(1, materialPass.TexCoordVertexBuffer); foreach (var meshPart in materialPass.MeshParts) { if (meshPart.PipelineStateHandle != pipelineStateHandle) { continue; } meshEffect.SetPrimitiveOffset(meshPart.StartIndex / 3); meshEffect.SetAlphaTest(meshPart.AlphaTest); meshEffect.SetTexturing(meshPart.Texturing); meshEffect.Apply(commandEncoder); commandEncoder.DrawIndexedInstanced( PrimitiveType.TriangleList, meshPart.IndexCount, instanceData.NumInstances, _indexBuffer, meshPart.StartIndex); } } }