1 registered members (TipmyPip),
18,449
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: EvilSOB]
#386604
11/06/11 12:11
11/06/11 12:11
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
User
|
User
Joined: Oct 2008
Posts: 513
|
First shader version: TerrainPainter.fx
//Application fed data
const float4x4 matWorld; // World matrix
texture Sand_BM, Grass_BM, Rock_BM, Snow_BM;
// ColorMap Samplers
sampler SandSampler = sampler_state
{
Texture = <Sand_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler GrassSampler = sampler_state
{
Texture = <Grass_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler RockSampler = sampler_state
{
Texture = <Rock_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler SnowSampler = sampler_state
{
Texture = <Snow_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
// Vertex Shader:
void TerrPaintVS(
in float4 InPos: POSITION,
in float2 InTex: TEXCOORD0,
out float4 OutPos: POSITION,
out float2 OutTex: TEXCOORD0)
{
// Transform the vertex from object space to world space:
OutPos = mul(InPos, matWorld);
// Pass the texture coordinate to the pixel shader:
OutTex = InTex;
}
// Pixel Shader:
float4 TerrPaintPS(
in float4 InPos: POSITION,
in float2 InTex: TEXCOORD0): COLOR
{
float4 Color;
if(InPos.y<16) Color = tex2D(SandSampler, InTex);
else if(InPos.y<50) Color = tex2D(GrassSampler, InTex);
else if(InPos.y<75) Color = tex2D(RockSampler, InTex);
else Color = tex2D(SnowSampler, InTex);
return Color;
}
// Technique:
technique AmbientTechnique
{
pass P0
{
VertexShader = compile vs_2_0 TerrPaintVS();
PixelShader = compile ps_2_0 TerrPaintPS();
}
}
Material Definition inside my game script:
MATERIAL* mtlTerrain =
{
effect = "TerrainPainter.fx";
flags = AUTORELOAD; // allows to edit the shader at runtime
}
How I assign the material in my game script:
int x; int y;
for(x=0;x<TERRAIN_COUNT;x++)
{
for(y=0;y<TERRAIN_COUNT;y++)
{
Terrain[x][y]=ent_create("Terrain33.mdl",vector(x*512,y*512,0),NULL);
ent_clone(Terrain[x][y]);
Terrain[x][y].material = mtlTerrain;
//ent_cloneskin(Terrain[x][y]);
}
}
Does not even compile, I get this error message: Malfunction W1550 Can't compile effect: TerrainPainter.fx(53,18): error X4502: invalid ps_2_0 input semantic 'POSITION' D:\TERRAIN TEST\memory(71,18): ID3DXEffectCompiler::CompileEffect: There was an error compiling expression ID3DXEffectCompiler: Compilation failed > in float4 InPos: POSITION, < any help with this is welcome @EvilSOB: Seems I am not an opera singer yet, but I am trying... At the moment it feels like I can't even sing in the shower, LOL!
Last edited by Carlos3DGS; 11/06/11 12:16.
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: Carlos3DGS]
#386610
11/06/11 13:28
11/06/11 13:28
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
Good start -- the pixel shader doesn't accept the "position" semantic. Instead, your vertex shader should also output the position to TEXCOORD1 or so, and have the pixel shader take in InPos through that semantic.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: JibbSmart]
#386612
11/06/11 14:17
11/06/11 14:17
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
User
|
User
Joined: Oct 2008
Posts: 513
|
Thanks for pointing me in the right direction! New version of TerrainPainter.fx:
//Application fed data
const float4x4 matWorld; // World matrix
texture Sand_BM, Grass_BM, Rock_BM, Snow_BM;
// ColorMap Samplers
sampler SandSampler = sampler_state
{
Texture = <Sand_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler GrassSampler = sampler_state
{
Texture = <Grass_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler RockSampler = sampler_state
{
Texture = <Rock_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
sampler SnowSampler = sampler_state
{
Texture = <Snow_BM>;
MipFilter = Linear;
AddressU = Wrap;
AddressV = Wrap;
};
// Vertex Shader:
void TerrPaintVS(
in float4 InPos: POSITION,
in float2 InTex: TEXCOORD0,
out float2 OutTex: TEXCOORD0,
out float4 OutPos: TEXCOORD1)
{
// Transform the vertex from object space to world space:
OutPos = mul(InPos, matWorld);
// Pass the texture coordinate to the pixel shader:
OutTex = InTex;
}
// Pixel Shader:
float4 TerrPaintPS(
in float2 InTex: TEXCOORD0,
in float4 InPos: TEXCOORD1): COLOR
{
float4 Color;
if(InPos.y<16) Color = tex2D(SandSampler, InTex);
else if(InPos.y<50) Color = tex2D(GrassSampler, InTex);
else if(InPos.y<75) Color = tex2D(RockSampler, InTex);
else Color = tex2D(SnowSampler, InTex);
return Color;
}
// Technique:
technique AmbientTechnique
{
pass P0
{
VertexShader = compile vs_2_0 TerrPaintVS();
PixelShader = compile ps_2_0 TerrPaintPS();
}
}
Now I get the following error: Malfunction W1550 Can't compile effect: TerrainPainter.fx: error X4541: vertex shader must minimally write all four components of POSITION D:\TERRAIN TEST\memory(70,18): ID3DXEffectCompiler::CompileEffect: There was an error compiling expression ID3DXEffectCompiler: Compilation failed What have I gotten myself into? I'm starting to regret it already! (But I won't stop till I get it done, I'm too stubborn to give up so easily)
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: Carlos3DGS]
#386613
11/06/11 14:35
11/06/11 14:35
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Im getting close too, I just dont know how to make sense of the Height data Ive captured...
//Application fed data
const float4x4 matWorldViewProj;
const float4x4 matWorld;
texture Sand_BM_bmap, Grass_BM_bmap, Rock_BM_bmap, Snow_BM_bmap;
// ColorMap Samplers
sampler SandSampler = sampler_state { Texture = <Sand_BM_bmap>; };
sampler GrassSampler = sampler_state { Texture = <Grass_BM_bmap>; };
sampler RockSampler = sampler_state { Texture = <Rock_BM_bmap>; };
sampler SnowSampler = sampler_state { Texture = <Snow_BM_bmap>; };
// Vertex Shader:
void TerrPaintVS( in float4 InPos: POSITION, in float2 InTex: TEXCOORD0,
out float4 OutPos: POSITION, out float2 OutTex: TEXCOORD0,
out float Height: TEXCOORD1 )
{
// Temporarily transform the vertex from object space to world space:
OutPos = mul(InPos, matWorld);
// Capture the HEIGHT data of the vertices
Height = 1-(OutPos.y/OutPos.w);
// Transform the vertex from object space to projected world space:
OutPos = mul(InPos, matWorldViewProj);
// Pass the texture coordinate to the pixel shader:
OutTex = InTex;
// Pass the calculated depth to the pixel shader: ... APPARENTLY
}
// Pixel Shader:
float4 TerrPaintPS( in float4 InPos: POSITION, in float2 InTex: TEXCOORD0,
in float Height:TEXCOORD1 ): COLOR
{
float4 Color;
if(Height < 0.16) Color = tex2D(SandSampler, InTex);
else if(Height < 0.50) Color = tex2D(GrassSampler, InTex);
else if(Height < 0.75) Color = tex2D(RockSampler, InTex);
else Color = tex2D(SnowSampler, InTex);
return Color;
}
// Technique:
technique AmbientTechnique
{
pass P0
{
VertexShader = compile vs_2_0 TerrPaintVS();
PixelShader = compile ps_2_0 TerrPaintPS();
}
}
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: Carlos3DGS]
#386614
11/06/11 14:35
11/06/11 14:35
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
User
|
User
Joined: Oct 2008
Posts: 513
|
Modified again now it outputs the position twice in two different variables. The error has dissapeared. I Don't understand why I need to output the pos twice if I don't use one them. Even though I don't get any error now my terrains are just invisible... Something is not working correctly:
// Vertex Shader:
void TerrPaintVS(
in float4 InPos: POSITION,
in float2 InTex: TEXCOORD0,
out float2 OutTex: TEXCOORD0,
out float4 OutPos: POSITION,
out float4 OutCoords: TEXCOORD1)
{
// Transform the vertex from object space to world space:
OutPos = mul(InPos, matWorld);
OutCoords=OutPos;
// Pass the texture coordinate to the pixel shader:
OutTex = InTex;
}
// Pixel Shader:
float4 TerrPaintPS(
in float2 InTex: TEXCOORD0,
in float4 InCoords: TEXCOORD1): COLOR
{
float4 Color;
if(InCoords.y<16) Color = tex2D(SandSampler, InTex);
else if(InCoords.y<50) Color = tex2D(GrassSampler, InTex);
else if(InCoords.y<75) Color = tex2D(RockSampler, InTex);
else Color = tex2D(SnowSampler, InTex);
return Color;
}
My brain hurts...
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: Carlos3DGS]
#386616
11/06/11 14:51
11/06/11 14:51
|
Joined: Mar 2006
Posts: 3,538 WA, Australia
JibbSmart
Expert
|
Expert
Joined: Mar 2006
Posts: 3,538
WA, Australia
|
I Don't understand why I need to output the pos twice if I don't use one them. You use TEXCOORD1. Your graphics card uses POSITION  That's all I've got for you for now -- I'm heading out the door.
Formerly known as JulzMighty. I made KarBOOM!
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: Carlos3DGS]
#386617
11/06/11 14:53
11/06/11 14:53
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Dude ... you really nee to terminate this thread, and go to the shaders forum...
We are SO FAR off topic, light from the big bang hasnt got here yet!
I love shaders SO MUCH!!! I think I will just go and shoot myself now...
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: EvilSOB]
#386619
11/06/11 15:05
11/06/11 15:05
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
To the best of my knowledge, WHICH IS PROBABLY WRONG, the POSITION output from the VS is needed by the PS even though it is not accessable to YOU. Thats why you need to send another copy of it via a 'spare' TEXTCOORD.
And I THINK the reason you can see anything, is cause you are translating the model using the "matWorld" matrix. It doesnt account for camera position I believe. So you should be translating "Out_Pos" with "matWorldViewProj" to see the model, even if you continue using "matWorld" with "OutCoords".
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: EvilSOB]
#386621
11/06/11 15:12
11/06/11 15:12
|
Joined: Oct 2008
Posts: 513
Carlos3DGS
User
|
User
Joined: Oct 2008
Posts: 513
|
@EvilSOB, tested your version "as is" and with yours I can at least see the terrains. They do get painted but in a wierd way: http://www.youtube.com/watch?v=2bAfOV1rA9s(they are in the inverse order and in very thin layers) Edit: took a closer look at your height var and modified a couple things. in your vertex function: changed this:
Height = 1-(OutPos.y/OutPos.w);
to this: in your pixel function: changed the ifs to this:
float4 Color;
if(Height < 16) Color = tex2D(SandSampler, InTex);
else if(Height < 50) Color = tex2D(GrassSampler, InTex);
else if(Height < 75) Color = tex2D(RockSampler, InTex);
else Color = tex2D(SnowSampler, InTex);
Results: http://www.youtube.com/watch?v=a6aU_ntY6iwOMG it works! THANKYOU very very very very very very very very very much! New painting speed tests: UNMESURABLY fast, super humanly, out of this world, superman gets owned by this speed, even the flash cannot paint this fast... (these are the results of the average times measured in a millionth of a second, taken with superhuman stopwatch) EDIT2: Yeah, sorry for the offtopic, started out talking about terrains and memory issues and this has slowly progressed to other terrain related topics, and now into a shader... Back on topic: Now I will start coding the moving of terrains based on my character position, reusing the old ones left behind (and for the rest of the stuff moving with the terrains, like trees etc, I will use the recycling linked-lists I posted before this turned into a shader discussion). I will update in this thread with any problems derived specifically from moving terrains and their sub-components, and in a separate thread anything related to your shader that I am now using for my terrains.
Last edited by Carlos3DGS; 11/06/11 15:34.
|
|
|
Re: Dumb terrain question.. Cannot get it to move...
[Re: Carlos3DGS]
#386622
11/06/11 15:31
11/06/11 15:31
|
Joined: Feb 2008
Posts: 3,232 Australia
EvilSOB
OP
Expert
|
OP
Expert
Joined: Feb 2008
Posts: 3,232
Australia
|
Yeah, it does seem a little faster then before...  Gimme a few minutes and I'll have another version for you. I think I have thouht of a wat to 'blend' the different maps at the changeover points to give a smoother transition... Let see how we go... [EDIT] Not as smooth as I wanted, but take a look and see what you think. Im gunna continue and TRY to make it actually SMOOTH, cause I need a rest from MY movement code. Im at the 'tidy and optimize' stage. And there is one particulat chunk of code Ive TRIED to optimize like 10 times, But it keeps going wrong and I need to backtrack the optimise. But theres my current shader, let me know what you think.
//Application fed data
const float4x4 matWorldViewProj;
texture Sand_BM_bmap, Grass_BM_bmap, Rock_BM_bmap, Snow_BM_bmap;
// ColorMap Samplers
sampler SandSampler = sampler_state { Texture = <Sand_BM_bmap>; };
sampler GrassSampler = sampler_state { Texture = <Grass_BM_bmap>; };
sampler RockSampler = sampler_state { Texture = <Rock_BM_bmap>; };
sampler SnowSampler = sampler_state { Texture = <Snow_BM_bmap>; };
// Vertex Shader:
void TerrPaintVS( in float4 InPos: POSITION, in float2 InTex: TEXCOORD0,
out float4 OutPos: POSITION, out float2 OutTex: TEXCOORD0,
out float Height: TEXCOORD1 )
{
// Transform the vertex from object space to projected world space:
OutPos = mul(InPos, matWorldViewProj);
// Pass the texture coordinate to the pixel shader:
OutTex = InTex;
// Capture the Height of this vertex from object space... SEEMINGLY
Height = abs(1-(InPos.y/InPos.w));
}
// Pixel Shader:
float4 TerrPaintPS( in float4 InPos: POSITION, in float2 InTex: TEXCOORD0,
in float Height:TEXCOORD1 ): COLOR
{
float4 Color;
if(Height < 16) Color = tex2D(SandSampler, InTex);
else if(Height < 26) Color = lerp(tex2D(SandSampler,InTex), tex2D(GrassSampler,InTex), 0.5);
else if(Height < 50) Color = tex2D(GrassSampler, InTex);
else if(Height < 55) Color = lerp(tex2D(GrassSampler,InTex), tex2D(RockSampler,InTex), 0.5);
else if(Height < 75) Color = tex2D(RockSampler, InTex);
else if(Height < 85) Color = lerp(tex2D(RockSampler,InTex), tex2D(SnowSampler,InTex), 0.5);
else Color = tex2D(SnowSampler, InTex);
return Color;
}
// Technique:
technique AmbientTechnique
{
pass P0
{
VertexShader = compile vs_2_0 TerrPaintVS();
PixelShader = compile ps_2_0 TerrPaintPS();
}
}
Last edited by EvilSOB; 11/06/11 15:55. Reason: Sdded shader
"There is no fate but what WE make." - CEO Cyberdyne Systems Corp. A8.30.5 Commercial
|
|
|
|