Initial conditions: there is a 3d scene consisting of many elements. All elements of the scene are packed in Model3dGroup containers, and stored in resources. Viewport3D is used as a container for the scene, in which a camera and lighting are added to the scene received from resources.

The question is: how to recognize which element of the scene is happening, for example, a mouse click? PS How to catch mouse events for the whole scene is clear. I would like to do this using standard WPF tools.

    2 answers 2

    Unfortunately, you have to manually calculate the place where the click happened. You will have to “draw a line” perpendicular to the plane of the camera through the point where the mouse clicked, and then find everyone who crosses this line. After that, the found should be sorted by distance to the intersection point of your line and the plane of the camera. Do not forget to also weed out the objects "behind the back." Dig in the direction of gamedev-articles. On the move found these:

    http://www.gamedev.ru/code/forum/?id=50092 http://www.gamedev.ru/code/forum/?id=86652 http://www.gamedev.ru/code/forum/ ? id = 17370

    • Well, with respect to objects on the drawn line, everything is somewhat simpler: it is in this scene that all objects are located in the same plane, perpendicular to which the camera is exposed (as if to a frontal view). MB in this regard, there are easier ways? - EVGEN
    • I still have to project. This assumption will simplify the formulas :) By the way, they depend on what kind of projection you have. I am not an expert on DirectX, but OpenGL has several standard options (orthogonal, perspective ...) and depending on this, the difference in calculations can return. I think in DX there is a similar danger too. - cy6erGn0m
    • I am not a DX expert either, but once I wrote a couple of programs on it as needed. There are special functions that take the coordinate of the mouse, they themselves conduct the beam as they should and return the object in which the beam is buried. Well it is, some memories ... - kirelagin
    • In what form are they interesting to return an object? How to recognize it, if all that is, is for the most part arrays of vertices and their properties? - cy6erGn0m
    • Most functions of this type are based on finding the intersection of a ray (straight line) and a triangle. And give the barycentric coordinates of the intersection point if it is. A programmer will have to take care of himself to understand to which object this triangle belongs. But it is not difficult, because in any case, it is first advisable to cut parts of the scene along envelope volumes (OOB / AAB / sphere), the triangles of those objects that went through the cut-off are sorted sequentially and sorted (discarded) by distance. Therefore, it is always known in which object we found the intersection. - outcast

    Viewport3dVisual2d allows you to place controls on the 3d scene. You can bind event handlers to controls.