繁体   English   中英

SceneKit如何绘制一个显示网格状表面而不是光滑的球体?

[英]SceneKit how to draw a sphere showing mesh like surface instead of smooth?

我正在 Scene Kit 中绘制一个球体,一切正常。 我是这样画的:

   ...
   let g = SCNSphere(radius: radius)
   geometria.firstMaterial?.diffuse.contents = myColor
   let node = SCNNode(geometry: g)
   node.position = SCNVector3(x: x, y: y, z: z)
   scene.rootNode.addChildNode(node)

这将绘制具有光滑表面的球体(见图)。 我会在此处输入图像描述

我想要完成的是让球体不像照片中那样呈现“平滑”,但我希望能够让它显示骨架......所以也许控制它使用多少个三角形来绘制表面球体,但三角形需要是空的,所以我只会看到三角形的边......

有什么建议吗?

所以这是 zI 试图使球体看起来像的图像:在此处输入图像描述

  • 你的愿望#1:“不光滑”的球体
  • 你的愿望#2:线框

过去进入Xcode游乐场:

import Cocoa
import SceneKit
import QuartzCore
import XCPlayground

// create a scene
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
var scene = SCNScene()
sceneView.scene = scene
XCPShowView("The Scene View", sceneView)
sceneView.autoenablesDefaultLighting = false

// create sphere
let g = SCNSphere(radius: 100)
g.firstMaterial?.diffuse.contents = NSColor.greenColor()

// WISH #1    
g.segmentCount = 12

let node = SCNNode(geometry: g)
node.position = SCNVector3(x: 10, y: 10, z: 10)
scene.rootNode.addChildNode(node)

// WISH #2
glPolygonMode(GLenum(GL_FRONT), GLenum(GL_LINE));
glPolygonMode(GLenum(GL_BACK), GLenum(GL_LINE));

// animate
var spin = CABasicAnimation(keyPath: "rotation")
spin.toValue = NSValue(SCNVector4: SCNVector4(x: 1, y: 1, z: 0, w: CGFloat(2.0*M_PI)))
spin.duration = 3
spin.repeatCount = HUGE // for infinity
node.addAnimation(spin, forKey: "spin around")

取自可能的重复问题Render an SCNGeometry as a wireframe ,我将在此处复制它以供后代使用:

可以将 Material 的fillMode设置为仅lines ,这会产生所需的效果:

g.firstMaterial?.fillMode = .lines 
// (or possibly geometria.firstMaterial?.fillMode, not clear from your example)

然后,您可以使用以下方法更改线框的“分辨率”:

g.segmentCount = 12 
// 48 is the default, lower is 'coarser', less than 3 
// is undefined and therefore unsupported

你可能还想设置:

g.isGeodesic = true

...这将在您的线框上为您提供三角形“瓷砖”,而不是默认的矩形。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM