Detail Shader
by Matt Vitelli · in Torque Game Engine Advanced · 10/21/2006 (12:20 am) · 5 replies
I've been studying shaders for a bit now and thought I'd share a very simple detail shader I made in about 2 minutes.
The pixel shader
And the vertex shader
So..very basic but pulls off some neat, if simple effects. What I want to do from here is add it to Legacy terrain. After a chat with Ben Garney he said I should start by looking in terrainP and terrainV. Well, I have the detail textures "working" but with very odd issues.
The pixel shader
#define IN_HLSL
#include "shdrConsts.h"
//--------------------------------------------------------------
//Structures
//--------------------------------------------------------------
struct ConnectData
{
float2 texCoord : TEXCOORD0;
};
struct Fragout
{
float4 col : COLOR0;
};
//--------------------------------------------------------------
//Main
//--------------------------------------------------------------
Fragout main(ConnectData IN, uniform sampler2D baseTex : register(S0),
uniform sampler2D bumpTex : register(S1),
uniform sampler2D detailTex : register(S2)
)
{
Fragout OUT;
float4 diffuseColor = tex2D( baseTex, IN.texCoord );
float4 bumpColor = tex2D( bumpTex, IN.texCoord );
float4 detailColor = tex2D( detailTex, IN.texCoord );
OUT.col = lerp( diffuseColor, bumpColor, detailColor );
return OUT;
}And the vertex shader
#define IN_HLSL
#include "shdrConsts.h"
//---------------------------------------------------------------
// Constants
//---------------------------------------------------------------
struct Appdata
{
float4 position : POSITION;
float4 texCoord : TEXCOORD0;
};
struct Conn
{
float4 HPOS : POSITION;
float2 outTexCoord : TEXCOORD0;
};
//---------------------------------------------------------------
// Main
//---------------------------------------------------------------
Conn main( Appdata In, uniform float4x4 modelview : register(VC_WORLD_PROJ),
uniform float4x4 texMat : register(VC_TEX_TRANS1)
)
{
Conn Out;
//take vert and get coord by transforming by modelviewmatrix
Out.HPOS = mul(modelview, In.position);
Out.outTexCoord = mul(texMat, In.texCoord);
return Out;
}So..very basic but pulls off some neat, if simple effects. What I want to do from here is add it to Legacy terrain. After a chat with Ben Garney he said I should start by looking in terrainP and terrainV. Well, I have the detail textures "working" but with very odd issues.
#2
Tom
10/22/2006 (4:40 pm)
Do you have screenshots you could link to? Also, could you describe the intent of the shader? It looks like it expects baseTex and detailTex to be the same size (both are indexed using the same IN.texCoord), so I'm not clear on the intent.Tom
#3
10/22/2006 (6:15 pm)
I actually got this to work. It is a custom shader and thus uses a custom material. Here's a screenshot:
#4
I'm currently waiting for my shader book. When it get here next week, I'll take a crack at it if you haven't already done so by then.
10/23/2006 (8:38 am)
It still repeats too much. Some talk on detail shaders suggest offsetting and combining two textures ( or the same texture scaled and offset to remove the obvious repeating.I'm currently waiting for my shader book. When it get here next week, I'll take a crack at it if you haven't already done so by then.
Torque Owner Matt Vitelli
TerrainP
//----------------------------------------------------------------------------- // Structures //----------------------------------------------------------------------------- #define IN_HLSL #include "../shdrConsts.h" struct ConnectData { float2 texCoord : TEXCOORD0; float2 fogCoord : TEXCOORD1; float2 lightMapCoord : TEXCOORD2; float2 texCoord2 : TEXCOORD3; }; struct Fragout { float4 col : COLOR0; }; //----------------------------------------------------------------------------- // Main //----------------------------------------------------------------------------- Fragout main( ConnectData IN, uniform sampler2D baseTex : register(S0), uniform sampler2D bumpTex : register(S1), uniform sampler2D detailTex : register(S2), uniform sampler2D fogMap : register(S3), uniform sampler2D lightMap : register(S4) ) { Fragout OUT; float4 diffuseColor = tex2D( baseTex, IN.texCoord ); //diffuseColor *= tex2D(lightMap, IN.lightMapCoord) * 4.0; float4 bumpColor = tex2D( bumpTex, IN.texCoord ); float4 detailColor = tex2D( detailTex, IN.texCoord ); float4 fogColor = tex2D(fogMap, IN.fogCoord); OUT.col = lerp( diffuseColor, bumpColor, detailColor/*, fogColor, fogColor.a*/ ); return OUT; }and TerrainV
#define IN_HLSL #include "../shdrConsts.h" //----------------------------------------------------------------------------- // Structures //----------------------------------------------------------------------------- struct VertData { float2 texCoord : TEXCOORD0; float4 position : POSITION; float3 normal : NORMAL; }; struct ConnectData { float4 hpos : POSITION; float2 texCoord : TEXCOORD0; float2 fogCoord : TEXCOORD1; float2 lightMapCoord : TEXCOORD2; //float2 texCoord2 : TEXCOORD3; }; //----------------------------------------------------------------------------- // Main //----------------------------------------------------------------------------- ConnectData main( VertData IN, uniform float4x4 modelview : register(C0), uniform float3 eyePos : register(VC_EYE_POS), uniform float3 fogData : register(VC_FOGDATA), uniform float1 terrainsize : register(C54), uniform float4x4 texMat : register(VC_TEX_TRANS1) ) { ConnectData OUT; OUT.hpos = mul(modelview, IN.position); OUT.texCoord = mul(texMat, IN.texCoord); //OUT.texCoord2 = IN.texCoord / 0.1; OUT.lightMapCoord = IN.position * terrainsize.x; OUT.fogCoord.x = 1.0 - ( distance( IN.position, eyePos ) / fogData.z ); OUT.fogCoord.y = (IN.position.z - fogData.x) * fogData.y; return OUT; }And data/terrains/materials.cs
new CustomMaterial(TerrainMaterial) { texture[0] = "highland13"; texture[1] = "detail1"; texture[2] = "detail2"; shader = TerrShader; version = 2.0; dynamicLightingMaterial = TerrainMaterialDynamicLighting; };As you can see the detail texture suffers. Any ideas or comments?
Thanks,
Matt Vitelli