我正在旋转一个三维模型,代码为:
_mesh = [[NGLMesh alloc] initWithFile:@"01.obj" settings:settings delegate:self];
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
[super touchesMoved:touches withEvent:event];
UITouch *touch;
CGPoint pointA,pointB;
if ([touches count]== 1) {
touch = [[touches allObjects]objectAtIndex:0];
pointA = [touch locationInView:self.view];
pointB = [touch previousLocationInView:self.view];
// _mesh.rotateY -= (pointA.x - pointB.x) * 0.5f;
// _mesh.rotateX -= (pointA.y - pointB.y) * 0.5f;
_mesh.rotateY += (pointA.x - pointB.x) * 0.5f;
_mesh.rotateX += (pointA.y - pointB.y) * 0.5f;
}
}
代码按照预期的方式旋转,但无论我点击视图中的哪个位置,它都会旋转。我希望它只在触摸导入模型内部时旋转。我该怎么做?
为此,您必须手动获取三维模型对象帧(对应于屏幕),并使用touchesMoved
代理中的CGRectContainsPoint
与当前触摸位置进行比较。
只有当结果CGRectContainsPoint
返回YES时,才编写代码_mesh.rotate
。
注意:NineveGL有自己的论坛来讨论与框架相关的事情。请将查询张贴在那里,以获得良好和快速的回复。
以下代码可能在一定程度上帮助您,
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesMoved:touches withEvent:event];
UITouch *touch;
CGPoint pointA,pointB;
if ([touches count]== 1)
{
touch = [[touches allObjects]objectAtIndex:0];
pointA = [touch locationInView:self.view];
pointB = [touch previousLocationInView:self.view];
NGLTouching touchingStruct = [_camera touchingUnderPoint: pointA];
if (touchingStruct.mesh)
{
_mesh.rotateY += (pointA.x - pointB.x) * 0.5f;
_mesh.rotateX += (pointA.y - pointB.y) * 0.5f;
}
}
但是它仍然不会改变网格的边界框。你可以参考这篇博客文章。