Awesome
ARKitEnvironmentMapper
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Installation
ARKitEnvironmentMapper is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'ARKitEnvironmentMapper'
Usage
To initialize with a base environment map with an image asset named, for example, "room", use the following code:
let environmentMapper = ARKitEnvironmentMapper(withImageName: "room")
Note that the input image has to have a 2:1 aspect ratio.
Alternatively, you can initialize it with a height and a color:
let environmentMapper = ARKitEnvironmentMapper(withMapHeight: 512, withDefaultColor: .red)
To start the mapping process, call the startMapping()
method. You should call this method a couple of seconds after running your ARSession
in order not to get wrong mappings on your environment map.
To stop the mapping process, simply call stopMapping()
.
To update the environment map with the current feed of the camera, you can use the following code in your class implementing ARSessionDelegate
:
func session(_ session: ARSession, didUpdate frame: ARFrame) {
environmentMapper.updateMap(withFrame: frame)
}
You can call updateMap(withFrame:)
in didUpdate
regardless of the preferred FPS, as it is not executed every frame. Its frequency is bound by the value ARKitEnvironmentMapper.Options.updatesPerSecond
and the default value is 10 updates per second.
After mapping the environment for a while you can get the current generated environment map and set it as the environment map of your ARSCNView
with the following code:
sceneView.scene.lightingEnvironment.contents = environmentMapper.currentEnvironmentMap(as: .cgImage)
Note: The ideal way to set this should be to use .mtlTexture
as input to bypass any image conversion and memory operation overhead. However, due to an Apple bug, setting a MTLTexture
as the environment map currently has no effect. If you think this is not an Apple bug and you do have a solution, please don't hesitate to send a pull request.
Output
Below is a sample output ARKitEnvironmentMapper
generates when initialized with a base environment map:
<img src="https://i.imgur.com/TBJzze1.jpg" width="384"> => <img src="https://i.imgur.com/hr4zPSQ.png" width="384">
Here is another sample output when initialized with a white background in the same environment:
<img src="https://i.imgur.com/2rtHYaM.png" width="384">Author
<img src="https://avatars0.githubusercontent.com/u/4161376?s=460&v=4" width="100px;"/> | Halil Ibrahim Kayim<br/><br/><sub>Software Engineer @ Surreal</sub><br/> |
---|
License
ARKitEnvironmentMapper is available under the MIT license. See the LICENSE file for more info.