SCNGeometry
问题是,如果我使用此构造函数创建,我将无法渲染任何内容init(sources: [AnyObject], elements: [AnyObject]?)
这是一个示例(这应该绘制一个立方体,取自本教程):
func test()
{
var cubeSide:Float = 15.0;
var halfSide:Float = cubeSide/2;
var positions:[SCNVector3] = [
SCNVector3Make(-halfSide, -halfSide, halfSide),
SCNVector3Make( halfSide, -halfSide, halfSide),
SCNVector3Make(-halfSide, -halfSide, -halfSide),
SCNVector3Make( halfSide, -halfSide, -halfSide),
SCNVector3Make(-halfSide, halfSide, halfSide),
SCNVector3Make( halfSide, halfSide, halfSide),
SCNVector3Make(-halfSide, halfSide, -halfSide),
SCNVector3Make( halfSide, halfSide, -halfSide)
];
var indices:[Int] = [
0, 2, 1,
1, 2, 3,
// back
2, 6, 3,
3, 6, 7,
// left
0, 4, 2,
2, 4, 6,
// right
1, 3, 5,
3, 7, 5,
// front
0, 1, 4,
1, 5, 4,
// top
4, 5, 6,
5, 7, 6
];
var geometrySource = SCNGeometrySource(vertices: positions, count: 8)
var data = NSData(bytes: indices, length: sizeof(Int) * indices.count)
var element = SCNGeometryElement(data: data, primitiveType: SCNGeometryPrimitiveType.Triangles, primitiveCount: 12, bytesPerIndex: sizeof(Int))
var geometry = SCNGeometry(sources: [geometrySource], elements: [element])
geometry.materials?.first?.diffuse.contents = UIColor.redColor()
var node = SCNNode(geometry: geometry)
/*var cameraNode:SCNNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position = SCNVector3Make(0, 12, 30);
self.rootNode.addChildNode(cameraNode);*/
self.rootNode.addChildNode(node)
}
如果我从已知图形创建几何图形,一切都很好。代码示例:
func showCone()
{
var radius:CGFloat = 1.0
let cone = SCNCone(topRadius: 0.1, bottomRadius: 3, height: 2)
cone.firstMaterial?.diffuse.contents = UIColor.redColor()
let coneNode = SCNNode(geometry: cone)
coneNode.position = SCNVector3(x: 4.0, y: 3.0, z: 20.0)
self.rootNode.addChildNode(coneNode)
}
我以为问题出在相机节点上,但我做了很多操作,没有任何改变。更改节点position
也没有帮助。此外,如果我创建一个新的 Xcode 项目,它的部署目标是 OS X,它也可以工作。但我需要一个 iOS 解决方案。
我的错误是什么,我做错了什么?
最初我的问题是我无法从解析的.stl
文件中绘制 3d 模型。我认为问题出在解析,但后来我尝试使用原始类型绘制任何东西——三角形及其位置,但它没有用。