iOS で SceneKit を使用していくつかのコードを開発しています。私のコードでは、z が 0.0 で x と y がタップ ジェスチャから決定されるグローバル z 平面上の x と y 座標を決定したいと考えています。私のセットアップは次のとおりです。
override func viewDidLoad() {
super.viewDidLoad()
// create a new scene
let scene = SCNScene()
// create and add a camera to the scene
let cameraNode = SCNNode()
let camera = SCNCamera()
cameraNode.camera = camera
scene.rootNode.addChildNode(cameraNode)
// place the camera
cameraNode.position = SCNVector3(x: 0, y: 0, z: 15)
// create and add an ambient light to the scene
let ambientLightNode = SCNNode()
ambientLightNode.light = SCNLight()
ambientLightNode.light.type = SCNLightTypeAmbient
ambientLightNode.light.color = UIColor.darkGrayColor()
scene.rootNode.addChildNode(ambientLightNode)
let triangleNode = SCNNode()
triangleNode.geometry = defineTriangle();
scene.rootNode.addChildNode(triangleNode)
// retrieve the SCNView
let scnView = self.view as SCNView
// set the scene to the view
scnView.scene = scene
// configure the view
scnView.backgroundColor = UIColor.blackColor()
// add a tap gesture recognizer
let tapGesture = UITapGestureRecognizer(target: self, action: "handleTap:")
let gestureRecognizers = NSMutableArray()
gestureRecognizers.addObject(tapGesture)
scnView.gestureRecognizers = gestureRecognizers
}
func handleTap(gestureRecognize: UIGestureRecognizer) {
// retrieve the SCNView
let scnView = self.view as SCNView
// check what nodes are tapped
let p = gestureRecognize.locationInView(scnView)
// get the camera
var camera = scnView.pointOfView.camera
// screenZ is percentage between z near and far
var screenZ = Float((15.0 - camera.zNear) / (camera.zFar - camera.zNear))
var scenePoint = scnView.unprojectPoint(SCNVector3Make(Float(p.x), Float(p.y), screenZ))
println("tapPoint: (\(p.x), \(p.y)) scenePoint: (\(scenePoint.x), \(scenePoint.y), \(scenePoint.z))")
}
func defineTriangle() -> SCNGeometry {
// Vertices
var vertices:[SCNVector3] = [
SCNVector3Make(-2.0, -2.0, 0.0),
SCNVector3Make(2.0, -2.0, 0.0),
SCNVector3Make(0.0, 2.0, 0.0)
]
let vertexData = NSData(bytes: vertices, length: vertices.count * sizeof(SCNVector3))
var vertexSource = SCNGeometrySource(data: vertexData,
semantic: SCNGeometrySourceSemanticVertex,
vectorCount: vertices.count,
floatComponents: true,
componentsPerVector: 3,
bytesPerComponent: sizeof(Float),
dataOffset: 0,
dataStride: sizeof(SCNVector3))
// Normals
var normals:[SCNVector3] = [
SCNVector3Make(0.0, 0.0, 1.0),
SCNVector3Make(0.0, 0.0, 1.0),
SCNVector3Make(0.0, 0.0, 1.0)
]
let normalData = NSData(bytes: normals, length: normals.count * sizeof(SCNVector3))
var normalSource = SCNGeometrySource(data: normalData,
semantic: SCNGeometrySourceSemanticNormal,
vectorCount: normals.count,
floatComponents: true,
componentsPerVector: 3,
bytesPerComponent: sizeof(Float),
dataOffset: 0,
dataStride: sizeof(SCNVector3))
// Indexes
var indices:[CInt] = [0, 1, 2]
var indexData = NSData(bytes: indices, length: sizeof(CInt) * indices.count)
var indexElement = SCNGeometryElement(
data: indexData,
primitiveType: .Triangles,
primitiveCount: 1,
bytesPerIndex: sizeof(CInt)
)
var geo = SCNGeometry(sources: [vertexSource, normalSource], elements: [indexElement])
// material
var material = SCNMaterial()
material.diffuse.contents = UIColor.redColor()
material.doubleSided = true
material.shininess = 1.0;
geo.materials = [material];
return geo
}
ご覧のように。高さ 4 単位、幅 4 単位の三角形があり、x、y (0.0, 0.0) を中心とする z 平面 (z = 0) に設定されています。カメラはデフォルトの SCNCamera で、負の z 方向を向いており、(0, 0, 15) に配置しました。zNear と zFar のデフォルト値は、それぞれ 1.0 と 100.0 です。私の handleTap メソッドでは、タップの x と y の画面座標を取得し、z = 0.0 の x と y のグローバル シーン座標を見つけようとします。unprojectPoint への呼び出しを使用しています。
unprojectPoint のドキュメントは、
Z 座標が 0.0 の点を非投影にすると、ニア クリッピング プレーン上の点が返されます。z 座標が 1.0 の点を投影解除すると、ファー クリッピング プレーン上の点が返されます。
間にある点については、近距離平面と遠距離平面の間に直線的な関係があるとは特に言いませんが、私はその仮定を立てて、screenZ の値を、近距離平面と遠距離平面の間のパーセント距離として計算しました。 0 面が配置されています。答えを確認するには、三角形の角の近くをクリックします。これは、それらがグローバル座標のどこにあるかを知っているからです。
私の問題は、カメラの zNear および zFar クリッピング プレーンを変更し始めると、正しい値が得られず、一貫した値が得られないことです。だから私の質問は、どうすればこれを行うことができますか? 最後に、新しいジオメトリを作成し、ユーザーがクリックした場所に対応する z 平面に配置します。
よろしくお願いします。