OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Oh, and to help ease your understanding of the shader, here is another version that works the same STRUCTURALLY, but all the configuration settings are hardcoded into the shader source-code itself, with descriptive variable names. Example lite-c script
#include <acknex.h>
#include <default.c>
BMAP* sand_tex = "sand.tga"; // Sand Texture
BMAP* grass_tex = "grass.tga"; // Grass Texture
BMAP* rock_tex = "rock.tga"; // Rock Texture
BMAP* snow_tex = "smoke.tga"; // Snow Texture
//
//==========================================================================
//==========================================================================
//CORE CODE
//==========================================================================
MATERIAL *RePaintSkin_mat =
{ effect= "TerrainPainter_hardcoded.fx"; event = skinChangeEvent;
flags = ENABLE_RENDER | AUTORELOAD; }
//--------------------------------------------------------------------------
function skinChangeEvent() { if(me!=render_view.genius) return(1); }
//--------------------------------------------------------------------------
void RePaint_Skin(ENTITY* ME)
{ if(!ME) return; //invalid entity to capture.
if(!ent_getskin(ME,1)) return; //entity has no skin to capture into.
//-----------------------------------------------------------------------
VIEW *view = view_create(1000); set(view, SHOW);
view.material = RePaintSkin_mat; view.genius = ME;
view.x=ME.x-(ME.max_x+ME.min_x)/2; view.y=ME.y-(ME.max_y+ME.min_y)/2;
view.z = ME.z + maxv(ME.max_x-ME.min_x, ME.max_y-ME.min_y)*0.75;
vec_set(view.pan,vector(0,-90,0)); view.bmap = ent_getskin(ME,1);
view.size_x = view.bmap.width; view.size_y = view.bmap.height;
//-----------------------------------------------------------------------
wait(1); ptr_remove(view); beep(); }
//==========================================================================
//End of CORE CODE
//==========================================================================
//==========================================================================
//
ENTITY* Terr;
void updateView() { RePaint_Skin(Terr); }
function main()
{
wait(1); level_load(NULL); wait(1); diag("\n\n\n");
draw_textmode("Times",0,32,100); warn_level = 3; terrain_chunk=256;
//------------------------------------------------------------------------------------
//
Terr = ent_create("terr.MDL", nullvector, NULL);
vec_set(camera.x, vector(-300,150,220)); vec_set(camera.pan, vector(350,-25,0));
//
on_space = updateView;
//
while(!key_esc)
{
if(key_f1) DEBUG_BMAP(ent_getskin(Terr,1), 0, 1);
if(key_f2) //swap entities
{ if(!str_cmp(Terr.type, "PLANE01.HMP"))
{ ent_morph(Terr, "plane01.hmp"); vec_fill(Terr.scale_x, 0.1); }
else
{ ent_morph(Terr, "Terr.mdl"); vec_fill(Terr.scale_x, 1); }
while(key_f2) wait(1); }
wait(1);
}
}
Shader code "TerrainPainter_hardcoded.fx"
//===================================================================================
//ColorMapping Samplers Texture-Sources
texture sand_tex_bmap, grass_tex_bmap, rock_tex_bmap, snow_tex_bmap;
sampler SandSampler = sampler_state { Texture = <sand_tex_bmap>; };
sampler GrassSampler = sampler_state { Texture = <grass_tex_bmap>; };
sampler RockSampler = sampler_state { Texture = <rock_tex_bmap>; };
sampler SnowSampler = sampler_state { Texture = <snow_tex_bmap>; };
//
//===================================================================================
//Configuration Settings
float Lap = 20; // Border Overlap Range (in quants)
//
float GrassStart = 37; // Start height of GRASS texture (in quants)
float RockStart = 80; // Start height of ROCK texture (in quants)
float SnowStart = 125; // Start height of SNOW texture (in quants)
//
float SandScale = 6; // SAND texture scale multiplier
float GrassScale = 6; // GRASS texture scale multiplier
float RockScale = 2; // ROCK texture scale multiplier
float SnowScale = 3; // SNOW texture scale multiplier
//
//===================================================================================
//Random Number Generator
#define cMult 0.0001002707309736288
#define aSubtract 0.2727272727272727
//
float random(float4 t)
{
float a, b, c, d;
a=t.x+t.z*cMult+aSubtract-floor(t.x);
a*=a; b=t.y+a; b-=floor(b);
c=t.z+b; c-=floor(c); d=c;
a+=c*cMult+aSubtract-floor(a);
a*=a; b+=a; b-=floor(b);
c+=b; c-=floor(c);
return ((a+b+c+d)/4);
}
//
//===================================================================================
//
void Painter_VS( in float4 inPos: POSITION, in float2 inTex: TEXCOORD0,
out float4 outPos: POSITION, out float outHgt: TEXCOORD0,
out float2 outTexD:TEXCOORD1, out float2 outTexG:TEXCOORD2,
out float2 outTexR:TEXCOORD3, out float2 outTexS:TEXCOORD4 )
{
outPos = float4(inTex.x*2-1, -inTex.y*2+1, 0,1); //output normalized XYpos
outHgt = inPos.y + (random(inPos)*20)-10; //output slightly-randomized height
outTexD = inTex * SandScale; // re-scale coords for SAND/DIRT texture lookup
outTexG = inTex * GrassScale; // re-scale coords for GRASS texture lookup
outTexR = inTex * RockScale; // re-scale coords for ROCK texture lookup
outTexS = inTex * SnowScale; // re-scale coords for SNOW texture lookup
}
//
//===================================================================================
//
float4 Painter_PS( in float inHgt: TEXCOORD0,
in float2 inTexD:TEXCOORD1, in float2 inTexG:TEXCOORD2,
in float2 inTexR:TEXCOORD3, in float2 inTexS:TEXCOORD4 ):COLOR0
{
float4 color = 0;
if(inHgt<(GrassStart-Lap)) color = tex2D(SandSampler, inTexD);
else if(inHgt<(GrassStart+Lap)) color = lerp(tex2D(SandSampler, inTexD), tex2D(GrassSampler,inTexG),saturate((inHgt-GrassStart)/Lap));
else if(inHgt<(RockStart-Lap)) color = tex2D(GrassSampler, inTexG);
else if(inHgt<(RockStart+Lap)) color = lerp(tex2D(GrassSampler,inTexG), tex2D(RockSampler, inTexR),saturate((inHgt-RockStart)/Lap));
else if(inHgt<(SnowStart-Lap)) color = tex2D(RockSampler, inTexR);
else if(inHgt<(SnowStart+Lap)) color = lerp(tex2D(RockSampler, inTexR), tex2D(SnowSampler, inTexS),saturate((inHgt-SnowStart)/Lap));
else color = tex2D(SnowSampler, inTexS);
return color;
}
//
//===================================================================================
//
technique terrain_paint
{
pass p0
{
CullMode = None;
VertexShader = compile vs_2_0 Painter_VS();
PixelShader = compile ps_2_0 Painter_PS();
}
}
//
//===================================================================================
//
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|