Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

soft shadow, shadow blur in SceneKit

Tags:

ios

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: enter image description 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 enter image description here

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!

like image 746
larva Avatar asked Jun 15 '17 12:06

larva


2 Answers

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.

like image 92
drewster Avatar answered Nov 12 '22 06:11

drewster


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.

like image 1
larva Avatar answered Nov 12 '22 07:11

larva