Question

i want to draw on the texture in cocos3d with my finger on a iOS device. I have the world coordinates after a touch for the 3d scene, now i want to draw that texture pixel.

Was it helpful?

Solution

You should be able to do that as follows:

Once your CC3MeshNode has been touched, in your implementation of nodeSelected:byTouchEvent:at:, you can create a ray from the touch point:

CC3Ray touchRay = [self.activeCamera unprojectPoint: touchPoint];

Then, convert the ray to the local coordinates of your mesh node:

CC3Ray localRay = [aNode.globalTransformMatrixInverted transformRay: touchRay];

Then, you can use the findFirst:intersections:ofLocalRay:acceptBackFaces:acceptBehindRay: method of your node to find exactly where the ray intersects the mesh. Or, if you don’t need the intersection location in local coordinates, you can use the findFirst:globalIntersections:ofGlobalRay:acceptBackFaces:acceptBehindRay: method instead, and skip the step above that converts the ray to local coordinates (the line above using transformRay:).

Either way, the returned CC3MeshIntersection structures provide a lot of info about the mesh intersection, including the index of the mesh face that was intersected, and the location within that face of the intersection.

Using the face index, you can retrieve the indices of each of the 3 vertices that make up the face, by using the faceIndicesAt: method of your mesh node.

Once you have the indices of the vertices you’re interested in, you can then use vertexTexCoord2FAt: to retrieve the texture coordinates, and setVertexTexCoord2F:at: to modify them, if you want.

Or, you might add color vertex attributes (ie- a color attribute for each vertex, in addition to a texture coordinate, normal, and location), and then use the setVertexColor4F:at: method to assign a color to the vertices that you are "painting" with your finger. That way, the same texture will show through, but will be tinted to a different color on the vertices that have been touched.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top