waving grass

Posted By: ventilator

waving grass - 10/01/03 05:13

i adapted the waving grass shader which can be found at the ati website for a6!

Code:
texture texSkin1;


matrix matWorldViewProj;
matrix matWorldView;

vector vecFog;
vector vecTime;

technique grass
{
pass p0
{
Texture[0]=<texSkin1>;
ZWriteEnable=true;
AlphaTestEnable=true;
CullMode=none;

VertexShaderConstant[0]=<matWorldViewProj>;
VertexShaderConstant[8]=<matWorldView>;
VertexShaderConstant[16]=<vecFog>;
VertexShaderConstant[32]=<vecTime>;
VertexShaderConstant[33]=(0.0f,0.5f,1.0f,2.0f); // common constants
VertexShaderConstant[34]=(-0.16161616f,0.0083333f,-0.00019841f,0.000002755731f); // (-1/3!,1/5!,-1/7!,1/9!)
VertexShaderConstant[35]=(4.0f,1.57079632f,3.14159265f,6.28318530f); // (4.0,pi/2,pi,pi*2)
VertexShaderConstant[36]=(1.07f,0.0f,0.0f,0.0f); // fixup factor

VertexShaderConstant[40]=(2.5f,1.0f,0.5f,0.1f); // wave distort x
VertexShaderConstant[41]=(5.0f,0.5f,0.25f,0.1f); // wave distort y
VertexShaderConstant[42]=(-1.0f,-0.01f,-0.04f,-0.01f); // wave distort z
VertexShaderConstant[43]=(-0.1f,-0.2f,0.1f,0.05f); // wave dir x
VertexShaderConstant[44]=(-0.005f,-0.01f,-0.005f,-0.0025f); // wave dir y
VertexShaderConstant[45]=(0.02f,0.04f,0.05f,0.1f); // wave speed
VertexShaderConstant[46]=(0.35f,0.1f,0.1f,0.025f); // lighting wave scale
VertexShaderConstant[47]=(0.6f,0.8f,0.2f,0.0f); // lighting scale bias

VertexShader=
decl
{
stream 0;
float v0[3]; //position
float v3[3]; //normal
float v7[2]; //uv
}
asm
{
vs.1.1

mov r1.w,c16.w // r1.w=1
dp4 r0,v0,c10 // distance to camera position (v0 is the position without sinusodial warping!)
add r0,r0,-c16.x // distance-fog_start
mad r0.x,-r0.x,c16.z,r1.w // 1-(distance-fog_start)*(1/(fog_end-fog_start))
max oFog.x,r0.x,c33.x // clamp with custom max value

mul r0,c43,v0.x // use vertex pos x as inputs to sinusoidal warp
mad r0,c44,v0.y,r0 // use vertex pos y as inputs to sinusoidal warp

mov r1,c32.w // get current time
mad r0,r1,c45,r0 // add scaled time
frc r0.xy,r0 // take fraction of all 4 components
frc r1.xy,r0.zwzw
mov r0.zw,r1.xyxy

mul r0,r0,c36.x // multiply by fixup factor (due to inaccuracy of taylor series)
sub r0,r0,c33.y // subtract 0.5
mul r1,r0,c35.w // range -pi to pi

mul r2,r1,r1 // wave_vec^2
mul r3,r2,r1 // wave_vec^3
mul r5,r3,r2 // wave_vec^5
mul r7,r5,r2 // wave_vec^7
mul r9,r7,r2 // wave_vec^9

mad r0,r3,c34.x,r1 // wave_vec-(wave_vec^3)/3!
mad r0,r5,c34.y,r0 // +(wave_vec^5)/5!
mad r0,r7,c34.z,r0 // -(wave_vec^7)/7!
mad r0,r9,c34.w,r0 // +(wave_vec^9)/9!

dp4 r3.x,r0,c40
dp4 r3.y,r0,c41
dp4 r3.zw,r0,c42

sub r4,c33.z,v7.y // attenuate sinusoidal warping by (1-uv.y)^2
mul r4,r4,r4
mul r3,r3,r4

mov r2.w,v0
add r2.xyz,r3,v0 // add sinusoidal warping to grass position
m4x4 oPos,r2,c0 // transform position to clip space

dp4 r1.x,r0,c46 // scale and add sin waves together
mad oD0,c47.xzxz,-r1.x,c47.y // scale and bias color values (green is scaled more than red and blue)
mov oT0.xy,v7.xy // output uvs
};

PixelShader=
asm
{
ps.1.3
def c0,1,1,1,1
def c1,0,0,0,0
tex t0 // sample t0
mov r0,t0
cnd r0.a,r0.a,c0,c1 // if(r0.a>0.5){r0.a=1;}else{r0.a=0;}
mul_x2 r0.rgb,r0,v0 // modulate with lighting
};
}
}



use some intersecting quads as grass model. the grass texture should be a tga bitmap with alphachannel. the polygons will be rendered two-sided.

there are a lot of parameters to tweak (c40-c47) and i am sure it's possible to achieve nicer motion than with my settings!


Posted By: Cameron_Aycock

Re: waving grass - 10/01/03 07:58

WOW - You never cease to amaze me Ven!

Can you give a better description of what you did? Can you release any files to play with? What do you recommend to learn shader development?

I re-wrote the terrain sewing code if you think you could use that. It is in another thread...no tracing.
Posted By: myrlyn68

Re: waving grass - 10/01/03 09:20

While I do not have access to the A6 shaders yet, this type of stuff is pretty straight forward. If you do a search for shader development groups - you will find a few different methods to accomplish it, including this one.

Also, if I recall they used grass as an example in ShaderX (the book) - though it is at work, so I can't look right now.
Posted By: ventilator

Re: waving grass - 10/01/03 09:47

as far as i know this shader from ati is exactly the same as in the shaderx book. i just did some changes to it so that it works with a6 and i created some grass models for it.

for learning the basics about shaders i read the vertex- and pixelshader introductions by wolfgang engel -> www.direct3d.net. they are great! the assembler language is easy but for more complex shaders my maths skills are somewhat lacking! for example vertex shaders have to use taylor series to calculate sine values and i don't really understand how taylor series work!
Posted By: myrlyn68

Re: waving grass - 10/01/03 09:50

Also - for those who are playing with the shaders, Gamasutra ran a short article on an interesting water effect (light refracting and causing the ground under water to show light and dark spots with the waters ripples). It contained the full code and what not (I forget if it used any DX9 commands or if it was all DX8...). Pretty interesting stuff (though completely off topic).
Posted By: cbw

Re: waving grass - 10/02/03 00:05

I don't think it's off topic, it's a shader effect, and this is the shader forum. I think I know what you're talking about, but what about the top of the water? http://mirror.ati.com/developer/samples/oceanwater.html If anyone (I'm looking at you Ven) wants to try coverting this, I think it'll make realistic water that much easier in 3DGS.
Posted By: Cameron_Aycock

Re: waving grass - 10/02/03 04:01

I second that!
Posted By: myrlyn68

Re: waving grass - 10/02/03 04:19

As opposed to hijacking this thread - here is one for water FX:

http://www.conitecserver.com/ubbthreads/showflat.php?Cat=&Number=301007&page=0&view=collapsed&sb=5&o=&fpart=1
Posted By: Orange Brat

Re: waving grass - 10/03/03 04:44

Check this out:

http://www.idvinc.com/
Posted By: nightshade

Re: waving grass - 10/05/03 20:36

Gute leistung ventilator!
Posted By: er_willy

Re: waving grass - 10/11/03 17:33

hey ventilator you is a crak men

buen trabajo tio
Posted By: Drew

Re: waving grass - 10/21/03 10:11

that looks fantastic! Now imagine a character or physics object moving through, parting the grass...is that possible?
Can you convert any other ATI demos? like the bubble or water? Great work!
Posted By: Drew

Re: waving grass - 10/31/03 15:11

Hmmm.. how do you implement it if there is no mat_

I tried to add a maetrial for my action to reference, but couldn't get it working... how are you calling the material?

Posted By: ventilator

Re: waving grass - 11/01/03 05:16

save the above shader as grass.fx and use this script:


material mat_grass{}

starter update_grass
{
effect_load(mat_grass,"grass.fx");
}

on_m=update_grass;

i did it this way because then it's possible to edit the parameters of the shader while the engine is running and update the effect with the m key.
Posted By: Drew

Re: waving grass - 11/01/03 06:45

Thanks! that did it! very, very cool! I like the motion you set!

Posted By: Drew

Re: waving grass - 11/05/03 11:56

doesn't seem to work in the latest beta 6.12.7
Crashes my pc...freezes


Posted By: Matt_Aufderheide

Re: waving grass - 11/05/03 12:14

hey can you get any shaders to work on the new beta? you had a problem with the specualr shader and now this.. i am thinking you somehow didnt install the beta correctly ..
Posted By: Drew

Re: waving grass - 11/05/03 12:20

the other shaders work fine though(vent's enviro mapping and A6 new fx), its installed correctly... this one is brutal though, complete freeze up..

Posted By: Drew

Re: waving grass - 11/05/03 12:50

No crash...

DOH! i guess it was something bad wuth my install, re-installed my app and scripts and its ok...

Sorry...

Posted By: indiGLOW

Re: waving grass - 11/20/03 18:27

I was wondering, little off the shader topic, but only a little.....

....could you create a grass sowing system based on an image of the world map, something like this?

say blue would be grass, white shrubbs etc...

Just that this could allow for fast delivery of grass or other shrubs etc...especially considering your Waving Grass. I am really impressed with EA Sports Tiger Woods 2004, its grass is lovely The only way I think they could have done it is with the method I suggest above, the grass is dense on certain parts of the course filling the rough areas perfectly, I wondered if there was a way to achieve this with your grass example?

If someone can point me in the right direction...
Posted By: Drew

Re: waving grass - 11/21/03 05:45

thats a great idea
Maybe this will help (from latest betareadme)

effect_load(material,string name) (Commercial & Pro)
Loads an effect file in Microsoft .fx format from the work folder
into the given material, and applies to effect to all models and
terrain that have this material sssigned. This instruction can be
used to edit effects and shaders in real time and observe the result
immediately in the game.
Posted By: indiGLOW

Re: waving grass - 11/21/03 20:56

That seems pretty good, so you can see how your changes effect the....effect....without having to relaunch it all the time. I would guess that you could link this to a image reader that maps the picture (above) from top left to bottom right and uses the alpha to calculate where to place the grass models....

i.e the models that the waving grass is attached too, is deployed according to the image...... The main problem I can see is the z position, i.e do the grass models have a -z until hit something, to place them on the terrain, of can you compare x,y position from the image with the z position of the terrain...and use them to deploy the grass/shrubb models?

I am sure I could get this to work, and may do so yet, but with my current work load it is likely to take some time....

This sounds like a job for the V man....
Posted By: sboyette

Re: waving grass - 01/09/04 13:57

Sorry to post a new quesiton..
I have added the grass to my terrain, and I can see it moving..
however it is all black..
how do I, and where is the targa image that is to be used defined?

Regards,
Sean
Posted By: cbw

Re: waving grass - 01/10/04 10:35

Hmm, I had this problem as well, I believe I removed these lines:

mul_x2 r0.rgb,r0,v0 // modulate with lighting

and

Texture[0]=<texSkin1>;

Just comment them out, it should work
Posted By: Thomas_Nitschke

Re: waving grass - 01/27/04 00:52

Hi,
umm, because you were just talking about lighting problems there... what can I do to control the brightness of the model with the effect assigned? The effect works great (nice work ventilator ) but the models that use it don't seem to be influenced by any level lighting, ambient parameters oder dynamic lights. So what can I do if I simply want it to be a little darker?
Posted By: Thomas_Nitschke

Re: waving grass - 01/29/04 21:47

Hey come on, noone!?
This is really important to me, doesn't anyone know how to control the brightness with this effect assigned?
To go a little off-topic, I guess the board's sloooow speed is most likely to be the reason for this massive "posting-break" but... come on let's keep the community alive!
Post it!
Back to bussiness:
ventilator, the matrix has you. In fact it's searching you, where are you? need help!

Posted By: ventilator

Re: waving grass - 01/29/04 22:05

either do the wanted lighting calculations with <veclight> and everything taken into account in the vertex shader and pass it to the pixelshader as oD0 or (the easier solution) remove the pixelshader and use the directx settings from the second post of this thread instead:

http://www.conitecserver.com/ubbthreads/showflat.php?Cat=&Number=297998&page=1&view=collapsed&sb=5&o=&fpart=1

should work but i haven't tested this!
Posted By: Thomas_Nitschke

Re: waving grass - 01/29/04 22:12

Hi,
now the matrix has you
Nice quick answer, thanks a lot! Could you maybe explain this <veclight> thing? actually I'm quite new to shaders...
And, btw, I need the pixelshader for the waving effect.
Posted By: ventilator

Re: waving grass - 01/29/04 22:17

the vertex shader does the waving effect!

...i would have to experiment with the lighting calculation and don't have the time at the moment...
Posted By: Thomas_Nitschke

Re: waving grass - 01/29/04 23:15

Oh does it???
Well what then do you need the pixelshader for?
Posted By: ventilator

Re: waving grass - 01/29/04 23:22

the condition in the pixelshader avoids sorting errors by making every pixel fully transparent or fully opaque. the directx settings i mentioned do the same but without the need to calculate proper lighting in the vertex shader.

...the original ati vertex shader only does a very simple lighting calculation which only fluctuates the brightness a little in sync with the waves...
Posted By: EX Citer

Re: waving grass - 01/29/04 23:23

Mit diesem Shader kann man bestimmt auch Haar/Fell machen, oder? So wie in der Dawn/Dusk demo
MfG EX
Posted By: Thomas_Nitschke

Re: waving grass - 01/30/04 19:31

Hi,
@ventilator: Is it possible to avoid sorting errors without the pixelshader then? Or did I get something wrong now?
@Ex Citer: Tjo das wär sicher nicht schlecht
Aber dann müsste man wohl 2 Modelle mergen, eins für das Tier oder Monster oder was auch immer, und eins für die Haare, weil man den Shader ja nur auf das ganze Modell anwenden kann wenn ich das richtig kapiert hab ^^
Posted By: ventilator

Re: waving grass - 01/30/04 19:36

the directx settings do the same as the pixel shader but with the lighting you want!

...
für haare eignet sich dieser shader eigentlich nicht, dafür gibt es andere techniken...
Posted By: Thomas_Nitschke

Re: waving grass - 01/30/04 20:22

Well, that's why I asked whether I understood something wrong
I'm sorry but in fact I'm quite new to this, so I just didn't get this was exactly the answer to my question O_o
Posted By: Darkstorm

Re: waving grass - 08/06/04 01:04

Hey I am having absolutely no luck with this one. Would someone be so kind as to upload a working demo of this so I can see what I am doing wrong? Thanks.
Posted By: Sebe

Re: waving grass - 08/23/04 05:38

I can't get it work too

Would anybody be so kind to produce a small demo or just an example? This shader would be perfect for my outdoor levels
Posted By: EX Citer

Re: waving grass - 08/23/04 14:55

I think that grass shader is only working on terrain right? Could it be re written to work on leveltextures? And would it be possible to use Models (spike pits, (I have a texture called spikes. If a entity steps on the texture it looses. Putting entities into alle these pits of different size would be a big waste. I think a shader would do that better. Important would be the same distant of every spike). I hope the entity is faded out if its to far away from the cam (like detail maps, which are only rendered of they are close to the cam. The distance should be adjustable).

EX Citer
Posted By: ello

Re: waving grass - 08/23/04 15:19

its as easy to implement. just place some gras sprites in your level and apply the action on it. just c&p the code from the wiki. there are no problems using it (if your card supports the needed versions).
Posted By: Thomas_Nitschke

Re: waving grass - 08/27/04 00:37

I thought this shader was only working with MDLs?
@ExCiter: No idea what you think this thing was... but it hast nothing to do with neither terrain nor level geometry
© 2024 lite-C Forums