Displaying background colour through transparent PNG on material?

纵饮孤独 提交于 2019-11-28 00:10:51

Three.js MeshBasicMaterial does not support what you are trying to do. In MeshBasicMaterial, if the PNG is partially transparent, then the material will be partially transparent.

What you want, is the material to remain opaque, and the material color to show through instead.

You can do this with a custom ShaderMaterial. In fact, it is pretty easy. Here is the fragment shader:

uniform vec3 color;
uniform sampler2D texture;

varying vec2 vUv;

void main() {

    vec4 tColor = texture2D( texture, vUv );

    gl_FragColor = vec4( mix( color, tColor.rgb, tColor.a ), 1.0 );

}

And here is a Fiddle: http://jsfiddle.net/g5btunz9/

In the fiddle, the texture is a circle on a transparent background. You can see the red color of the material show through.

three.js r.72

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!