Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

scenekit

I am drawing a sphere in Scene Kit, and it all works ok. I am drawing it like so:

   ...
   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)

This draws the sphere with a smooth surface (see image). I would enter image description here

What I am trying to accomplish is to have the sphere not rendered "smooth" like in the photo but I want to be able to have it so it shows the skeleton... so maybe control how many triangles it uses to draw the surface of the sphere but the triangles need to be empty, so I would just see the sides of the triangles...

Any suggestion?

So here's an image of what zI am trying to make the sphere look like: enter image description here

like image 460
zumzum Avatar asked Oct 19 '22 22:10

zumzum


2 Answers

  • Your wish #1: "not smooth" sphere
  • Your wish #2: wireframe

Past this into Xcode playground:

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")
like image 128
Ef Dot Avatar answered Nov 03 '22 21:11

Ef Dot


Taken from the possible duplicate question Render an SCNGeometry as a wireframe, I'm going to copy it here for posterity:

it's possible to set the fillMode of the Material to just lines, which gives the desired effect:

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

you can then change the 'resolution' of the wireframe using:

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

and you can probably also want to set:

g.isGeodesic = true

... which will give you triangular 'tiles' on your wireframe rather than the default rectangles.

like image 40
mistertim Avatar answered Nov 03 '22 22:11

mistertim