Translucency

hi everyone,



i have a question.

I load a model from a obj+mtl file using assertManager.loadModel(…). Now how can do translucency or apply a tintcolor to the Node/Spatial?



thank you!

I believe this is the tutorial you’re looking for:

https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:beginner:hello_material



[java]/** A cube with base color “leaking” through a partially transparent texture */

Box boxshape4 = new Box(new Vector3f(3f,-1f,0f), 1f,1f,1f);

Geometry cube_leak = new Geometry(“Leak-through color cube”, boxshape4);

Material mat_tl = new Material(assetManager, “Common/MatDefs/Misc/ColoredTextured.j3md”);

mat_tl.setTexture(“m_ColorMap”, assetManager.loadTexture(“Textures/ColoredTex/Monkey.png”));

mat_tl.setColor(“m_Color”, new ColorRGBA(1f,0f,1f, 1f)); // purple

cube_leak.setMaterial(mat_tl);

rootNode.attachChild(cube_leak);

[/java]

thank you for your reply.



I load a model and materials from an obj/mtl file …

(wiimote.obj; wiimote.mtl; wiimote.jpg)

example:



Spatial wiimote = assetManager.loadModel(“Models/wiimote2/wiimote.obj”);



if I now set

wiimote.setMaterial(…) … the metarials of the wiimote including it’s texture is replaced.

What I want is to add transluency or a tint color to the existing model/material? without loosing it’s material definition from the mtl file.

Your texture needs an alpha channel where the material color can leak through.

Change the texture (in an image editor) to a .png (or format that allows alpha), then either change its opacity all together, or remove the data where you want it to be transparent.

Then use the material in the tutorial, but instead of loading “Monkey.png”, load “wiimote.png”

I want to load user-supplied models during runtime … so I may no have the ability to change the textures in advance… Is there a possibility to change the loaded materials/textures during runtime, so I can add an alpha channel and let the users decide the a slider what the alpha factor is.



Thank you!

You can do that, but it requires some knowledge about shaders.

You can define a float m_Alpha value in a shader, then mix() the alpha value of the texture with your alpha value.

m_Alpha can then be set from your program.

For reference, see how the Lighting.j3md shader handles an alpha map.

Actually you don’t need to do that, there’s a built-in way to do it.



Once you load the model, you need to go through each geometry and modify its material to change the color/alpha:



[java]Geometry geom = …

Material material = geom.getMaterial();[/java]



Then set the m_UseAlpha parameter to true, this will use the alpha from the diffuse color as the model’s alpha. Set the m_Diffuse parameter to modify both the color and alpha of the model:



[java]material.setBoolean(“m_UseAlpha”, true); // use alpha from diffuse color

material.setColor(“m_Diffuse”, … ); // specify diffuse color[/java]



You also need to enable alpha blending and put the model in the appropriate render queue to indicate it is transparent:

[java]

material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); // enables alpha blending mode

geom.setQueueBucket(Bucket.Transparent); // enables back-to-front sorting

[/java]