Smooth out SCNGeometry using subdivisionLevel - ios

We are working on an app similar to iPhone's Animoji app. We have created our own 3d model, which looks quite nice, the only issue is that there are edges around the eyes or nose of the model which are not smooth. Model has been prepared in Maya 3D software.
See the screenshot
You can see the rough edges around eyes and around eye brows.
We are trying following code to smooth these edges:
let node = self.childNodes[0]
let geometry = node.geometry
let verticies = self.change(geometry: geometry!)
geometry?.edgeCreasesSource = SCNGeometrySource(vertices: verticies)
geometry?.subdivisionLevel = 0
guard let element = geometry?.elements.first else {
return
}
//print(element.debugDescription)
geometry?.edgeCreasesElement = SCNGeometryElement(data: element.data, primitiveType: .line, primitiveCount: element.primitiveCount, bytesPerIndex: element.bytesPerIndex)
We are trying to set subdivisionLevel so that edges can be smoothed out. Is this the right approach? Or we need to do something else to get this fixed programmatically. 3d Designer has very smooth edges when we see it in Maya.

Using subdivision level is the right approach to smooth out geometries. In the code snippet however, you're not setting any subdivision level that's higher than 0. If you're working with a small number of objects, it's a good idea to use SceneKit editor and set the subdivision level in the attributes inspector.

Related

Smooth Shading in SceneKit

I'm currently working on an iPadOS app that uses SceneKit to render some 3D models, nothing too fancy but I hit a big of a snag when it comes to shading these models...
Basically what I do is just set up a SceneKit scene (using pretty much all the default settings, I don't even have a camera) and instantiate the 3D objects from some .obj files I have, like so.
let url = <URL of my .obj file>
let asset = MDLAsset(url: url)
let object = asset.object(at: 0)
let node = SCNNode(mdlObject: object)
let texture = UIImage(data: try Data(contentsOf: <URL of the texture>))
let material = SCNMaterial()
material.diffuse.contents = texture
node.geometry!.materials = [material]
self.sceneView.scene!.rootNode.addChildNode(node)
The textures are loaded manually because unfortunately that's how the client set up the files for me to use. The code works fine and I can see the mesh and its texture, however it also looks like this
As you can see the shading is not smooth at all... and I have no idea how to fix it.
The client has been bothering me to implement Phong shading, and according to Apple's Documentation this is how you do it.
material.lightingModel = .phong
Unfortunately that's still what it looks like with Phong enabled. I'm an absolute beginner when it comes to 3D rendering so this might be laughably easy but I swear I cannot figure out how to get a smoother shading on this model.
I have tried looking left and right and the only thing that has had any kind of noticeable result was to use subdivisionLevel to increase the actual faces in the geometry but this does not scale well as the actual app needs to load a ton of these meshes and it runs out of memory fast even when subdivision set to just 1
Surely there must be a way to smooth those shadows without improving the actual geometry?
Thanks in advance!
Shading requires having correct normals for your geometry. Have you tried using Model IO to generate them?
https://developer.apple.com/documentation/modelio/mdlmesh/1391284-addnormals

ARKit - SCNNode continuous movement

I'm trying to create an application in which the user stacks different geometric shapes. In a .scn file, which is loaded inside the ARSCNView, I insert a static plane and then at each tap of the user, the app inserts a dynamic SCNNode.
The first node is being inserted a few inches above the plane, to replicate a falling object. And then, each other node is being dropped on top of another.
This is the main idea of the application; the problem appears after adding 3 or 4 nodes, they appear to slide of each other, almost jiggle, and the whole structure collapses.
This is my node I'm inserting:
let dimension: CGFloat = 0.075
let cube = SCNBox(width: dimension, height: dimension, length: dimension, chamferRadius: 0.0)
let node = SCNNode(geometry: cube)
node.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.dynamic, shape: nil)
node.physicsBody?.mass = 2.0
node.physicsBody?.friction = 1.0
node.physicsBody?.restitution = 0.01
node.physicsBody?.damping = 0.0
node.physicsBody?.angularDamping = 0.0
node.physicsBody?.rollingFriction = 1.0
node.physicsBody?.allowsResting = true
let insertionYOffset = 0.3
node.position = SCNVector3(hitResult.worldCoordinates.x, hitResult.worldCoordinates.y + Float(insertionYOffset), hitResult.worldCoordinates.z)
I've tried to play with the values and these are the best ones, but they aren't enough to create a stable structure of blocks.
As a requirement, I need to keep the blocks dynamic, they need to be affected by gravity, wind, etc.
The problem most likely has something to do with a factor called Dynamic which will allow it to continuously move or may have to do with the objects hitting each other to fix that problem all you have to do is change the collision mask to two different numbers.
I see two points that may be the flaws of your simulation:
Your objects are stacked. When two objects collides there's a small amount of error added in order to keep the physics in a stable phase and prevent weird behaviors. So when you stack objects you add error amounts and the simulation becomes inaccurate. I suggest making objects static over time or depending of the position, or reduce gravity by a bit.
Most of the physics engines works better with objects sized with the unit size (most of the cases, it's 1 like in Box2D or PhysX). Objects that are very small or very big tend to act less natural. 0.075 sounds a bit small for a simulation. I don't see a obvious solution, maybe look for a way to change the reference size.

Back face culling in SceneKit

I am currently trying to set up a rotating ball in scene kit. I have created the ball and applied a texture to it.
ballMaterial.diffuse.contents = UIImage(named: ballTexture)
ballMaterial.doubleSided = true
ballGeometry.materials = [ballMaterial]
The current ballTexture is a semi-transparent texture as I am hoping to see the back face roll around.
However I get some strange culling where only half of the back facing polygons are shown even though the doubleSided property is set to true.
Any help would be appreciated, thanks.
This happens because the effects of transparency are draw-order dependent. SceneKit doesn't know to draw the back-facing polygons of the sphere before the front-facing ones. (In fact, it can't really do that without reorganizing the vertex buffers on the GPU for every frame, which would be a huge drag on render performance.)
The vertex layout for an SCNSphere has it set up like the lat/long grid on a globe: the triangles render in order along the meridians from 0° to 360°, so depending on how the sphere is oriented with respect to the camera, some of the faces on the far side of the sphere will render before the nearer ones.
To fix this, you need to force the rendering order — either directly, or through the depth buffer. Here's one way to do that, using a separate material for the inside surface to illustrate the difference.
// add two balls, one a child of the other
let node = SCNNode(geometry: SCNSphere(radius: 1))
let node2 = SCNNode(geometry: SCNSphere(radius: 1))
scene.rootNode.addChildNode(node)
node.addChildNode(node2)
// cull back-facing polygons on the first ball
// so we only see the outside
let mat1 = node.geometry!.firstMaterial!
mat1.cullMode = .Back
mat1.transparent.contents = bwCheckers
// my "bwCheckers" uses black for transparent, white for opaque
mat1.transparencyMode = .RGBZero
// cull front-facing polygons on the second ball
// so we only see the inside
let mat2 = node2.geometry!.firstMaterial!
mat2.cullMode = .Front
mat2.diffuse.contents = rgCheckers
// sphere normals face outward, so to make the inside respond
// to lighting, we need to invert them
let shader = "_geometry.normal *= -1.0;"
mat2.shaderModifiers = [SCNShaderModifierEntryPointGeometry: shader]
(The shader modifier bit at the end isn't required — it just makes the inside material get diffuse shading. You could just as well use a material property that doesn't involve normals or lighting, like emission, depending on the look you want.)
You can also do this using a single node with a double-sided material by disabling writesToDepthBuffer, but that could also lead to undesirable interactions with the rest of your scene content — you might also need to mess with renderingOrder in that case.
macOS 10.13 and iOS 11 added SCNTransparencyMode.dualLayer which as far as I can tell doesn't even require setting isDoubleSided to true (the documentation doesn't provide any information at all). So a simple solution that's working for me would be:
ballMaterial.diffuse.contents = UIImage(named: ballTexture)
ballMaterial.transparencyMode = .dualLayer
ballGeometry.materials = [ballMaterial]

cheap shadow in SceneKit

Is it possible to cast a static shadow in SceneKit? Don't know if static is the right word. I would only like a smooth black circle underneath my object when it falls. The object moves in y- and x-direction. I know that I can use sampleRadius property but that has a significant impact on performance. I have seen such thing in other game engines and I am wondering if I can achieve it in SceneKit too.
EDIT:
I used this, but I only ge black scene with very little lightning. It looks like that floor is completely black. I have tried different gobo images, but no luck. What have I missed?
let spotNode = scene.rootNode.childNodeWithName("spot", recursively: true)
let spotlight = spotNode?.light
spotlight?.categoryBitMask = 1
spotlight!.shadowMode = SCNShadowMode.Modulated
spotlight?.gobo?.contents = UIImage(named: "goboImage")
floorNode?.categoryBitMask = 1
//Apple code:
// Use modulated mode
light.shadowMode = SCNShadowModeModulated;
// Configure the projected shadow
light.gobo.contents = aShadowImage;
// Use bit masks to specify receivers
light.categoryBitMask = kProjectorLightMask;
floor.categoryBitMask = kProjectorLightMask;
you'll want to use the SCNShadowModeModulated shadow mode. The different techniques for shadows are explained in depth in the Building a Game with SceneKit presentation from WWDC 2014.
General on this:
The categoryBitMask represents "categories".
So when you set a categoryBitMask on a light you are saying "This light will only hit objects in this category"
Ex:
static let WorldCategory: Int = 1 << 0 // Category for background objects
static let GameObjectsCat: Int = 1 << 1 // Category for monsters in the game :)
...
// When setting up the lights
spotlight.categoryBitMask = GameObjectsCat // Light will only affect game objects
spotlight.castsShadow = true
...
// Pretend this is a SCNNode representing a 3d fortress...
// Will not be affected the by spotlight or project it's shadows
fortress.categoryBitMask = WorldCategory
// Pretend this is a SCNNode representing a evil 3d monster...
// Affected by spotlight and projects shadows
orc.categoryBitMask = GameObjectsCat
orc.castsShadow = true
That was the general take on categoryBitMask.
In your case we want to:
Create a modulated light (SCNShadowMode.Modulated)
Set the gobo image
Give light and floor the same categoryBitMask but make sure it is not set for any other node (like game characters or whatever). Tip would be to create a new category like static let SimpleShadow: Int = 1 << 2
This light will not illuminate anything in the scene (not even the floor), it will only project your gobo where ever it is pointed. So a second light source is needed to see something (Ambient will be easiest).
I have not had the possibility to test this out, so I am writing from what I remember. :)
But please note. With proper use of categoryBitMask you could easily create a "special light" designed to project a real shadow of your main character against the floor node. This would be very cheap, as the shadow would only be calculated for one single node, projected against one single node.
Hope this will help.

fixing sharp looking geometry objects with widthSegmentCount property

Is it possible to assign widthSegmentCount (or height or chamfer) to a custom geometry object created in Blender. My geometry is rather sharp looking when imported to SceneKit. It looks great in Blender though. The sharpness is depicted in pictures.
The object is moving so setting enableJittering to true doesn't help.
I tried using this code since my object is basically a box:
let box = boxNode.geometry as! SCNBox
box.widthSegmentCount = 150
box.heightSegmentCount = 150
box.chamferSegmentCount = 150
and I'm getting an error: Thread 1: signal SIGABRT
Is this the best SceneKit can do or do I need to export my object from Blender differently?
This has nothing to do with the quality of your mesh. You are just seeing individual pixels without smoothing.
SCNView exposes the antialiasingMode property that will help you get smoother edges (try .Multisampling2X or .Multisampling4X).

Resources