简体   繁体   English

iOS Metal:启用用户交互

[英]iOS Metal: Enabling user interaction

I am trying to build a simple app using iOS metal which requires user interaction. 我正在尝试使用需要用户交互的iOS metal构建一个简单的应用程序。 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. 就像Zen Garden应用程序在触摸时一样,您可以更靠近被触摸的点。 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. 您可以将UITapGestureRecognizer链接到应用程序的UIView。 With it you can get the coordinates of the tap like in the following question: 有了它,您可以像下面的问题一样获得水龙头的坐标:

How to get a CGPoint from a tapped location? 如何从点击的位置获取CGPoint?

The new coordinates you can hand over to the shader functions with a MTLBuffer. 您可以使用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. 如果您希望用户能够像Zen Garden一样在场景中选择3D对象,则可以实现选择缓冲区。

You assign each selectable object a unique color (or integer ID) and store it in a simple table. 您为每个可选对象分配唯一的颜色(或整数ID),并将其存储在简单表中。 You render to a “selection buffer” (actually a renderable texture) and writing the unique colors or integer IDs. 您渲染到“选择缓冲区”(实际上是可渲染的纹理)并写入唯一的颜色或整数ID。 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). 您可以A)在渲染要查看的场景时将此选择缓冲区作为另一个附件渲染,或者B)您可以在另一个通道中再次渲染场景(绘制一次以查看场景,另一次填充选择缓冲区)。

When you get a touch event, you can use the event 2D coordinate to choose the pixel at that coordinate. 发生触摸事件时,可以使用事件2D坐标选择该坐标处的像素。 You'd read the selection buffer back and check the the color (or integer ID). 您将读回选择缓冲区并检查颜色(或整数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. 至于A)是对选择缓冲区使用附件还是B)在第二遍渲染它:A)的优点是不需要绘制每个对象两次,因为您在渲染场景以供查看时渲染选择缓冲区。 With B) your selection buffer can be smaller than the buffer used for viewing, making it faster in terms of fill rate. 使用B),您的选择缓冲区可以小于用于查看的缓冲区,从而使其填充速度更快。 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. 同样使用B),更容易将呈现供查看的视图与可供选择的渲染区分开来,因此,仅在发生触摸事件时才需要渲染选择缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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