Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

transparency - Transparent textures behaviour in WebGL

Environment: WebGL, Chrome. I have the following behavior when using transparent png's as textures for models:

  1. Image A - the tree hides the building behind it and I see the world box texture. It also hides itself (back branches are not visible)
  2. At the same time - Image B - works properly, the window is transparent and I see what's behind

A: Tree over house B: Window transparency

Both screenshots were made on same the scene at the same time from different camera positions. Textures are produced by the same algorithm.

I can't understand what's the difference between window and branches transparency. My main question is - how to fix the branches so as to not hide the objects behind them? Shader code is:

gl_FragColor = vec4(textureColor.rgb * vLightWeighting, textureColor.a);

I played with enable/disable blending and depth_test, sometimes getting the desired results, but not sure if it's the proper way to do things.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're running into depth buffer issues, it has nothing to do with your shader or blend modes.

What's happening is that the order that you render your transparent geometry in is affecting your ability to render behind it. This is because the depth buffer has no concept of transparent or non-transparent. As a result, even though they don't visually contribute to the scene those transparent pixels write themselves into the depth buffer anyway, and after that any pixels that you draw behind them will be discard because they're "not visible". If you drew the geometry behind the transparent object first, though, it would show correctly because it gets written into the frame before the transparent depth is put in place to discard it.

This is something that even large commercial game engines still struggle with to some degree, so don't feel bad about it causing some confusion. :)

There's no "perfect solution" to this problem, but what it really boils down to is trying to structure your scene like so:

  1. Render any opaque geometry sorted by state (shader/texture/etc)
  2. Render any transparent geometry next. If possible sort these by depth, so that you draw the furthest one from the camera first.

Simply by flagging the bits of geometry that are transparent and rendering them after everything else you'll solve 90% of this problem, but the issue may still remain for overlapping transparent objects. That may not be an issue for you, depending on your scene, but if it's still causing artifacts you'll need to sort transparent objects by depth before you draw.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...