This project is read-only.

inbuilt shader system

May 16, 2009 at 12:30 AM



I was tryng to import a mesh and just test the light like with Sphere geometry in the first tutorial but it didn't work the way I wanted. I look into th eother tutorial to find a hint but I din't see anything that différent.


Can just explain me how to add a inbuilt shader to a load mesh ?



May 16, 2009 at 12:54 AM
Edited May 16, 2009 at 12:56 AM

When a mesh is loaded, it internally creates a series of MeshData and GeometryData objects. Each GeometryData object defines the geometry and also the material that was originally defined in the model.
The material can be accessed directly, however, it also is used to generate a MaterialShader instance for that geometry (The MaterialShader property in GeometryData).
So, basically, when loading a model, it creates the materials for you, based on what's in the mesh.

The lights used by a material are separate, the model instance itself stores a list of lights - the 'LightCollection' property. Setting this will be used with the materials in the model.


If you want to set the lights, then that's easy - just set the LightsCollection on the model instance.

If you want to modify the material properties, such as textures, etc, you can with a bit more work. Just loop through the MeshData -> GeometryData in the model and edit the MaterialShaders.
I demonstrated something similar here:

To replace the shaders outright with custom shaders is trickier, because of things like animation, instancing, etc. For this you need to implement a shader provider class.


May 16, 2009 at 1:02 AM

thanks it works.

I really enjoy Xen ! I may have an hundred of question in the incomming weeks



May 16, 2009 at 7:19 AM

is it normal if I get some but about the tangent when I try to use MaterialShader.NormalMap = manager.Load<Texture2D>(@"box_normal"); ?



May 16, 2009 at 8:25 AM

Yes, normal mapping requires binormals and tangents in the vertex data.
A model will automatically generate these if it has a normal map in it's material definition at build time, or you can set the 'Generate Tangent Frames' property of the model (in the content project) to true. This will force the tangents to be generated.

May 17, 2009 at 11:03 AM

I tried with a normal mapped mesh it works perfectly thanks.


is there a way to force generate tangent frame using the in built geometry ?



May 17, 2009 at 11:11 AM

If you mean generate tangents and binormals from a vertices<> object, then no, there isn't any built in method. (you'd also need an index array if there were one).

It'd be possible, but very tricky and possibly very slow.
It'd be more efficient to generate them manually, sorry.