Click on 3d object


I was able to do this a long time ago with something like:

public void OnPointerMove(Context c)
    Uno.Geometry.Ray CursorRay = c.WorldRay;

    var dist = Uno.Geometry.Distance.RayPoint(CursorRay, PiecePosition);

    //check if distance is close enough to that piece.

I see that the Geometry class is still available with all necessary methods. Only missing piece is the cursor ray.

Is there a way to get that with current API? Or other way?


There is the Node.WindowToWorldRay function which takes a windowCoord (This is what you will get in a pointer event, the WindowPoint argument).

This will use the current viewport, meaning the current Scene in the UI tree, to resolve the coordinates. You can also directly call the Viewport.PointToWorldRay to resolve a point via the Viewport. For many setups this is the exact same as the window and viewport coordinates are the same. Do what is logically correct in your case.

If you implement OnHitTest and HitTestBounds for your node you can also participate in normal hit logic. This allows the standard triggers, like Clicked and Tapped to work on the node.

Perfect, thanks! And thanks for the quick response, you are the best.

I’m unable to register clicks on preview or in iOS (android and DotNetExe works just fine)

<Panel Pointer.Pressed="MyRenderNode.PointerPressed">
      <SolidColor Color="0,0,0,0"/>

I have a custom RenderNode and a panel on top of evertyhing that should register clicks/taps. How do I “implement OnHitTest and HitTestBounds” ?

If you use HitTestMode="LocalBounds" on that panel it should get clicks (though having a background color, even transparent, should have also worked).

I should probably do a simple 3D example of an object that can be clicked. Let me try doing that before attempting to explain those functions.

This example shows a OnHitTest and HitTestBounds function. The demo uses these to implement a WhileHovering and Tapped trigger.

This “works” in the current release except for a couple problems I identified:

  • it doesn’t properly invalidate/redraw on mobile devices. Recall that you added an InvalidateVisual before to a component, just do that here and it’ll work around the problem for now. That is, each frame invalidate the visual to force redraw. My fix is that RenderNode does this automatically.
  • some gestures, like Clicked don’t behave correctly. I’ve fixed this now (a world ray was missing in gesture’s hit test). Tapped works though.

Hmm that doesn’t help me out. I want to call a UNO function when user tap.

Here’s an example project that works a same way then mine does:

You can compile it by opening the project file at Startup/compile/RenderNodeBug.unoproj

When compiling to DotNetExe or Android the PointerPressed works, but on preview or iOS it doesn’t.

How can I call a Uno function when user tap a screen in this case?

Note that for Uno code to work within preview you need to place all the Uno code in separate project and reference it from the main project. Are you doing that? I don’t think “live” update works then either, it’ll have to rebuild the project when you change it.

I’m doing that.

Getting a click or tap to my uno code isn’t working. Please, take a look at the project I posted.

I’ll have to file an issue to figure out why it isn’t working in the simulator. That panel should always get those messages – it has nothing to do with the 3D stuff.