将SCNNode或UIView添加到SCNView的中心以检测其他SCNNode

时间:2019-06-14 13:21:32

标签: swift scenekit arkit

我试图将UIView居中于SCNView的中心,以便检测场景中其他添加的SCNTorus节点。 enter image description here

我将视图添加到了我的sceneView的中心,如下所示

package com.example.databasedemo;

import java.io.IOException;
import java.io.PrintWriter;

import com.fazecast.jSerialComm.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.fazecast.jSerialComm.SerialPort.getCommPort;
import static com.fazecast.jSerialComm.SerialPort.getCommPorts;

@RestController
public class arduino {
    static SerialPort chosenPort;
    @RequestMapping(value="/get")
   public String getdata() throws IOException {

       com.fazecast.jSerialComm.SerialPort[] portNames =getCommPorts();
       chosenPort =getCommPort(portNames[1].toString());
       chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);

       if(chosenPort.openPort()) {




                   // enter an infinite loop that sends text to the arduino
                   PrintWriter output = new PrintWriter(chosenPort.getOutputStream());

                       output.print("first try");
                       output.flush();
return "send ";

       }
    else {
        // disconnect from the serial port
        chosenPort.closePort();
        return "disconnect";

    }

    }


}

然后我尝试了两种方法:

1-

void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
  Serial.setTimeout(50);
}

void loop() {
  // put your main code here, to run repeatedly:
 String text = Serial.readString();
 Serial.println("name= "+text);
 delay(1000);
}

2-

var focusPoint: CGPoint {
    return CGPoint(
        x: sceneView.bounds.size.width / 2,
        y: sceneView.bounds.size.height - (sceneView.bounds.size.height / 1.618))
}

这两种方法返回错误的结果,距离很大,x和y也很大。

1 个答案:

答案 0 :(得分:0)

最后我得到了解决方案!

原来,我应该使用convertPosition(SCNVector3,to:)

方法将位置转换为场景的世界坐标空间

这里是完整的代码:

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {

        DispatchQueue.main.async { [weak self] in

            guard let strongSelf = self else { return }

            if !strongSelf.inEditMode { return }

            for node in strongSelf.selectionRingsNodes {

                let position = node.convertPosition(SCNVector3Zero, to: nil)
                let projectedPoint = renderer.projectPoint(position)
                let projectedCGPoint = CGPoint(x: CGFloat(projectedPoint.x), y: CGFloat(projectedPoint.y))
                let distance = projectedCGPoint.distance(to: strongSelf.focusPoint)
                if distance < 50 {
                    strongSelf.showToast(message: node.getTopMostParentNode().name!, font: .systemFont(ofSize: 30))
                }
            }
        }
    }