How to increase the size of an SKLightNode in iOS - ios

I'm playing around with SKLightNodes and attempting to have an object (sun) shine light on anything around it. The problem is that when I add the SKLightNode to the sun node, the SKLightNode only appears in the very center of the sun. I've tried scaling the SKLightNode, but it seems to have no effect on the size or range of the SKLightNode. Here is how The SKLightNode is implemented.
let light = SKLightNode()
light.categoryBitMask = 1;
light.falloff = 1;
light.ambientColor = UIColor.whiteColor();
light.setScale(50)
star!.addChild(light)
If anyone has any input on how I can increase the effectiveness of the light source, I sure would appreciate it.

I think the only way to increase the "size" of an SKLightNode is decreasing the falloff value. You must need to set some value between 0 and 1.0.
// very bright
light?.falloff = 0.1
// a little less bright
light?.falloff = 0.5
// the default brightness
light?.falloff = 1.0
// a little darker
light?.falloff = 2.0
// very dark
light?.falloff = 5.0

Related

SceneKit and ARKit – Soft shadows or blurred shadows

I have set up shadow in ARKit, But it's not satisfied results, we have required the same shade as quick view in safari. Please help me how to set up it. We have attached two images.
Code
var light = SCNLight()
var lightNode = SCNNode()
light.castsShadow = true
light.automaticallyAdjustsShadowProjection = true
light.maximumShadowDistance = 20.0
light.orthographicScale = 1
light.type = .directional
light.shadowMapSize = CGSize(width: 2048, height: 2048)
light.shadowMode = .deferred
light.shadowSampleCount = 128
light.shadowRadius = 3
light.shadowBias = 32
light.zNear = 1
light.zFar = 1000
light.shadowColor = UIColor.black.withAlphaComponent(0.36)
lightNode.light = light2
lightNode.eulerAngles = SCNVector3(-Float.pi / 2, 0, 0)
self.sceneView.scene.rootNode.addChildNode(lightNode)
Provide shadow offset and increase the shadow radius. Play with these values to get the desired output.
light.shadowOffset = CGSize(width: 1, height: 1) //controls spread
light.shadowOpacity = 0.5 // controls opacity
light.shadowRadius = 5.0 // controls blur level
If you need a more blurry shadows in your scene use a greater values for shadowRadius instance property. shadowRadius specifies the sample radius used to render the receiver’s shadow.
Default value is 3.0.
var shadowRadius: CGFloat { get set }
...in real code it looks like this:
lightNode.light?.shadowRadius = 20.0
Apple documentation says:
shadowRadius is a number that specifies the amount of blurring around the edges of shadows cast by the light. SceneKit produces soft-edged shadows by rendering the silhouettes of geometry into a 2D shadow map and then using several weighted samples from the shadow map to determine the strength of the shadow at each pixel in the rendered scene. This property controls the radius of shadow map sampling. Lower numbers result in shadows with sharply defined, pixelated edges, higher numbers result in blurry shadows.
Also, use a spotlight instead of directional light, `cause the first one produces nice and blurry shadows.
lightNode.light?.type = .spot
And one more tip: keep you spotlight fixture at the distance of more than 2 meters from model, and assign a value of 179 degrees to spotOuterAngle instance property:
lightNode.light?.spotOuterAngle = 179.0 /* default is 45 degrees */
P.S.
If you wanna know how to use blurred shadows in RealityKit, please read this post.

Make rotation stop realisticly in Swift

I have a square = SKSpriteNode() that turns that rotates 360° when you touch it. I want to stop the spinning when you touch it again.
Now I would make square rotate through the SKAction.rotate but how can I stop the spinning realisticly meaning that I want the sprite to spin slower and slower until it stands still.
Did you try and ease out curve for the timingMode? If you don't like this effect you can provide your own custom timingFunction. There are several websites where you can explore animation curves online. I like this one.
You may want to consider using a physicsbody and applying an angular force.
let square = SKSpriteNode(color:.white,size:CGSize(10,10))
if let physicsBody = SKPhysicsBody(rectangleOf:square.frame.size)
{
physicsBody.isDynamic = true
physicsBody.allowsRotation = true
physicsBody.affectedByGravity = false
physicsBody.angularDamping = 0.1 //Adjust this to speed up or slow down the resistance of the spin
square.physicsBody = physicsBody
}
...
When you need to spin:
square.physicsBody!.angularImpulse(1) //Adjust this to change the amount of force applied to the spin

Glass effect in SceneKit material

I want to make glass effect in SceneKit.
I searched in google but there's no perfect answer.
So I'm finding SceneKit warrior who can solve my problem clearly.
There's an image that I'm going to make.
It should be looks like real.
The glass effect, reflection and shadow are main point here.
I have obj and dae file already.
So, Is there anyone to help me?
Create a SCNMaterial and configure the following properties and assign it to the bottle geometry of a SCNNode :
.lightingModel = .blinn
.transparent.content = // an image/texture whose alpha channel defines
// the area of partial transparency (the glass)
// and the opaque part (the label).
.transparencyMode = .dualLayer
.fresnelExponent = 1.5
.isDoubleSide = true
.specular.contents = UIColor(white: 0.6, alpha: 1.0)
.diffuse.contents = // texture image including the label (rest can be gray)
.shininess = // somewhere between 25 and 100
.reflective.contents = // glass won’t look good unless it has something
// to reflect, so also configure this as well.
// To at least a gray color with value 0.7
// but preferably an image.
Depending on what else is in your scene, the background, and the lighting used, you will probably have to tune the values above to get the desired results. If you want a bottle without the label, use the .transparency property (set its contents to a gray color) instead of the .transparent property.

soft shadow, shadow blur in SceneKit

I add one node and try to setting shadow blur with SceneKit
here's my light config, I did try to set shadowRadius
light = [SCNLight light];
light.type = SCNLightTypeDirectional;
light.castsShadow = true;
light.shadowMode = SCNShadowModeForward;
light.shadowRadius = 5;
light.shadowMapSize=CGSizeMake(4000, 4000);
light.orthographicScale=25;
light.zNear=1;
light.zFar=1000;
but the result is not softer than when I not set shadowRadius
it' here:
I did try to add samplecount
light = [SCNLight light];
light.type = SCNLightTypeDirectional;
light.castsShadow = true;
light.shadowMode = SCNShadowModeForward;
light.shadowRadius = 5;
// add samplecount
light.shadowSampleCount = 5;
light.shadowMapSize=CGSizeMake(4000, 4000);
light.orthographicScale=25;
light.zNear=1;
light.zFar=1000;
result look like following
shadow seem soft but this shadow start from bottom of the node (z coordinate is 0). I spend a lot of time to set soft shadow only in the edge of node, not from bottom. But no result.
This problem also occurred when add two node cross over(not only node and geometry as SCNFloor)
My problem is how to get shadow blur(soft shadow) with direction light.
any help would be appreciated!
Swift 4 / Xcode 9.2
I got a pretty good result with these settings:
light2.castsShadow = true
light2.automaticallyAdjustsShadowProjection = true
light2.maximumShadowDistance = 20.0
light2.orthographicScale = 1
light2.shadowMapSize = CGSize(width: 2048, height: 2048)
light2.shadowMode = .forward
light2.shadowSampleCount = 128
light2.shadowRadius = 3
light2.shadowBias = 32
Increasing the shadowRadius to 12 helped a lot with my model, but then I needed to increase shadowSampleCount and shadowBias to not get artifacts.
I really can make shadow blur with orthographicScale. I don't know why, but this trick work for me. Hope can help someone
light.shadowMapSize=CGSizeMake(4000, 4000);
light.orthographicScale=100; // bigger is softer
I also change shadowMapSize to bigger value and setting isJitteringEnabled antialiasingMode to reduce aliasing.

SpriteKit fast fullscreen vignette / lighting with alpha blending

I am making a platforming game with SpriteKit. I want to achieve the effect that only the area around the player is lit and everything else fades into darkness. Imagine something like a fake light source or a strong vignette. For testing purpose I created the following code:
let effect = SKEffectNode()
effect.zPosition = 100
effect.position = CGPoint(x: 0, y: 0)
effect.shouldRasterize = false
effect.blendMode = SKBlendMode.Multiply
let gray = SKSpriteNode()
gray.color = SKColor.grayColor()
gray.size = self.frame.size
gray.blendMode = SKBlendMode.Multiply
gray.position = CGPoint(x: 0, y: 0)
let shine = SKSpriteNode(imageNamed: "Shine")
shine.position = CGPoint(x: 0, y: 0)
shine.blendMode = SKBlendMode.Screen
shine.setScale(3.0)
effect.addChild(gray)
effect.addChild(shine)
self.world.addChild(effect)
This works as desired but has a somewhat significant impact on the frame rate. Considering that there might be additional effects like these from e. g. torches or other light sources on the map, I expect the frame rate to drop even more.
Next, I wanted to make the light flicker, so I assigned a random alpha value to the shine sprite in the update function. This really killed the frame rate, letting it drop instantly to about 4 frames.
I have read about SKEffectNodes and their heavy impact on the frame rate. Is there any other way to achieve this sort of fullscreen alpha blending, that is reasonably fast, even with multiple "lights"?
Any advice is truly appreciated.
Could this be solved with a SKLightNode perhaps?
https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKLightNode_Ref/index.html
Here's an article about how to use the SKLightNode:
http://www.ymc.ch/en/playing-with-ios-8-sprite-kit-and-sklightnode
Hope that's of any usage to you

Resources