Skip to main content
gamedev.net
🔒 Locked

increase transparency of an object as you get closer

Started by winsrp 7 replies 1.8k views
Original Post
winsrp
winsrp
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.
lomateron
lomateron
check out the tutorial 14 of directx 10 in the directx SDK
iedoc
iedoc
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);
winsrp
winsrp
hmm... interesting, i'll give it a shot and get back with an update.
iedoc
iedoc
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
winsrp
winsrp
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.
kauna
kauna

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.
mancubit
mancubit

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
iedoc
iedoc
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.