简体   繁体   English

Libgdx 3D 纹理透明度

[英]Libgdx 3D Texture Transparency

I'm having a problem with a texture which has transparent areas.我在处理具有透明区域的纹理时遇到问题。 Basically it is a texture of a coin rendered onto a cube.基本上它是渲染到立方体上的硬币纹理。 I can't get the corners to appear transparent, they just show up as white/gray.我无法让角落看起来透明,它们只是显示为白色/灰色。

I made sure to enable G20.GL_Blend , so that can't be it.我确保启用G20.GL_Blend ,所以不可能。

在此处输入图片说明

This is the code I use in the render() method (I tried different combinations):这是我在render()方法中使用的代码(我尝试了不同的组合):

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
    Gdx.gl20.glEnable(GL20.GL_BLEND);
    Gdx.gl20.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
    Gdx.gl20.glEnable(GL20.GL_TEXTURE_2D);
    Gdx.gl20.glBlendEquation(GL20.GL_BLEND);
    
    modelBatch.begin(cam);
    texture.bind();
    modelBatch.render(instance, environment);
    modelBatch.end();
        
    Gdx.gl20.glDisable(GL20.GL_TEXTURE_2D);

Add a blending attribute to the model:向模型添加混合属性:

http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=10016&p=45952#p45369 http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=10016&p=45952#p45369

Edit.- Link dead.编辑.- 链接已死。 It is like this:它是这样的:

modelInstance.materials.get(0).set(
    new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)
)

You need to add a BlendingAttribute to your ModelInstance :您需要将BlendingAttribute添加到您的ModelInstance

modelInstance.materials.get(0).set(
    new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA)
)

You can also add it in your ModelBuilder code (where you create a Model which can create ModelInstances):您还可以将它添加到您的模型构建器代码中(您可以在其中创建一个可以创建模型实例的模型):

new Material(
    TextureAttribute.createDiffuse(yourTransparentTexture), 
    new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA))
)

ModelBatch.begin(...) will disable Blending by calling Gdx.gl.glDisable(GL20.GL_BLEND). ModelBatch.begin(...) 将通过调用 Gdx.gl.glDisable(GL20.GL_BLEND) 禁用混合。 So make sure to enable blending AFTER calling ModelBatch.begin().因此,请确保在调用 ModelBatch.begin() 后启用混合。

modelBatch.begin(camera); // resets Blending to false
// enable Blending
Gdx.gl.glEnable(GL20.GL_BLEND);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
// draw mesh
mesh.render(shader, GL20.GL_TRIANGLES);
modelBatch.end();

Source: https://stackoverflow.com/a/66820414/2413469来源: https : //stackoverflow.com/a/66820414/2413469

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM