Search code examples
iosshaderscenekitshadow

ios Scenekit shadow differs when use custom surface modifier


I have a simple scene with two planes. First plane with flower texture and shadow is cast on the plane in the background(wall texture).

I have tried to use a surface modifier SCNShaderModifierEntryPointSurface and shadow became totally different. Instead of the flower now it cast the shadow of the plane it self.

Pls see attached images.

1. enter image description here

2. enter image description here

My codes Flower Plane

SCNPlane *planeGeometry = [SCNPlane planeWithWidth:16.0 height:8.0];
SCNNode *planeNode = [SCNNode nodeWithGeometry:planeGeometry];

code for 1st picture which is ok

planeGeometry.firstMaterial.diffuse.contents = [UIImage imageNamed:@"flowerTexture"];

code for 2nd picture which is not ok

NSString * surfModifier = ...;

planeGeometry.firstMaterial.shaderModifiers = @{SCNShaderModifierEntryPointSurface : surfModifier};
SCNMaterialProperty* diffuseTexture = [SCNMaterialProperty materialPropertyWithContents:[UIImage imageNamed:@"flowerTexture"]];

[planeGeometry.firstMaterial setValue:diffuseTexture forKey:@"mixTexture"];

And the surfModifier shader

uniform sampler2D mixTexture;

#pragma transparent
#pragma body

vec4 originalTexture = texture2D(mixTexture, _surface.diffuseTexcoord);
_surface.diffuse = originalTexture;

Pls help me to get the 2nd one working

thanks


Solution

  • Your shadermodifier is applied specifically when that flower plane node is rendered. You are essentially post processing the flower plane node which won’t affect the shadows on objects after that.

    A solution would be to basically pre-process the flower plane texture, and then assign it to the plane as you do in example 1. So for example, render the flower image to a texture using Metal and you can still modify it with shader code, and then use the result as the texture for the flower plane in Scenekit.