Getting texture pixel color

Hi JME Team,

i have a problem while getting a pixel color out of an intersected/hitted point on a triangle in a textured mesh. So here is the question: How can i get it? Do you have any hint on how can i simply get it, perhaps with the jme 3 api. If not, is there a simple calculation for it, i’ve tried thinking about rotation matrix and quaternion calculation on the mesh, but i don’t have any idea how to do it.

I would appreciate any suggestion to solve my problem.

I guess you would have to get the indice of the vertices of the hit triangle (you can get that from collision result).

Get the corresponding texture coordinates from the mesh for those indices.

Then interpolate the popper texture coordinates from the hit point.

And in the end, fetch the texel from the texture.

ACtaully thats where I got stuck trying the same thing, how can I do the interpolation?

I get:

My hit point:

(0.0, 1.0, 0.0)

The triangles:

(-1.0, 1.0, 1.0)

(1.0, 1.0, 1.0)

(1.0, 1.0, -1.0)

Now I have to calculate (how to?) the scalars to get my hitpoint

(Manually I know how to calculate this, but how to programm ?)

result would be (0.5, 0.0, 0.5)

Then with the texutre coordinates:

(0.0, 1.0)

(0.0, 0.0)

(1.0, 0.0)

→ I get the texture coordinate (0.5, 0.5) (wich is correct its the middle of a centerd box)


package online.newhorizons.shared.vegetation;

import java.nio.ByteBuffer;

import java.nio.FloatBuffer;

import java.util.List;

import com.jme3.collision.CollisionResult;

import com.jme3.collision.CollisionResults;

import com.jme3.material.MatParamTexture;

import com.jme3.math.ColorRGBA;

import com.jme3.math.Ray;

import com.jme3.math.Vector2f;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.Node;

import com.jme3.scene.VertexBuffer;

import com.jme3.scene.VertexBuffer.Format;

import com.jme3.scene.VertexBuffer.Type;

import com.jme3.scene.mesh.IndexBuffer;

import com.jme3.texture.Image;

import com.jme3.texture.Texture;

import com.jme3.util.BufferUtils;

public class MeshUtils {

public static PointInfo getColor(final float x, final float z, final Node terrain) {

final Ray ray = new Ray(new Vector3f(x, 3000, z), new Vector3f(0, -1, 0));

final CollisionResults results = new CollisionResults();

terrain.collideWith(ray, results);

if(results.size() > 0){

final PointInfo rvalue = new PointInfo();

final CollisionResult closest = results.getClosestCollision();

rvalue.hitnormal = closest.getContactNormal();

rvalue.position = closest.getContactPoint();

final int triindex = closest.getTriangleIndex();

final Geometry geom = closest.getGeometry();

System.out.println("Hit " + geom.getName());

final MatParamTexture texture = geom.getMaterial().getTextureParam(“ColorMap”);

final VertexBuffer textureBuffer = geom.getMesh().getBuffer(Type.TexCoord);

final VertexBuffer positionBuffer = geom.getMesh().getBuffer(Type.Position);

final IndexBuffer ib = geom.getMesh().getIndicesAsList();

if (textureBuffer != null && textureBuffer.getFormat() == Format.Float && textureBuffer.getNumComponents() == 2) {

final FloatBuffer textureFloatBuffer = (FloatBuffer) textureBuffer.getData();

final FloatBuffer positionFloatBuffer = (FloatBuffer) positionBuffer.getData();

// aquire triangle’s vertex indices

final int vertIndex = triindex * 3;

final int vertindex1 = ib.get(vertIndex);

final int vertindex2 = ib.get(vertIndex + 1);

final int vertindex3 = ib.get(vertIndex + 2);

// Position


final Vector3f px1 = new Vector3f();

final Vector3f px2 = new Vector3f();

final Vector3f px3 = new Vector3f();

BufferUtils.populateFromBuffer(px1, positionFloatBuffer, vertindex1);

BufferUtils.populateFromBuffer(px2, positionFloatBuffer, vertindex2);

BufferUtils.populateFromBuffer(px3, positionFloatBuffer, vertindex3);






// Texture coordinats


final Vector2f tx1 = new Vector2f();

final Vector2f tx2 = new Vector2f();

final Vector2f tx3 = new Vector2f();

BufferUtils.populateFromBuffer(tx1, textureFloatBuffer, vertindex1);

BufferUtils.populateFromBuffer(tx2, textureFloatBuffer, vertindex2);

BufferUtils.populateFromBuffer(tx3, textureFloatBuffer, vertindex3);




while (tx1.x > 1) {



while (tx1.y > 1) {



final Texture tex = texture.getTextureValue();

final Image img = tex.getImage();

Magic interpolation here!

System.out.println(tpx + “,” + tpy);

assert img.getFormat().equals(Image.Format.ABGR8) : "Invalid format " + img.getFormat();

final List<ByteBuffer> imgd = img.getData();


final ByteBuffer imgbytebuffer = imgd.get(0);

final int start = (tpx + tpy * img.getWidth()) * 4;





final float alpha = imgbytebuffer.get() / 255f;

final float blue = imgbytebuffer.get() / 255f;

final float green = imgbytebuffer.get() / 255f;

final float red = imgbytebuffer.get() / 255f;

rvalue.color = new ColorRGBA(red, green, blue, alpha);


} else {

throw new UnsupportedOperationException(“Position buffer not set or " + " has incompatible format”);


return rvalue;


return null;