Casting shadows using a directional light, plane, and cast shadow - ios

In the WWDC 2017 video, apple explains about a trick to add shadows by using a directional light. Adding a directional light will cast a shadow and adding a plane will show the shadow. But I cannot seem to find mode and colour options of the cast shadow property. I am not looking to do this programatically. So is there any way there is to change the shadow type to Deferred in Xcode?
https://developer.apple.com/videos/play/wwdc2017/604/?time=2715

If I understand your problem.... Then this image explains what you are trying to do using scen view in Xcode.
You are right, adding plane and directional light with Deferred property will cast shadow. You have to set the x,y and z values of all your scene objects accordingly to show this in a proper way.

Related

RealityKit: Render an object under a grounding shadow

I have a placement marker (a simple plane with green corners) to visualize detected planes (via ARRaycastResult) in an ARView. This placement marker uses UnlitMaterial with a texture. Everything works fine as long as there is no other object added.
When I add another object, RealityKit also adds grounding shadow (invisible plane) right under the object. It works as a shadow plane and occludes everything behind it - including my placement marker.
Here is a picture of the placement marker (part of which is hidden under the shadow plane):
Is there any way to prevent this clipping? I was lookig for something like rendering order (as ARKit does have one), but have not fond anything in RealityKit yet.
I would like to keep the shadow plane if possible.
Edit: Added official name of the invisible plane (grounding shadow)
Reality kit automatically adds "grounding shadows" if the AnchoringComponent.Target is of type "plane". According to an Apple Engineer this can be aso simulated using a DirectionalLight with a Shadow and placing a plane (with an OcclusionMaterial) under the model. So the grounding shadow is probably also made out of OcclusionMaterial. This also explains why this grounding shadow occludes other objects.
There is an option to disable rendering of these grounding shadows. It can be done by inserting of disableGroundingShadows option into ARView's renderOptions property.
arView.renderOptions.insert(.disableGroundingShadows)
I have not yet found a way to override OcclusionMaterial (if any).

Unwanted self shadowing

I have an ARKit App using SceneKit to render.
I am using a directional light that casts shadows onto an invisible floor plane. In order to do this I have to (?) use shadowMode = .deferred on the light. But this results in the ugly self shadowing in the image:
How can I avoid this and still keep the shadow on the floor plane? The lighting seems fine with shadowMode = .forward, but then I get no shadow on the floor.
There are few methods that could fix this problem.
To use .constant SCNMaterial.LightingModel.
To manipulate with SCNMaterialProperty
To manipulate with categoryBitMask of the node and the light source.
Hope it helps!

How to add directional light for shadows properly in sceneView of SceneKit?

I am learning ARKit. I'm placing Virtual Objects in Augmented Reality scene and struggling in these problems!
I am using this demo project from github
1- How to add only one directional light for all (separate) nodes in SceneView of SceneKit & Move directional light with camera position? So that my added shadow can also move with light direction.
If I translate the object shadows are working as it should be. But If I rotate object now shadow should not move on the plan. They are moving because of light is at fixed position.
2- Shadow is looking fine only in case If I add only one object on the plane. But If I add two or more objects more directional lights are adding in SceneView. Now every object has more than one shadows. I want it to restrict only one shadow.
I have added light and shadow plane in sceneKit editor. (not programmatically). Here are my scenekit editor's screenshots.
3- I have read and confirmed that shadow are adding only If I set directional light property to deffered. But in this case app is crashing If I call remove all nodes from sceneView's root node. My removing nodes code is.
self.sceneView.scene.rootNode.enumerateChildNodes { (node, stop) -> Void in
node.removeFromParentNode()
print("removed ", node.name as Any)
}
You can watch my apps video for more clearance. Apps video, How it is working now
My requirement is to add only one shadow for every object. When I Rotate and translate objects shadows should look real.
I also have tried it as removing light from scn file of vase, and add a separate light.scn file having only light in it. added These two (vase and light) nodes in sceneView. But No shadow is appearing.
Directional lights are interpreted from the shader only with their direction (that one light-red vector coming out of the node in Xcode). It does not matter where the position of the light is. Directional light are often used for imitating sun light.
I implemented a similar project so here is my attempt.
I added one directional light from a separate SCN-File to the scene when I initialize the SCNScene.
My settings for it:
castsShadow: true
mode: deferred (my App is not crashing, if I remove my objects from the scene :/ )
And actually thats it to make it work in my project.
One thing about your planes: I think you have not disabled castsShadow on the planes. Therefore a user can see the planes.
Edit:
I could reproduce the crash. It does occur when removing the directional light. Thats why the app is not crashing in my project. So you could do it like me and add the directional light in viewDidLoad() for example.

SceneKit Directional Light causing flickering

I'm trying to add a directional light in Scenekit to cast shadows, but it is causing weird artefacts on objects.
The orange block below has a material with default settings and the diffuse set to orange.
The directional light is pointing downwards, and the scale is increased, otherwise it has default settings. (Making the scale smaller still has the same issue).
When I pan the camera around the texture is covered in flickering lines and dots, it looks terrible.
This isn't visible on the simulator, only the device. What is going on and how can I fix it?
Thanks to Toyos I now know that self-shadowing is what's causing the lines. The docs for shadowBias say setting this value should correct it, but for me it made no difference.
In the end I fixed it by rotating the directional light by 2 degrees. It was originally at -90, pointing straight down. Changing this to -88 has completely removed all the artefacts.
Configure the zNear/zFar range of your light to make it as small as possible (but not clipping your world). The smaller the zRange is the more precision you will get.
You can also play with the shadowBias to limit the self shadowing artefacts

ccDraw. Fill screen everywhere except on sprite

So I am trying to get a very basic "flashlight"-style thing going in one of my games.
The way I was getting it to work, was having a layer on top of my game screen, and this layer would draw a black rectangle with ~ 80% opacity, creating the look of darkness on top of my game scene.
ccDrawSolidRect(ccp(0,0), ccp(480,320), ccc4f(0, 0, 0, 0.8));
What I want to do is draw this rectangle EVERYWHERE on the screen, except for around a cone of vision that will represent the "light source".
What this would create would be a dark overlay on top of everything except for the light, giving it the illusion of a torch/light/flashlight.
The only way I can foresee this happening is by using ccDrawSolidPoly(), but since the position of the light source changes, so would the vertices for the poly.
Any suggestions on how to achieve this would be great.
You can use ccDrawSolidPoly() and avoid having to manually update vertices. For this you can create a new subclass of CCNode representing your light object, and do your custom shape drawing in its -(void)draw method.
The ccDraw...() functions will draw relative to the local sprite coordinates, so you can then move and rotate your new sprite to suit your needs and cocos2d will do the vertices transformations for you.
Update: I found out that you might be better off subclassing CCDrawNode instead of CCNode, as it has some facilities for raw OpenGL drawing (OpenGL's vertexArrayBuffer and vertexBufferObject internal variables and a buffer for vertices, their colors and their texCoords). If your stuff is very simple, maybe subclassing the plain CCNode is enough.
Could a png be used instead as a mask, as the layer above
Like that binocular vision you sometimes see in cartoons?
Or a filter similar to a photoshop mask that darkens as it grows outwardly to wards the edge of the screen
Just a thought anyway...
A picture of more of what your trying to explain might be good too

Resources