Vertex shader question

Posted By: SirSven

Vertex shader question - 10/01/05 14:59

Is it possible to make a shader (i think vertex shader is needed - but i'm a shader noob) which turns all vertices of an modell to the camera? Like an sprite with facing=on and oriented=off but for every vertex of a modell.

Thanks
Posted By: Specterdragon

Re: Vertex shader question - 10/03/05 18:17

If I'm understanding you correctly, you're trying to flatten the model into a plane and then have that plane facing the camera? If so, does the model recalculate and reflatten itself on every frame, or once flattened does this fixed image face the camera?

Depending on your aplication, you may or may not need a vertex shader vs. a different model. Any chance of getting a more in-depth explination of the effect you're trying to achieve?
Posted By: SirSven

Re: Vertex shader question - 10/04/05 16:57

I mean it this way:
http://www.directupload.net/show/d/475/36peoRzZ.jpg#

I have a model with 4 (unconectet) vertices and i would like every vertex to turn to the camera.
Posted By: Rigoletto

Re: Vertex shader question - 10/05/05 11:00

That could be the solution:

web page
Posted By: SirSven

Re: Vertex shader question - 10/05/05 11:58

Thank you, but I'm so bad in shader programming (shame on me). I can't get it work. If tried Rhuarc's code, but I get errors. Maybe someone can have a look at this code.

Code:
DELETED -> IT WAS DAMN BAD, SORRY


Posted By: Rigoletto

Re: Vertex shader question - 10/05/05 16:40

Well, there are a lot of mistakes. Take a look at the wiki tutorials and make a second try. If there a still errors post again.
Posted By: SirSven

Re: Vertex shader question - 10/05/05 17:41

Thanks Rigoletto (you must excuse because thats the first shader I'm triing to program and it's very hard to learn with my bad english )

So here is my 2nd try

Code:
 material material_test
{
effect
"
float4x4 matWorldViewProj;
texture entSkin1;

sampler ScreenProjSampler = sampler_state
{
texture = <entSkin1>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
};

float3 mainVS(float4 Pos : POSITION0) : TEXCOORD0
{
float4 toScreen = mul(float4(Pos.xyz,1),matWorldViewProj);
float3 tex0;
tex0.x = 0.5f * (toScreen.z+toScreen.x);
tex0.y = 0.5f * (toScreen.z-toScreen.y);
tex0.z = 1.0f * toScreen.w;

return tex0;
}

float4 mainPS(float3 tex0 : TEXCOORD0) : COLOR0
{
float2 screenCoords;
screenCoords.x = tex0.x/tex0.z;
screenCoords.y = tex0.y/tex0.z;
float4 ScreenSpaceSample = tex2D(ScreenProjSampler,screenCoords.xy);

return ScreenSpaceSample;
}

technique test
{
pass p0
{
VertexShader = compile vs_2_0 mainVS();
PixelShader = compile ps_2_0 mainPS();
}

}
";
}



Now I get an error with the position in the vs and i can't understand why.

Posted By: Rhuarc

Re: Vertex shader question - 10/05/05 17:54

Quote:

Thanks Rigoletto (you must excuse because thats the first shader I'm triing to program and it's very hard to learn with my bad english )

So here is my 2nd try

<code omitted>

Now I get an error with the position in the vs and i can't understand why.





You need to do just that, write out the position coordinates.

Code:
 material material_test
{
effect
"
float4x4 matWorldViewProj;
texture entSkin1;

sampler ScreenProjSampler = sampler_state
{
texture = <entSkin1>;
magfilter = LINEAR;
minfilter = LINEAR;
mipfilter = LINEAR;
};

struct VS_OUT
{
float4 pos : POSITION;
float3 tex0 : TEXCOORD0;
};

VS_OUT mainVS(float4 Pos : POSITION0)
{
VS_OUT outStruct;
outStruct.pos = mul(Pos,matWorldViewProj);
float4 toScreen = mul(float4(Pos.xyz,1),matWorldViewProj);
outStruct.tex0.x = 0.5f * (toScreen.z+toScreen.x);
outStruct.tex0.y = 0.5f * (toScreen.z-toScreen.y);
outStruct.tex0.z = 1.0f * toScreen.w;

return outStruct;
}

float4 mainPS(float3 tex0 : TEXCOORD0) : COLOR0
{
float2 screenCoords;
screenCoords.x = tex0.x/tex0.z;
screenCoords.y = tex0.y/tex0.z;
float4 ScreenSpaceSample = tex2D(ScreenProjSampler,screenCoords.xy);

return ScreenSpaceSample;
}

technique test
{
pass p0
{
VertexShader = compile vs_2_0 mainVS();
PixelShader = compile ps_2_0 mainPS();
}

}
";
}



This shader however, transforms the texture to be displayed flat on a 3d object, not flatten out the polys of a 3d object (alike to a sprite) and face it to the camera, that would be a bit different.

-Rhuarc
Posted By: SirSven

Re: Vertex shader question - 10/05/05 18:28

Thank you Rhuarc

Now it works without errors, but I just see a black object!
Posted By: Rigoletto

Re: Vertex shader question - 10/05/05 18:35

I have make a short try to archive your desired effect in effectedit (DX9 SDK, good for fast testing). It is easy possible to flat all vertex on ONE plane, but setting the z-coord gives sorting errors and looks bad. Manipulating the vertex in the way you want needs to know which vertex belongs together, and which is used as z-coord. I think this is nearly impossible!? Perhaps VS3.0...
Posted By: SirSven

Re: Vertex shader question - 10/05/05 18:50

Maybe it is possible to go around the z-sorting errors with this code from the wiki?

Code:
function mtl_vegetation_init
{
vec_set(mtl.emissive_blue,mat_model.emissive_blue);
vec_set(mtl.ambient_blue,mat_model.ambient_blue);
vec_set(mtl.diffuse_blue,mat_model.diffuse_blue);
vec_set(mtl.specular_blue,mat_model.specular_blue);
mtl.power=mat_model.power;
mtl.albedo=mat_model.albedo;
mtl.skill1=pixel_for_vec(vector(128,0,0),0,8888); // the first value in the vector is the threshold
}

material mtl_vegetation
{
event=mtl_vegetation_init;
effect=
"
texture entSkin1;
dword mtlSkill1;

technique vegetation
{
pass p0
{
Texture[0]=<entSkin1>;
ZWriteEnable=True;
AlphaBlendEnable=False;
AlphaTestEnable=True;
AlphaRef=<mtlSkill1>;
AlphaFunc=Greater;
CullMode=CCW; // CCW or None

ColorArg1[0]=Texture;
ColorOp[0]=Modulate2X;
ColorArg2[0]=Diffuse;
}
}
technique fallback{pass p0{}}
";
}

action vegetation
{
my.transparent=off;
my.flare=off;
my.material=mtl_vegetation;
}


Posted By: Rigoletto

Re: Vertex shader question - 10/06/05 11:12

No, this won´t work, cause there are no values to sort if they are on one plane.
Posted By: Specterdragon

Re: Vertex shader question - 10/06/05 18:19

Quote:

Now it works without errors, but I just see a black object!




It could be the fog setting for the pass. I forget the exact command off the top of my head (at work, no code to review). Someone want to help on this? fogenable=false ?? or is it enablefog=false ?? (which goes in the "pass" section of the shader.
© 2024 lite-C Forums