Rendering.NET allows your to create vertex and pixel shaders with 100% .NET code. In the process of render, Rendering.NET takes the shaders and compile them to native shaders languages (HLSL for DirectX, GLSL for OpenGL, GLSL ES for WebGL, etc)

Examples of shaders written in C#

This is a simple vertex shader:
Matrix4x4 world;
Matrix4x4 view;
Matrix4x4 projection;

[ShaderType]
public struct VSIn
{
	[Position]
	public Vector3 Position;
}

[ShaderType]
public struct VSOut
{
	[Projected]
	public Vector4 Projected;
	[Diffuse]
	public Vector4 Diffuse;
}

public VSOut VertexShader(VSIn In)
{
	VSOut Out = new VSOut();

	var worldPosition = GMath.mul(new Vector4(In.Position, 1), world);
	var viewPosition = GMath.mul(worldPosition, view);
		
	Out.Projected = GMath.mul(viewPosition, projection);
	Out.Diffuse = new Vector4(1.0f, 0.0f, 0.0f, 1.0f);

	return Out;
}

This is simple pixel shader:
[ShaderType]
public struct PSIn
{
	[Diffuse]
	public Vector4 Diffuse;
}

[ShaderType]
public struct PSOut
{
	[Diffuse]
	public Vector4 Diffuse;
}

public PSOut PixelShader(PSIn In)
{
	PSOut Out = new PSOut();

	Out.Diffuse = In.Diffuse;

	return Out;
}

This is a complete example of vertex and pixel shader with Gouraud ilumination technique:
[ShaderType]
public struct VSIn
{
	[Position]
	public Vector3 Position;
	[Normal]
	public Vector3 Normal;
	[TextureCoordinates]
	public Vector4 TexCoord;
}

[ShaderType]
public struct VSOut
{
	[Projected]
	public Vector4 Projected;
	[Diffuse]
	public Vector4 Diffuse;
	[Specular]
	public Vector4 Specular;
	[TextureCoordinates]
	public Vector4 TexCoord;
}

[ShaderType]
public struct PSIn
{
	[Diffuse]
	public Vector4 Diffuse;
	[Specular]
	public Vector4 Specular;
	[TextureCoordinates]
	public Vector4 TexCoord;
}

[ShaderType]
public struct PSOut
{
	[Diffuse]
	public Vector4 Diffuse;
}

public VSOut VertexShader(VSIn In)
{
	VSOut Out = new VSOut();
	Matrix4x4 world = Context.World;
	Matrix4x4 view = Context.View;
	Matrix4x4 projection = Context.Projection;

	var worldPosition = GMath.mul(new Vector4(In.Position, 1), world);
	var viewPosition = GMath.mul(worldPosition, view);
	var viewNormal = GMath.normalize((Vector3)GMath.mul(new Vector4(GMath.normalize(In.Normal), 0), GMath.mul(world, view)));

	var viewLPosition = GMath.normalize((Vector3)GMath.mul(Context.Light0.Position - worldPosition, view));
	Vector3 V = GMath.normalize(-1*(Vector3)viewPosition);

	float NdotL = GMath.max(GMath.dot(viewNormal, viewLPosition), 0.0f);
	Vector3 H = GMath.normalize(viewLPosition + V);
	float NdotH = GMath.max(GMath.dot(viewNormal, H), 0.0f);

	Out.Diffuse = new Vector4((Vector3)Context.Material.Ambient,0) + new Vector4 ((Vector3)Context.Material.Diffuse * (Vector3)Context.Light0.Diffuse * NdotL, Context.Material.Diffuse.W);
	Out.Specular = Context.Material.Specular * Context.Light0.Specular * GMath.pow(NdotH, Context.Material.Shininess);

	Out.TexCoord = In.TexCoord;
	Out.Projected = GMath.mul(viewPosition, projection);

	return Out;
}

public virtual PSOut PixelShader(PSIn In)
{
	PSOut Out = new PSOut();

	Vector4 texColor = new Vector4 (1,1,1,1);
	if (Context.HasSampling)
		texColor = Context.Sampler.Sample((Vector2)In.TexCoord);

	Out.Diffuse = texColor * In.Diffuse + new Vector4((Vector3)In.Specular, 0);

	return Out;
}

Last edited Sep 26, 2012 at 7:51 PM by acoto, version 1

Comments

No comments yet.