简体   繁体   中英

iOS Metal: Enabling user interaction

I am trying to build a simple app using iOS metal which requires user interaction. Upon touching the screen, the focus on the screen shifts to the point touched. Kind of like the Zen Garden app where upon touch, you move closer to the point that was touched. Any thoughts/suggestions/discussion on what methods to use or how to go about designing this would be greatly appreciated.

Thanks!

You could link a UITapGestureRecognizer to the UIView of your app. With it you can get the coordinates of the tap like in the following question:

How to get a CGPoint from a tapped location?

The new coordinates you can hand over to the shader functions with a MTLBuffer.

If you'd like the user to be able to select a 3D object in a scene as Zen Garden does, you can implement a selection buffer.

You assign each selectable object a unique color (or integer ID) and store it in a simple table. You render to a “selection buffer” (actually a renderable texture) and writing the unique colors or integer IDs. You can A) render this selection buffer as another attachment when you render the scene for viewing, or B) you can render the scene again in anothe pass (draw once for viewing the scene and another time to fill the selection buffer).

When you get a touch event, you can use the event 2D coordinate to choose the pixel at that coordinate. You'd read the selection buffer back and check the the color (or integer ID). Since the colors are unique to each object, you can use the color to lookup the object in the table you created.

As far as whether to A) use an attachment for the selection buffer or B) render it in a second pass: A) has the advantage of not needing to draw each object twice since your rendering the selection buffer while rendering the scene for viewing. With B) your selection buffer can be smaller than the buffer used for viewing, making it faster in terms of fill rate. Also with B) it's easier to separate out rendering for viewing from rendering for selection So you only need to only render the selection buffer when you get a touch event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM