在增强现实中计算与墙尺寸对应的图像尺寸并平滑拖动
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在增强现实中计算与墙尺寸对应的图像尺寸并平滑拖动相关的知识,希望对你有一定的参考价值。
我正在创建一个应用程序,从库中选择图像并使用ARKit垂直平面检测将其放置在墙上。我也能够正确地缩放它,但拖动不是预期的。另外,我也对图像大小感到困惑。我想它就像假设墙壁是10英尺而我想要放置3 * 2英尺的图像。我引用了很多链接和docs.Below是渲染和添加地点图像的代码:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for
anchor: ARAnchor)
if nodeWeCanChange == nil
guard let planeAnchor = anchor as? ARPlaneAnchor else return
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
nodeWeCanChange = SCNNode(geometry: SCNPlane(width: width,
height: height))
nodeWeCanChange?.position = SCNVector3(planeAnchor.center.x, 0,
planeAnchor.center.z)
nodeWeCanChange?.transform = SCNMatrix4MakeRotation(-Float.pi / 2.0, 1.0, 0.0, 0.0)
nodeWeCanChange?.geometry?.firstMaterial?.diffuse.contents = UIColor.white
DispatchQueue.main.async(execute:
node.addChildNode(self.nodeWeCanChange!)
self.zIndex = (self.nodeWeCanChange?.simdPosition.z)!)
func placeImage(image : UIImage)
guard let pointsPerInch = UIScreen.pointsPerInch else
return
let pixelWidth = (image.size.width) * (image.scale)
let pixelHeight = (image.size.height) * (image.scale)
let inchWidth = pixelWidth/pointsPerInch
let inchHeight = pixelHeight/pointsPerInch
let widthInMetres = (inchWidth * 2.54) / 100
let heightInMeters = (inchHeight * 2.54) / 100
let photo = SCNPlane(width: (widthInMetres), height: (heightInMeters))
nodeWeCanChange?.geometry = photo
nodeWeCanChange?.geometry?.firstMaterial?.diffuse.contents = image`
@objc func handleTap(_ gestureRecognize: ThresholdPanGesture)
// Function that handles drag
let position = gestureRecognize.location(in: augentedRealityView)
var foundNode:SCNNode? = nil
do
if gestureRecognize.state == .began
print("Pan state began")
//let hitTestOptions = [SCNHitTestOption: Any]()
let hitResult: [SCNHitTestResult] = (self.augentedRealityView?.hitTest(gestureRecognize.location(in: self.augentedRealityView), options: nil))!
guard let firstNode = hitResult.first else
return
print("first node =\(firstNode.node)")
if firstNode.node.isEqual(nodeWeCanChange)
foundNode = nodeWeCanChange
print("node found")
else if (firstNode.node.parent?.isEqual(nodeWeCanChange))! print("node found")
else return print("node not found")
latestTranslatePos = position
if gestureRecognize.state == .changed
let deltaX = Float(position.x - latestTranslatePos.x)/700
let deltaY = Float(position.y - latestTranslatePos.y)/700
nodeWeCanChange?.localTranslate(by: SCNVector3Make(deltaX, -deltaY , zIndex))
latestTranslatePos = position
print("Pan State Changed")
if gestureRecognize.state == .ended
print("Done moving object")
let deltaX = Float(position.x - latestTranslatePos.x)/700
let deltaY = Float(position.y - latestTranslatePos.y)/700
nodeWeCanChange?.localTranslate(by: SCNVector3Make(deltaX, -deltaY , zIndex))
latestTranslatePos = position
foundNode = nil
`
任何人都可以帮助我,我错过了什么?
答案
这里有两个问题...... ^ __________ ^。
关于规划你的qazxsw poi,最简单的方法是使用qazxsw poi,尽管你可以很容易地修改下面的例子。
假设你的内容被放置在SCNNode
上并且你想在飞机上移动它然后你可以做这样的事情(假设你也选择了touches
作为你的ARPlaneAnchor
)在我的例子中将是图片:
SCNNode
或者,您可以尝试以下方法:
nodeToDrag
关于你的尺寸,你不清楚你想要做什么。但是这里有一个小例子,评论应该有所帮助:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
//1. Get The Current Touch Point
guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
//2. Get The Existing ARPlaneAnchor
let hitTest = augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else return
//3. Convert To World Coordinates
let worldTransform = hitTest.worldTransform
//4. Set The New Position
let newPosition = SCNVector3(worldTransform.columns.3.x, worldTransform.columns.3.y, worldTransform.columns.3.z)
//5. Apply To The Node
nodeToDrag.simdPosition = float3(newPosition.x, newPosition.y, newPosition.z)
您还可以创建一个辅助函数,例如:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?)
//1. Get The Current Touch Point
guard let currentTouchPoint = touches.first?.location(in: self.augmentedRealityView),
//2. Get The Existing ARPlaneAnchor
let hitTest = augmentedRealityView.hitTest(currentTouchPoint, types: .existingPlane).first else return
//3. Convert To Local Coordinates
nodeToDrag.simdPosition.x = Float(hitTest.localTransform.columns.3.x)
nodeToDrag.simdPosition.y = Float(-hitTest.localTransform.columns.3.z)
您可以根据锚点的范围来缩放图片,例如:
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor)
//1. Check We Have An ARPlaneAnchor
guard let planeAnchor = anchor as? ARPlaneAnchor else return
//2. Get The Approximate Width & Height Of The Anchor
let width = CGFloat(planeAnchor.extent.x)
let height = CGFloat(planeAnchor.extent.z)
//3. Create A Dummy Wall To Illustrate The Vertical Plane
let dummyWall = SCNNode(geometry: SCNPlane(width: width, height: height))
dummyWall.geometry?.firstMaterial?.diffuse.contents = UIColor.black
dummyWall.opacity = 0.5
dummyWall.eulerAngles.x = -.pi/2
node.addChildNode(dummyWall)
//4. Create A Picture Which Will Be Half The Size Of The Wall
let picture = SCNNode(geometry: SCNPlane(width: width/2, height: height/2))
picture.geometry?.firstMaterial?.diffuse.contents = UIColor.white
//5. Add It To The Dummy Wall (As We Dont Assign A Position It Will Be Centered Automatically At SCNVector3Zero)
dummyWall.addChildNode(picture)
//6. To Prevent Z-Fighting Move The Picture Forward Slightly
picture.position.z = 0.0001
更新:
既然你无法让这个例子起作用,并且现在对你的要求更加具体,那么这里是一个完全有效的例子(需要稍加调整),但它应该足以让你指向正确的方向:
//--------------------------
//MARK: - CGFloat Extensions
//--------------------------
extension CGFloat
/// Desired Scale Factor
enum Scalar
case Quarter
case Third
case Half
case ThreeQuarters
case FullSize
var value: CGFloat
switch self
case .Quarter: return 0.25
case .Third: return 0.33
case .Half: return 0.5
case .ThreeQuarters: return 0.75
case .FullSize: return 1
/// Returns A CGFloat Based On The Desired Scale
///
/// - Parameter size: Scalar
/// - Returns: CGFloat
func scaledToSize(_ size: Scalar) -> CGFloat
return self * size.value
希望能帮助到你...
以上是关于在增强现实中计算与墙尺寸对应的图像尺寸并平滑拖动的主要内容,如果未能解决你的问题,请参考以下文章