Per face materials

Started by
4 comments, last by NightcrawlerEX 11 years, 2 months ago
Hi all,

I have been looking through a directx x file exporter and have realised that it exports materials in a way different than i am used to. I am used to assigning a 1 material per object and when it comes to the shader, just passing in the material to be used.

The way this exporter does it is by first outputting each material to be used, then outputting a set of per face indices to the relevant materials.

I was wondering if anyone knew how to go about this, or possibly a link to some texts regarding this?

Also is there a way to tell which face you are working on during the pixel shader? This would be good for many reasons but i dont think this is likely

[EDIT] so far my train of though is sorting all the faces per material into seperate objects
Advertisement

What the exporter does is give you a number of submeshes. Now a single object (or mesh) is build up from a number of submeshes.

Think of it like a human body, both the head and the arms are part of it, yet completely different.

Change your code layout (vertex and index buffer for each object) to a vertex buffer and an array of submeshes.

Each submesh contains an index buffer and a material pointer.

During the rendering, simply loop over the submesh array and bind the material and draw the submesh.

If you want to know witch triangle you are processing, you could use PrimitiveID (http://msdn.microsoft.com/en-us/library/windows/desktop/bb205118(v=vs.85).aspx ) and pass it to your pixel shader.

It's far more efficient to order on material in the render call then it is to do this in the pixel shader. For meshes in an object that share the material you only upload that material once and then draw them, with the pixel shader approach you need to upload all of the materials for all draw calls of that object. Sometimes a submesh can also need a different shader to be rendered with, if you do that decision in the pixelshader you need to make that choice for each pixel which isn't great for performance.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Im using directx 10. Do i just loop and call ID3D10Device::DrawIndexed() for each index array? Is this efficient or is there a better way?

Also the primative id semantic seems cool. Seems usefull for a number of effects

Im using directx 10. Do i just loop and call ID3D10Device::DrawIndexed() for each index array? Is this efficient or is there a better way?

yes, that will work. If you sort the submeshes based on material, this will be as efficient as it gets.

thanks, works great.

This topic is closed to new replies.

Advertisement