Find vertex color at rayCast hit

Greetings everyone!
Is there a way to detect the vertex color of the mesh at the hit point of rayCas()? Thanks!


cheapest cpu cycle way = on frame zero build KD tree

on raycast hit -> convert worldspace(hitpoint) to local space-> use KD tree to find closest vertex

Sample color from vertex.

You and your KD tree…
If you don’t understand the problem, the correct action is not to put ketchup on it.

Not that this problem even requires a tree, KD will give false positives, its that simple.


Doesn’t raycast return a polyproxy?
From which you get 3 to 4 vertices…

problem solved with O(4) instead of O(nlogn).

kdtree does not give false data - (the vertex positions are local to the world transform of the target)

and poly proxy only works on triangle mesh physics bounds.

and KDtree can give the closest element to the hitpoint - one command - no iteration

#Build tree
if 'KD' not in own:
    VLIST =[]
    for mesh in own.meshes:
        for m_index in range(len(mesh.materials)):
            for v_index in range(mesh.getVertexArrayLength(m_index)):
                vertex = mesh.getVertex(m_index, v_index)
    size = len(VLIST)
    kd = mathutils.kdtree.KDTree(size)
    index =0
    for Vert in VLIST:
        kd.insert(VLIST[index][1], index)



you store a kd tree inside the mesh, to be used later if hit by ray**

to localize hitPoint to ray hit object

local = hitPoint - hitObject.worldPosition
local = hitObject.worldOrientation.inverted()*local
location, closestVert, dist = hitObject['KD'].find(local)
color = closestVert.color

KDTree and BVHTree are used by blender in the viewport ALL THE TIME :smiley:

Smart guy, riddle me this, which color would your KD say the ray hit?

Also, “no iterations” :eyebrowlift2::eyebrowlift2::eyebrowlift2:

Finding the nearest point is an [O]( <i>n</i>) operation in the case of randomly distributed points, although analysis in general is tricky. 

You haven’t looked into what your code even does under the hood…

python is not iterating, C is

and I use 1 bound per actor with 1 shape

PolyProxy is indeed the best way to go if your mesh has physics and it’s a triangle mesh

(if not you can BVH tree raycast against objects with no physics)
if you have physics
(use kd tree since your ray can use bge physics)

kdtree-> look up closest point local to actor that was hit**

Unless you use vertex paint, vertex color is (1.0, 1.0, 1.0, 1.0)

Here is a file with 2 scripts: 1 to read pixels color and 1 to read vertex color… I hope I made no mistake:

Note: when you read pixels, if the rayCast hitpoint is not visible, the color returned will be the color you can see at screenspace hitpoint position.

The poly data seems very fine as I use triangle mesh on the specific object. Thanks for info!