Sunday, August 11, 2013

Setting shader values

Both cocos3D and XNA support the use of shaders. In the former case, these shader programs are written in GLSL, while XNA makes use of Microsoft's HLSL. As MonoGame (which Cocos3D-XNA  relies on) is a cross-platform implementation of XNA, there is a need to be shader language-agnostic, which by and large MonoGame achieves.

With that in mind, I wanted to show how a user can interact with a shader in the port Cocos3D-XNA. Loading a shader file is done by:

CC3ShaderProgram shader = new CC3ShaderProgram(0, "MyShader", semanticDelegate, "shaderFilename");
// Params info: tag, name, knows shader var types, location of shader file     

Once we have the shader object, we can start playing around with its parameters. Below is a sample of using a shader, originally written in HLSL, to incorporate lighting effects into the scene



Such an effect was produced by setting values to around ten different shader uniforms (constants), which is achieved by:

CC3ShaderUniform shaderUniform = shader.UniformNamed("uniformName");
shaderUniform.SetValue(someValue); 
// someValue can be of type float, vector, texture etc  

You might think manually setting so many uniforms is a cumbersome task, and in general, it would be nice if a user could use a bunch of common effects like multi texturing without having to roll their own shader. Thankfully, cocos3d has you covered, and in the upcoming weeks I'll (hopefully) showcase how this done in Cocos3D-XNA.


No comments:

Post a Comment