ARKit – How to convert a PNG file into a OBJ file? [closed] - swift

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am currently studying ARKit and I wish to display a 2d image in 3D environment. The image I want to display is a .png file.
Is there a way to convert this into an .obj file?

There's no way to convert a 2D .png raster file into 3D .obj geometry file. These formats are different like green apple and small button...
Although, the simplest way to see your image in ARKit's or SceneKit's 3D environment is to assign it on a 3D plane as a texture. Here's how you can do it:
#IBOutlet var sceneView: ARSCNView!
sceneView.scene = SCNScene()
let config = ARWorldTrackingConfiguration()
sceneView.session.run(config)
let node = SCNNode()
node.geometry = SCNPlane(width: 1.0, height: 1.0)
node.geometry?.firstMaterial?.isDoubleSided = true
node.geometry?.firstMaterial?.diffuse.contents = UIImage(named: "dir/image.png")
sceneView.scene.rootNode.addChildNode(node)

Related

SWIFTUI....Is it possible with PKCanvasView [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last month.
Improve this question
I have created a drawing app. I have the canvas and tools working correctly. I have also added the ability to insert some predefined shapes into the canvas.
My question or where I am running into difficulty is when you use the PKLassoTool() I am unable to size or rotate anything. I am only able to move the selected object around.
Is this by design or are there ways to extend the tool to be able to accomplish the size and rotate aspects?
I haven't been able to find anything that would allow me to accomplish the size and rotate aspects of the PKLassoTool().
class PKLassoTool: PKTool {
var scale: CGFloat = 1.0 // Scaling factor of the object
var rotation: CGFloat = 0.0 // Rotation angle of the object
func setScale(scale: CGFloat) {
self.scale = scale
}
func setRotation(rotation: CGFloat) {
self.rotation = rotation
}
}

How to find the CGColor at a CGPoint in a CGImage [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am trying to find the CGColor at a CGPoint in a CGImage. There are multiple Stack Overflow posts that have something similar (like UIImage instead of CGImage or Objective C instead of Swift), but nothing specific enough to help me.
Does anyone know how to get the color at a point? Thanks.
Im assuming based on your macOS tag that you want to do this on macOS and thus can use NSImage and NSBitmapImageRep:
let cgImage = //your CGImage
let bitmap = NSBitmapImageRep(cgImage: cgImage)
if let colorAtPoint = bitmap.colorAt(x: 0, y: 0) {
let cgColor = colorAtPoint.cgColor
print(cgColor)
}

How to use SpriteKit Particle File in a project developed with SceneKit?

I'm making a simple ARKit App with SceneKit and need to add some particle effect. Usually I could create a SceneKit Particle File, design some particle effect and use it like this
let smoke = SCNParticleSystem(named: "xxx.scnp", inDirectory: nil)!
let hole = SCNNode()
hole.addParticleSystem(smoke)
But since Xcode12, I can only create a SpritKit Particle File whose suffix is .sks. The code above could not work anymore with this file. I am a newbie in ARKit. May anyone tell me how to integrate the particle effect into my SceneKit project? Thanks a lot.
SpriteKit particles implementation
If you wanna use 2D particles in 3D scene, there are two regular ways to show SpriteKit scenes in SceneKit. The first way is to assign SKScene as material for SceneKit's geometry:
// 2D scene
let skScene = SKScene()
guard let emitter = SKEmitterNode(fileNamed: "sparks.sks")
else { return }
emitter.position = CGPoint(x: 10, y: 25)
emitter.targetNode = skScene
skScene.addChild(emitter)
// 3D scene
let plane = SCNPlane(width: 5, height: 5)
plane.materials.first?.diffuse.contents = skScene
let planeNode = SCNNode(geometry: plane)
sceneView.scene.rootNode.addChildNode(planeNode)
The second way – assign SKScene to virtual surrounding environment of SceneKit.
sceneView.scene.lightingEnvironment.contents = skScene
SceneKit particles implementation
The regular way for working with particles in SceneKit is its native particle system:
let particleSystem = SCNParticleSystem()
particleSystem.birthRate = 50
particleSystem.particleSize = 0.5
particleSystem.particleLifeSpan = 10
particleSystem.particleColor = .green
let particlesNode = SCNNode()
particlesNode.addParticleSystem(particleSystem)
sceneView.scene.rootNode.addChildNode(particlesNode)
P. S.
About deprecated preconfigured SCNP file and its replacement read HERE.
If you want to setup such phenomenon as 3D fire, the best way to do it – do it in Scene graph. For better result use 2 or 3 particle system objects with different parameters.
For better understanding how to setup 3D fire based on png samples (sources) open Apple Motion 5 app scene and click on Inspector button.
Then, try to implement a fire with the same look in SceneKit's Scene graph.
For some unknown reason Apple removed the SCNP (SceneKit particle file) template from the recent XCode editions.
You have the following options:
Download any sample code project from Apple or GitHub that contains one or more .scnp files. Grab the file from there and implement it into your AR-Project. You can still edit the file within XCode as usual. (Particle Systems Editor)
Create your particle system entirely in code.
like so:
let myParticleSystem = SCNParticleSystem()
then you configure everything you want within the instance: myParticleSystem

Change Texture In Scene Editor Programmatically

I've added a color sprite, named "arm" to my Scene Editor but for a bunch of reasons I need to access that color sprite and change the texture programmatically. Another way of stating my goal, I want to link the node to my GameScene swift file and change texture using a texture from an array. Does anyone know how to do this?
First things first, you want to link the sprite node from your GameScene.sks file to your GameScene.swift file.
To do this, in GameScene.swift near the top (under the "class GameScene: SKScene {") use this:
var armNode: SKSpriteNode?
In your didMove(to view:) section do this:
armNode = childNode(withName: "arm") as? SKSpriteNode
Now 3 different ways to change the color/texture of the SKSpriteNode depending on what you are doing:
armNode.colour = UIColor.red
armNode.texture = SKTexture(imageNamed: "your arm image here")
armNode.texture = textureArray[2]
The 3rd version uses a texture from an array of textures

Move UIView per UIBezierPath [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I've a UIBezierPath drawing this
And I want the red point to move on the blue path infinitely (from right to left, from left to right)
How can I do that?
You can use CAKeyFrameAnimation. Assign your path to the path property of the animation object, then run the animation on the view's layer.
Edit:
A little snippet as a hint:
let path = ... // the UIBezierPath
let animation = CAKeyFrameAnimation()
animation.path = path.CGPath
animation.calculationMode = kCAAnimationPaced // This give the animation an even pace
animation.repeatCount = HUGE // This makes the animation repeat forever
viewToAnimation.layer.addAnimation(animation, forKey: nil)