对象不是 rendering/drawing 使用三角形和坐标,SceneKit
Object is not rendering/drawing using triangles and coordinates, SceneKit
问题是,如果我使用此构造函数创建 SCNGeometry
,我将无法渲染任何内容 init(sources: [AnyObject], elements: [AnyObject]?)
这是一个示例(这应该绘制一个立方体,取自此 tutorial ):
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 模型。我以为问题出在解析上,但后来我尝试使用原始类型绘制任何东西 - 三角形及其位置,但它没有用。
SceneKit 不处理 Int
索引,因为它们的值可能太大。您应该在控制台中看到:
SceneKit: error, C3DRendererContextBindMeshElement unsupported byte per index (8)
改用UInt8
:
var indices:[UInt8] = ...
另请注意,在 Swift 中,有一个构建几何元素的助手。
下面几行
var data = NSData(bytes: indices, length: sizeof(Int) * indices.count)
var element = SCNGeometryElement(data: &data, primitiveType: .Triangles, primitiveCount: 12, bytesPerIndex: sizeofValue(indices[0]))
成为
var element = SCNGeometryElement(indices: indices, primitiveType: .Triangles)
问题是,如果我使用此构造函数创建 SCNGeometry
,我将无法渲染任何内容 init(sources: [AnyObject], elements: [AnyObject]?)
这是一个示例(这应该绘制一个立方体,取自此 tutorial ):
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 模型。我以为问题出在解析上,但后来我尝试使用原始类型绘制任何东西 - 三角形及其位置,但它没有用。
SceneKit 不处理 Int
索引,因为它们的值可能太大。您应该在控制台中看到:
SceneKit: error, C3DRendererContextBindMeshElement unsupported byte per index (8)
改用UInt8
:
var indices:[UInt8] = ...
另请注意,在 Swift 中,有一个构建几何元素的助手。 下面几行
var data = NSData(bytes: indices, length: sizeof(Int) * indices.count)
var element = SCNGeometryElement(data: &data, primitiveType: .Triangles, primitiveCount: 12, bytesPerIndex: sizeofValue(indices[0]))
成为
var element = SCNGeometryElement(indices: indices, primitiveType: .Triangles)