Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

ios - Adding a Point Light in RealityKit

I have a very simple RealityKit scene (without AR) with a box on it. While the sides of the box are colored (I assume due to a default light), the front face is black. So I decided to add a point light at camera's position (based on other StackOverflow answers, and same anchor than the box one), but the box remains black. What am I missing ?

override func viewDidLoad() {
        super.viewDidLoad()
        
        myARView.environment.background = .color(.black)

        let anchor = AnchorEntity()
        myARView.scene.anchors.append(anchor)
        
        let box = MeshResource.generateBox(size: 0.3) // Generate mesh
        let material = SimpleMaterial(color: .blue, isMetallic: true)

        let entity = ModelEntity(mesh: box, materials: [material])
        entity.name="My Box"
        entity.generateCollisionShapes(recursive: true)
        entity.position=SIMD3(x: 0.2, y: 0.8, z: -1)
        anchor.addChild(entity)
        
        let pointLight = PointLight()
        pointLight.light.color = .red
        pointLight.light.intensity = 15000000
        pointLight.light.attenuationRadius = 7.0
        pointLight.position = myARView.cameraTransform.translation // 0, 0, 2
        
        anchor.addChild(pointLight)
    }

enter image description here

question from:https://stackoverflow.com/questions/65933029/adding-a-point-light-in-realitykit

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There's a couple of things here, the most noticeable is that your material is set to .blue, and you're trying to light it using a .red light. The material's made from the colour contains zero red (in rgb form), so the light will have no effect on it. If you're using glasses with a red filter on them, green and blue will just appear black, only the reds will shine through.

Even if you change it to a .white light, it won't look much different though. This is just what it looks like with the default SimpleMaterial with isMetallic set to true; all you'll see is reflections of light, rather than see a light hitting it.

This is because the roughness of the material is set to 0, increase it just a tiny bit you'll see the cube light up with your point light.

var material = SimpleMaterial(color: .blue, isMetallic: true)
material.roughness = 0.1

Also worth noting, your light intensity is quite high, I assume this is just because you weren't seeing an effect before!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...