Sign in to follow this  
Halsafar

Pixel Shader blending

Recommended Posts

Halsafar    205
I do not know/cannot figure out how to write a PixelShader to take 4 textures and blend them simply. All 0,0,0 black needs to be 100% transparent. From 1,1,1 to 255,255,255 the transparency needs to go backwards (solid---transparent) Picture clouds in the sky, the darker greyer clouds are more solid but the whiter clouds are light and see through.

Share this post


Link to post
Share on other sites
What PixelShader are you using: HLSL/GLSL/DirectX ASM/GL ASM? What version: 1.1, 1.4, 2.0?

Also, are you saying that a value of 0 is totally transparent yet 1 - 255 ranges go from opaque to transparent? Why not just have 255 transparent instead? :/

Share this post


Link to post
Share on other sites
Gorax    202
TheGibberingFool does have a point there.

At any rate, I assume you want white things to be transparent, but anything else should be less transparent, correct?

An inverse alpha value is just (1.0 - alpha). Since in this case, you're using the colours to calculate the alpha value, you need to use this for all of the components, which'd give you this: ((1.0 - red + 1.0 - green + 1.0 - blue) / 3.0) (it's divided by 3, since we have 3 components). The result is ((1.0 + 1.0 + 1.0 - (red + green + blue)) / 3.0).

The shader code for this would be something like this:

outCol.a = (3.0f - (inCol.r + inCol.g + inCol.b)) / 3.0f;


Combined with the zero alpha for black:

outCol.a = 0.0f;
if ((inCol.r != 0.0f) || (inCol.g == 0.0f) || (inCol.b == 0.0f)){
outCol.a = (3.0f - (inCol.r + inCol.g + inCol.b)) / 3.0f;
}

That should give you the desired result.

Share this post


Link to post
Share on other sites
Halsafar    205
Black needs to be fully transparent since on for example the starmap texture, the only thing I want to see are its stars, so the rest of the texture is black. But in the same render the clouds needs everything but 0,0,0 black and the darker it is the more solid it is, as he said above, the inverse alpha.




I assume that code you posted, since I understood it was written for HLSL?


I was hoping to use PS1.4 since I haven't begun to even learn how to setup HLSL.

I need to do this to 4 textures in the first 4 stages.
I assume if I see how its done to 1 I can do the rest.

Share this post


Link to post
Share on other sites
Guimo    463
Use a shader for the stars and other for the clouds. Remember that you should try to keep your code simple. Shaders are useful for a single purpose. Dont try to create a single shader for all cases.

Luck!
Guimo

Share this post


Link to post
Share on other sites
Halsafar    205
Yes, that is my goal.
I already have a different shader for the terrain (texture splatting PS1.4 code off the recent Texture splatting article).


I need one specific for the whole thing.
The first texture will be a color gradient
The next is a starmap (all black except the stars)
The next 2 are different cloud textures


I was pondering a single-pass multitexture but I think this can be achieved nicer with a shader. I of course have only seen 1 piece of pixel shader code and didn't get it at all. I have read the first part of the Pixel Shader Programming article but once the article got into the RazorX code I was confused. So I'm really hoping to see a commented snippet so I can figure out how it works.

Share this post


Link to post
Share on other sites
Quote:
Original post by HalsafarI assume that code you posted, since I understood it was written for HLSL?


I was hoping to use PS1.4 since I haven't begun to even learn how to setup HLSL.


You can compile the HLSL into Direct3D ASM using tools like the compilers on the Microsoft MSDN website (somewhere) or a tool like RenderMonkey from ATi or Cg from nVidia. But you should be able to get the gist of that code segment. I would suggest having a look at the MSDN - I'd also recommend RenderMonkey. I'm learning to program shaders at the moment and both of those have been invaluable tools in furthering my knowledge of how it works.

Share this post


Link to post
Share on other sites
Halsafar    205
Okay I have been playing with RenderMonkey.

Is it possible to use it to actually program the HLSL, cuz I can seem to only test it... Rather, open up the examples.


Also, where is a good site that may show some basics on this stuff...
How do I pass variables into the HLSL...
I figured out how PS and VS work, you can SetConstantRegisters is one method...What would be the equivalent?

How do I use the fx files?
Is it simple, like an API call?

Share this post


Link to post
Share on other sites
Halsafar    205
ps.1.4
texcrd r0.rgb, t0
texld r1, t1
texcrd r2.rgb, t2
texld r3, t3
mad r0.rgb, c1, r1_bx2, r0 // distort base map coords
// by cloud coords
mad r2.rgb, c3, r3_bx2, r2 // distort base map coords
// by cloud coords
phase
texld r0, r0
texld r2, r2
mul_sat r1.rgb, r0_x2, c2 // cloud 0, saturate out color
+mul r1.a, r0.r, c2.a
mul_sat r3.rgb, r2_x2, c4
lrp r0.rgb, r2.r, r3, r1




This came off an ATI document, just curious. r0-r3 being used is textures in stages 0,1,2,3 right?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this