🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Material Systems in Lua

Published February 06, 2009
Advertisement


Material System Updated with Lua

I have been spending the past few days on upgrading my material system to be closely integrated with the available scripting system. I've posted several times in the past on the design of the material system, which is more or less based on render views. The concept is that each style of rendering can be encapsulated in a render view. The material definition basically provides everything needed to render some geometry in each of the render view types that are going to be used.

The materials are defined as such in XML files. While XML is a fine data description technique, it isn't necessarily easy to modify the format of the file loader. As an example, here is one of the XML files for a simple per-vertex lighting material:

"1.0" ?>"Phong">	"0">1 1 1 1	"1">0 0 0 0	"2">0 0 0 0	"3">0 0 0 0	"4">0 0 0 0	"5">0 0 0 0	"6">0 0 0 0	"7">0 0 0 0	"perspective">		phong_vertex_lighting.fx		"screen_space_ambient_occlusion">		view_space_depth.fx		


This is a nice clean format, but it fell apart quickly when you try to use shared render views across multiple instances of a material - a good example being shadow mapping. Any object that receives shadows from a light need to have a shadow map render view run before it can be rendered in the normal perspective view. So how to define that relationship in an XML file? To make it even more fun, what happens if you have six shadow casting lights in a scene? How can you specify these relationships with an XML file?

The answer is: not very easily. This is were Lua really shines - it is great for data description, and is a scripting language which provides a very easy to use syntax. After wrapping the appropriate classes and making some new interfaces for scripting objects, I am now able to put the same functionality into a simple Lua function that creates the object directly. Check this out:

function CreatePhongMaterial()  local material = MaterialDX9:Create();  material:SetRenderFlag( "perspective", [1] );  material:SetRenderEffect( "perspective", RendererDX9.LoadEffect("Data/Effects/Phong_Vertex_Lighting.fx") );	  return material;end


The clarity of the script is worth the three days of work to add the scripting functionality. Here's one more example of a significantly more complex material which uses dual paraboloid environment maps - I'll spare you the XML version:

function CreateMirrorMaterial()  local material = MaterialDX9:Create();	  paraboloid = ViewDualParaboloidEnvMap:Create( [256], [256] );  front = RendererDX9.GetRTTexID( paraboloid:GetRTID( [0] ) );  back = RendererDX9.GetRTTexID( paraboloid:GetRTID( [0] ) );  material:SetRenderFlag( "perspective", [1] );  material:SetRenderEffect( "perspective", RendererDX9.LoadEffect("dual_paraboloid_environment_map_lookup.fx") );	  material:SetTexture( "perspective", [0], front );  material:SetTexture( "perspective", [1], back );  material:AddRenderView( "perspective", paraboloid );  material:SetRenderFlag( "paraboloid_env_map", [1] );  material:SetRenderEffect( "paraboloid_env_map", RendererDX9.LoadEffect("dual_paraboloid_environment_map_lookup_gen.fx") );  material:SetTexture( "paraboloid_env_map", [0], front );  material:SetTexture( "paraboloid_env_map", [1], back );  material:AddRenderView( "paraboloid_env_map", paraboloid );	  return material;end


This is another one of those achievements that will pay dividends in the long run - being able to rapidly iterate work on a complex material at runtime will certainly be a benefit.

The Scripts Move On

Now that I am satisfied with the material system, it's time to get back to work on FeedBack. After all, that is why I started improving the scripting system in the first place... I've been considering how to structure the component and machine framework, and think I have a pretty good idea of how I want to do it.

In addition, I am beginning to explore volume rendering for some work on my thesis (I need to finish my Master's sooner or later...) - which should definitely produce some interesting visual results! [grin]
Previous Entry Node Classes Scripted
Next Entry 3D UI + Scripting
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement