Högni and Amy's game project

Thought in deed

Archive for the ‘Programming’ Category

Behave your trees

leave a comment »

I had already started using the good ol’ switch/case statement method to start implementing the AI for spacewars, when I bumped into Behave for Unity3D. This library implements Behavior tree logic in Unity with a nice visual editor. After a few minor niggles, finding workarounds for a couple of bugs and a lot of wrapping my head around how B-Trees really work, I love it.

The concept is so simple and intuitive, once you get past the initial (low) barrier you can be pushing out complex algorithms in no time. When you have your logic mapped out, all you have to do is implement the individual action and decorator code snippets. This fits so well into the Unity style scripting – where there are lots of short scripts – the workflow does just that.

Behaviour trees also integrate fairly well with FSMs, so you don’t have to instantly throw them out. You can have the tree actions change states or alter parameters if needed, maintaining interoperability.

There are other tools out there to create B-trees, but this one for Unity is very much sufficient.

Written by Högni Gylfason

20.07.10 at 19:16

Unity3D vs. UDK

with one comment

We tried to use UDK.

The GUI editor is pretty good. Graphics are nice. The material editor is great. Asset browser, definate plus.

Scripting engine. Bad. Really outdated bad. Script prefab instantiation non-existent. Bye bye.

We are now using Unity3D

Asset browser is primitive but usable.

Low level integration with asset tools. Native 3dmax conversions. Very nice! Assets even open directly from the asset browser in the art tools or visual studio (for scripts).

Game object component stack. Fucking brilliant. Dragging scripts onto a game object and setting its properties directly. So god damn good.

Shame there is no node based material editor or GUI editor. There is an excellent 3rd party GUI editor available though, so that’s not a problem.

UDK hereby kicked to the curb. Welcome Unity!

Written by Högni Gylfason

20.07.10 at 11:19

Standard material done

leave a comment »

For all practical purposes I consider the work I’ve done on exporting the standard material type from ShaderFX to Ogre3D readable format, complete.

It now exports correctly all the subnodes and lights and the calculations all appear correct now, including advanced functions like projection mapping based on vertex positions, alphas based on vertex colors, ambient occlusion, diffuse, normal and more.

I’m now going to proceed to create an exporter for the Glow and Subsurface scattering materials, as those are the ones I see us needing in the near future.

Even though this has taken a lot of time to implement I think it will save us untold hours of work in the future. This applies even if I leave it standing at only the standard material.

Unfortunately I’ve not gotten any further response from Luminox on this matter, so I don’t know if this work will ever reach the public. I can’t release it on my own as it is proprietary software that I’ve made extensions on…

I’ll update on this status when I can.

Written by Högni Gylfason

06.10.09 at 12:43

Ogre3D and ShaderFX… Living in (almost) perfect harmony

leave a comment »

There is some progress on the exporter for Ogre in ShaderFX. I’ve almost finished the StandardMat support. There are still some niggles, like light projection not rotating with the object, but I think I can figure that out.

Planet in Max with SFX Panel

Planet in Max with SFX Panel

This is the material as displayed in Max in mode CGFX. The normal is scaled and UV Rotated with time input and the diffuse texture is plugged into the ambient socket with a modulator as well as the diffuse socket.

Planet in OgreMax Viewport

Planet in OgreMax Viewport

This is the same object in the OgreMax viewport in max. Looks pretty neat. I also tested it in the OgreMax viewer to test the UV rotation and it works like a charm with the time input. Below are the shader settings in the OgreMax material editor panes.

Diffuse shader settings

Diffuse shader settings

Ambient shader settings

Ambient shader settings

I’m quite happy with it so far. I’m hoping to do the Glow material next, not because it’s next in order of general importance, but because I need it 🙂

Written by Högni Gylfason

26.09.09 at 13:21

Ogre3D and ShaderFX

leave a comment »

I’ve been using Lumonix’ excellent ShaderFX plugin for 3ds max to generate shaders, and then altering the generated shaders to comply with the Ogre3D  material framework. To say the least this has not been without problems. The main problem is that Ogre3D is designed to be very open as far as the content pipeline is concerned. The material structure is very flexible, but at the same time suffers for it in its inability to use techniques native to shader programs.

A shader program usually follows a set pattern. At the top are definitions of constants and “tweakables”. These are the inputs to the shader program, such as colours, values for specular and gloss levels, texture samplers and more. After these follow definitions of the matrix structures the shader requires to work properly, such as the world matrix, world view projection, view matrix and many many others. Thereafter come definitions for input and output structs for the vertex and fragment shaders. Then comes the real meat, the vertex and fragment shader entry points themselves and their internals. Last come the technique definitions, where everything is woven together.

Most 3d engines read the techniques and apply materials to the objects in the 3d representation. Ogre goes about this circuitously at best. It does not read the techniques at all, but leaves it to the programmer to know the entry point names and then declare shader definitions that are in turn used in material scripts. To add to this, most shaders exported from any shader authoring software do not compile properly with Ogre without alteration. For example matrix multiplication is reversed when multiplying multi column matrices with single column (vectors). The shader declarations need to include the entry point name, the target (vs_2_0, ps_3_0, etc) and the source file for the shader proper. Then you define the bindings for the world matrices to the shader inputs, e.g. wvp -> world view projection matrix. These, in turn, are read by entirely different files, called material files, where you point to the declarations you made in the previous files, called program files.

This process is labourious to say the least. So, I’ve been trying to find ways to automate some of the steps. The biggest hurdle at first was editing the shader files to conform to Ogre’s format. If you’ve ever seen the contents of a shader file, you know what I mean when I say that it is not pretty. Second was writing the shader definition files (.program) and thirdly including it in the materials. I wrote a short python script that cleaned up the shader file for comsumption by ogre, but it seemed a cumbersome way to do things, parsing the code of the shader and then outputting it to a new file. After trying this with a few different shaders, I decided to trash the project and try something else.

I started looking into how ShaderFX is constructed. Without going into too much detail (proprietary software etc), I managed to build a half assed export extension that exports Ogre compatible shaders. Now I just need to get some support from Lumonix to extend the extension so that it generates the necessary material and program files. So far Lumonix have responded well to this project, so I hope they will be able to answer the questions I have raised with them.

Hopefully to be continued.

Written by Högni Gylfason

24.09.09 at 13:04

Ogre3D issues

leave a comment »

I’ve been poking at the texturing and graphics part of things for the last couple of days after slaying the physics/networking dragon. In the course of things I’m running into the biggest issue I find with using the Ogre3D rendering engine. The issue revolves around shaders.

There are several good shader authoring utilities out there. One of the more prominent is FX Composer from nVidia, another is ShaderFX, a plugin for 3ds Max developed by Luminox. There is also one from ATI, RenderMonkey. These work in different formats, FX Composer primarily works with cgfx which ShaderFX also supports. ShaderFX also has limited support for DXSAS, a format from Microsoft for their Direct3D API. RenderMonkey works with glsl as far as I can tell. RenderMonkey seems to have been designed by aliens for use by unknown other entities, and I have been unable to penetrate its obtuse user interface.

After a bit of research and testing we decided – when I say we, I really mean me – to use ShaderFX as it integrates  nicely into Max and manages to export fairly clean DXSAS code. RenderMonkey, as mentioned, seems not to be designed for use by mere mortals, and FX Composer seems to be developed for those who are not really creating shaders but are more playing around. At least converting shader programs from FX Composer to a usable format, e.g. hlsl or glsl, takes too much work. It’s preview function is also too finicky and hard to debug.

So ShaderFX. When I export from there, I “only” need to do the following:

  • Register the texture maps to be used in the texture units.
  • Change the vertex program input struct by actually populating the binormal property. Yay for cross(In.tangent, In.normal).
  • Remove a bunch of extra code that is not neccesary, e.g. lots of variable declarations in the script itself.

Not so bad. But then I need to create the Ogre3D program and material definition scripts. This drives me nuts – every time. Through a series of obscure and less than satisfactorily documented definitions and assignments I get a usable pair of scripts. This involves binding things like teh world view projection matrix, the world matrix, inverse world matrix, inverse transpose world view projection matrix – seriously, that’s a real term – and bind various scene variables to the shader defined variables. Joy.

There really needs to be an easier, i.e. less time-consuming as it is not really all that complicated once accomplished the first time. In other engines there are tools to export directly from FX Composer and also from ShaderFX and use the techniques both generate directly, without all this confumbleness along the way. I’m looking into a method to convert the scripts generated by ShaderFX to program/material files directly at this time. I’m not going to spend a lot of time on it though, as the various options would take months to account for in any conversion software. I can see myself creating support for constants, texture units and lights, but I doubt I’d go further than that.

I was quite happy using Ogre3D up until this point, and I’m pretty sure we’ll finish the current iteration of our game using it as our renderer, but I am very happy that I kept Ogre3D’s event system out of the main message loop and kept to a more standard process flow control. This means I can plug another renderer in without much trouble at a future date.

As it stands, the advantages (replaceable scene manager, decent quality, FREE) outweigh the disadvantages (cumbersome content tool chain, poor documentation), but I continue to look around for a replacement.

Written by Högni Gylfason

21.07.09 at 16:59

Posted in Programming

Tagged with , ,