Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realitykit - Custom Material

Using Realitykit, trying to change the material of moon Entity to a custom .jpg and then tapping the screen to spawn that object based off hitTest. Nothing shows up when I tap and getting the following error in debug: [Collision] Bad paramater (SphereRadius), value = 0.000000, passed to shape creation.

import UIKit
import RealityKit

class ViewController: UIViewController {

    @IBOutlet var arView: ARView!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

        override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
            if let touchLocation = touches.first?.location(in: arView){
                let hitTest = arView.hitTest(touchLocation)
                if let hitResult = hitTest.first {
                    addObject(at: hitResult)
                }
            }
        }

        func addObject(at hitResult: CollisionCastHit) {
            let moonAnchor = try! Galaxy.loadWorld()
            let moon = moonAnchor.moon! as! ModelEntity
            var material = SimpleMaterial()
            material.baseColor = try! MaterialColorParameter.texture(TextureResource.load(named: "8k_moon.jpg"))
            moon.model?.materials = [material]
            moon.position = SIMD3(x: hitResult.position.x, y: hitResult.position.y, z: hitResult.position.z)
            arView.scene.addAnchor(moonAnchor)
        }

}
like image 980
Lawlitrix Avatar asked Jun 01 '20 19:06

Lawlitrix


People also ask

What is RealityKit?

The RealityKit framework was built from the ground up specifically for augmented reality with photo-realistic rendering, camera effects, animations, physics, and more.

What is a simple material?

Simple Material is a Jekyll-powered theme with a clean design following Google's Material Design guidelines. It provides a a beautiful, clean and mobile-first fully-responsive interface for building your next website with minimal code.


Video Answer


1 Answers

I have encountered that error when trying to hitTest an entity without a CollisionComponent. Hit testing requires a CollisionComponent on target entities:

"The method ignores entities that lack a CollisionComponent." https://developer.apple.com/documentation/realitykit/arview/3243230-hittest

If this is the problem, and since your model is being loaded from Reality Composer, a solution would be to check the "Participates" box in the Physics section of RC.

like image 181
Rick Free Avatar answered Oct 17 '22 04:10

Rick Free