Basic texture example (texture+sampler+vertex shader+pixel shader) for FX Composer 2.5 (cgfx)

Basic texture example (texture+sampler+vertex shader+pixel shader) for FX Composer 2.5 (cgfx)

Post by Skybuck Fl » Wed, 23 Sep 2009 05:37:45


Hello,

Yesterday I read some documentation, and today I make a nice simple example
of a:

"texture+sampler+vertex shader+pixel shader" for FX Composer 2.5

It's a simple "effect/material" which uses a bitmap which is displayed on a
plane.

The code is written in a Delphi-samiliar style ;)

(The bitmap needs to be set in the properties, see little manual below ;))

// *** Begin of Example ***

/*

Basic Texture example

(Also known (by me) as Spaceship Texture example, uses "Warpship.bmp")

version 0.01 created on 21 september 2009 by Skybuck Flying

Features:

FX Composer 2.5 examples:

+ Basic 2d texture declaration with annotations

+ Basic 2d sampler declaration with sampler states

+ Basic vertex shader with:
position 3d in/ 4d out
texture coordinate0 2d in/ 2d out

+ Basic pixel shader with:
texture coordinate 2d in
pixel color 4d out

+ Uses nice input/output structures no stupid/confusing return values ;) :)
+ Uses nice naming convention, delphi like ;)

Usage:

Step 1: Drag and drop material to object.

Step 2: Select object.

Step 3: Change SpaceShipTexture parameter in properties, and add/select an
image.

*/

texture SpaceShipTexture
<
string ResourceName = ""; // must be set in fx composer gui/properties.
string UIName = "SpaceShipTexture";
string ResourceType = "2D";

sampler2D SpaceShipSampler2D = sampler_state
{
Texture = <SpaceShipTexture>;
MinFilter = Linear;
MipFilter = Linear;
MagFilter = Linear;
AddressU = Clamp;
AddressV = Clamp;
};

float4x4 WorldViewProj : WorldViewProjection;

struct TVertexShaderIn
{
float3 mPosition : POSITION;
float2 mTextureCoordinate0 : TEXCOORD0;
};

struct TVertexShaderOut
{
float4 mPosition : POSITION;
float2 mTextureCoordinate0 : TEXCOORD0;
};

void TVertexShader_Main( in TVertexShaderIn ParaIn , out TVertexShaderOut
ParaOut )
{
ParaOut.mPosition = mul(WorldViewProj, float4(ParaIn.mPosition.xyz, 1.0));
ParaOut.mTextureCoordinate0 = ParaIn.mTextureCoordinate0;
}

struct TPixelShaderIn
{
float2 mPosition : TEXCOORD0;
};

struct TPixelShaderOut
{
float4 mColor : COLOR;
};

void TPixelShader_Main( in TPixelShaderIn ParaIn, out TPixelShaderOut
ParaOut )
{
ParaOut.mColor = tex2D( SpaceShipSampler2D, ParaIn.mPosition );
}

technique technique0 {
pass p0 {
CullFaceEnable = false;
VertexProgram = compile vp40 TVertexShader_Main();
FragmentProgram = compile fp40 TPixelShader_Main();
}
}

// *** End of Example ***

Bye,
Skybuck.