void RenderMaterial(DeviceContext context, D3DAnimRenderComponent render, CMOAnimateMeshComponent mesh) { var materialCount = mesh.Materials.Count; // If there are no materials if (materialCount == 0) { RenderMeshes(context, render, mesh.SubMeshes); return; } // Draw sub-meshes grouped by material for (var mIndx = 0; mIndx < materialCount; mIndx++) { // Retrieve sub meshes for this material var subMeshesForMaterial = mesh.SubMeshes.Where(x => x.MaterialIndex == mIndx).ToList(); try { // If the material buffer is available and there are submeshes // using the material update the PerMaterialBuffer if (subMeshesForMaterial.Count > 0) { // update the PerMaterialBuffer constant buffer var material = new ConstantBuffers.PerMaterial() { Ambient = mesh.Materials[mIndx].Ambient, Diffuse = mesh.Materials[mIndx].Diffuse, Emissive = mesh.Materials[mIndx].Emissive, Specular = mesh.Materials[mIndx].Specular, SpecularPower = mesh.Materials[mIndx].SpecularPower, UVTransform = mesh.Materials[mIndx].UVTransform, }; // Bind textures to the pixel shader int texIndxOffset = mIndx * CMOAnimateMeshComponent.MaxTextures; material.HasTexture = (uint)(render.TextureViews.Get()[texIndxOffset] != null ? 1 : 0); // 0=false context.PixelShader.SetShaderResources(0, render.TextureViews.Get().GetRange(texIndxOffset, CMOAnimateMeshComponent.MaxTextures).ToArray()); context.PixelShader.SetSampler(0, render.SamplerState.Get()); context.UpdateSubresource(ref material, render.PerMaterialBuffer.Get()); } } catch (Exception ex) { ex.ToString(); } // For each sub-mesh RenderMeshes(context, render, subMeshesForMaterial); } }
void RenderMeshes(DeviceContext context, D3DAnimRenderComponent render, List <CMOAnimateMeshComponent.SubMesh> meshes) { foreach (var subMesh in meshes) { try { // Ensure the vertex buffer and index buffers are in range // if (subMesh.VertexBufferIndex < vertexBuffers.Count && subMesh.IndexBufferIndex < indexBuffers.Count) { // Retrieve and set the vertex and index buffers var vertexBuffer = render.GetVertexBuffers((int)subMesh.VertexBufferIndex); context.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertexBuffer, Unsafe.SizeOf <Vertex>(), 0)); context.InputAssembler.SetIndexBuffer(render.GetIndexBuffers((int)subMesh.IndexBufferIndex), SharpDX.DXGI.Format.R16_UInt, 0); // Set topology context.InputAssembler.PrimitiveTopology = render.PrimitiveTopology; // } // Draw the sub-mesh (includes Primitive count which we multiply by 3) // The submesh also includes a start index into the vertex buffer context.DrawIndexed((int)subMesh.PrimCount * 3, (int)subMesh.StartIndex, 0); } catch (Exception ex) { ex.ToString(); } } }
void UpdateRenderComponent(Device device, D3DAnimRenderComponent render, CMOAnimateMeshComponent mesh) { render.ClearIndexBuffer(); render.ClearVertexBuffer(); render.TextureViews.Set(new List <ShaderResourceView>()); try { // Initialize vertex buffers for (int indx = 0; indx < mesh.VertexBuffers.Count; indx++) { var vb = mesh.VertexBuffers[indx]; Vertex[] vertices = new Vertex[vb.Length]; for (var i = 0; i < vb.Length; i++) { // Retrieve skinning information for vertex var skin = new SkinningVertex(); if (mesh.SkinningVertexBuffers.Count > 0) { skin = mesh.SkinningVertexBuffers[indx][i]; } // Create vertex vertices[i] = new Vertex(vb[i].Position, vb[i].Normal, (Vector4)vb[i].Color, vb[i].UV, skin); } render .AddVertexBuffer(Buffer.Create(device, BindFlags.VertexBuffer, vertices)) .DebugName = "VertexBuffer_" + indx.ToString(); } // Initialize index buffers for (var i = 0; i < mesh.IndexBuffers.Count; i++) { var ib = mesh.IndexBuffers[i]; render .AddIndexBuffer(Buffer.Create(device, BindFlags.IndexBuffer, ib)) .DebugName = "IndexBuffer_" + i.ToString(); } var tloader = new TextureLoader(device); //Load textures if a material has any. foreach (var mat in mesh.Materials) { for (var i = 0; i < mat.Textures.Length; i++) { if (System.IO.File.Exists(mat.Textures[i])) { render.TextureViews.Get().Add(tloader.LoadShaderResource(new FileInfo(mat.Textures[i]))); } else { render.TextureViews.Get().Add(null); } } } } catch (Exception ex) { ex.ToString(); } render.PerMaterialBuffer.Set(new Buffer(device, Unsafe.SizeOf <ConstantBuffers.PerMaterial>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)); render.PerObjectBuffer.Set(new Buffer(device, Unsafe.SizeOf <ConstantBuffers.PerObject>(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)); render.PerArmatureBuffer.Set(new Buffer(device, ConstantBuffers.PerArmature.Size(), ResourceUsage.Default, BindFlags.ConstantBuffer, CpuAccessFlags.None, ResourceOptionFlags.None, 0)); }