gpt4 book ai didi

ios - 获取相对于 SKEffectNode 屏幕的绝对位置,因为它被 SKWarpGeometryGrid 扭曲

转载 作者:行者123 更新时间:2023-11-28 11:43:15 25 4
gpt4 key购买 nike

我有一个 SKVideoNode 作为 SKEffectNode 的子节点,这使我能够使用 1 行 x 1 列的 SKWarpGeometryGrid 来倾斜视频。

当您在每个象限中拖动手指时,在 touchMoved(toPoint fingerPos: CGPoint) 中计算新的角坐标,然后在 updateGrid() 中更新 SKWarpGeometryGrid

我还有一个以 SKView 为中心的蓝色方 block (因此位于视频的初始中心)。当 effectNode 扭曲时,我希望蓝色方 block 跟随 videoNode 的中心。我不希望将正方形作为子级添加到 effectNode,因为正方形也会变形。

我尝试了 convert(:to:) 和 convert(:from:) 的各种参数排列

例如

blueSquare?.position = self.convert(effectNode!.position, to: self)

……但是蓝色方 block 永远不会移动。有什么想法吗?

代码:(用视频文件替换myFriend.mp4进行测试)

import SpriteKit
import GameplayKit

class GameScene: SKScene {

let warpGeometryGrid1Col = SKWarpGeometryGrid(columns: 1,rows: 1)

var warpGridPositions: [float2] = [
float2(0.0, 0.0), float2(1.0, 0.0),
float2(0.0, 1.0), float2(1.0, 1.0)]
var adjust: Float = 0.005
var previousPosX: CGFloat = 0
var previousPosY: CGFloat = 0
var topLeftX:Float = 0.0
var topLeftY:Float = 1.0
var topRightX:Float = 1.0
var topRightY:Float = 1.0
var bottomLeftX:Float = 0.0
var bottomLeftY:Float = 0.0
var bottomRightX:Float = 1.0
var bottomRightY:Float = 0.0

var blueSquare:SKSpriteNode?
var effectNode: SKEffectNode?

override func didMove(to view: SKView) {

// **** add a video file to the project
let videoNode = SKVideoNode.init(fileNamed: "myFriend.mp4")
videoNode.size.width = self.frame.size.height/3*1.5
videoNode.size.height = self.frame.size.width/3*1.5
videoNode.play()

effectNode = SKEffectNode()
effectNode!.addChild(videoNode)
self.addChild(effectNode!)

blueSquare = SKSpriteNode.init(color: .blue, size: CGSize(width: 50, height: 50))
self.addChild(blueSquare!)
}

func updateGrid(){
warpGridPositions = [float2(bottomLeftX, bottomLeftY), float2(bottomRightX, bottomRightY),
float2(topLeftX, topLeftY), float2(topRightX, topRightY)]
effectNode!.warpGeometry = warpGeometryGrid1Col.replacingByDestinationPositions(positions: warpGridPositions)

//*********************************
//*********************************
// TRY TO ADJUST POSITION OF blueSquare AS EFFECT
// NODE IS WARPED KEEPING IT CENTERED ON THE
// EFFECT NODE'S CHILD VIDEO
blueSquare?.position = self.convert(effectNode!.position, to: self)
//*********************************
//*********************************
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for t in touches { self.touchMoved(toPoint: t.location(in: self)) }
}

func touchMoved(toPoint fingerPos : CGPoint) {
if fingerPos.y < 0{
if fingerPos.x > 0{// bottom right
if fingerPos.x > previousPosX {
bottomRightX += adjust
}
if fingerPos.x < previousPosX {
bottomRightX -= adjust
}
if fingerPos.y > previousPosY {
bottomRightY += adjust
}
if fingerPos.y < previousPosY {
bottomRightY -= adjust
}
}
else{ // bottom left
if fingerPos.x > previousPosX {
bottomLeftX += adjust
}
if fingerPos.x < previousPosX {
bottomLeftX -= adjust
}
if fingerPos.y > previousPosY {
bottomLeftY += adjust
}
if fingerPos.y < previousPosY {
bottomLeftY -= adjust
}
}
} else{
// top right
if fingerPos.x > 0{
if fingerPos.x > previousPosX {
topRightX += adjust
}
if fingerPos.x < previousPosX {
topRightX -= adjust
}
if fingerPos.y > previousPosY {
topRightY += adjust
}
if fingerPos.y < previousPosY {
topRightY -= adjust
}
}
else{ // top left
if fingerPos.x > previousPosX {
topLeftX += adjust
}
if fingerPos.x < previousPosX {
topLeftX -= adjust
}
if fingerPos.y > previousPosY {
topLeftY += adjust
}
if fingerPos.y < previousPosY {
topLeftY -= adjust
}
}
}

updateGrid()
previousPosX = fingerPos.x
previousPosY = fingerPos.y
}


}

最佳答案

如果effectNode是从SKScene的中心开始,直接计算。可以缓存 viewFrame 以加速计算。

   var videoFrame : CGRect!
override func didMove(to view: SKView) {

// **** add a video file to the project
let videoNode = SKVideoNode.init(fileNamed: "myFriend.mp4")
videoNode.size.width = self.frame.size.height/3*1.5
videoNode.size.height = self.frame.size.width/3*1.5
videoNode.play()
videoFrame = (videoNode.frame)

effectNode = SKEffectNode()
effectNode!.addChild(videoNode)

self.addChild(effectNode!)

blueSquare = SKSpriteNode.init(color: .blue, size: CGSize(width: 50, height: 50))
self.addChild(blueSquare!)
}

func updateGrid(){
warpGridPositions = [float2(bottomLeftX, bottomLeftY), float2(bottomRightX, bottomRightY),
float2(topLeftX, topLeftY), float2(topRightX, topRightY)]
effectNode!.warpGeometry = warpGeometryGrid1Col.replacingByDestinationPositions(positions: warpGridPositions)
let finalRatio = ( warpGridPositions.reduce(float2(),+) / 4.0)
let finalWidth = videoFrame.origin.x + CGFloat(finalRatio.x) * videoFrame.size.width
let finalHeight = videoFrame.origin.y + CGFloat(finalRatio.y) * videoFrame.size.height
blueSquare?.position = CGPoint.init(x:finalWidth , y: finalHeight)

}

希望没有问题

关于ios - 获取相对于 SKEffectNode 屏幕的绝对位置,因为它被 SKWarpGeometryGrid 扭曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53196061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com