I want to add drop shadow effect for a SKShapeNode
. I found a Emboss
shader here.
Here is my code:
let node = SKShapeNode(rectOf: CGSize(width: w, height: h),cornerRadius: w / 4.0)
node.position = CGPoint(x: x, y: frame.size.height + h / 2)
node.fillColor = color
node.strokeColor = .clear
node.fillShader = createColorEmboss()
let v = simd_make_float2(Float(w),Float(h))
node.setValue(SKAttributeValue(vectorFloat2: v), forAttribute: "a_size")
func createColorEmboss() -> SKShader {
let source = "void main() {" +
"vec4 current_color = SKDefaultShading();" +
"if (current_color.a > 0.0) {" +
"vec2 pixel_size = 1.0 / a_size;" +
"vec4 new_color = current_color;" +
"new_color += texture2D(u_texture, v_tex_coord + pixel_size) * u_strength;" +
"new_color -= texture2D(u_texture, v_tex_coord - pixel_size) * u_strength;" +
"gl_FragColor = vec4(new_color.rgb, 1) * current_color.a * v_color_mix.a;" +
"} else {" +
"gl_FragColor = current_color;" +
"}" +
"}"
let shader = SKShader(source: source, uniforms: [SKUniform(name: "u_strength", float: 1)])
shader.attributes = [SKAttribute(name: "a_size", type: .vectorFloat2)]
return shader
}
But nothing happened. Any idea?