Gamestudio Links
Zorro Links
Newest Posts
Zorro 2.70
by jcl. 09/29/25 09:24
optimize global parameters SOLVED
by dBc. 09/27/25 17:07
ZorroGPT
by TipmyPip. 09/27/25 10:05
assetHistory one candle shift
by jcl. 09/21/25 11:36
Plugins update
by Grant. 09/17/25 16:28
AUM Magazine
Latest Screens
Rocker`s Revenge
Stug 3 Stormartillery
Iljuschin 2
Galactic Strike X
Who's Online Now
1 registered members (TipmyPip), 18,618 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
krishna, DrissB, James168, Ed_Love, xtns
19168 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 10 of 11 1 2 8 9 10 11
Re: Dumb terrain question.. Cannot get it to move... [Re: EvilSOB] #386624
11/06/11 16:19
11/06/11 16:19
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline
User
Carlos3DGS  Offline
User

Joined: Oct 2008
Posts: 513
That was going to be my next step for the shader, thanks for saving me the headache.

Originally Posted By: EvilSOB
take a look and see what you think

I think I love you!

(There is only one small problem, it now paints in both directions, upwards and downwards. I'm working on a quickfix.
http://www.youtube.com/watch?v=vDD66R_hIIg

For the fix I just changed this line from your vertex function:
Quote:
Height = abs(1-(InPos.y/InPos.w));

to this:
Quote:
Height = InPos.y;

http://www.youtube.com/watch?v=FrJyLcX49sk

Last edited by Carlos3DGS; 11/06/11 16:20.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: Dumb terrain question.. Cannot get it to move... [Re: Carlos3DGS] #386626
11/06/11 16:28
11/06/11 16:28
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
hehe... thats what i get for testing with a model that is entirely above 0.


"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] #386629
11/06/11 19:25
11/06/11 19:25
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline
User
Carlos3DGS  Offline
User

Joined: Oct 2008
Posts: 513
how is that smoother version going? I'm really interested in it, I am reading alot of stuff about shaders meanwhile to add some stuff to it later. Lighting and stuff like that (at least I'm good with those parts that rely heavily on vectormath)

Edit:
I played around with the code a bit to get smoothing working.

I only needed to edit the pixel function's "if" statement's calculations like this:
Quote:
if(Height < 32) Color = tex2D(SandSampler, InTex);
else if(Height < 42) Color = lerp(tex2D(SandSampler,InTex), tex2D(GrassSampler,InTex), (Height-32)/10);
else if(Height < 75) Color = tex2D(GrassSampler, InTex);
else if(Height < 85) Color = lerp(tex2D(GrassSampler,InTex), tex2D(RockSampler,InTex), (Height-75)/10);
else if(Height < 120) Color = tex2D(RockSampler, InTex);
else if(Height < 130) Color = lerp(tex2D(RockSampler,InTex), tex2D(SnowSampler,InTex), (Height-120)/10);
else Color = tex2D(SnowSampler, InTex);


Preview:
http://www.youtube.com/watch?v=gjGJeNkAO3M

Note: I used a height of 10 for the smoothing between each layer (mainly for mathematical conveniance).

Note2: In all of the "shader versions" we posted previously you have probably noticed that the original lighting and shadows have dissapeared. Entities have their default shader which does things like that. But now that we are using our own, it dosn't use that default shader and since we are not takng those things into acount, any lighting or shadows have dissapeared from our terrains... I will modify the current code to calculate shadows and lighting and repost the final version when it is done.

Last edited by Carlos3DGS; 11/07/11 03:26.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: Dumb terrain question.. Cannot get it to move... [Re: Carlos3DGS] #386645
11/07/11 06:43
11/07/11 06:43
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
I dont think I'll get any better than what youve done already,
so Ive integrated yours into mine (below).

Yeah, I knew shadows and lighting needs to be redone but ORIGINALLY I just
wanted this shader to run ONCE and calculate a new skin for the terrain.
Not to be kept running like this one is...

Im still going to keep heading in that direction, for educational purposes.

Im posting my 'current' so you can see the height 'randomiser' Ive added
to the VERTEX shader, in order to 'roughen up' the 'flat' edges.
But its is NOW hard to spot with my existing test-model with your smoothing.
So IF you take a look, disable your smoothing to actually see whats happening.

Click to reveal..
Code:
//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>;  };




#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); 
}


// 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 = InPos.y;
	// OPTIONALLY add some randomness to the demarkation line
	Height += (random(InPos)*30)-10;
}


// Pixel Shader: 
float4 TerrPaintPS(	in float4 InPos: POSITION,	 in float2 InTex: TEXCOORD0,
							in float  Height:TEXCOORD1											): COLOR 
{
	float4 Color = 0;

	if(Height < 32) Color = tex2D(SandSampler, InTex); 
	else if(Height < 42) Color = lerp(tex2D(SandSampler,InTex), tex2D(GrassSampler,InTex), (Height-32)/10);
	else if(Height < 75) Color = tex2D(GrassSampler, InTex); 
	else if(Height < 85) Color = lerp(tex2D(GrassSampler,InTex), tex2D(RockSampler,InTex), (Height-75)/10);
	else if(Height < 120) Color = tex2D(RockSampler, InTex); 
	else if(Height < 130) Color = lerp(tex2D(RockSampler,InTex), tex2D(SnowSampler,InTex), (Height-120)/10);
	else Color = tex2D(SnowSampler, InTex);

	return Color; 
} 



// Technique: 
technique AmbientTechnique 
{ 
	pass P0 
	{ 
		VertexShader = compile vs_2_0 TerrPaintVS(); 
		PixelShader  = compile ps_2_0 TerrPaintPS(); 
	} 
}



Ill do some more playing once I get to work tonight...


"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] #386651
11/07/11 12:05
11/07/11 12:05
Joined: Oct 2008
Posts: 513
Carlos3DGS Offline
User
Carlos3DGS  Offline
User

Joined: Oct 2008
Posts: 513
tested your code and looks alot better now that the edges are going up and down instead of straight across, and when combined with my smoothing it looks great.
Using it only once is probably a better idea for framerate, but I have no idea how to make it create an actuall texture and pass it back to the game's script so it can assign it as a skin.

EDIT: and something I have been wondering about since your first post related to shaders... Why is "if" considered dirty?

Last edited by Carlos3DGS; 11/07/11 12:12.

"The more you know, the more you realize how little you know..."

I <3 HORUS
http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=401929&page=1
Re: Dumb terrain question.. Cannot get it to move... [Re: Carlos3DGS] #386680
11/08/11 10:31
11/08/11 10:31
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
From what Ive read over the years, from many sources,
putting an IF in a shader, ESPECIALLY a pixel shader, is 'frowned upon'.
In much the same way as a GOTO is frowned upon in any BASIC language.

Mostly, as far as I can tell, its because GPU hardware is optimised/designed primarily for
color/position calculations (VECTORs), rather than logic/arithmatic calculations like for/if.

So using an IF in a pixel-shader is 'inefficient' at a hardware level, and can therefore
cause a shader to be significantly slower because IF itself is so slow...


Thats my theory anyway. Ive never seen a 'concise' explaination of why...

And Im going to look into the 'saving' the shader result into the entities 'skin' later tonight.
I never got a chance to do ANYTHING last night...


"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] #386690
11/08/11 14:12
11/08/11 14:12
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
If I recall correctly, anything below Shader Model 3.0 (and sometimes Shader Model 3.0 if it gets "optimised" that way) will calculate the results of both sides of a branch and then use the results of the comparison to choose which results to keep. So, it works, and it doesn't really make things worse, but keep in mind performance-wise that you're not skipping branches. As such, it'd probably be best to change your if/else to get the four colours once and do some interpolation from there.

For example, in the code you (Carlos) posted above, you're probably sampling each texture twice.


Formerly known as JulzMighty.
I made KarBOOM!
Re: Dumb terrain question.. Cannot get it to move... [Re: JibbSmart] #386693
11/08/11 14:29
11/08/11 14:29
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Im still 'thinking' about a way to remove the if's and replace with consecutive lerps. Something LIKE this...
Code:
float4  Color = tex2D(SandSampler, InTex); 
        Color = lerp(Color, tex2D(GrassSampler,InTex), [insert evil formula here]);
        Color = lerp(Color, tex2D(RockSampler,InTex),  [insert evil formula here]);
        Color = lerp(Color, tex2D(SnowSampler, InTex), [insert evil formula here]);


But while Im thinking about that "evil formula", Im also looking to
my own future needs with this THREAD HERE . Which, if possible,
would make this optimisation (for ME) unnecessary...


"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] #386695
11/08/11 14:43
11/08/11 14:43
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
Yeah, that's the kind of thing I was thinking. And the evil forumla would be something like... um... [saturate(SCALE_FACTOR * (Height - OFFSET))]. OFFSET would differ per lerp, and SCALE_FACTOR will require some further thought and/or trial and error, I guess.


Formerly known as JulzMighty.
I made KarBOOM!
Re: Dumb terrain question.. Cannot get it to move... [Re: JibbSmart] #386698
11/08/11 15:14
11/08/11 15:14
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline OP
Expert
EvilSOB  Offline OP
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Same as what Im thinking. I just want to see how the other thread goes,
so HOPEFULLY I wont need to work on this evil formula...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Page 10 of 11 1 2 8 9 10 11

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1