In this tutorial, you will learn how to paint walls using the Remodel-AR framework's Shader Paint approach.
Before starting this tutorial, make sure you've completed Setting up Remodel-AR in UIKit. A link to the starting project is provided here for your convenience.
Step 1
Add a new ViewController in Main.storyboard. Next create a new ViewController.swift file and name it ShaderPaintViewController. Now set the ViewController's class to ShaderPaintViewController in Main.storyboard.
Step 2
Open ShaderPaintViewController.swift and import the RemodelAR and ARKit framework.
importARKitimportRemodelAR
Step 3
Open Main.storyboard and add ARSCNView to the storyboard. Next, connect the ARSCNView outlet to ShaderPaintViewController.swift.
Step 4
In ShaderPaintViewController.swift create a lazy ARController property and assign it using the RemodelARLib.makeShaderARController(with: arscnView) method.
makeShaderARController(with: arscnView) is a method that takes an ARSCNView's instance as an argument.
You have to provide an ARSCNView instance that you have connected from the storyboard in this method or one you created and added programmatically.
Create a method for Configuring UI and call this method in ShaderPaintViewController’s ViewDidLoad() method. Also create a function for creating an ARController object.
overridefuncviewDidLoad() { super.viewDidLoad()configureUI()}privatefunccreateARView() {// Creates an ARController that uses colors to mask room walls for // painting instead of geometry. Can be used with any device. arController = RemodelARLib.makeLidarARController(with: arscnView)}privatefuncconfigureUI() {createARView()}
Step 6
Run the project and allow camera permission, then test that the camera feed displays correctly.
Step 7
Open Main.storyboard and add a CollectionView, then name it ColorPickerCollectionView. Don't forget to connect its Outlet.
Update the bottom constraint to safe area. Also, add any other necessary constraints. Your CollectionView should now be placed at the bottom of the ViewController. This CollectionView will work as a ColorPicker.
In the CollectionView's cell, add a UIView. Next, add all the necessary implementation details for the CollectionView and its cell.
Set this array as the CollectionView's datasource. Set WallPaint's color as the UIView's background color in the cell.
After finishing the CollectionView's setup, run the app and verify that the CollectionView displays different colored cells you can scroll through.
Step 9
Open Main.storyboard and add five buttons, then change the first four buttons title to Average, Light, Dark and Brightness. Clear the remaining button's title to "" and update its image to the reset image.
Embed these buttons in a Horizontal StackView. Now place this StackView on top of the ColorPickerCollectionView. Update the StackView's constraints as needed.
Now, connect the button's IBActions to ShaderPaintViewController.swift. Next, modify the IBActions as shown below.
Open Main.storyboard and add another three buttons, then change the buttons titles to Record, Stop and Idle.
Embed these buttons in a Horizontal StackView. Now place this StackView on top of the above-created buttons. Update the StackView's constraints as needed.
Next, connect the buttons' IBActions to ShaderPaintViewController.swift, then modify the IBActions as shown below.
Open Main.storybarod and create a UISlider, then place it on top of above-created buttons. Connect its outlet to ShaderPaintViewController.swift and name it thresholdSlider.
Now, modify the configureUI() method as shown below:
Next, create an IBAction for the UISlider in ShaderPaintViewController.swift.
@IBActionfunconThresoldSliderChanged(_sender: UISlider) { arController.setColorThreshold(threshold: sender.value)//Sets the current threshold used for masking foreground objects using colors}
Step 12
Open Main.storyboard and add a new button on top of the above-created Slider. Clear the button's title and set its image to camera.fill.
Next, create an IBAction for the button in ShaderPaintViewController.swift.
Next, modify the createARView() method as shown below:
privatefunccreateARView() { arController = RemodelARLib.makeShaderARController(with: arscnView)addGestureOnARView() arController.setColor(paint: colorPicker[0].color)//Use this to set the current selected color that will be applied to the next mesh that is tapped arController.setTouchMode(mode: TouchMode(rawValue:3)!)createCenterPoint()}
Step 15
Finally, Override the touchesEnded(_ touches: Set, with event: UIEvent?) method in ShaderPaintViewController.swift.
publicoverridefunctouchesEnded(_touches: Set<UITouch>, withevent: UIEvent?) {guardlet touch = touches.firstelse { return }let point = touch.location(in: arscnView)// Call this method when the user touches the ARSCNView for mesh // hit testing to determine which mesh to color when the users // taps on the view arController.handleTouch(point: point)}
Now, you should be able to use colors to mask room walls for painting instead of geometry.
If at any point you need help from the Passio team, please reach out to us at support@passiolife.com