SKLightNode: Changing alpha of ambientColor - swift

Is there a way to change the alpha value of an SKLightNodes ambientColor so that e.g. their background can be seen even if the lightNode is on the other side of the screen?
//Add LightNode
let lightNode = SKLightNode()
lightNode.ambientColor = UIColor.blackColor()
lightNode.lightColor = UIColor.whiteColor()
lightNode.shadowColor = UIColor.blackColor()
lightNode.categoryBitMask = LightCategory.Light1.rawValue
lightNode.enabled = true
lightNode.falloff = 0.05
self.addChild(lightNode)
//Sprite
sprite.lightingBitMask = LightCategory.Light1.rawValue

It says here in the SKLightNode Class Reference that, "The alpha value of the color is ignored." If you want things on the other end of the screen to be visible, you should change the falloff.

Related

SceneKit – Adding vignette to SCNView has no effect

I'm trying to set up a vignette effect within a SCNView. I did this following this tutorial using more or less the same code as I'm unexperienced with the range of the values. But when I apply that to my SCNViews camera object, nothing happens.
The docs about vignette read that it is necessary to set wantsHDR = true so I did that without any noticeable difference.
// scene setup (light, models, etc.)
...
sceneView.backgroundColor = .gray
sceneView.allowsCameraControl = true
let camera = sceneView.scene?.rootNode.camera
camera?.wantsHDR = true
camera?.vignettingPower = 0.6
camera?.bloomIntensity = 1.4
camera?.bloomBlurRadius = 1.0
camera?.fStop = 20.0
camera?.fStop = 5.0
camera?.focusDistance = 1.0
I've only changed the parameters which were marked as deprecated but that wasn't the issue.
I've instanciated the SCNView with the Storyboard and I'm accessing it by having an Outlet in my ViewController and I can use lot of functions with success.
Further I experienced problems setting up MSAA4x with sceneView.antialiasingMode = .multisampling4X. No difference in the outcome. And some more methods/parameters with skybox/environment lighting not doing anything (see this post).
No errors shown in the console.
If you added a new camera to the scene, it would render a vignette.
let cameraNode = SCNNode()
cameraNode.camera = SCNCamera()
cameraNode.position.z = 10
cameraNode.camera?.wantsHDR = true
cameraNode.camera?.vignettingPower = 1
cameraNode.camera?.vignettingIntensity = 1
sceneView.scene?.rootNode.addChildNode(cameraNode)
Your approach doesn't work because you're accessing the default camera.
cameraNode.camera = sceneView.scene?.rootNode.camera

SCNParticleSystem: animating "particleColor" property in code

I would like to animate a certain color sequence in a SceneKit Particle System in code only. (Like one can do within the Particle System Editor in XCode.)
I was trying the following, and the App is crashing each time the Animation is attached to the particle system. The compiler does not complain, Xcode does not indicate any error within the syntax. (without the animation part, the particle system works fine)
func particleSystemTesting(shape: SCNGeometry) -> SCNParticleSystem {
let explosion = SCNParticleSystem(named: "explosion.scnp", inDirectory: nil)!
explosion.emitterShape = shape
explosion.birthLocation = .surface
explosion.birthDirection = .surfaceNormal
explosion.isAffectedByGravity = true
explosion.isLightingEnabled = false
explosion.loops = true
explosion.sortingMode = .none
explosion.isBlackPassEnabled = true
explosion.blendMode = .additive
explosion.particleColor = UIColor.white // using as default, should even not be required
// Animation Part
let animation = CABasicAnimation(keyPath: "particleColor")
animation.fromValue = UIColor.blue
animation.toValue = UIColor.red
animation.duration = 2.0
animation.isRemovedOnCompletion = true
explosion.addAnimation(animation, forKey: nil) // causing the crash
return explosion
}
This are the errors the console is printing out:
2020-08-23 18:25:53.281120+0200 MyTestApp[1684:892874] Metal GPU Frame Capture Enabled
2020-08-23 18:25:53.281349+0200 MyTestApp[1684:892874] Metal API Validation Enabled
2020-08-23 18:25:56.563208+0200 MyTestApp[1684:892874] -[SCNParticleSystem copyAnimationChannelForKeyPath:animation:]: unrecognized selector sent to instance 0x101041500
2020-08-23 18:25:56.564524+0200 MyTestApp[1684:892874] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SCNParticleSystem copyAnimationChannelForKeyPath:animation:]: unrecognized selector sent to instance 0x101041500'
*** First throw call stack:
(0x1998d5654 0x1995f7bcc 0x1997d9dd8 0x1998d97f8 0x1998db71c 0x1ade571f8 0x1ade55f24 0x1ade59268 0x1ade5af2c 0x1ade23d84 0x1adf18cf0 0x199852f2c 0x19984de20 0x19984e29c 0x19984dba8 0x1a39bd344 0x19d9893e4 0x100566384 0x1996d58f0)
libc++abi.dylib: terminating with uncaught exception of type NSException
After some more research and consulting apples docs reference it seems that my first approach was wrong, because that would affect all spawned particles at the same time.
BUT IT STILL DOES NOT WORK AS EXPECTED - all particles are gone/invisible on scene - otherwise no errors. this should change the colours of each particle over time. What is wrong?
func particleSystemTesting(shape: SCNGeometry) -> SCNParticleSystem {
let explosion = SCNParticleSystem(named: "explosion_testing.scnp", inDirectory: nil)!
// let explosion = SCNParticleSystem() // makes no difference
explosion.emitterShape = shape
explosion.birthLocation = .surface
explosion.birthDirection = .surfaceNormal
explosion.isAffectedByGravity = true
explosion.isLightingEnabled = false
explosion.loops = true
explosion.sortingMode = .none
explosion.isBlackPassEnabled = true
explosion.blendMode = .additive
// explosion.particleColor = UIColor.black
let red = UIColor.red
let green = UIColor.green
let blue = UIColor.blue
let yellow = UIColor.yellow
let color1 = SCNVector4(red.redValue, red.greenValue, red.blueValue, 1.0)
let color2 = SCNVector4(green.redValue, green.greenValue, green.blueValue, 1.0)
let color3 = SCNVector4(blue.redValue, blue.greenValue, blue.blueValue, 1.0)
let color4 = SCNVector4(yellow.redValue, yellow.greenValue, yellow.blueValue, 1.0)
let animation = CAKeyframeAnimation()
// animation.keyPath = "color" // has like no effect (?...)
animation.values = [color1,color2,color3,color4]
animation.keyTimes = [0, 0.333, 0.666, 1]
animation.duration = 2.0
animation.isAdditive = false // should overwrite default colours
animation.isRemovedOnCompletion = true
let colorController = SCNParticlePropertyController(animation: animation)
explosion.propertyControllers = [SCNParticleSystem.ParticleProperty.color: colorController]
return explosion
}
Apple Docs says this:
Apple Discussion
This property’s value is a four-component vector (an NSValue object containing an SCNVector4 value for particle property controllers, or an array of four float values for particle event or modifier blocks).
The particle system’s particleColor and particleColorVariation properties determine the initial color for each particle.
I believe the documentation is wrong. Your animation values array should contain UIColor objects.
animation.values = [UIColor.red, UIColor.green, UIColor.blue, UIColor.yellow]
Interesting problem
This: https://developer.apple.com/documentation/scenekit/animation/animating_scenekit_content shows a similar call with an SCNTransaction, so it looks like what you are doing "should" work. However, it wasn't an SCNParticle system component. Particles can be a bit tricky to work with. I'm not great with selector stuff, so I can't really tell from just looking at the code.
particleColorVaration - vector randomizes the color specified. That doesn't sound exactly like what you want to do, but it might get you close - vector specifies ranges (hue, saturation, brightness, alpha) in that order.

Can't set the same color for background and border in UISegmentedControl

I am trying to customize my segmented control with Swift, but I have run into a problem. I set the same color (e.g. UIColor.red) for border and background, but they look different. My code is:
segmentedControl.layer.cornerRadius = 12
segmentedControl.layer.borderWidth = 2
segmentedControl.layer.borderColor = UIColor.red.cgColor
segmentedControl.layer.masksToBounds = true
segmentedControl.backgroundColor = .red
Maybe someone knows how can I fix it?
Try setting the tintColor and selectedSegmentTintColor:
segmentedControl.tintColor = .white
segmentedControl.selectedSegmentTintColor = .white

How to make particles in SCNParticleSystem opaque?

I created a SceneKit Scene File > Particle System and I can't figure out how to make all the particles opaque. The default particles alpha setting seems random. I change the image and a few other properties, and took a screen shot:
I've tried:
particle.particleColorVariation = SCNVector4(0, 0, 0, 0)
Which only makes the particles around 80%-90% transparent, but I cannot get it 100% opaque.
To make a particle system be fully opaque you need to set a blendMode instance property to .alpha (default value is .additive) and sortingMode instance property set to .distance (default value is .none)
var blendMode: SCNParticleBlendMode { get set }
var sortingMode: SCNParticleSortingMode { get set }
According to Apple documentation:
.blendMode is the blending mode for compositing particle images into the rendered scene.
There are six compositing blend modes for particles in SceneKit:
.additive
.alpha
.multiply
.replace
.screen
.subtract
Here's how it looks in real code:
let scnView = self.view as! SCNView
scnView.scene = scene
scnView.allowsCameraControl = true
scnView.backgroundColor = NSColor.black
let particleSystem = SCNParticleSystem()
particleSystem.birthRate = 5
particleSystem.blendMode = .alpha // 100% opaque if alpha = 1.0
particleSystem.sortingMode = .distance
particleSystem.particleSize = 1.0
particleSystem.emitterShape = SCNSphere(radius: 5)
particleSystem.particleLifeSpan = 100
particleSystem.particleColor = .red
// No Alpha variation
particleSystem.particleColorVariation = SCNVector4(1, 1, 1, 0)
let particlesNode = SCNNode()
particlesNode.addParticleSystem(particleSystem)
scnView.scene!.rootNode.addChildNode(particlesNode)

How to remove alpha of iconView in GoogleMaps?

I know it sounds easy but by default GoogleMaps default function iconView has a default alpha of 0.9. I tried changing it to 1 but it's still the same. Any suggestion? Thanks in advance.
[EDIT]
This is my code
let cView = UIView(frame: CGRectMake(0,0,400*PT,300*PT))
let innerView = UIView(frame: CGRectMake(0,0,400*PT,190*PT))
innerView.backgroundColor = UIColor.whiteColor()
innerView.layer.cornerRadius = 3
innerView.layer.masksToBounds = true
innerView.alpha = 1
cView.addSubview(innerView)
let cIcon = UIImageView(frame: CGRectMake(0*PT, 200*PT, 90*PT, 100*PT))
cIcon.image = UIImage(named: "1.png")
cView.addSubview(cIcon)
cView.backgroundColor = UIColor.clearColor()
item.iconView = cView
[EDIT]
Added screenshot of the output
[EDIT]
Final output after the answer below
Might be a bit obvious, but can you try adding this?
item.opacity = 1