Spritekit中扩展圆形抠图转场效果
Expanding circle cutout transition effect in Spritekit
我正在尝试在 sprite kit 中获得类似于超级马里奥 运行 游戏中的过渡效果
我只想要一个圆形切口来扩展以显示下一个场景或仅显示当前场景。这不能用作标准 iOS 精灵套件转换之一。
我用一个覆盖整个场景的全黑层并使用 SKCropNode 为一个不断扩大的圆圈设置动画以显示场景。
以下是didMove(to view: SKView)
中的代码
let fullScreen = SKSpriteNode(color: .black, size: self.size)
let mask = SKSpriteNode(color: .black, size: self.size)
let circle = SKShapeNode(circleOfRadius: self.size.height / 2)
circle.fillColor = .white
circle.blendMode = .subtract
circle.setScale(0.001)
circle.isHidden = true
circle.name = "circleShape"
mask.addChild(circle)
let crop = SKCropNode()
crop.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
crop.maskNode = mask
crop.name = "cropNode"
crop.addChild(fullScreen)
crop.zPosition = 100
self.addChild(crop)
let waitAction = SKAction.wait(forDuration: 2.0)
let callAction = SKAction.run({
let cropNode = self.childNode(withName: "cropNode") as! SKCropNode
let maskNode = cropNode.maskNode as! SKSpriteNode
let circleNode = maskNode.childNode(withName: "circleShape") as! SKShapeNode
circleNode.isHidden = false
let scaleAction = SKAction.scale(to: 2.0, duration: 2.0)
scaleAction.timingFunction = scaleAction.easeInQuartic
circleNode.run(scaleAction, completion: {
cropNode.removeFromParent()
})
})
let seqAction = SKAction.sequence([waitAction,callAction])
self.run(seqAction)
这在模拟器中有效,但在设备上 运行ning 时无效(iPad pro 2016,最新的 iOS 10)。在设备上,缩放动作完成后,没有出现扩大的圆圈,黑色叠加层消失。
我的问题:
1) 为什么这不能在设备上运行而只能在模拟器上运行?
2) 在sprite kit中有没有更好更高效的方法来实现这种转换?
提前致谢。
可能有 100 种不同的方法可以实现这一点。我的方法比你的好吗?可能不是,但这对我来说在模拟器和我的 iPad.
上都有效
只需过渡到持续时间为 0.0 的场景,然后 运行 来自 didMove(to view:)
的乐趣
你需要一个单独的圆圈图像的原因是它在放大时保持漂亮的清晰边缘
bgOverlay 只是为了从全黑屏幕开始,然后应用伪过渡
func circleTransition() {
let bgMask = SKSpriteNode(texture: SKTexture(imageNamed: "bg_mask"), color: .clear, size: CGSize(width: self.size.width, height: self.size.height))
bgMask.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
bgMask.zPosition = 5000
self.addChild(bgMask)
let transitionCircle = SKSpriteNode(texture: SKTexture(imageNamed: "transition_circle"), color: .clear, size: CGSize(width: 13, height: 13))
transitionCircle.position = CGPoint.zero
transitionCircle.zPosition = 1
bgMask.addChild(transitionCircle)
let bgOverlay = SKSpriteNode(texture: SKTexture(imageNamed: "bg_overlay"), color: .clear, size: CGSize(width: self.size.width, height: self.size.height))
bgOverlay.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
bgOverlay.zPosition = 5001
self.addChild(bgOverlay)
bgMask.run(SKAction.sequence([SKAction.scale(to: 100.0, duration: 2.0), SKAction.removeFromParent()]))
bgOverlay.run(SKAction.sequence([SKAction.fadeOut(withDuration: 0.1), SKAction.removeFromParent()]))
}
我最终使用了带有片段着色器的 SKSpriteNode。以下是主场景中的代码:
let fullScreen = SKSpriteNode(color: UIColor.clear, size: CGSize(width: self.size.width, height: self.size.height))
fullScreen.position = CGPoint(x: self.size.width / 2.0, y: self.size.height / 2.0)
fullScreen.zPosition = 200
self.addChild(fullScreen)
let shader = SKShader(fileNamed: "transitionShader.fsh")
shader.attributes = [SKAttribute(name: "a_sprite_size", type: .vectorFloat2),
SKAttribute(name:"a_duration", type: .float)]
fullScreen.shader = shader
let spriteSize = vector_float2(Float(fullScreen.frame.size.width),Float(fullScreen.frame.size.height))
fullScreen.setValue(SKAttributeValue(vectorFloat2: spriteSize),forAttribute: "a_sprite_size")
fullScreen.setValue(SKAttributeValue(float: Float(mainGameTransitionDuration)), forAttribute: "a_duration")
下面是名为 transitionShader.fsh
的文件中的着色器代码
#define M 1000.0 // Multiplier
void main()
{
float aspect = a_sprite_size.y / a_sprite_size.x;
float u = v_tex_coord.x;
float v = v_tex_coord.y * aspect;
vec2 uv = vec2(u,v) * M;
vec2 center = vec2(0.60,0.55 * aspect) * M;
float t = u_time / a_duration;
if ( t < 1.0 )
{
float easeIn = pow(t,5.0);
float radius = easeIn * 2.0 * M;
float d = length(center - uv) - radius;
float a = clamp(d, 0.0, 1.0);
gl_FragColor = vec4(0.0,0.0,0.0, a);
}
else
{
gl_FragColor = vec4(0.0,0.0,0.0,0.0);
}
}
它似乎没有问题,而且在 CPU 或内存上似乎并不昂贵。
我正在尝试在 sprite kit 中获得类似于超级马里奥 运行 游戏中的过渡效果
我只想要一个圆形切口来扩展以显示下一个场景或仅显示当前场景。这不能用作标准 iOS 精灵套件转换之一。
我用一个覆盖整个场景的全黑层并使用 SKCropNode 为一个不断扩大的圆圈设置动画以显示场景。
以下是didMove(to view: SKView)
let fullScreen = SKSpriteNode(color: .black, size: self.size)
let mask = SKSpriteNode(color: .black, size: self.size)
let circle = SKShapeNode(circleOfRadius: self.size.height / 2)
circle.fillColor = .white
circle.blendMode = .subtract
circle.setScale(0.001)
circle.isHidden = true
circle.name = "circleShape"
mask.addChild(circle)
let crop = SKCropNode()
crop.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
crop.maskNode = mask
crop.name = "cropNode"
crop.addChild(fullScreen)
crop.zPosition = 100
self.addChild(crop)
let waitAction = SKAction.wait(forDuration: 2.0)
let callAction = SKAction.run({
let cropNode = self.childNode(withName: "cropNode") as! SKCropNode
let maskNode = cropNode.maskNode as! SKSpriteNode
let circleNode = maskNode.childNode(withName: "circleShape") as! SKShapeNode
circleNode.isHidden = false
let scaleAction = SKAction.scale(to: 2.0, duration: 2.0)
scaleAction.timingFunction = scaleAction.easeInQuartic
circleNode.run(scaleAction, completion: {
cropNode.removeFromParent()
})
})
let seqAction = SKAction.sequence([waitAction,callAction])
self.run(seqAction)
这在模拟器中有效,但在设备上 运行ning 时无效(iPad pro 2016,最新的 iOS 10)。在设备上,缩放动作完成后,没有出现扩大的圆圈,黑色叠加层消失。
我的问题:
1) 为什么这不能在设备上运行而只能在模拟器上运行?
2) 在sprite kit中有没有更好更高效的方法来实现这种转换?
提前致谢。
可能有 100 种不同的方法可以实现这一点。我的方法比你的好吗?可能不是,但这对我来说在模拟器和我的 iPad.
上都有效只需过渡到持续时间为 0.0 的场景,然后 运行 来自 didMove(to view:)
的乐趣你需要一个单独的圆圈图像的原因是它在放大时保持漂亮的清晰边缘
bgOverlay 只是为了从全黑屏幕开始,然后应用伪过渡
func circleTransition() {
let bgMask = SKSpriteNode(texture: SKTexture(imageNamed: "bg_mask"), color: .clear, size: CGSize(width: self.size.width, height: self.size.height))
bgMask.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
bgMask.zPosition = 5000
self.addChild(bgMask)
let transitionCircle = SKSpriteNode(texture: SKTexture(imageNamed: "transition_circle"), color: .clear, size: CGSize(width: 13, height: 13))
transitionCircle.position = CGPoint.zero
transitionCircle.zPosition = 1
bgMask.addChild(transitionCircle)
let bgOverlay = SKSpriteNode(texture: SKTexture(imageNamed: "bg_overlay"), color: .clear, size: CGSize(width: self.size.width, height: self.size.height))
bgOverlay.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
bgOverlay.zPosition = 5001
self.addChild(bgOverlay)
bgMask.run(SKAction.sequence([SKAction.scale(to: 100.0, duration: 2.0), SKAction.removeFromParent()]))
bgOverlay.run(SKAction.sequence([SKAction.fadeOut(withDuration: 0.1), SKAction.removeFromParent()]))
}
我最终使用了带有片段着色器的 SKSpriteNode。以下是主场景中的代码:
let fullScreen = SKSpriteNode(color: UIColor.clear, size: CGSize(width: self.size.width, height: self.size.height))
fullScreen.position = CGPoint(x: self.size.width / 2.0, y: self.size.height / 2.0)
fullScreen.zPosition = 200
self.addChild(fullScreen)
let shader = SKShader(fileNamed: "transitionShader.fsh")
shader.attributes = [SKAttribute(name: "a_sprite_size", type: .vectorFloat2),
SKAttribute(name:"a_duration", type: .float)]
fullScreen.shader = shader
let spriteSize = vector_float2(Float(fullScreen.frame.size.width),Float(fullScreen.frame.size.height))
fullScreen.setValue(SKAttributeValue(vectorFloat2: spriteSize),forAttribute: "a_sprite_size")
fullScreen.setValue(SKAttributeValue(float: Float(mainGameTransitionDuration)), forAttribute: "a_duration")
下面是名为 transitionShader.fsh
的文件中的着色器代码#define M 1000.0 // Multiplier
void main()
{
float aspect = a_sprite_size.y / a_sprite_size.x;
float u = v_tex_coord.x;
float v = v_tex_coord.y * aspect;
vec2 uv = vec2(u,v) * M;
vec2 center = vec2(0.60,0.55 * aspect) * M;
float t = u_time / a_duration;
if ( t < 1.0 )
{
float easeIn = pow(t,5.0);
float radius = easeIn * 2.0 * M;
float d = length(center - uv) - radius;
float a = clamp(d, 0.0, 1.0);
gl_FragColor = vec4(0.0,0.0,0.0, a);
}
else
{
gl_FragColor = vec4(0.0,0.0,0.0,0.0);
}
}
它似乎没有问题,而且在 CPU 或内存上似乎并不昂贵。