Game Development Community

Custom material/shader

by paul le henaff · in Technical Issues · 05/10/2013 (7:14 pm) · 1 replies

Hello,
I am new to shader programming and Torque in general. I followed some tutorials and got vertex and pixel shaders as well as a shader and material definition.
The problem I have is that I cant seem to link the new material to the model. the model just displays the basic material from a fresh import.

The new material doesn't even show up in the material editor. Could it be something to do with the export process? I'm exporting to .dae form blender and the file appears to have some references to textures. Havent looked any further yet.

When I do a fresh import of the model it imports a basic material with the correct diffuse etc. Modifying teh material files doesnt seem to work.

This is a screenshot. I don't know where the solid colours are coming from since my texture is way more detailed.
http://puu.sh/2QWN4.jpg


Any ideas what could be wrong? Could someone outline the process for apply a custom material to a model because I think I'm missing something.

Thanks.

The code:

material.cs
// Build out the references to the HLSL files for advanced
// lighting
singleton ShaderData(HeroShader)
{
	DXVertexShaderFile = "shaders/common/HeroShader/
	HeroV.hlsl";
	DXPixelShaderFile = "shaders/common/HeroShader/
	HeroP.hlsl";
	// Pixel shader version 2 is required
	pixVersion = 2.0;
};

// Our custom material that uses advanced lighting to replace
// the standard one
singleton CustomMaterial(crab_mat)
{
	// Map this custom material to the soldier's body
	mapTo = "crab";
	
	// The first sampler points to our diffuse texture. This
	// is the same texture that is used by the standard
	// material.
	sampler["diffuse"] = "art/shapes/crab/crabDiff.png";
	
	// The second sampler points to a cube map.
	sampler["cube0"] = "$cubemap";
	// The third sampler is the lighting info generated by
	// advanced lighting
	sampler["lightInfoBuffer"] = "#lightInfo";
	// Define the cube map that we will use. This is a
	// standard cube map defined by the core game code.
	cubemap = DesertSkyCubemap;
	// Point to our shader data we defined above
	shader = HeroShader;
	// Our minimum shader version requirements
	version = 2.0;
};

HeroV.hlsl
#include "shaders/common/hlslStructs.h"
// Matrix to convert into view space
uniform float4x4 modelview;
// Matrix to go from object to cube map space
uniform float3x3 cubeTrans;
// Eye position relative to the cube map
uniform float3 cubeEyePos;
// Eye position relative to the object
uniform float3 eyePos;
// The vertex shader uses one of the input structures
// as defined in hlslStructs.h. In this particular
// case it is the VertexIn_PNT struct.
// Output to the pixel shader
struct VS_OUTPUT
{
// Standard position not passed to the pixel shader
float4 pos : POSITION;
// Texture coordinates
float2 uv0 : TEXCOORD0;
// Calculated reflection vector used by the pixel shader
// to perform a cube map lookup
float3 reflectVec : TEXCOORD1;
// Calculated reflection scale that depends on the
// relationship between the surface normal and
// the eye position.
float reflectScale : TEXCOORD2;
// Pass the screen space position used for lighting
// calculations
float4 screenspacePos : TEXCOORD3;
};
VS_OUTPUT main(VertexIn_PNT IN)
{
VS_OUTPUT OUT = (VS_OUTPUT)0;
// Calculate the vertex position for the view
OUT.pos = mul(modelview,IN.pos);
// Pass along the texture coordinates
OUT.uv0 = IN.uv0;
// Calculate the reflection vector used in the
// cube map lookup
float3 cubeVertPos = mul(cubeTrans, IN.pos.xyz);
float3 cubeNormal = normalize( mul(cubeTrans,
normalize(IN.normal)).xyz );
float3 eyeToVert = cubeVertPos - cubeEyePos;
OUT.reflectVec = reflect(eyeToVert, cubeNormal);
// Power factor used to control the amount to scale the
// reflection by. The lower the value the less cube map
// reflection there will be.
float power = 0.7;
// Calculate the amount to scale the reflection by
float3 eyeVec = normalize( eyePos.xyz - IN.pos.xyz );
OUT.reflectScale = saturate( pow( abs(dot( eyeVec,
IN.normal.xyz )), power ) );
// Store the screen space position for RT lighting
// calculations
OUT.screenspacePos = OUT.pos;
// Return the output struct to the system
return OUT;
}


HeroP.hlsl
#include "shadergen:/autogenConditioners.h"
#include "shaders/common/torque.hlsl"
// Diffuse map sampler
uniform sampler2D diffuse : register(S0);

// Cube map sampler
uniform samplerCUBE cube0 : register(S1);

// Advanced lighting info sampler
uniform sampler2D lightInfoBuffer : register(S2);

// Advanced lighting constants
uniform float4 rtParams2;

// Input from vertex shader
struct PS_INPUT
{
	// Texture coordinates
	float2 uv0 : TEXCOORD0;
	// Calculated reflection vector used to perform a cube map
	// lookup
	float3 reflectVec : TEXCOORD1;
	// Calculated reflection scale that depends on the
	// relationship between the surface normal and the eye
	// position.
	float reflectScale : TEXCOORD2;
	// Screen space position used for lighting calculations
	float4 screenspacePos : TEXCOORD3;
};

// Output to the system
struct PS_OUTPUT
{
float4 color : COLOR0;
};

PS_OUTPUT main(PS_INPUT IN)
{
	PS_OUTPUT OUT = (PS_OUTPUT)0;
	// Blend between the diffuse color and the cube map
	// reflection based on the reflection scale calculated
	// by the vertex shader
	OUT.color = lerp(texCUBE( cube0, IN.reflectVec ),
	tex2D(diffuse, IN.uv0),
	IN.reflectScale);
	// Deferred RT Lighting
	float2 uvScene = IN.screenspacePos.xy /
	IN.screenspacePos.w;
	uvScene = ( uvScene + 1.0 ) / 2.0;
	uvScene.y = 1.0 - uvScene.y;
	uvScene = ( uvScene * rtParams2.zw ) + rtParams2.xy;
	float3 d_lightcolor;
	float d_NL_Att;
	float d_specular;
	lightinfoUncondition(tex2D(lightInfoBuffer, uvScene),
	d_lightcolor, d_NL_Att, d_specular);
	OUT.color *= float4(d_lightcolor, 1.0);
	// Perform any necessary HDR encoding using a function
	// defined in torque.hlsl
	OUT.color = hdrEncode( OUT.color );
	// Return the output struct to the system
	return OUT;
}

About the author

Recent Threads


#1
05/14/2013 (6:06 pm)
Hey, I made some progress the above material ended up working fine. Made a silly mistake in the name somewhere.

Anyway Ive now got another problem with another material. I made this in FX composer and modified the format to match existing torque materials.
works in fx composer but not in torque. Ive read its something to do with inputs and outputs but cant seem to find any useful information or at least anything I understand.

Ive been reading into shading in general which got me to this shader, which works in FX composer, but I cant seem to find much info about getting this into an engine.
Could someone have a look at my files and shed some light? Id really appreciate it, thanks.

material.cs
http://pastebin.com/PZw77U1T

shader.cs
http://pastebin.com/Bkk6eJ9C

heroV.hlsl
http://pastebin.com/H6VMvJWw

heroP.hlsl
http://pastebin.com/xjTxWzTL