# color per pixel

This topic is 2512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

This should be pretty basic stuff, but i just can't get my head around it.

Basically what i want to do is check wether a pixel is facing the camera or not. If the pixel is directly facing the camera, then i want to color it completely black. When it's 90 degrees rotated, then it should have full transparency.

I guess i have all the needed stuff in my vertex shader:

 varying vec3 Normal; varying vec3 EyeDir; varying vec4 EyePos; void main(void) { gl_Position = ftransform(); Normal = normalize(gl_NormalMatrix * gl_Normal); vec4 pos = gl_ModelViewMatrix * gl_Vertex; EyeDir = pos.xyz; EyePos = gl_ModelViewProjectionMatrix * gl_Vertex; } 

In my fragment shader i have two textures. One is just some random image and the other is completely black. I guess i have to "mix" them some how, depending on the position of the pixel. But i have no idea how to calculate that...

## Anyone any idea how to do this..??

##### Share on other sites

Basically what i want to do is check wether a pixel is facing the camera or not. If the pixel is directly facing the camera, then i want to color it completely black. When it's 90 degrees rotated, then it should have full transparency.
[/quote]
If it's 45 degrees rotated is it should contain 50% - black and 50% - transparent? If yes:
 float shade = max(dot(normalize(EyeDir), normalize(Normal)),0.0); // fixing 'solve' because dot returns [-1.0..1.0] but mix expects result in range [0.0...1.0] 
Otherwise:
 float shade = float(dot(normalize(EyeDir), normalize(Normal))>0.0); 

Is this what you want?

Best wishes, FXACE.

##### Share on other sites

If it's 45 degrees rotated is it should contain 50% - black and 50% - transparent? If yes:
 float shade = max(dot(normalize(EyeDir), normalize(Normal)),0.0); // fixing 'solve' because dot returns [-1.0..1.0] but mix expects result in range [0.0...1.0] 

Is this what you want?

Best wishes, FXACE.

Yes, that's exactly what i mean. When it's 45 degrees then the color should be 50% transparent.

I tried your example, but it still paints my entire sphere black. I have no compile errors though. So the shader is fine. The calculation is somehow not correct.

Any idea what the problem could be?

##### Share on other sites
It's working now. I had to change the EyeDir with EyePos.

Just one other thing that i'm struggling with. Now the gradient goes from all black (0 degrees; a pixel which faces the camera), to 90 degrees, fully transparent (a pixel that isn't facing the camera). Like i wanted.

What if i want it to go from 0 degrees (fully black) to 30 degrees (100% transparent) instead of 90 degrees..?

I've tried to do some additions, subtractions on the dot product. But they didn't gave the desired result.

Anyone any idea?

##### Share on other sites
shade = max((dp - 0.866025) / (1.0 - 0.866025),0.0); where: "dp" is a dot production of EyePos to Normal; 0.866025 - cos(30 <your degrees> * Pi / 180).

Best wishes, FXACE.

##### Share on other sites
Hi, thanks alot for your help! It seems to be working fine when i use the following:

 float dp = (max(dot(normalize(EyePos), normalize(Normal)), 0.0) + MixRatio); //MixRatio = for strength float shade = max((dp - 0.866025) / (1.0 - 0.866025),0.0); //float shade = dp - cos(30 * 3.14159 * 180); 

Although i wasn't sure what the 2nd formula you posted was for? It didn't seem to give the desired effect. And i'm also not sure how you got to the number '866025'. It looks like that when i change that number, that it also changes the range of the falloff. Kinda like i'm doing with the MixRatio variable when i caluclate the 'dp' variable.

##### Share on other sites
2nd formula from my first post?... Never mind. (It was variant "did you want to make it blended or not")

Hmmm, I would try to rewrite my previous post to make it more clear...

1. Dot product calculation looks like this:
 float dp = dot(normalize(EyePos),normalize(Normal)); 

2. Compute value for 'gradient' (from post #4, 0 degrees - black and 30 degrees - transparent):
 float cMaxDegreesOfFacing = cos(30 * Pi / 180); // which equals to (0.866025...) <<<! 

0.866025..... was returned from calculation I made in 'Microsoft Calculator' (calc.exe) -> cos of 30 degrees. I don't think that was incorrect.

 float shade = max((dp - cMaxDegreesOfFacing) / (1.0 - cMaxDegreesOfFacing),0.0); 

That's all what I meaned...

Best wishes, FXACE.

##### Share on other sites
I see, now i get it

It works excellent. Thanks alot for your help!

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633736
• Total Posts
3013601
×