2 registered members (Akow, tomaslolo),
1,536
guests, and 12
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Generating random numbers in a pixel shader
#443382
07/14/14 22:01
07/14/14 22:01
|
Joined: Mar 2010
Posts: 120 Switzerland
TehV
OP
Member
|
OP
Member
Joined: Mar 2010
Posts: 120
Switzerland
|
Hi, I'm in the process of learning my way around shaders and thought I'd start with something simple. What I'm trying to achieve is a simple pixel shader that will color each pixel in a model a random gray tone. The pixels should be colored independently of eachother. So basically, I'm trying to draw a random monochrome noise onto the screen, in the shape of a 3D model, if that makes more sense. I was able to make the whole model a solid gray color and have it flicker as a whole, but I have not been able to color individual pixels. I've done a bit of research on this topic and came up with this:
//Flicker/Noise Shader
float4 vecTime;
float4 ps_flicker(in float2 screenSpace : VPOS): COLOR {
return noise(float3(screenSpace.x,screenSpace.y,vecTime.w));
}
technique flicker
{
pass p0
{
PixelShader = compile ps_1_1 ps_flicker();
}
}
I'm basically trying to use the screen position of the pixel as a sort of random seed for my random number generator. The problem is that the engine doesn't seem to recognize the : VPOS semantic. The DirectX9 documentation listed it under Pixel Shader Semantics and the examples along with it said that this is how it should be used. However, when I first look at the model, I get an error X4502: invalid ps_2_0 input semantic 'VPOS'. I've also tried the DirectX10 equivalent, 'SV_Position', but to no avail. I receive a similar error when I do that. Can anyone help me with this?
|
|
|
Re: Generating random numbers in a pixel shader
[Re: TehV]
#443386
07/14/14 22:37
07/14/14 22:37
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
Use this:
float rand_1_05(in float2 uv)
{
float2 noise = (frac(sin(dot(uv ,float2(12.9898,78.233)*2.0)) * 43758.5453));
return abs(noise.x + noise.y) * 0.5;
}
There is no random option on a GPU, but you can use some "random" math functions to generate unregular fractal parts to generate numbers between 0 and 1 Just input your screen or texture coordinates. Note that the random will stay constant until you add something like vecTime.w to your input coordinates. To get screen coordinates you need to pass the transformed position into your pixel shader from the vertex shader also use ps_2_0 or ps_3_0 as 1_1 is imho much too old and limiting for you
|
|
|
Re: Generating random numbers in a pixel shader
[Re: MasterQ32]
#443414
07/15/14 21:23
07/15/14 21:23
|
Joined: Mar 2010
Posts: 120 Switzerland
TehV
OP
Member
|
OP
Member
Joined: Mar 2010
Posts: 120
Switzerland
|
Thanks! That helped! I was now able to adjust the shader to my liking... somewhat. There's still a little problem that needs taking care of: As you can see, parts of the model that overlap are darker than ones that don't overlap. I only want to see the outline of the model, filled with the noise I currently have, without any of the overlaps showing. I would like the model to be translucent. Is this possible? If it helps (or if you want a copy of it for yourself), here's the shader I have right now:
//Flicker/Noise Shader
float4 vecTime;
float rand_1_05(in float2 uv)
{
float2 noise = (frac(sin(dot(uv ,float2(12.9898,78.233)*2.0)) * 43758.5453));
return abs(noise.x + noise.y) * 0.5;
}
float4 ps_flicker(in float2 screenSpace : VPOS): COLOR {
float rnd = rand_1_05(screenSpace*fmod(vecTime.w,1));
return float4(0,0,0,0.1 + (rnd * 0.5));
}
technique flicker
{
pass p0
{
PixelShader = compile ps_3_0 ps_flicker();
}
}
Thanks again for your help!
Last edited by TehV; 07/15/14 21:32.
|
|
|
Re: Generating random numbers in a pixel shader
[Re: Superku]
#443423
07/16/14 06:35
07/16/14 06:35
|
Joined: Mar 2011
Posts: 3,150 Budapest
sivan
Expert
|
Expert
Joined: Mar 2011
Posts: 3,150
Budapest
|
maybe using a depth view from camera position that includes only that entity, then use depth map as an inverse of in case of shadowmapping to ignore pixels or set to semi-transparent?
Last edited by sivan; 07/16/14 06:36.
|
|
|
Re: Generating random numbers in a pixel shader
[Re: sivan]
#443431
07/16/14 10:20
07/16/14 10:20
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
AAaaah much complicatated, such work! Simple use a two pass approach: First pass renders only depth of your model (One + Zero), second pass renders your model as a silhuette (remove first part to see overlapping)
float4 ps() : COLOR0 {
return float4(0.3, 0, 0, 1);
}
technique {
pass {
ZEnable = true;
ZFunc = Less;
ZWriteEnable = true;
AlphaBlendEnable = true;
SrcBlend = Zero;
DestBlend = One;
}
pass {
ZEnable = true;
ZFunc = LessEqual;
ZWriteEnable = false;
AlphaBlendEnable = true;
SrcBlend = One;
DestBlend = One;
PixelShader = compile ps_2_0 ps();
}
}
|
|
|
Re: Generating random numbers in a pixel shader
[Re: Superku]
#443461
07/16/14 21:45
07/16/14 21:45
|
Joined: Nov 2007
Posts: 2,568 Germany, BW, Stuttgart
MasterQ32
Expert
|
Expert
Joined: Nov 2007
Posts: 2,568
Germany, BW, Stuttgart
|
Yeah just wanted to clarify things also for not so shader affine people The Src-/DestBlend stuff is awesome, you can do things you don't want to know Like SrcBlend = OneMinusDestAlpha; DestBlend = OneMinusSrcAlpha;
|
|
|
|