How can I reduce the opacity of the shadows in RealityKit?
The shadows appear darker when I use "Hide" action sequence on "Scene Start" and post a notification to call "Show" action sequence on tap gesture.
The shadows were fixed when I scaled the Object to 0% and post Notification to call "Move,Rotate,Scale to" action sequence on tap gesture.
Scaled Image
Unhide Image
Object Difference with hidden and scaled actions
import UIKit
import RealityKit
import ARKit
class Lighting: Entity, HasDirectionalLight {
required init() {
super.init()
self.light = DirectionalLightComponent(color: .red, intensity: 1000, isRealWorldProxy: true)
}
}
class SpotLight: Entity, HasSpotLight {
required init() {
super.init()
self.light = SpotLightComponent(color: .yellow,
intensity: 50000,
innerAngleInDegrees: 90,
outerAngleInDegrees: 179, // greater angle – softer shadows
attenuationRadius: 10) // can't be Zero
}
}
class ViewController: UIViewController {
@IBOutlet var arView: ARView!
enum TapObjects {
case None
case HiddenChair
case ScaledChair
}
var furnitureAnchor : Furniture._Furniture!
var tapObjects : TapObjects = .None
override func viewDidLoad() {
super.viewDidLoad()
furnitureAnchor = try! Furniture.load_Furniture()
arView.scene.anchors.append(furnitureAnchor)
addTapGesture()
}
func addTapGesture() {
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(onTap))
arView.addGestureRecognizer(tapGesture)
}
@objc func onTap(_ sender: UITapGestureRecognizer) {
switch tapObjects {
case .None:
furnitureAnchor.notifications.unhideChair.post()
tapObjects = .HiddenChair
case .HiddenChair:
furnitureAnchor.notifications.scaleChair.post()
tapObjects = .ScaledChair
default:
break
}
}
}
RealityKit – How to edit or add a Lighting?
At the moment you can't do it in Reality Composer, you need to use a RealityKit. So, you need to create a custom class that inherits from Entity
class and conforms to HasPointLight
protocol. Run this code in macOS project to find out how a PointLight setup works:
import AppKit
import RealityKit
class Lighting: Entity, HasPointLight {
required init() {
super.init()
self.light = PointLightComponent(color: .red,
intensity: 100000,
attenuationRadius: 20)
}
}
class GameViewController: NSViewController {
@IBOutlet var arView: ARView!
override func awakeFromNib() {
arView.environment.background = .color(.black)
let pointLight = Lighting().light
let boxAnchor = try! Experience.loadBox()
boxAnchor.components.set(pointLight)
arView.scene.anchors.append(boxAnchor)
boxAnchor.steelBox!.scale = [9,9,9]
boxAnchor.steelBox!.position.z = -0.5
}
}
The same way you can add a Directional Light to the scene. But remember: a position of Directional Light does not important, but an orientation does! By default it's oriented to north (-Z).
class Lighting: Entity, HasDirectionalLight {
required init() {
super.init()
self.light = DirectionalLightComponent(color: .red,
intensity: 100000,
isRealWorldProxy: true)
}
}
Also can read my STORY about lights on Medium.
How can I get an entity to constantly look at the camera in RealityKit similar to Billboard Constraint from SceneKit
I was able to figure out an answer to my question. Adding the following block of code allowed the entity to constantly look at the camera:
func session(_ session: ARSession, didUpdate frame: ARFrame) {
planeModelEntity.look(at: arView.cameraTransform.translation, from: planeModelEntity.position(relativeTo: nil), relativeTo: nil)
}
Related Topics
Need Explanation About Random Function Swift
Using Foreach with a String Array - [String] Has No Member 'Identified'
Is There a Way I Can Combine a Geofirestore Query with a Normal Firestore Query
Swift Generics: Non-Nominal Type Does Not Support Explicit Initialization
Same Class Extension in Two Different Modules
Get a List of Nodes in an Specific Area
Which Value Types in Swift Supports Copy-On-Write
Testing If a Decimal Is a Whole Number in Swift
Xcode 9 Fails to Build Swift 4 Project with Pod
How to Release a Cgdataprovider in Swift on iOS
Swiftui Pass Two Child Views to View
Swiftui: Localizedstringkey with Indices
Split String by Components and Keep Components in Place
Use Quick Look Inside a Swift Cocoa Application to Preview Audio Files
Swiftui How to Animate Each Character in Textfield
Expression Pattern of Type 'String' Cannot Match Values of Type 'Nsstoryboardsegue.Identifier