[英]SceneKit – Drawing on 3D Object
我創建了在3d對象上繪制的示例應用程序,輸出圖形不平滑(如圖所示)。 我花了將近一天的時間來弄清楚仍然無法解決的問題。 可能是什么問題?
我正在創建如下的自定義繪圖幾何
//I am creating self.drawingNode touches begin and adding to the rootnode
//points are nothing but 2d points from touches
func createGeometryForPoints(points:[CGPoint]) -> SCNGeometry {
var all_3d_points:[SCNVector3] = []
for point in points {
let result = self.get3dPoint(point)
if result.1 == true {
all_3d_points.append(result.0)
} else {
print("INVALID POINT")
}
}
var indices: [Int32] = []
var index:Int32 = 0
var previousIndex:Int32 = -1
for _ in all_3d_points {
if(previousIndex != -1) {
indices.append(previousIndex)
}
indices.append(index)
index = index + 1
previousIndex = index
}
if indices.count > 0 {
indices.removeLast();
}
let indexData = NSData(bytes: indices, length: sizeof(Int32) * indices.count)
let source = SCNGeometrySource.init(vertices: all_3d_points, count: all_3d_points.count)
let element = SCNGeometryElement.init(data: indexData, primitiveType: .Line, primitiveCount: indices.count/2, bytesPerIndex: sizeof(Int32))
let line = SCNGeometry(sources: [source], elements: [element])
line.materials.first?.diffuse.contents = UIColor.redColor()
line.materials.first?.doubleSided = true
return line
}
func get3dPoint(point:CGPoint) -> (SCNVector3,Bool) {
var canAdd = false
let scnView = self.view as! SCNView
var unprojectedStartPoint = SCNVector3.init(x:Float(point.x), y:Float(point.y), z:0)
let hitResults = scnView.hitTest(point, options: nil)
if hitResults.count > 0 {
let result: AnyObject! = hitResults[0]
if hitResults.count > 1 {
print("SOME PROBLEM")
}
unprojectedStartPoint = result.localCoordinates
unprojectedStartPoint = result.node.convertPosition(unprojectedStartPoint, toNode: self.drawingNode!)
canAdd = true
}
return (unprojectedStartPoint,canAdd)
}
編輯
我正在考慮找到偏移以及相機方向或正常,因為我是scenekit的新手,我沒有得到如何將這個偏移添加到繪圖節點,在這個方向尋找幫助
func createMarkup() -> Void { let drawingNode = SCNNode() drawingNode.renderingOrder = 1000 self.parentNode!.addChildNode(drawingNode) let geometry = self.createGeometryForPoints(allPoints,node: drawingNode) drawingNode.geometry = geometry drawingNode.geometry?.materials.first?.doubleSided = true }
編輯:2
通過在命中測試的正常情況下添加次要偏移來解決問題
let offsetToAvoidFlickering: Float = 0.05
unprojectedStartPoint = unprojectedStartPoint + result.localNormal.normalized() * offsetToAvoidFlickering
我懷疑你有深度精度問題; 看起來你的部分線被剪裁了,因為它們是球體的相交部分。 嘗試繪制禁用深度測試的線。
SceneKit狀態的Union演示示例代碼顯示了類似的效果,您可以在圓環上繪制。 它的功能更強大,因為你可以畫任何東西(不僅僅是線,但是從質地等飛濺),並更有效,因為它不依賴於創造新的幾何體。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.