maxxfrazer / FocusEntity

Bringing the scanning box from SceneKit to RealityKit

Home Page:https://maxxfrazer.github.io/FocusEntity/documentation/focusentity/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Texture mapping capability

vegiwoo opened this issue · comments

@maxxfrazer
Question:
Thanks for bug fix with the color change when creating FocusEntity. The second part of my question was about how to apply a different shape for the FocusEntity (by default it is a square, I need a circle (see Ikea Place app)), besides how I should be able to add texture, not just color.

Decision:
You need to add this functionality as a must have.

The alternatives I have considered:

  1. Using FocusEntity, I create it custom - I can apply MeshResource and colored, but there is no way to apply a texture:
let meshResource = MeshResource.generatePlane (width: 0.2, depth: 0.2)
let focusEntity = FocusEntity (on: self, focus: FocusEntityComponent (style: .colored (onColor: .red, offColor: .green, nonTrackingColor: .blue, mesh: meshResource)))
  1. Without using FocusEntity, I created a ModelEntity with a solid fill or texture, after which I generated a rayCast from center of screen and positioned (moved) ModelEntity to a point on the detected plane:
// ...
let meshResource = MeshResource.generatePlane (width: width, depth: width, cornerRadius: 2)
var material: UnlitMaterial!
 if let url = Bundle.main.url (forResource: "Files/targetShapeTexture_001", withExtension: "png") {
 texture = try TextureResource.load (contentsOf: url)
material = UnlitMaterial()
material! .baseColor = .texture (texture!)
material! .tintColor = UIColor (red: 25, green: 25, blue: 25, alpha: 0.5)
// ...
// modelEntity
let modelEntity = ModelEntity (mesh: meshResource, materials: [material])
modelEntity.name = "modelEntity_targetShape"
// anchorEntity
let anchorEntity = AnchorEntity (plane: .any)
anchorEntity.addChild (modelEntity)
anchorEntity.name = "anchorEntity_targetShape"
return anchorEntity
// ...
// Moving
targetShape.move (to: transform, relativeTo: nil, duration: 0.4, timingFunction: .easeOut)
// ...

This works, but there is a noticeable lag in texture rendering when moving, moreover, when changing the ARCamera.TrackingState status other than .normal, unpredictable texture behavior is observed.

Thanks!

Hi @vegiwoo, you should be able to add your ModelEntity as a child of FocusEntity to achieve this.

To get rid of the default shape try using .colored style with the colours set to UIColor.clear, or use a tiny mesh. Bit of a hack for now, in the future maybe an .empty style would be good to have.

Hi @vegiwoo, you should be able to add your ModelEntity as a child of FocusEntity to achieve this.

To get rid of the default shape try using .colored style with the colours set to UIColor.clear, or use a tiny mesh. Bit of a hack for now, in the future maybe an .empty style would be good to have.

@maxxfrazer Thanks for the quick response! This works, but just like my version of creating ModelEntity without using the FocusEntity package at all - there is a lag when drawing a texture when the FocusEntity is moved along the plane.

Plus, when I make a custom ModelEntity, I can control the smoothness of its movement, this is not available to me here.

I'm a little lost - is the lag the smoothing that is applied to avoid the entity from jumping too much between raycasts, or does the scene freeze for a moment?

I worked a little with color and transparency - this looks quite good:

// create FocusEntity
let meshResource = MeshResource.generatePlane(width: 0.3, depth: 0.3)
var texture: TextureResource?
var material: UnlitMaterial!

do {
   if let textureURL = Bundle.main.url(forResource: "targetShapeTexture_001", withExtension: "png") {
   texture = try TextureResource.load(contentsOf: textureURL)
   material = UnlitMaterial()
   material!.baseColor = .texture (texture!)
   material!.tintColor = UIColor(red: 0.89, green: 0.89, blue: 0.91, alpha: 0.45)
   } else {
      material = UnlitMaterial(color: .systemGray3)
   }
} catch {
   material = UnlitMaterial(color: .systemGray3)
}

let modelEntity = ModelEntity(mesh: meshResource, materials: [material])
focusEntity = FocusEntity(on: self, style: .classic(color: UIColor.clear))
focusEntity.addChild(modelEntity)

In the future, I would recommend that you add this option out of the box + plus you need control over the smoothness of movement - the design requirements in AR applications are different and you want to control it. Thanks for your work!💾

Post a video if you can, I'd like to see how it looks! Also I'd recommend not using classic if you're setting it to clear, as there's some additional logic that you don't need with .classic but are excluded from .colored

Feel free to add a PR, this is an open source project so improvements are welcome :)

@maxxfrazer If I add previously created meshResource to this initializer:

focusEntity = FocusEntity(on: self, style: .colored(onColor: .clear, offColor: .clear, nonTrackingColor: .clear, mesh: meshResource))

this is buggy. My first version works stably.
Video 📼

Your circular marker looks great, @vegiwoo! Would love to test out something similar. @maxxfrazer would you be incorporating this into the framework? Awesome work, by the way!

@ibmeister absolutely! I'm hopeful that @vegiwoo would open a pull request with a feature like this added to the package - as this is an open source project.

@ibmeister absolutely! I'm hopeful that @vegiwoo would open a pull request with a feature like this added to the package - as this is an open source project.

I just opened a pull request with a solution to this :)

@Reality-Dev legend, I'm looking at it now, some slight changes and then I'll release as 1.3.0 🎉