How to add node relative to tracked image position with ARKit?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
When an image is detected, I add a node in front of it.
Image - what i need garantee
The node is this picture and in relation to this picture I add other nodes, one above (1) of the picture and another next (2). Node 1 always gets in the correct position, but node 2 sometimes gets in front of the image, which is wrong, and sometimes gets in the right position that it's like the photo.
Image - wrong
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) //node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) //node 2
This is the code that I'm using to set the initial position of my two nodes.
So i would like to know how i can garantee that my second node will stay always in the right position, that it is next to picture.
How I add the nodes:
let tappedNode = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [:])
if let result = tappedNode.first{
nodeInformation.transform = result.modelTransform
nodeAuthorInformation.transform = result.modelTransform
}
print("TAP PLACE = ", gesture.location(in: gesture.view))
if !tappedNode.isEmpty{
let node = tappedNode[0].node
print("NODE TAP = ", node.position)
let artPlane = SCNPlane(width: 0.3, height: 0.3)
artPlane.firstMaterial?.diffuse.contents = arArtDetailsScene
artPlane.firstMaterial?.isDoubleSided = true
let planeNode = SCNNode(geometry: artPlane)
planeNode.eulerAngles.x = .pi
authorPlane = SCNPlane(width: 0.2, height: 0.3)
authorPlane.firstMaterial?.diffuse.contents = arAuthorDetailsScene
authorPlane.firstMaterial?.isDoubleSided = true
let planeAuthorNode = SCNNode(geometry: authorPlane)
planeAuthorNode.eulerAngles.x = .pi
nodeInformation.addChildNode(planeNode)
nodeAuthorInformation.addChildNode(planeAuthorNode)
changeArtDetails(artId: artIndex + 1)
sceneView.scene.rootNode.addChildNode(nodeInformation)
sceneView.scene.rootNode.addChildNode(nodeAuthorInformation)
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) // initial position node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) // initial position node 2
}
}
And I update the position of the node like this:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
if let imageAnchor = anchor as? ARImageAnchor{
DispatchQueue.main.async {
self.nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100)
self.nodeAuthorInformation.position = SCNVector3Make(self.nodeInformation.position.x + 0.3, self.nodeInformation.position.y - 0.3, self.nodeInformation.position.z)//SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100)
}
}
}
}
}
ios swift arkit sknode
add a comment |
When an image is detected, I add a node in front of it.
Image - what i need garantee
The node is this picture and in relation to this picture I add other nodes, one above (1) of the picture and another next (2). Node 1 always gets in the correct position, but node 2 sometimes gets in front of the image, which is wrong, and sometimes gets in the right position that it's like the photo.
Image - wrong
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) //node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) //node 2
This is the code that I'm using to set the initial position of my two nodes.
So i would like to know how i can garantee that my second node will stay always in the right position, that it is next to picture.
How I add the nodes:
let tappedNode = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [:])
if let result = tappedNode.first{
nodeInformation.transform = result.modelTransform
nodeAuthorInformation.transform = result.modelTransform
}
print("TAP PLACE = ", gesture.location(in: gesture.view))
if !tappedNode.isEmpty{
let node = tappedNode[0].node
print("NODE TAP = ", node.position)
let artPlane = SCNPlane(width: 0.3, height: 0.3)
artPlane.firstMaterial?.diffuse.contents = arArtDetailsScene
artPlane.firstMaterial?.isDoubleSided = true
let planeNode = SCNNode(geometry: artPlane)
planeNode.eulerAngles.x = .pi
authorPlane = SCNPlane(width: 0.2, height: 0.3)
authorPlane.firstMaterial?.diffuse.contents = arAuthorDetailsScene
authorPlane.firstMaterial?.isDoubleSided = true
let planeAuthorNode = SCNNode(geometry: authorPlane)
planeAuthorNode.eulerAngles.x = .pi
nodeInformation.addChildNode(planeNode)
nodeAuthorInformation.addChildNode(planeAuthorNode)
changeArtDetails(artId: artIndex + 1)
sceneView.scene.rootNode.addChildNode(nodeInformation)
sceneView.scene.rootNode.addChildNode(nodeAuthorInformation)
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) // initial position node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) // initial position node 2
}
}
And I update the position of the node like this:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
if let imageAnchor = anchor as? ARImageAnchor{
DispatchQueue.main.async {
self.nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100)
self.nodeAuthorInformation.position = SCNVector3Make(self.nodeInformation.position.x + 0.3, self.nodeInformation.position.y - 0.3, self.nodeInformation.position.z)//SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100)
}
}
}
}
}
ios swift arkit sknode
Welcome. Could you please add more code? It will be useful to know the life cycle of your nodes and when you are setting the position.
– Bruno Berisso
Nov 23 '18 at 18:22
1
@BrunoBerisso I've added more details, hope it helps (:
– Bruna machado costa
Nov 25 '18 at 13:10
add a comment |
When an image is detected, I add a node in front of it.
Image - what i need garantee
The node is this picture and in relation to this picture I add other nodes, one above (1) of the picture and another next (2). Node 1 always gets in the correct position, but node 2 sometimes gets in front of the image, which is wrong, and sometimes gets in the right position that it's like the photo.
Image - wrong
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) //node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) //node 2
This is the code that I'm using to set the initial position of my two nodes.
So i would like to know how i can garantee that my second node will stay always in the right position, that it is next to picture.
How I add the nodes:
let tappedNode = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [:])
if let result = tappedNode.first{
nodeInformation.transform = result.modelTransform
nodeAuthorInformation.transform = result.modelTransform
}
print("TAP PLACE = ", gesture.location(in: gesture.view))
if !tappedNode.isEmpty{
let node = tappedNode[0].node
print("NODE TAP = ", node.position)
let artPlane = SCNPlane(width: 0.3, height: 0.3)
artPlane.firstMaterial?.diffuse.contents = arArtDetailsScene
artPlane.firstMaterial?.isDoubleSided = true
let planeNode = SCNNode(geometry: artPlane)
planeNode.eulerAngles.x = .pi
authorPlane = SCNPlane(width: 0.2, height: 0.3)
authorPlane.firstMaterial?.diffuse.contents = arAuthorDetailsScene
authorPlane.firstMaterial?.isDoubleSided = true
let planeAuthorNode = SCNNode(geometry: authorPlane)
planeAuthorNode.eulerAngles.x = .pi
nodeInformation.addChildNode(planeNode)
nodeAuthorInformation.addChildNode(planeAuthorNode)
changeArtDetails(artId: artIndex + 1)
sceneView.scene.rootNode.addChildNode(nodeInformation)
sceneView.scene.rootNode.addChildNode(nodeAuthorInformation)
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) // initial position node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) // initial position node 2
}
}
And I update the position of the node like this:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
if let imageAnchor = anchor as? ARImageAnchor{
DispatchQueue.main.async {
self.nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100)
self.nodeAuthorInformation.position = SCNVector3Make(self.nodeInformation.position.x + 0.3, self.nodeInformation.position.y - 0.3, self.nodeInformation.position.z)//SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100)
}
}
}
}
}
ios swift arkit sknode
When an image is detected, I add a node in front of it.
Image - what i need garantee
The node is this picture and in relation to this picture I add other nodes, one above (1) of the picture and another next (2). Node 1 always gets in the correct position, but node 2 sometimes gets in front of the image, which is wrong, and sometimes gets in the right position that it's like the photo.
Image - wrong
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) //node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) //node 2
This is the code that I'm using to set the initial position of my two nodes.
So i would like to know how i can garantee that my second node will stay always in the right position, that it is next to picture.
How I add the nodes:
let tappedNode = self.sceneView.hitTest(gesture.location(in: gesture.view), options: [:])
if let result = tappedNode.first{
nodeInformation.transform = result.modelTransform
nodeAuthorInformation.transform = result.modelTransform
}
print("TAP PLACE = ", gesture.location(in: gesture.view))
if !tappedNode.isEmpty{
let node = tappedNode[0].node
print("NODE TAP = ", node.position)
let artPlane = SCNPlane(width: 0.3, height: 0.3)
artPlane.firstMaterial?.diffuse.contents = arArtDetailsScene
artPlane.firstMaterial?.isDoubleSided = true
let planeNode = SCNNode(geometry: artPlane)
planeNode.eulerAngles.x = .pi
authorPlane = SCNPlane(width: 0.2, height: 0.3)
authorPlane.firstMaterial?.diffuse.contents = arAuthorDetailsScene
authorPlane.firstMaterial?.isDoubleSided = true
let planeAuthorNode = SCNNode(geometry: authorPlane)
planeAuthorNode.eulerAngles.x = .pi
nodeInformation.addChildNode(planeNode)
nodeAuthorInformation.addChildNode(planeAuthorNode)
changeArtDetails(artId: artIndex + 1)
sceneView.scene.rootNode.addChildNode(nodeInformation)
sceneView.scene.rootNode.addChildNode(nodeAuthorInformation)
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
let imageAnchor = anchor as! ARImageAnchor
nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100) // initial position node 1
nodeAuthorInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100) // initial position node 2
}
}
And I update the position of the node like this:
func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
if sceneView.session.currentFrame != nil {
for anchor in sceneView.session.currentFrame!.anchors {
if let imageAnchor = anchor as? ARImageAnchor{
DispatchQueue.main.async {
self.nodeInformation.position = SCNVector3Make(imageAnchor.transform.columns.3.x/100, imageAnchor.transform.columns.3.y/100 + 0.3, imageAnchor.transform.columns.3.z/100)
self.nodeAuthorInformation.position = SCNVector3Make(self.nodeInformation.position.x + 0.3, self.nodeInformation.position.y - 0.3, self.nodeInformation.position.z)//SCNVector3Make(imageAnchor.transform.columns.3.x/100 + 0.3, imageAnchor.transform.columns.3.y/100, imageAnchor.transform.columns.3.z/100)
}
}
}
}
}
ios swift arkit sknode
ios swift arkit sknode
edited Nov 25 '18 at 13:05
Bruna machado costa
asked Nov 23 '18 at 17:13
Bruna machado costaBruna machado costa
53
53
Welcome. Could you please add more code? It will be useful to know the life cycle of your nodes and when you are setting the position.
– Bruno Berisso
Nov 23 '18 at 18:22
1
@BrunoBerisso I've added more details, hope it helps (:
– Bruna machado costa
Nov 25 '18 at 13:10
add a comment |
Welcome. Could you please add more code? It will be useful to know the life cycle of your nodes and when you are setting the position.
– Bruno Berisso
Nov 23 '18 at 18:22
1
@BrunoBerisso I've added more details, hope it helps (:
– Bruna machado costa
Nov 25 '18 at 13:10
Welcome. Could you please add more code? It will be useful to know the life cycle of your nodes and when you are setting the position.
– Bruno Berisso
Nov 23 '18 at 18:22
Welcome. Could you please add more code? It will be useful to know the life cycle of your nodes and when you are setting the position.
– Bruno Berisso
Nov 23 '18 at 18:22
1
1
@BrunoBerisso I've added more details, hope it helps (:
– Bruna machado costa
Nov 25 '18 at 13:10
@BrunoBerisso I've added more details, hope it helps (:
– Bruna machado costa
Nov 25 '18 at 13:10
add a comment |
1 Answer
1
active
oldest
votes
ARKit
updates position of anchors, not nodes. You will need to provide a node with three custom nodes as children in ARSCNViewDelegate.renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode?
if anchor is ARImageAnchor
instead of adding it manually to the scene.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450693%2fhow-to-add-node-relative-to-tracked-image-position-with-arkit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
ARKit
updates position of anchors, not nodes. You will need to provide a node with three custom nodes as children in ARSCNViewDelegate.renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode?
if anchor is ARImageAnchor
instead of adding it manually to the scene.
add a comment |
ARKit
updates position of anchors, not nodes. You will need to provide a node with three custom nodes as children in ARSCNViewDelegate.renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode?
if anchor is ARImageAnchor
instead of adding it manually to the scene.
add a comment |
ARKit
updates position of anchors, not nodes. You will need to provide a node with three custom nodes as children in ARSCNViewDelegate.renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode?
if anchor is ARImageAnchor
instead of adding it manually to the scene.
ARKit
updates position of anchors, not nodes. You will need to provide a node with three custom nodes as children in ARSCNViewDelegate.renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode?
if anchor is ARImageAnchor
instead of adding it manually to the scene.
answered Nov 23 '18 at 20:46
Maxim VolginMaxim Volgin
2,2301422
2,2301422
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53450693%2fhow-to-add-node-relative-to-tracked-image-position-with-arkit%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Welcome. Could you please add more code? It will be useful to know the life cycle of your nodes and when you are setting the position.
– Bruno Berisso
Nov 23 '18 at 18:22
1
@BrunoBerisso I've added more details, hope it helps (:
– Bruna machado costa
Nov 25 '18 at 13:10