Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@lukaspj
Created July 19, 2017 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukaspj/ecd9d46426889c611a565b6edde12572 to your computer and use it in GitHub Desktop.
Save lukaspj/ecd9d46426889c611a565b6edde12572 to your computer and use it in GitHub Desktop.
Example terrain shader
//*****************************************************************************
// Torque -- HLSL procedural shader
//*****************************************************************************
// Dependencies:
#include "shaders/common/terrain/terrain.hlsl"
#include "shaders/common/torque.hlsl"
// Features:
// Vert Position
// Terrain Base Texture
// Blank Matinfo map
// Terrain Parallax Texture 0
// Terrain Detail Texture 0
// Terrain Normal Texture 0
// Terrain Detail Texture 1
// Terrain Normal Texture 1
// Terrain Detail Texture 2
// Terrain Normal Texture 2
// Terrain Detail Texture 3
// Terrain Normal Texture 3
// Eye Space Depth (Out)
// GBuffer Conditioner
// Deferred Material
struct ConnectData
{
float4 vpos : SV_Position;
float3 texCoord : TEXCOORD0;
float3 outNegViewTS : TEXCOORD1;
float4 detCoord : TEXCOORD2;
float3x3 viewToTangent : TEXCOORD3;
float4 wsEyeVec : TEXCOORD6;
};
struct Fragout
{
float4 col : SV_Target0;
float4 col1 : SV_Target1;
float4 col2 : SV_Target2;
};
//-----------------------------------------------------------------------------
// Main
//-----------------------------------------------------------------------------
Fragout main( ConnectData IN,
uniform SamplerState baseTexMap : register(S0),
uniform Texture2D baseTexture : register(T0),
uniform SamplerState layerTex : register(S1),
uniform Texture2D layerTexObj : register(T1),
uniform float layerSize : register(C8),
uniform float4 detailScaleAndFade0 : register(C0),
uniform float3 detailIdStrengthParallax0 : register(C1),
uniform SamplerState detailMap : register(S2),
uniform SamplerState normalMap : register(S3),
uniform Texture2D normalMapTex0 : register(T5),
uniform Texture2D detailTex0 : register(T4),
uniform float4 detailScaleAndFade1 : register(C2),
uniform float3 detailIdStrengthParallax1 : register(C3),
uniform Texture2D detailTex1 : register(T6),
uniform Texture2D normalMapTex1 : register(T7),
uniform float4 detailScaleAndFade2 : register(C4),
uniform float3 detailIdStrengthParallax2 : register(C5),
uniform Texture2D detailTex2 : register(T8),
uniform Texture2D normalMapTex2 : register(T9),
uniform float4 detailScaleAndFade3 : register(C6),
uniform float3 detailIdStrengthParallax3 : register(C7),
uniform Texture2D detailTex3 : register(T10),
uniform Texture2D normalMapTex3 : register(T11),
uniform float3 vEye : register(C9),
uniform float4 oneOverFarplane : register(C10)
)
{
Fragout OUT;
// Vert Position
// Terrain Base Texture
float4 baseColor = baseTexture.Sample( baseTexMap, IN.texCoord.xy );
OUT.col1 = baseColor;
// Blank Matinfo map
OUT.col2 = float4(0.0,0.0,0.0,0.0001);
// Terrain Parallax Texture 0
// Terrain Detail Texture 0
float3 negViewTS = normalize( IN.outNegViewTS );
float4 layerSample = round( layerTexObj.Sample( layerTex, IN.texCoord.xy ) * 255.0f );
float4 detCoord0;
detCoord0.xyz = IN.detCoord.xyz * detailScaleAndFade0.xyx;
detCoord0.w = clamp((detailScaleAndFade0.z - IN.detCoord.w) * detailScaleAndFade0.w, 0.0, 1.0);
float detailBlend0 = calcBlend( detailIdStrengthParallax0.x, IN.texCoord.xy, layerSize, layerSample );
float blendTotal = 0;
blendTotal += detailBlend0;
float4 detailColor;
detCoord0.xy += parallaxOffset( normalMapTex0, normalMap, detCoord0.xy, negViewTS, detailIdStrengthParallax0.z * detailBlend0 );
if ( detailBlend0 > 0.0f )
{
detailColor = ( detailTex0.Sample( detailMap, detCoord0.xy ) * 2.0 ) - 1.0;
detailColor *= detailIdStrengthParallax0.y * detCoord0.w;
OUT.col1 += detailColor * detailBlend0;
}
// Terrain Normal Texture 0
float3 gbNormal = IN.viewToTangent[2];
if ( detailBlend0 > 0.0f )
{
float4 bumpNormal = normalMapTex0.Sample(normalMap, detCoord0.xy);
bumpNormal.xyz = bumpNormal.xyz * 2.0 - 1.0;
gbNormal = lerp( gbNormal, mul( bumpNormal.xyz, IN.viewToTangent ), min( detailBlend0, detCoord0.w ) );
}
// Terrain Detail Texture 1
float4 detCoord1;
detCoord1.xyz = IN.detCoord.xyz * detailScaleAndFade1.xyx;
detCoord1.w = clamp((detailScaleAndFade1.z - IN.detCoord.w) * detailScaleAndFade1.w, 0.0, 1.0);
float detailBlend1 = calcBlend( detailIdStrengthParallax1.x, IN.texCoord.xy, layerSize, layerSample );
blendTotal += detailBlend1;
if ( detailBlend1 > 0.0f )
{
detailColor = ( detailTex1.Sample( detailMap, detCoord1.xy ) * 2.0 ) - 1.0;
detailColor *= detailIdStrengthParallax1.y * detCoord1.w;
OUT.col1 += detailColor * detailBlend1;
}
// Terrain Normal Texture 1
if ( detailBlend1 > 0.0f )
{
float4 bumpNormal = normalMapTex1.Sample(normalMap, detCoord1.xy);
bumpNormal.xyz = bumpNormal.xyz * 2.0 - 1.0;
gbNormal = lerp( gbNormal, mul( bumpNormal.xyz, IN.viewToTangent ), min( detailBlend1, detCoord1.w ) );
}
// Terrain Detail Texture 2
float4 detCoord2;
detCoord2.xyz = IN.detCoord.xyz * detailScaleAndFade2.xyx;
detCoord2.w = clamp((detailScaleAndFade2.z - IN.detCoord.w) * detailScaleAndFade2.w, 0.0, 1.0);
float detailBlend2 = calcBlend( detailIdStrengthParallax2.x, IN.texCoord.xy, layerSize, layerSample );
blendTotal += detailBlend2;
if ( detailBlend2 > 0.0f )
{
detailColor = ( detailTex2.Sample( detailMap, detCoord2.xy ) * 2.0 ) - 1.0;
detailColor *= detailIdStrengthParallax2.y * detCoord2.w;
OUT.col1 += detailColor * detailBlend2;
}
// Terrain Normal Texture 2
if ( detailBlend2 > 0.0f )
{
float4 bumpNormal = normalMapTex2.Sample(normalMap, detCoord2.xy);
bumpNormal.xyz = bumpNormal.xyz * 2.0 - 1.0;
gbNormal = lerp( gbNormal, mul( bumpNormal.xyz, IN.viewToTangent ), min( detailBlend2, detCoord2.w ) );
}
// Terrain Detail Texture 3
float4 detCoord3;
detCoord3.xyz = IN.detCoord.xyz * detailScaleAndFade3.xyx;
detCoord3.w = clamp((detailScaleAndFade3.z - IN.detCoord.w) * detailScaleAndFade3.w, 0.0, 1.0);
float detailBlend3 = calcBlend( detailIdStrengthParallax3.x, IN.texCoord.xy, layerSize, layerSample );
blendTotal += detailBlend3;
if ( detailBlend3 > 0.0f )
{
detailColor = ( detailTex3.Sample( detailMap, detCoord3.xy ) * 2.0 ) - 1.0;
detailColor *= detailIdStrengthParallax3.y * detCoord3.w;
OUT.col1 += detailColor * detailBlend3;
}
// Terrain Normal Texture 3
if ( detailBlend3 > 0.0f )
{
float4 bumpNormal = normalMapTex3.Sample(normalMap, detCoord3.xy);
bumpNormal.xyz = bumpNormal.xyz * 2.0 - 1.0;
gbNormal = lerp( gbNormal, mul( bumpNormal.xyz, IN.viewToTangent ), min( detailBlend3, detCoord3.w ) );
}
// Eye Space Depth (Out)
#ifndef CUBE_SHADOW_MAP
float eyeSpaceDepth = dot(vEye, (IN.wsEyeVec.xyz / IN.wsEyeVec.w));
#else
float eyeSpaceDepth = length( IN.wsEyeVec.xyz / IN.wsEyeVec.w ) * oneOverFarplane.x;
#endif
// GBuffer Conditioner
float4 normal_depth = float4(normalize(gbNormal), eyeSpaceDepth);
// output buffer format: GFXFormatR16G16B16A16F
// g-buffer conditioner: float4(normal.X, normal.Y, depth Hi, depth Lo)
float4 _gbConditionedOutput = float4(sqrt(half(2.0/(1.0 - normal_depth.y))) * half2(normal_depth.xz), 0.0, normal_depth.a);
// Encode depth into hi/lo
float2 _tempDepth = frac(normal_depth.a * float2(1.0, 65535.0));
_gbConditionedOutput.zw = _tempDepth.xy - _tempDepth.yy * float2(1.0/65535.0, 0.0);
OUT.col = _gbConditionedOutput;
// Deferred Material
return OUT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment