Jump to content
  • Advertisement
Sign in to follow this  
winsrp

increase transparency of an object as you get closer

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

Simple question.

I have a 3rd person camera and when i scroll the mouse wheel the camera gets closer to the player, but I want to add some transparency to the player as I get closer, so when I'm very close I can still see the back ground and not the back of the player.

the player is made from a non model character (made in code), and I would like to not have to modify the vertexbuffer to do so, I was hopping to be able to do this in a shader, or a blendstate. please advice the best way to do it.

regards,

Winsrp.

Share this post


Link to post
Share on other sites
Advertisement
in your main app, set the maximum distance (the distance that the player is not transparent), and get the distance between the camera and your player:

maxDistance = 5;
distance = camPos - playerPos;

Now get the alpha value, between 0 and 1. the closer you are to the model, the closer to 0 it will be, the closer you are to max distance, the closer to 1 it will be (0 is transparent, 1 is opaque)

if(distance >= 5)
alpha = 1;
elseif(distance <= 0)
alpha = 0;
else
alpha = distance / maxDistance;

If you don't want the player to be completely transparent when you get too close, you can check to see if the alpha value is below a certain level, and if it is, just put it back up to that level:

if(alpha < 0.25)
alpha = 0.25;

Now you put the alpha value (and maybe a boolean that is true if your rendering your player, and false if your not, otherwise you will need two pixel shaders or something) in a constant buffer and send it to your shaders. You can easily look up how to do this if you don't know how. So now, in your pixel shader, you get the alpha value from the constant buffer, and set the alpha value of your players pixels to that value:

if(isPlayer)
return float4(playerColor.x, PlayerColor.y, PlayerColor.z, alpha);

Share this post


Link to post
Share on other sites
remember that transparent objects need to be rendered LAST, otherwise there will be nothing for them to "blend" with if they are the first objects rendered

Share this post


Link to post
Share on other sites
hmm.. and if I have water that is transparent, and the player that can be transparent, I can imagine I might have some issues there.

Share this post


Link to post
Share on other sites

hmm.. and if I have water that is transparent, and the player that can be transparent, I can imagine I might have some issues there.


Correct. You may do coarse scale depth sorting for the objects in order to minimize blending issues.

Share this post


Link to post
Share on other sites

hmm.. and if I have water that is transparent, and the player that can be transparent, I can imagine I might have some issues there.


as long as the player does not enter the water, i guess its safe to render water always before the player

Share this post


Link to post
Share on other sites
so, just for the sake of learning, how would you (is there even a legit technique) go about a transparent object HALFWAY in the water? If the player is below the water level and the camera above (opposite sides of the water level), just draw the player first, and if he's above and the camera is above (both on the same side), just draw the water first. I suppose theoretically if they were both 50% transparent, it wouldn't matter which one is drawn first. and now that i'm thinking about it, if the player is touching the water, just change his transparency to be the opposite of the water, then it won't matter which one is drawn first. I've always thought transparency sorting was troublesome, so i'm probably wrong, but just a thought

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!