Unable to get color / opacity information from Geometry

I’d trying to programmatically get the color &/or opacity of a Geometry so that I can alter it & then change it back again without cloning the Geometry or its Material. I can change the values just fine, but all my attempts to read the values have failed. Here’s my material setup:
String matSource = "Common/MatDefs/Misc/Unshaded.j3md"; Material mat = new Material(assetManager, matSource); mat.setTexture("ColorMap", assetManager.loadTexture("Textures/ColoredTex/Monkey.png")); mat.setColor("Color", new ColorRGBA(1,0,1,1)); mat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.Alpha); mat.getAdditionalRenderState().setFaceCullMode(RenderState.FaceCullMode.Off);

Here’s what I’ve tried:
MaterialDef materialDef = material.getMaterialDef(); MatParam materialParam = materialDef.getMaterialParam("Color"); Object obj = materialParam.getValue(); // obj is set to null

and

Mesh mesh = geometry.getMesh(); VertexBuffer buffer = mesh.getBuffer(VertexBuffer.Type.Color); // buffer is set to null

        Material material=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
        ColorRGBA color=ColorRGBA.randomColor();
        material.setColor("Color",color);
        System.out.println(color);
        System.out.println(material.getParam("Color").getValue());
Color[0.2596203, 0.9265085, 0.51936495, 1.0]
Color[0.2596203, 0.9265085, 0.51936495, 1.0]
2 Likes

Depends on the type of opacity you want to get:

The color buffer may be null (and often is - it’s one of the less used and most miss-used buffers today - people store all kinds of things in the color buffer, but not really colors for the vertices).
If the color buffer is not null and really serves as color information, then you get the color associated with each vertex. If you have a triangle soup, then this is equal to the “color of the triangle”. If you have a mesh, e.g. a regular terrain mesh, then this is equal to the “color at this particular vertex”.

MatDefs are not Materials. They are descriptions of the parameters that are allowed for the Material.

If you want to get the “overall color” of a material (hence a color which is applied to all triangles of the Geometry at the same time), then do what zzuegg showed. Note, that this is intended as a multiplication factor when computing the final color of the surface.

@zzuegg Thanks that works as desired.
@Ogli To clarify, did my MaterialDef approach fail because the color was being set outside the material definition?

Regarding the mesh approach, I had (incorrectly) assumed that the various vertex buffers would have been populated by jmonkey when it generated the mesh. At the moment changing the overall opacity works, but what if I did need to read/change the opacity per vertex?

You can add a color buffer yourself and populate it - problem is that you must know which vertex stands for what. But since all shapes (Box, Sphere, etc.) are open source, that is possible to add by creating your own Box2, Sphere2 etc. (or adding the color buffer to a Box) (adding to a Sphere is complicate).

As I wrote - most people don’t use color buffers anymore, because textures are available. Some people have stored, e.g. game physics data in this buffer on mobile games (like “bridge simulator”) and other kinds of data. I know of the “Low Poly” style (modern cubism with low triangle count) which often uses color buffers.
You are pretty much on your own here - but you could use tools like Blender to save vertex colors (if any of the crappy jME asset pipelines allows to import that kind of data).
Note that color buffers are not that important anymore because you can add any kind of vertex buffer today. The mentioned mobile game used color buffers because they are supported on almost all mobile devices (they said - don’t know if it’s true).

You don’t set Colors on MatDefs, but on Mats.
MaterialDefinition is a description (like an XSD file).
Material is a concrete example (like an XML file).

And you might often want to clone Materials, so that you can change colors for some objects but not all, but still re-use most of the Material. (so, many Geometry spatials can share one Material but sometimes it’s not desired).

Also have a look at .j3md files and .j3m files - then things become clear.